2020-01-12 22:50:13 +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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "DOMSecurityMonitor.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2020-01-16 19:06:42 +03:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsILoadInfo.h"
|
2020-01-12 22:50:13 +03:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIURI.h"
|
2020-11-10 00:03:52 +03:00
|
|
|
#include "nsJSUtils.h"
|
2020-01-12 22:50:13 +03:00
|
|
|
|
2021-08-22 13:21:55 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2020-01-22 14:04:17 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
|
|
|
|
2020-01-12 22:50:13 +03:00
|
|
|
/* static */
|
|
|
|
void DOMSecurityMonitor::AuditParsingOfHTMLXMLFragments(
|
|
|
|
nsIPrincipal* aPrincipal, const nsAString& aFragment) {
|
|
|
|
// if the fragment parser (e.g. innerHTML()) is not called in chrome: code
|
|
|
|
// or any of our about: pages, then there is nothing to do here.
|
|
|
|
if (!aPrincipal->IsSystemPrincipal() && !aPrincipal->SchemeIs("about")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the fragment is empty, if so, we can return early.
|
|
|
|
if (aFragment.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there is a JS caller, if not, then we can can return early here
|
|
|
|
// because we only care about calls to the fragment parser (e.g. innerHTML)
|
|
|
|
// originating from JS code.
|
|
|
|
nsAutoString filename;
|
|
|
|
uint32_t lineNum = 0;
|
|
|
|
uint32_t columnNum = 0;
|
|
|
|
JSContext* cx = nsContentUtils::GetCurrentJSContext();
|
2021-08-03 19:25:17 +03:00
|
|
|
if (!cx ||
|
|
|
|
!nsJSUtils::GetCallingLocation(cx, filename, &lineNum, &columnNum)) {
|
2020-01-12 22:50:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we should skip assertion. Please only ever set this pref to
|
|
|
|
// true if really needed for testing purposes.
|
2020-11-10 00:03:52 +03:00
|
|
|
if (mozilla::StaticPrefs::dom_security_skip_html_fragment_assertion()) {
|
2020-01-12 22:50:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* WARNING: Do not add any new entries to the htmlFragmentAllowlist
|
|
|
|
* withiout proper review from a dom:security peer!
|
|
|
|
*/
|
|
|
|
static nsLiteralCString htmlFragmentAllowlist[] = {
|
|
|
|
"chrome://global/content/elements/marquee.js"_ns,
|
|
|
|
nsLiteralCString(
|
|
|
|
"chrome://pocket/content/panels/js/vendor/jquery-2.1.1.min.js"),
|
2021-04-14 13:24:38 +03:00
|
|
|
"chrome://browser/content/certerror/aboutNetError.js"_ns,
|
2020-01-12 22:50:13 +03:00
|
|
|
nsLiteralCString("chrome://devtools/content/shared/sourceeditor/"
|
|
|
|
"codemirror/codemirror.bundle.js"),
|
|
|
|
nsLiteralCString(
|
|
|
|
"resource://activity-stream/data/content/activity-stream.bundle.js"),
|
|
|
|
nsLiteralCString("resource://devtools/client/debugger/src/components/"
|
|
|
|
"Editor/Breakpoint.js"),
|
|
|
|
nsLiteralCString("resource://devtools/client/debugger/src/components/"
|
|
|
|
"Editor/ColumnBreakpoint.js"),
|
|
|
|
nsLiteralCString(
|
|
|
|
"resource://devtools/client/shared/vendor/fluent-react.js"),
|
|
|
|
"resource://devtools/client/shared/vendor/react-dom.js"_ns,
|
2020-05-13 01:19:53 +03:00
|
|
|
nsLiteralCString(
|
|
|
|
"resource://devtools/client/shared/vendor/react-dom-dev.js"),
|
2020-01-12 22:50:13 +03:00
|
|
|
nsLiteralCString(
|
|
|
|
"resource://devtools/client/shared/widgets/FilterWidget.js"),
|
|
|
|
nsLiteralCString("resource://devtools/client/shared/widgets/tooltip/"
|
|
|
|
"inactive-css-tooltip-helper.js"),
|
|
|
|
"resource://devtools/client/shared/widgets/Spectrum.js"_ns,
|
|
|
|
"resource://gre/modules/narrate/VoiceSelect.jsm"_ns,
|
|
|
|
"resource://normandy-vendor/ReactDOM.js"_ns,
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// test pages
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
"chrome://mochikit/content/harness.xhtml"_ns,
|
|
|
|
"chrome://mochikit/content/tests/"_ns,
|
|
|
|
"chrome://mochitests/content/"_ns,
|
|
|
|
"chrome://reftest/content/"_ns,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const nsLiteralCString& allowlistEntry : htmlFragmentAllowlist) {
|
|
|
|
if (StringBeginsWith(NS_ConvertUTF16toUTF8(filename), allowlistEntry)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
aPrincipal->GetAsciiSpec(uriSpec);
|
|
|
|
|
|
|
|
// Ideally we should not call the fragment parser (e.g. innerHTML()) in
|
|
|
|
// chrome: code or any of our about: pages. If you hit that assertion,
|
|
|
|
// please do *not* add your filename to the allowlist above, but rather
|
|
|
|
// refactor your code.
|
|
|
|
fprintf(stderr,
|
|
|
|
"Do not call the fragment parser (e.g innerHTML()) in chrome code "
|
|
|
|
"or in about: pages, (uri: %s), (caller: %s, line: %d, col: %d), "
|
|
|
|
"(fragment: %s)",
|
|
|
|
uriSpec.get(), NS_ConvertUTF16toUTF8(filename).get(), lineNum,
|
|
|
|
columnNum, NS_ConvertUTF16toUTF8(aFragment).get());
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2020-01-16 19:06:42 +03:00
|
|
|
|
|
|
|
/* static */
|
|
|
|
void DOMSecurityMonitor::AuditUseOfJavaScriptURI(nsIChannel* aChannel) {
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
|
2020-04-06 21:57:36 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrincipal = loadInfo->GetLoadingPrincipal();
|
2020-01-16 19:06:42 +03:00
|
|
|
|
|
|
|
// We only ever have no loadingPrincipal in case of a new top-level load.
|
|
|
|
// The purpose of this assertion is to make sure we do not allow loading
|
|
|
|
// javascript: URIs in system privileged contexts. Hence there is nothing
|
|
|
|
// to do here in case there is no loadingPrincipal.
|
|
|
|
if (!loadingPrincipal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the javascript: URI is not loaded by a system privileged context
|
|
|
|
// or an about: page, there there is nothing to do here.
|
|
|
|
if (!loadingPrincipal->IsSystemPrincipal() &&
|
|
|
|
!loadingPrincipal->SchemeIs("about")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"Do not use javascript: URIs in chrome code or in about: pages");
|
|
|
|
}
|