Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / dom / security / DOMSecurityMonitor.cpp
blob7c7fcb53bc3b98994f83580ec14eb6eb9eeec00f
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DOMSecurityMonitor.h"
8 #include "nsContentUtils.h"
10 #include "nsIChannel.h"
11 #include "nsILoadInfo.h"
12 #include "nsIPrincipal.h"
13 #include "nsIURI.h"
14 #include "nsJSUtils.h"
15 #include "xpcpublic.h"
17 #include "mozilla/BasePrincipal.h"
18 #include "mozilla/StaticPrefs_dom.h"
20 /* static */
21 void DOMSecurityMonitor::AuditParsingOfHTMLXMLFragments(
22 nsIPrincipal* aPrincipal, const nsAString& aFragment) {
23 // if the fragment parser (e.g. innerHTML()) is not called in chrome: code
24 // or any of our about: pages, then there is nothing to do here.
25 if (!aPrincipal->IsSystemPrincipal() && !aPrincipal->SchemeIs("about")) {
26 return;
29 // check if the fragment is empty, if so, we can return early.
30 if (aFragment.IsEmpty()) {
31 return;
34 // check if there is a JS caller, if not, then we can can return early here
35 // because we only care about calls to the fragment parser (e.g. innerHTML)
36 // originating from JS code.
37 auto loc = mozilla::JSCallingLocation::Get();
38 if (!loc) {
39 return;
42 // check if we should skip assertion. Please only ever set this pref to
43 // true if really needed for testing purposes.
44 if (mozilla::StaticPrefs::dom_security_skip_html_fragment_assertion()) {
45 return;
49 * WARNING: Do not add any new entries to the htmlFragmentAllowlist
50 * without proper review from a dom:security peer!
52 static nsLiteralCString htmlFragmentAllowlist[] = {
53 "chrome://global/content/elements/marquee.js"_ns,
54 nsLiteralCString(
55 "chrome://pocket/content/panels/js/vendor/jquery-2.1.1.min.js"),
56 nsLiteralCString("chrome://devtools/content/shared/sourceeditor/"
57 "codemirror/codemirror.bundle.js"),
58 nsLiteralCString(
59 "resource://activity-stream/data/content/activity-stream.bundle.js"),
60 nsLiteralCString("resource://devtools/client/debugger/src/components/"
61 "Editor/Breakpoint.js"),
62 nsLiteralCString("resource://devtools/client/debugger/src/components/"
63 "Editor/ColumnBreakpoint.js"),
64 nsLiteralCString(
65 "resource://devtools/client/shared/vendor/fluent-react.js"),
66 "resource://devtools/client/shared/vendor/react-dom.js"_ns,
67 nsLiteralCString(
68 "resource://devtools/client/shared/vendor/react-dom-dev.js"),
69 nsLiteralCString(
70 "resource://devtools/client/shared/widgets/FilterWidget.js"),
71 nsLiteralCString("resource://devtools/client/shared/widgets/tooltip/"
72 "inactive-css-tooltip-helper.js"),
73 "resource://devtools/client/shared/widgets/Spectrum.js"_ns,
74 "resource://gre/modules/narrate/VoiceSelect.sys.mjs"_ns,
75 "chrome://global/content/vendor/react-dom.js"_ns,
76 // ------------------------------------------------------------------
77 // test pages
78 // ------------------------------------------------------------------
79 "chrome://mochikit/content/browser-harness.xhtml"_ns,
80 "chrome://mochikit/content/harness.xhtml"_ns,
81 "chrome://mochikit/content/tests/"_ns,
82 "chrome://mochitests/content/"_ns,
83 "chrome://reftest/content/"_ns,
86 for (const nsLiteralCString& allowlistEntry : htmlFragmentAllowlist) {
87 if (StringBeginsWith(loc.FileName(), allowlistEntry)) {
88 return;
92 nsAutoCString uriSpec;
93 aPrincipal->GetAsciiSpec(uriSpec);
95 // Ideally we should not call the fragment parser (e.g. innerHTML()) in
96 // chrome: code or any of our about: pages. If you hit that assertion,
97 // please do *not* add your filename to the allowlist above, but rather
98 // refactor your code.
99 fprintf(stderr,
100 "Do not call the fragment parser (e.g innerHTML()) in chrome code "
101 "or in about: pages, (uri: %s), (caller: %s, line: %d, col: %d), "
102 "(fragment: %s)",
103 uriSpec.get(), loc.FileName().get(), loc.mLine, loc.mColumn,
104 NS_ConvertUTF16toUTF8(aFragment).get());
106 xpc_DumpJSStack(true, true, false);
107 MOZ_ASSERT(false);
110 /* static */
111 void DOMSecurityMonitor::AuditUseOfJavaScriptURI(nsIChannel* aChannel) {
112 nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
113 nsCOMPtr<nsIPrincipal> loadingPrincipal = loadInfo->GetLoadingPrincipal();
115 // We only ever have no loadingPrincipal in case of a new top-level load.
116 // The purpose of this assertion is to make sure we do not allow loading
117 // javascript: URIs in system privileged contexts. Hence there is nothing
118 // to do here in case there is no loadingPrincipal.
119 if (!loadingPrincipal) {
120 return;
123 // if the javascript: URI is not loaded by a system privileged context
124 // or an about: page, there there is nothing to do here.
125 if (!loadingPrincipal->IsSystemPrincipal() &&
126 !loadingPrincipal->SchemeIs("about")) {
127 return;
130 MOZ_ASSERT(false,
131 "Do not use javascript: URIs in chrome code or in about: pages");