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 nsCSPUtils_h___
8 #define nsCSPUtils_h___
11 #include "nsIContentSecurityPolicy.h"
13 #include "nsLiteralString.h"
16 #include "nsUnicharUtils.h"
17 #include "mozilla/Logging.h"
26 } // namespace mozilla
28 /* =============== Logging =================== */
30 void CSP_LogLocalizedStr(const char* aName
, const nsTArray
<nsString
>& aParams
,
31 const nsAString
& aSourceName
,
32 const nsAString
& aSourceLine
, uint32_t aLineNumber
,
33 uint32_t aColumnNumber
, uint32_t aFlags
,
34 const nsACString
& aCategory
, uint64_t aInnerWindowID
,
35 bool aFromPrivateWindow
);
37 void CSP_GetLocalizedStr(const char* aName
, const nsTArray
<nsString
>& aParams
,
38 nsAString
& outResult
);
40 void CSP_LogStrMessage(const nsAString
& aMsg
);
42 void CSP_LogMessage(const nsAString
& aMessage
, const nsAString
& aSourceName
,
43 const nsAString
& aSourceLine
, uint32_t aLineNumber
,
44 uint32_t aColumnNumber
, uint32_t aFlags
,
45 const nsACString
& aCategory
, uint64_t aInnerWindowID
,
46 bool aFromPrivateWindow
);
48 /* =============== Constant and Type Definitions ================== */
50 #define INLINE_STYLE_VIOLATION_OBSERVER_TOPIC \
51 "violated base restriction: Inline Stylesheets will not apply"
52 #define INLINE_SCRIPT_VIOLATION_OBSERVER_TOPIC \
53 "violated base restriction: Inline Scripts will not execute"
54 #define EVAL_VIOLATION_OBSERVER_TOPIC \
55 "violated base restriction: Code will not be created from strings"
56 #define SCRIPT_NONCE_VIOLATION_OBSERVER_TOPIC "Inline Script had invalid nonce"
57 #define STYLE_NONCE_VIOLATION_OBSERVER_TOPIC "Inline Style had invalid nonce"
58 #define SCRIPT_HASH_VIOLATION_OBSERVER_TOPIC "Inline Script had invalid hash"
59 #define STYLE_HASH_VIOLATION_OBSERVER_TOPIC "Inline Style had invalid hash"
61 // these strings map to the CSPDirectives in nsIContentSecurityPolicy
62 // NOTE: When implementing a new directive, you will need to add it here but
63 // also add a corresponding entry to the constants in
64 // nsIContentSecurityPolicy.idl and also create an entry for the new directive
65 // in nsCSPDirective::toDomCSPStruct() and add it to CSPDictionaries.webidl.
66 // Order of elements below important! Make sure it matches the order as in
67 // nsIContentSecurityPolicy.idl
68 static const char* CSPStrDirectives
[] = {
69 "-error-", // NO_DIRECTIVE
70 "default-src", // DEFAULT_SRC_DIRECTIVE
71 "script-src", // SCRIPT_SRC_DIRECTIVE
72 "object-src", // OBJECT_SRC_DIRECTIVE
73 "style-src", // STYLE_SRC_DIRECTIVE
74 "img-src", // IMG_SRC_DIRECTIVE
75 "media-src", // MEDIA_SRC_DIRECTIVE
76 "frame-src", // FRAME_SRC_DIRECTIVE
77 "font-src", // FONT_SRC_DIRECTIVE
78 "connect-src", // CONNECT_SRC_DIRECTIVE
79 "report-uri", // REPORT_URI_DIRECTIVE
80 "frame-ancestors", // FRAME_ANCESTORS_DIRECTIVE
81 "reflected-xss", // REFLECTED_XSS_DIRECTIVE
82 "base-uri", // BASE_URI_DIRECTIVE
83 "form-action", // FORM_ACTION_DIRECTIVE
84 "manifest-src", // MANIFEST_SRC_DIRECTIVE
85 "upgrade-insecure-requests", // UPGRADE_IF_INSECURE_DIRECTIVE
86 "child-src", // CHILD_SRC_DIRECTIVE
87 "block-all-mixed-content", // BLOCK_ALL_MIXED_CONTENT
88 "sandbox", // SANDBOX_DIRECTIVE
89 "worker-src", // WORKER_SRC_DIRECTIVE
90 "navigate-to" // NAVIGATE_TO_DIRECTIVE
93 inline const char* CSP_CSPDirectiveToString(CSPDirective aDir
) {
94 return CSPStrDirectives
[static_cast<uint32_t>(aDir
)];
97 inline CSPDirective
CSP_StringToCSPDirective(const nsAString
& aDir
) {
98 nsString lowerDir
= PromiseFlatString(aDir
);
99 ToLowerCase(lowerDir
);
101 uint32_t numDirs
= (sizeof(CSPStrDirectives
) / sizeof(CSPStrDirectives
[0]));
102 for (uint32_t i
= 1; i
< numDirs
; i
++) {
103 if (lowerDir
.EqualsASCII(CSPStrDirectives
[i
])) {
104 return static_cast<CSPDirective
>(i
);
107 return nsIContentSecurityPolicy::NO_DIRECTIVE
;
110 #define FOR_EACH_CSP_KEYWORD(MACRO) \
111 MACRO(CSP_SELF, "'self'") \
112 MACRO(CSP_UNSAFE_INLINE, "'unsafe-inline'") \
113 MACRO(CSP_UNSAFE_EVAL, "'unsafe-eval'") \
114 MACRO(CSP_NONE, "'none'") \
115 MACRO(CSP_NONCE, "'nonce-") \
116 MACRO(CSP_REPORT_SAMPLE, "'report-sample'") \
117 MACRO(CSP_STRICT_DYNAMIC, "'strict-dynamic'") \
118 MACRO(CSP_UNSAFE_ALLOW_REDIRECTS, "'unsafe-allow-redirects'")
121 #define KEYWORD_ENUM(id_, string_) id_,
122 FOR_EACH_CSP_KEYWORD(KEYWORD_ENUM
)
125 // CSP_LAST_KEYWORD_VALUE always needs to be the last element in the enum
126 // because we use it to calculate the size for the char* array.
127 CSP_LAST_KEYWORD_VALUE
,
129 // Putting CSP_HASH after the delimitor, because CSP_HASH is not a valid
130 // keyword (hash uses e.g. sha256, sha512) but we use CSP_HASH internally
131 // to identify allowed hashes in ::allows.
135 // The keywords, in UTF-8 form.
136 static const char* gCSPUTF8Keywords
[] = {
137 #define KEYWORD_UTF8_LITERAL(id_, string_) string_,
138 FOR_EACH_CSP_KEYWORD(KEYWORD_UTF8_LITERAL
)
139 #undef KEYWORD_UTF8_LITERAL
142 // The keywords, in UTF-16 form.
143 static const char16_t
* gCSPUTF16Keywords
[] = {
144 #define KEYWORD_UTF16_LITERAL(id_, string_) u"" string_,
145 FOR_EACH_CSP_KEYWORD(KEYWORD_UTF16_LITERAL
)
146 #undef KEYWORD_UTF16_LITERAL
149 #undef FOR_EACH_CSP_KEYWORD
151 inline const char* CSP_EnumToUTF8Keyword(enum CSPKeyword aKey
) {
152 // Make sure all elements in enum CSPKeyword got added to gCSPUTF8Keywords.
153 static_assert((sizeof(gCSPUTF8Keywords
) / sizeof(gCSPUTF8Keywords
[0]) ==
154 CSP_LAST_KEYWORD_VALUE
),
155 "CSP_LAST_KEYWORD_VALUE != length(gCSPUTF8Keywords)");
157 if (static_cast<uint32_t>(aKey
) <
158 static_cast<uint32_t>(CSP_LAST_KEYWORD_VALUE
)) {
159 return gCSPUTF8Keywords
[static_cast<uint32_t>(aKey
)];
161 return "error: invalid keyword in CSP_EnumToUTF8Keyword";
164 inline const char16_t
* CSP_EnumToUTF16Keyword(enum CSPKeyword aKey
) {
165 // Make sure all elements in enum CSPKeyword got added to gCSPUTF16Keywords.
166 static_assert((sizeof(gCSPUTF16Keywords
) / sizeof(gCSPUTF16Keywords
[0]) ==
167 CSP_LAST_KEYWORD_VALUE
),
168 "CSP_LAST_KEYWORD_VALUE != length(gCSPUTF16Keywords)");
170 if (static_cast<uint32_t>(aKey
) <
171 static_cast<uint32_t>(CSP_LAST_KEYWORD_VALUE
)) {
172 return gCSPUTF16Keywords
[static_cast<uint32_t>(aKey
)];
174 return u
"error: invalid keyword in CSP_EnumToUTF16Keyword";
177 inline CSPKeyword
CSP_UTF16KeywordToEnum(const nsAString
& aKey
) {
178 nsString lowerKey
= PromiseFlatString(aKey
);
179 ToLowerCase(lowerKey
);
181 for (uint32_t i
= 0; i
< CSP_LAST_KEYWORD_VALUE
; i
++) {
182 if (lowerKey
.Equals(gCSPUTF16Keywords
[i
])) {
183 return static_cast<CSPKeyword
>(i
);
186 NS_ASSERTION(false, "Can not convert unknown Keyword to Enum");
187 return CSP_LAST_KEYWORD_VALUE
;
190 nsresult
CSP_AppendCSPFromHeader(nsIContentSecurityPolicy
* aCsp
,
191 const nsAString
& aHeaderValue
,
194 /* =============== Helpers ================== */
198 nsCSPHostSrc
* CSP_CreateHostSrcFromSelfURI(nsIURI
* aSelfURI
);
199 bool CSP_IsEmptyDirective(const nsAString
& aValue
, const nsAString
& aDir
);
200 bool CSP_IsDirective(const nsAString
& aValue
, CSPDirective aDir
);
201 bool CSP_IsKeyword(const nsAString
& aValue
, enum CSPKeyword aKey
);
202 bool CSP_IsQuotelessKeyword(const nsAString
& aKey
);
203 CSPDirective
CSP_ContentTypeToDirective(nsContentPolicyType aType
);
205 class nsCSPSrcVisitor
;
207 void CSP_PercentDecodeStr(const nsAString
& aEncStr
, nsAString
& outDecStr
);
208 bool CSP_ShouldResponseInheritCSP(nsIChannel
* aChannel
);
210 void CSP_ApplyMetaCSPToDoc(mozilla::dom::Document
& aDoc
,
211 const nsAString
& aPolicyStr
);
213 /* =============== nsCSPSrc ================== */
218 virtual ~nsCSPBaseSrc();
220 virtual bool permits(nsIURI
* aUri
, const nsAString
& aNonce
,
221 bool aWasRedirected
, bool aReportOnly
,
222 bool aUpgradeInsecure
, bool aParserCreated
) const;
223 virtual bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
224 bool aParserCreated
) const;
225 virtual bool visit(nsCSPSrcVisitor
* aVisitor
) const = 0;
226 virtual void toString(nsAString
& outStr
) const = 0;
228 virtual void invalidate() const { mInvalidated
= true; }
230 virtual bool isReportSample() const { return false; }
233 // invalidate srcs if 'script-dynamic' is present or also invalidate
234 // unsafe-inline' if nonce- or hash-source specified
235 mutable bool mInvalidated
;
238 /* =============== nsCSPSchemeSrc ============ */
240 class nsCSPSchemeSrc
: public nsCSPBaseSrc
{
242 explicit nsCSPSchemeSrc(const nsAString
& aScheme
);
243 virtual ~nsCSPSchemeSrc();
245 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
246 bool aReportOnly
, bool aUpgradeInsecure
,
247 bool aParserCreated
) const override
;
248 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
249 void toString(nsAString
& outStr
) const override
;
251 inline void getScheme(nsAString
& outStr
) const { outStr
.Assign(mScheme
); };
257 /* =============== nsCSPHostSrc ============== */
259 class nsCSPHostSrc
: public nsCSPBaseSrc
{
261 explicit nsCSPHostSrc(const nsAString
& aHost
);
262 virtual ~nsCSPHostSrc();
264 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
265 bool aReportOnly
, bool aUpgradeInsecure
,
266 bool aParserCreated
) const override
;
267 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
268 void toString(nsAString
& outStr
) const override
;
270 void setScheme(const nsAString
& aScheme
);
271 void setPort(const nsAString
& aPort
);
272 void appendPath(const nsAString
& aPath
);
274 inline void setGeneratedFromSelfKeyword() const {
275 mGeneratedFromSelfKeyword
= true;
278 inline void setIsUniqueOrigin() const { mIsUniqueOrigin
= true; }
280 inline void setWithinFrameAncestorsDir(bool aValue
) const {
281 mWithinFrameAncstorsDir
= aValue
;
284 inline void getScheme(nsAString
& outStr
) const { outStr
.Assign(mScheme
); };
286 inline void getHost(nsAString
& outStr
) const { outStr
.Assign(mHost
); };
288 inline void getPort(nsAString
& outStr
) const { outStr
.Assign(mPort
); };
290 inline void getPath(nsAString
& outStr
) const { outStr
.Assign(mPath
); };
297 mutable bool mGeneratedFromSelfKeyword
;
298 mutable bool mIsUniqueOrigin
;
299 mutable bool mWithinFrameAncstorsDir
;
302 /* =============== nsCSPKeywordSrc ============ */
304 class nsCSPKeywordSrc
: public nsCSPBaseSrc
{
306 explicit nsCSPKeywordSrc(CSPKeyword aKeyword
);
307 virtual ~nsCSPKeywordSrc();
309 bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
310 bool aParserCreated
) const override
;
311 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
312 bool aReportOnly
, bool aUpgradeInsecure
,
313 bool aParserCreated
) const override
;
314 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
315 void toString(nsAString
& outStr
) const override
;
317 inline CSPKeyword
getKeyword() const { return mKeyword
; };
319 inline void invalidate() const override
{
320 // keywords that need to invalidated
321 if (mKeyword
== CSP_SELF
|| mKeyword
== CSP_UNSAFE_INLINE
||
322 mKeyword
== CSP_REPORT_SAMPLE
) {
327 bool isReportSample() const override
{ return mKeyword
== CSP_REPORT_SAMPLE
; }
333 /* =============== nsCSPNonceSource =========== */
335 class nsCSPNonceSrc
: public nsCSPBaseSrc
{
337 explicit nsCSPNonceSrc(const nsAString
& aNonce
);
338 virtual ~nsCSPNonceSrc();
340 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
341 bool aReportOnly
, bool aUpgradeInsecure
,
342 bool aParserCreated
) const override
;
343 bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
344 bool aParserCreated
) const override
;
345 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
346 void toString(nsAString
& outStr
) const override
;
348 inline void getNonce(nsAString
& outStr
) const { outStr
.Assign(mNonce
); };
350 inline void invalidate() const override
{
351 // overwrite nsCSPBaseSRC::invalidate() and explicitily
352 // do *not* invalidate, because 'strict-dynamic' should
353 // not invalidate nonces.
360 /* =============== nsCSPHashSource ============ */
362 class nsCSPHashSrc
: public nsCSPBaseSrc
{
364 nsCSPHashSrc(const nsAString
& algo
, const nsAString
& hash
);
365 virtual ~nsCSPHashSrc();
367 bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
368 bool aParserCreated
) const override
;
369 void toString(nsAString
& outStr
) const override
;
370 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
372 inline void getAlgorithm(nsAString
& outStr
) const {
373 outStr
.Assign(mAlgorithm
);
376 inline void getHash(nsAString
& outStr
) const { outStr
.Assign(mHash
); };
378 inline void invalidate() const override
{
379 // overwrite nsCSPBaseSRC::invalidate() and explicitily
380 // do *not* invalidate, because 'strict-dynamic' should
381 // not invalidate hashes.
389 /* =============== nsCSPReportURI ============ */
391 class nsCSPReportURI
: public nsCSPBaseSrc
{
393 explicit nsCSPReportURI(nsIURI
* aURI
);
394 virtual ~nsCSPReportURI();
396 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
397 void toString(nsAString
& outStr
) const override
;
400 nsCOMPtr
<nsIURI
> mReportURI
;
403 /* =============== nsCSPSandboxFlags ================== */
405 class nsCSPSandboxFlags
: public nsCSPBaseSrc
{
407 explicit nsCSPSandboxFlags(const nsAString
& aFlags
);
408 virtual ~nsCSPSandboxFlags();
410 bool visit(nsCSPSrcVisitor
* aVisitor
) const override
;
411 void toString(nsAString
& outStr
) const override
;
417 /* =============== nsCSPSrcVisitor ================== */
419 class nsCSPSrcVisitor
{
421 virtual bool visitSchemeSrc(const nsCSPSchemeSrc
& src
) = 0;
423 virtual bool visitHostSrc(const nsCSPHostSrc
& src
) = 0;
425 virtual bool visitKeywordSrc(const nsCSPKeywordSrc
& src
) = 0;
427 virtual bool visitNonceSrc(const nsCSPNonceSrc
& src
) = 0;
429 virtual bool visitHashSrc(const nsCSPHashSrc
& src
) = 0;
432 explicit nsCSPSrcVisitor() = default;
433 virtual ~nsCSPSrcVisitor() = default;
436 /* =============== nsCSPDirective ============= */
438 class nsCSPDirective
{
440 explicit nsCSPDirective(CSPDirective aDirective
);
441 virtual ~nsCSPDirective();
443 virtual bool permits(nsIURI
* aUri
, const nsAString
& aNonce
,
444 bool aWasRedirected
, bool aReportOnly
,
445 bool aUpgradeInsecure
, bool aParserCreated
) const;
446 virtual bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
447 bool aParserCreated
) const;
448 virtual void toString(nsAString
& outStr
) const;
449 void toDomCSPStruct(mozilla::dom::CSP
& outCSP
) const;
451 virtual void addSrcs(const nsTArray
<nsCSPBaseSrc
*>& aSrcs
) {
452 mSrcs
= aSrcs
.Clone();
455 inline bool isDefaultDirective() const {
456 return mDirective
== nsIContentSecurityPolicy::DEFAULT_SRC_DIRECTIVE
;
459 virtual bool equals(CSPDirective aDirective
) const;
461 void getReportURIs(nsTArray
<nsString
>& outReportURIs
) const;
463 bool visitSrcs(nsCSPSrcVisitor
* aVisitor
) const;
465 virtual void getDirName(nsAString
& outStr
) const;
467 bool hasReportSampleKeyword() const;
470 CSPDirective mDirective
;
471 nsTArray
<nsCSPBaseSrc
*> mSrcs
;
474 /* =============== nsCSPChildSrcDirective ============= */
477 * In CSP 3 child-src is deprecated. For backwards compatibility
478 * child-src needs to restrict:
479 * (*) frames, in case frame-src is not expicitly specified
480 * (*) workers, in case worker-src is not expicitly specified
482 class nsCSPChildSrcDirective
: public nsCSPDirective
{
484 explicit nsCSPChildSrcDirective(CSPDirective aDirective
);
485 virtual ~nsCSPChildSrcDirective();
487 void setRestrictFrames() { mRestrictFrames
= true; }
489 void setRestrictWorkers() { mRestrictWorkers
= true; }
491 virtual bool equals(CSPDirective aDirective
) const override
;
494 bool mRestrictFrames
;
495 bool mRestrictWorkers
;
498 /* =============== nsCSPScriptSrcDirective ============= */
501 * In CSP 3 worker-src restricts workers, for backwards compatibily
502 * script-src has to restrict workers as the ultimate fallback if
503 * neither worker-src nor child-src is present in a CSP.
505 class nsCSPScriptSrcDirective
: public nsCSPDirective
{
507 explicit nsCSPScriptSrcDirective(CSPDirective aDirective
);
508 virtual ~nsCSPScriptSrcDirective();
510 void setRestrictWorkers() { mRestrictWorkers
= true; }
512 virtual bool equals(CSPDirective aDirective
) const override
;
515 bool mRestrictWorkers
;
518 /* =============== nsBlockAllMixedContentDirective === */
520 class nsBlockAllMixedContentDirective
: public nsCSPDirective
{
522 explicit nsBlockAllMixedContentDirective(CSPDirective aDirective
);
523 ~nsBlockAllMixedContentDirective();
525 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
526 bool aReportOnly
, bool aUpgradeInsecure
,
527 bool aParserCreated
) const override
{
531 bool permits(nsIURI
* aUri
) const { return false; }
533 bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
534 bool aParserCreated
) const override
{
538 void toString(nsAString
& outStr
) const override
;
540 void addSrcs(const nsTArray
<nsCSPBaseSrc
*>& aSrcs
) override
{
541 MOZ_ASSERT(false, "block-all-mixed-content does not hold any srcs");
544 void getDirName(nsAString
& outStr
) const override
;
547 /* =============== nsUpgradeInsecureDirective === */
550 * Upgrading insecure requests includes the following actors:
552 * The CSP implementation allowlists the http-request
553 * in case the policy is executed in enforcement mode.
554 * The CSP implementation however does not allow http
555 * requests to succeed if executed in report-only mode.
556 * In such a case the CSP implementation reports the
557 * error back to the page.
560 * The evalution of MixedContent allowlists all http
561 * requests with the promise that the http requests
562 * gets upgraded to https before any data is fetched
566 * Does not consider the http request to be of a
567 * different origin in case the scheme is the only
568 * difference in otherwise matching URIs.
571 * Before connecting, the channel gets redirected
574 * (5) WebSocketChannel:
575 * Similar to the httpChannel, the websocketchannel
576 * gets upgraded from ws to wss.
578 class nsUpgradeInsecureDirective
: public nsCSPDirective
{
580 explicit nsUpgradeInsecureDirective(CSPDirective aDirective
);
581 ~nsUpgradeInsecureDirective();
583 bool permits(nsIURI
* aUri
, const nsAString
& aNonce
, bool aWasRedirected
,
584 bool aReportOnly
, bool aUpgradeInsecure
,
585 bool aParserCreated
) const override
{
589 bool permits(nsIURI
* aUri
) const { return false; }
591 bool allows(enum CSPKeyword aKeyword
, const nsAString
& aHashOrNonce
,
592 bool aParserCreated
) const override
{
596 void toString(nsAString
& outStr
) const override
;
598 void addSrcs(const nsTArray
<nsCSPBaseSrc
*>& aSrcs
) override
{
599 MOZ_ASSERT(false, "upgrade-insecure-requests does not hold any srcs");
602 void getDirName(nsAString
& outStr
) const override
;
605 /* =============== nsCSPPolicy ================== */
610 virtual ~nsCSPPolicy();
612 bool permits(CSPDirective aDirective
, nsIURI
* aUri
, const nsAString
& aNonce
,
613 bool aWasRedirected
, bool aSpecific
, bool aParserCreated
,
614 nsAString
& outViolatedDirective
) const;
615 bool allows(CSPDirective aDirective
, enum CSPKeyword aKeyword
,
616 const nsAString
& aHashOrNonce
, bool aParserCreated
) const;
617 void toString(nsAString
& outStr
) const;
618 void toDomCSPStruct(mozilla::dom::CSP
& outCSP
) const;
620 inline void addDirective(nsCSPDirective
* aDir
) {
621 mDirectives
.AppendElement(aDir
);
624 inline void addUpgradeInsecDir(nsUpgradeInsecureDirective
* aDir
) {
625 mUpgradeInsecDir
= aDir
;
629 bool hasDirective(CSPDirective aDir
) const;
631 inline void setDeliveredViaMetaTagFlag(bool aFlag
) {
632 mDeliveredViaMetaTag
= aFlag
;
635 inline bool getDeliveredViaMetaTagFlag() const {
636 return mDeliveredViaMetaTag
;
639 inline void setReportOnlyFlag(bool aFlag
) { mReportOnly
= aFlag
; }
641 inline bool getReportOnlyFlag() const { return mReportOnly
; }
643 void getReportURIs(nsTArray
<nsString
>& outReportURIs
) const;
645 void getDirectiveStringAndReportSampleForContentType(
646 CSPDirective aDirective
, nsAString
& outDirective
,
647 bool* aReportSample
) const;
649 void getDirectiveAsString(CSPDirective aDir
, nsAString
& outDirective
) const;
651 uint32_t getSandboxFlags() const;
653 inline uint32_t getNumDirectives() const { return mDirectives
.Length(); }
655 bool visitDirectiveSrcs(CSPDirective aDir
, nsCSPSrcVisitor
* aVisitor
) const;
657 bool allowsNavigateTo(nsIURI
* aURI
, bool aWasRedirected
,
658 bool aEnforceAllowlist
) const;
661 nsUpgradeInsecureDirective
* mUpgradeInsecDir
;
662 nsTArray
<nsCSPDirective
*> mDirectives
;
664 bool mDeliveredViaMetaTag
;
667 #endif /* nsCSPUtils_h___ */