2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-05-14 22:03:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef nsCSPParser_h___
|
|
|
|
#define nsCSPParser_h___
|
|
|
|
|
|
|
|
#include "nsCSPUtils.h"
|
|
|
|
#include "nsIURI.h"
|
2018-05-02 17:36:51 +03:00
|
|
|
#include "PolicyTokenizer.h"
|
2014-05-14 22:03:43 +04:00
|
|
|
|
|
|
|
class nsCSPParser {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The CSP parser only has one publicly accessible function, which is
|
|
|
|
* parseContentSecurityPolicy. Internally the input string is separated into
|
|
|
|
* string tokens and policy() is called, which starts parsing the policy. The
|
|
|
|
* parser calls one function after the other according the the source-list
|
|
|
|
* from http://www.w3.org/TR/CSP11/#source-list. E.g., the parser can only
|
|
|
|
* call port() after the parser has already processed any possible host in
|
|
|
|
* host(), similar to a finite state machine.
|
|
|
|
*/
|
|
|
|
static nsCSPPolicy* parseContentSecurityPolicy(const nsAString& aPolicyString,
|
|
|
|
nsIURI* aSelfURI,
|
|
|
|
bool aReportOnly,
|
2015-11-15 06:27:59 +03:00
|
|
|
nsCSPContext* aCSPContext,
|
|
|
|
bool aDeliveredViaMetaTag);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
private:
|
2018-05-02 17:36:51 +03:00
|
|
|
nsCSPParser(policyTokens& aTokens, nsIURI* aSelfURI,
|
2015-11-15 06:27:59 +03:00
|
|
|
nsCSPContext* aCSPContext, bool aDeliveredViaMetaTag);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
~nsCSPParser();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
// Parsing the CSP using the source-list from
|
|
|
|
// http://www.w3.org/TR/CSP11/#source-list
|
2016-05-31 12:14:00 +03:00
|
|
|
nsCSPPolicy* policy();
|
|
|
|
void directive();
|
|
|
|
nsCSPDirective* directiveName();
|
|
|
|
void directiveValue(nsTArray<nsCSPBaseSrc*>& outSrcs);
|
2016-07-31 08:57:22 +03:00
|
|
|
void referrerDirectiveValue(nsCSPDirective* aDir);
|
2016-08-19 19:41:45 +03:00
|
|
|
void reportURIList(nsCSPDirective* aDir);
|
|
|
|
void sandboxFlagList(nsCSPDirective* aDir);
|
2016-05-31 12:14:00 +03:00
|
|
|
void sourceList(nsTArray<nsCSPBaseSrc*>& outSrcs);
|
|
|
|
nsCSPBaseSrc* sourceExpression();
|
|
|
|
nsCSPSchemeSrc* schemeSource();
|
|
|
|
nsCSPHostSrc* hostSource();
|
|
|
|
nsCSPBaseSrc* keywordSource();
|
|
|
|
nsCSPNonceSrc* nonceSource();
|
|
|
|
nsCSPHashSrc* hashSource();
|
|
|
|
nsCSPHostSrc* host();
|
|
|
|
bool hostChar();
|
|
|
|
bool schemeChar();
|
|
|
|
bool port();
|
|
|
|
bool path(nsCSPHostSrc* aCspHost);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-29 17:48:44 +03:00
|
|
|
bool subHost(); // helper function to parse subDomains
|
|
|
|
bool atValidUnreservedChar(); // helper function to parse unreserved
|
|
|
|
bool atValidSubDelimChar(); // helper function to parse sub-delims
|
|
|
|
bool atValidPctEncodedChar(); // helper function to parse pct-encoded
|
|
|
|
bool subPath(nsCSPHostSrc* aCspHost); // helper function to parse paths
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
inline bool atEnd() { return mCurChar >= mEndChar; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
inline bool accept(char16_t aSymbol) {
|
|
|
|
if (atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (*mCurChar == aSymbol) && advance();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-14 22:03:43 +04:00
|
|
|
|
|
|
|
inline bool accept(bool (*aClassifier)(char16_t)) {
|
|
|
|
if (atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (aClassifier(*mCurChar)) && advance();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-14 22:03:43 +04:00
|
|
|
|
|
|
|
inline bool peek(char16_t aSymbol) {
|
|
|
|
if (atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return *mCurChar == aSymbol;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-14 22:03:43 +04:00
|
|
|
|
|
|
|
inline bool peek(bool (*aClassifier)(char16_t)) {
|
|
|
|
if (atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return aClassifier(*mCurChar);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-14 22:03:43 +04:00
|
|
|
|
|
|
|
inline bool advance() {
|
|
|
|
if (atEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mCurValue.Append(*mCurChar++);
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
inline void resetCurValue() { mCurValue.Truncate(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-13 00:06:19 +04:00
|
|
|
bool atEndOfPath();
|
|
|
|
bool atValidPathChar();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
void resetCurChar(const nsAString& aToken);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
void logWarningErrorToConsole(uint32_t aSeverityFlag, const char* aProperty,
|
2019-06-11 18:51:51 +03:00
|
|
|
const nsTArray<nsString>& aParams);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
/**
|
|
|
|
* When parsing the policy, the parser internally uses the following helper
|
|
|
|
* variables/members which are used/reset during parsing. The following
|
|
|
|
* example explains how they are used.
|
|
|
|
* The tokenizer separats all input into arrays of arrays of strings, which
|
|
|
|
* are stored in mTokens, for example:
|
|
|
|
* mTokens = [ [ script-src, http://www.example.com, 'self' ], ... ]
|
|
|
|
*
|
2018-11-28 03:54:56 +03:00
|
|
|
* When parsing starts, mCurdir always holds the currently processed array of
|
|
|
|
* strings.
|
2014-05-14 22:03:43 +04:00
|
|
|
* In our example:
|
|
|
|
* mCurDir = [ script-src, http://www.example.com, 'self' ]
|
|
|
|
*
|
|
|
|
* During parsing, we process/consume one string at a time of that array.
|
|
|
|
* We set mCurToken to the string we are currently processing; in the first
|
|
|
|
* case that would be: mCurToken = script-src which allows to do simple string
|
|
|
|
* comparisons to see if mCurToken is a valid directive.
|
|
|
|
*
|
2018-11-28 03:54:56 +03:00
|
|
|
* Continuing parsing, the parser consumes the next string of that array,
|
|
|
|
* resetting:
|
2014-05-14 22:03:43 +04:00
|
|
|
* mCurToken = "http://www.example.com"
|
|
|
|
* ^ ^
|
|
|
|
* mCurChar mEndChar (points *after* the 'm')
|
|
|
|
* mCurValue = ""
|
|
|
|
*
|
2018-11-28 03:54:56 +03:00
|
|
|
* After calling advance() the first time, helpers would hold the following
|
|
|
|
* values:
|
2014-05-14 22:03:43 +04:00
|
|
|
* mCurToken = "http://www.example.com"
|
|
|
|
* ^ ^
|
|
|
|
* mCurChar mEndChar (points *after* the 'm')
|
|
|
|
* mCurValue = "h"
|
|
|
|
*
|
2018-11-28 03:54:56 +03:00
|
|
|
* We continue parsing till all strings of one directive are consumed, then we
|
|
|
|
* reset mCurDir to hold the next array of strings and start the process all
|
|
|
|
* over.
|
2014-05-14 22:03:43 +04:00
|
|
|
*/
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-14 22:03:43 +04:00
|
|
|
const char16_t* mCurChar;
|
|
|
|
const char16_t* mEndChar;
|
|
|
|
nsString mCurValue;
|
|
|
|
nsString mCurToken;
|
|
|
|
nsTArray<nsString> mCurDir;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-11-08 15:08:33 +03:00
|
|
|
// helpers to allow invalidation of srcs within script-src and style-src
|
|
|
|
// if either 'strict-dynamic' or at least a hash or nonce is present.
|
2015-04-07 19:06:05 +03:00
|
|
|
bool mHasHashOrNonce; // false, if no hash or nonce is defined
|
2016-11-08 15:08:33 +03:00
|
|
|
bool mStrictDynamic; // false, if 'strict-dynamic' is not defined
|
2015-04-07 19:06:05 +03:00
|
|
|
nsCSPKeywordSrc* mUnsafeInlineKeywordSrc; // null, otherwise invlidate()
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-30 20:45:36 +03:00
|
|
|
// cache variables for child-src, frame-src and worker-src handling;
|
|
|
|
// in CSP 3 child-src is deprecated. For backwards compatibility
|
|
|
|
// child-src needs to restrict:
|
|
|
|
// (*) frames, in case frame-src is not expicitly specified
|
|
|
|
// (*) workers, in case worker-src is not expicitly specified
|
|
|
|
// If neither worker-src, nor child-src is present, then script-src
|
|
|
|
// needs to govern workers.
|
|
|
|
nsCSPChildSrcDirective* mChildSrc;
|
|
|
|
nsCSPDirective* mFrameSrc;
|
|
|
|
nsCSPDirective* mWorkerSrc;
|
|
|
|
nsCSPScriptSrcDirective* mScriptSrc;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-06 10:12:13 +03:00
|
|
|
// cache variable to let nsCSPHostSrc know that it's within
|
|
|
|
// the frame-ancestors directive.
|
|
|
|
bool mParsingFrameAncestorsDir;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-02 17:36:51 +03:00
|
|
|
policyTokens mTokens;
|
2014-05-14 22:03:43 +04:00
|
|
|
nsIURI* mSelfURI;
|
|
|
|
nsCSPPolicy* mPolicy;
|
2015-11-11 17:23:57 +03:00
|
|
|
nsCSPContext* mCSPContext; // used for console logging
|
2015-11-15 06:27:59 +03:00
|
|
|
bool mDeliveredViaMetaTag;
|
2014-05-14 22:03:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCSPParser_h___ */
|