no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / security / nsCSPParser.h
blob21679d86a05154286f6279c91288e600ffc2cf1a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsCSPParser_h___
8 #define nsCSPParser_h___
10 #include "nsCSPUtils.h"
11 #include "nsCSPContext.h"
12 #include "nsIURI.h"
13 #include "PolicyTokenizer.h"
15 bool isNumberToken(char16_t aSymbol);
16 bool isValidHexDig(char16_t aHexDig);
18 // clang-format off
19 const char16_t COLON = ':';
20 const char16_t SEMICOLON = ';';
21 const char16_t SLASH = '/';
22 const char16_t PLUS = '+';
23 const char16_t DASH = '-';
24 const char16_t DOT = '.';
25 const char16_t UNDERLINE = '_';
26 const char16_t TILDE = '~';
27 const char16_t WILDCARD = '*';
28 const char16_t SINGLEQUOTE = '\'';
29 const char16_t NUMBER_SIGN = '#';
30 const char16_t QUESTIONMARK = '?';
31 const char16_t PERCENT_SIGN = '%';
32 const char16_t EXCLAMATION = '!';
33 const char16_t DOLLAR = '$';
34 const char16_t AMPERSAND = '&';
35 const char16_t OPENBRACE = '(';
36 const char16_t CLOSINGBRACE = ')';
37 const char16_t EQUALS = '=';
38 const char16_t ATSYMBOL = '@';
39 // clang-format on
41 class nsCSPParser {
42 public:
43 /**
44 * The CSP parser only has one publicly accessible function, which is
45 * parseContentSecurityPolicy. Internally the input string is separated into
46 * string tokens and policy() is called, which starts parsing the policy. The
47 * parser calls one function after the other according the the source-list
48 * from http://www.w3.org/TR/CSP11/#source-list. E.g., the parser can only
49 * call port() after the parser has already processed any possible host in
50 * host(), similar to a finite state machine.
52 static nsCSPPolicy* parseContentSecurityPolicy(const nsAString& aPolicyString,
53 nsIURI* aSelfURI,
54 bool aReportOnly,
55 nsCSPContext* aCSPContext,
56 bool aDeliveredViaMetaTag,
57 bool aSuppressLogMessages);
59 private:
60 nsCSPParser(policyTokens& aTokens, nsIURI* aSelfURI,
61 nsCSPContext* aCSPContext, bool aDeliveredViaMetaTag,
62 bool aSuppressLogMessages);
64 ~nsCSPParser();
66 // Parsing the CSP using the source-list from
67 // http://www.w3.org/TR/CSP11/#source-list
68 nsCSPPolicy* policy();
69 void directive();
70 nsCSPDirective* directiveName();
71 void directiveValue(nsTArray<nsCSPBaseSrc*>& outSrcs);
72 void referrerDirectiveValue(nsCSPDirective* aDir);
73 void reportURIList(nsCSPDirective* aDir);
74 void sandboxFlagList(nsCSPDirective* aDir);
75 void sourceList(nsTArray<nsCSPBaseSrc*>& outSrcs);
76 nsCSPBaseSrc* sourceExpression();
77 nsCSPSchemeSrc* schemeSource();
78 nsCSPHostSrc* hostSource();
79 nsCSPBaseSrc* keywordSource();
80 nsCSPNonceSrc* nonceSource();
81 nsCSPHashSrc* hashSource();
82 nsCSPHostSrc* host();
83 bool hostChar();
84 bool schemeChar();
85 bool port();
86 bool path(nsCSPHostSrc* aCspHost);
88 bool subHost(); // helper function to parse subDomains
89 bool atValidUnreservedChar(); // helper function to parse unreserved
90 bool atValidSubDelimChar(); // helper function to parse sub-delims
91 bool atValidPctEncodedChar(); // helper function to parse pct-encoded
92 bool subPath(nsCSPHostSrc* aCspHost); // helper function to parse paths
94 inline bool atEnd() { return mCurChar >= mEndChar; }
96 inline bool accept(char16_t aSymbol) {
97 if (atEnd()) {
98 return false;
100 return (*mCurChar == aSymbol) && advance();
103 inline bool accept(bool (*aClassifier)(char16_t)) {
104 if (atEnd()) {
105 return false;
107 return (aClassifier(*mCurChar)) && advance();
110 inline bool peek(char16_t aSymbol) {
111 if (atEnd()) {
112 return false;
114 return *mCurChar == aSymbol;
117 inline bool peek(bool (*aClassifier)(char16_t)) {
118 if (atEnd()) {
119 return false;
121 return aClassifier(*mCurChar);
124 inline bool advance() {
125 if (atEnd()) {
126 return false;
128 mCurValue.Append(*mCurChar++);
129 return true;
132 inline void resetCurValue() { mCurValue.Truncate(); }
134 bool atEndOfPath();
135 bool atValidPathChar();
137 void resetCurChar(const nsAString& aToken);
139 void logWarningErrorToConsole(uint32_t aSeverityFlag, const char* aProperty,
140 const nsTArray<nsString>& aParams);
143 * When parsing the policy, the parser internally uses the following helper
144 * variables/members which are used/reset during parsing. The following
145 * example explains how they are used.
146 * The tokenizer separats all input into arrays of arrays of strings, which
147 * are stored in mTokens, for example:
148 * mTokens = [ [ script-src, http://www.example.com, 'self' ], ... ]
150 * When parsing starts, mCurdir always holds the currently processed array of
151 * strings.
152 * In our example:
153 * mCurDir = [ script-src, http://www.example.com, 'self' ]
155 * During parsing, we process/consume one string at a time of that array.
156 * We set mCurToken to the string we are currently processing; in the first
157 * case that would be: mCurToken = script-src which allows to do simple string
158 * comparisons to see if mCurToken is a valid directive.
160 * Continuing parsing, the parser consumes the next string of that array,
161 * resetting:
162 * mCurToken = "http://www.example.com"
163 * ^ ^
164 * mCurChar mEndChar (points *after* the 'm')
165 * mCurValue = ""
167 * After calling advance() the first time, helpers would hold the following
168 * values:
169 * mCurToken = "http://www.example.com"
170 * ^ ^
171 * mCurChar mEndChar (points *after* the 'm')
172 * mCurValue = "h"
174 * We continue parsing till all strings of one directive are consumed, then we
175 * reset mCurDir to hold the next array of strings and start the process all
176 * over.
179 const char16_t* mCurChar;
180 const char16_t* mEndChar;
181 nsString mCurValue;
182 nsString mCurToken;
183 nsTArray<nsString> mCurDir;
185 // helpers to allow invalidation of srcs within script-src and style-src
186 // if either 'strict-dynamic' or at least a hash or nonce is present.
187 bool mHasHashOrNonce; // false, if no hash or nonce is defined
188 bool mHasAnyUnsafeEval; // false, if no (wasm-)unsafe-eval keyword is used.
189 bool mStrictDynamic; // false, if 'strict-dynamic' is not defined
190 nsCSPKeywordSrc* mUnsafeInlineKeywordSrc; // null, otherwise invlidate()
192 // cache variables for child-src, frame-src and worker-src handling;
193 // in CSP 3 child-src is deprecated. For backwards compatibility
194 // child-src needs to restrict:
195 // (*) frames, in case frame-src is not expicitly specified
196 // (*) workers, in case worker-src is not expicitly specified
197 // If neither worker-src, nor child-src is present, then script-src
198 // needs to govern workers.
199 nsCSPChildSrcDirective* mChildSrc;
200 nsCSPDirective* mFrameSrc;
201 nsCSPDirective* mWorkerSrc;
202 nsCSPScriptSrcDirective* mScriptSrc;
203 nsCSPStyleSrcDirective* mStyleSrc;
205 // cache variable to let nsCSPHostSrc know that it's within
206 // the frame-ancestors directive.
207 bool mParsingFrameAncestorsDir;
209 policyTokens mTokens;
210 nsIURI* mSelfURI;
211 nsCSPPolicy* mPolicy;
212 nsCSPContext* mCSPContext; // used for console logging
213 bool mDeliveredViaMetaTag;
214 bool mSuppressLogMessages;
217 #endif /* nsCSPParser_h___ */