Bug 1688354 [wpt PR 27298] - Treat 'rem' as an absolute unit for font size, a=testonly
[gecko.git] / dom / security / nsCSPUtils.h
blob9ba39d782994f47bd9c5adf05d97388bff890859
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___
10 #include "nsCOMPtr.h"
11 #include "nsIContentSecurityPolicy.h"
12 #include "nsIURI.h"
13 #include "nsLiteralString.h"
14 #include "nsString.h"
15 #include "nsTArray.h"
16 #include "nsUnicharUtils.h"
17 #include "mozilla/Logging.h"
19 class nsIChannel;
21 namespace mozilla {
22 namespace dom {
23 struct CSP;
24 class Document;
25 } // namespace dom
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'")
120 enum CSPKeyword {
121 #define KEYWORD_ENUM(id_, string_) id_,
122 FOR_EACH_CSP_KEYWORD(KEYWORD_ENUM)
123 #undef 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.
132 CSP_HASH
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,
192 bool aReportOnly);
194 /* =============== Helpers ================== */
196 class nsCSPHostSrc;
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 ================== */
215 class nsCSPBaseSrc {
216 public:
217 nsCSPBaseSrc();
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; }
232 protected:
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 {
241 public:
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); };
253 private:
254 nsString mScheme;
257 /* =============== nsCSPHostSrc ============== */
259 class nsCSPHostSrc : public nsCSPBaseSrc {
260 public:
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); };
292 private:
293 nsString mScheme;
294 nsString mHost;
295 nsString mPort;
296 nsString mPath;
297 mutable bool mGeneratedFromSelfKeyword;
298 mutable bool mIsUniqueOrigin;
299 mutable bool mWithinFrameAncstorsDir;
302 /* =============== nsCSPKeywordSrc ============ */
304 class nsCSPKeywordSrc : public nsCSPBaseSrc {
305 public:
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) {
323 mInvalidated = true;
327 bool isReportSample() const override { return mKeyword == CSP_REPORT_SAMPLE; }
329 private:
330 CSPKeyword mKeyword;
333 /* =============== nsCSPNonceSource =========== */
335 class nsCSPNonceSrc : public nsCSPBaseSrc {
336 public:
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.
356 private:
357 nsString mNonce;
360 /* =============== nsCSPHashSource ============ */
362 class nsCSPHashSrc : public nsCSPBaseSrc {
363 public:
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.
384 private:
385 nsString mAlgorithm;
386 nsString mHash;
389 /* =============== nsCSPReportURI ============ */
391 class nsCSPReportURI : public nsCSPBaseSrc {
392 public:
393 explicit nsCSPReportURI(nsIURI* aURI);
394 virtual ~nsCSPReportURI();
396 bool visit(nsCSPSrcVisitor* aVisitor) const override;
397 void toString(nsAString& outStr) const override;
399 private:
400 nsCOMPtr<nsIURI> mReportURI;
403 /* =============== nsCSPSandboxFlags ================== */
405 class nsCSPSandboxFlags : public nsCSPBaseSrc {
406 public:
407 explicit nsCSPSandboxFlags(const nsAString& aFlags);
408 virtual ~nsCSPSandboxFlags();
410 bool visit(nsCSPSrcVisitor* aVisitor) const override;
411 void toString(nsAString& outStr) const override;
413 private:
414 nsString mFlags;
417 /* =============== nsCSPSrcVisitor ================== */
419 class nsCSPSrcVisitor {
420 public:
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;
431 protected:
432 explicit nsCSPSrcVisitor() = default;
433 virtual ~nsCSPSrcVisitor() = default;
436 /* =============== nsCSPDirective ============= */
438 class nsCSPDirective {
439 public:
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;
469 protected:
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 {
483 public:
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;
493 private:
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 {
506 public:
507 explicit nsCSPScriptSrcDirective(CSPDirective aDirective);
508 virtual ~nsCSPScriptSrcDirective();
510 void setRestrictWorkers() { mRestrictWorkers = true; }
512 virtual bool equals(CSPDirective aDirective) const override;
514 private:
515 bool mRestrictWorkers;
518 /* =============== nsBlockAllMixedContentDirective === */
520 class nsBlockAllMixedContentDirective : public nsCSPDirective {
521 public:
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 {
528 return false;
531 bool permits(nsIURI* aUri) const { return false; }
533 bool allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce,
534 bool aParserCreated) const override {
535 return false;
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:
551 * (1) CSP:
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.
559 * (2) MixedContent:
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
563 * from the network.
565 * (3) CORS:
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.
570 * (4) nsHttpChannel:
571 * Before connecting, the channel gets redirected
572 * to use https.
574 * (5) WebSocketChannel:
575 * Similar to the httpChannel, the websocketchannel
576 * gets upgraded from ws to wss.
578 class nsUpgradeInsecureDirective : public nsCSPDirective {
579 public:
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 {
586 return false;
589 bool permits(nsIURI* aUri) const { return false; }
591 bool allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce,
592 bool aParserCreated) const override {
593 return false;
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 ================== */
607 class nsCSPPolicy {
608 public:
609 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;
626 addDirective(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;
660 private:
661 nsUpgradeInsecureDirective* mUpgradeInsecDir;
662 nsTArray<nsCSPDirective*> mDirectives;
663 bool mReportOnly;
664 bool mDeliveredViaMetaTag;
667 #endif /* nsCSPUtils_h___ */