Skip to content

Truly independent web browser

LibWeb: Withhold the nonce of script elements that are not nonceable

Tim Ledbetter35d7c644b5a8
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
index 2f955d4c1b5..d6c7a8019f0 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.cpp
@@ -106,13 +106,8 @@ static AllowsResult does_a_source_list_allow_all_inline_behavior_for_type(Vector
return allow_all_inline ? AllowsResult::Allows : AllowsResult::DoesNotAllow;
}
-enum class NonceableResult {
- NotNonceable,
- Nonceable,
-};
-
// https://w3c.github.io/webappsec-csp/#is-element-nonceable
-[[nodiscard]] static NonceableResult is_element_nonceable(GC::Ptr<DOM::Element const> element)
+Nonceable is_element_nonceable(GC::Ptr<DOM::Element const> element)
{
// SPEC ISSUE 7: This processing is meant to mitigate the risk of dangling markup attacks that steal the nonce from
// an existing element in order to load injected script. It is fairly expensive, however, as it
@@ -123,17 +118,17 @@ enum class NonceableResult {
// FIXME: See FIXME in `does_element_match_source_list_for_type_and_source`
if (!element)
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
// 1. If element does not have an attribute named "nonce", return "Not Nonceable".
if (!is<HTML::HTMLElement>(element.ptr()) && !is<SVG::SVGElement>(element.ptr()))
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
// INTEROP: An element whose [[CryptographicNonce]] was set through the nonce IDL attribute, which intentionally
// does not update the content attribute, is treated as if it had a nonce attribute.
auto const& internal_nonce = is<HTML::HTMLElement>(element.ptr()) ? as<HTML::HTMLElement>(*element).nonce() : as<SVG::SVGElement>(*element).nonce();
if (!element->has_attribute(HTML::AttributeNames::nonce) && internal_nonce.is_empty())
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
// 2. If element is a script element, then for each attribute of element’s attribute list:
// FIXME: File spec issue to ask if this should include SVGScriptElement.
@@ -151,12 +146,12 @@ enum class NonceableResult {
// 1. If attribute’s name contains an ASCII case-insensitive match for "<link", "<script", or "<style",
// return "Not Nonceable".
if (contains_dangling_markup(attribute->name().view()))
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
// 2. If attribute’s value contains an ASCII case-insensitive match for "<link", "<script", or "<style",
// return "Not Nonceable".
if (contains_dangling_markup(attribute->value()))
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
}
}
@@ -164,10 +159,10 @@ enum class NonceableResult {
// SPEC ISSUE 6: We need some sort of hook in HTML to record this error if we’re planning on using it here.
// [Issue #whatwg/html#3257] (https://github.com/whatwg/html/issues/3257)
if (element->had_duplicate_attribute_during_tokenization())
- return NonceableResult::NotNonceable;
+ return Nonceable::No;
// 4. Return "Nonceable".
- return NonceableResult::Nonceable;
+ return Nonceable::Yes;
}
// https://w3c.github.io/webappsec-csp/#match-element-to-source-list
@@ -193,7 +188,7 @@ static MatchResult does_element_match_source_list_for_type_and_source(GC::Ptr<DO
// the content attribute. Keep applying the nonceability checks above to script elements to prevent dangling markup
// attacks.
auto is_nonceable = (type == Directive::InlineType::Style && element && (is<HTML::HTMLStyleElement>(element.ptr()) || is<SVG::SVGStyleElement>(element.ptr())))
- || (type == Directive::InlineType::Script && is_element_nonceable(element) == NonceableResult::Nonceable);
+ || (type == Directive::InlineType::Script && is_element_nonceable(element) == Nonceable::Yes);
if (is_nonceable) {
// 1. For each expression of list:
for (auto const& expression : source_list) {
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h
index 26153218fa5..9a42133cf76 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h
@@ -16,6 +16,13 @@
namespace Web::ContentSecurityPolicy::Directives {
+enum class Nonceable {
+ No,
+ Yes,
+};
+
+[[nodiscard]] Nonceable is_element_nonceable(GC::Ptr<DOM::Element const>);
+
// https://w3c.github.io/webappsec-csp/#effective-directive-for-inline-check
[[nodiscard]] Utf16FlyString get_the_effective_directive_for_inline_checks(Directive::InlineType);
diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
index d84a5261be5..4d9db7e0a77 100644
--- a/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.cpp
@@ -9,6 +9,7 @@
#include <AK/Utf16StringBuilder.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
+#include <LibWeb/ContentSecurityPolicy/Directives/HTMLIntegration.h>
#include <LibWeb/DOM/DOMTokenList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
@@ -215,26 +216,30 @@ void HTMLScriptElement::execute_script()
}
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
-ScriptFetchOptions script_element_fetch_options(ScriptElementFetchAttributes const& element)
+ScriptFetchOptions script_element_fetch_options(DOM::Element const& element, ScriptElementFetchAttributes const& attributes)
{
// 25. Let module script credentials mode be the CORS settings attribute credentials mode for el's crossorigin content attribute.
- auto module_script_credential_mode = cors_settings_attribute_credentials_mode(element.crossorigin);
+ auto module_script_credential_mode = cors_settings_attribute_credentials_mode(attributes.crossorigin);
// 26. Let cryptographic nonce be el's [[CryptographicNonce]] internal slot's value.
- auto cryptographic_nonce = element.cryptographic_nonce;
+ // AD-HOC: A script element that is not nonceable gets no nonce. Content Security Policy only checks this for inline
+ // scripts, which lets dangling markup take the nonce of a following element to load an external script.
+ auto cryptographic_nonce = ContentSecurityPolicy::Directives::is_element_nonceable(element) == ContentSecurityPolicy::Directives::Nonceable::Yes
+ ? attributes.cryptographic_nonce
+ : Utf16String {};
// 27. If el has an integrity attribute, then let integrity metadata be that attribute's value.
// Otherwise, let integrity metadata be the empty string.
- auto integrity_metadata = element.integrity.value_or({});
+ auto integrity_metadata = attributes.integrity.value_or({});
// 28. Let referrer policy be the current state of el's referrerpolicy content attribute.
- auto referrer_policy = element.referrer_policy;
+ auto referrer_policy = attributes.referrer_policy;
// 29. Let fetch priority be the current state of el's fetchpriority content attribute.
- auto fetch_priority = element.fetch_priority;
+ auto fetch_priority = attributes.fetch_priority;
// 30. Let parser metadata be "parser-inserted" if el is parser-inserted, and "not-parser-inserted" otherwise.
- auto parser_metadata = element.parser_inserted
+ auto parser_metadata = attributes.parser_inserted
? Fetch::Infrastructure::Request::ParserMetadata::ParserInserted
: Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted;
@@ -426,14 +431,15 @@ void HTMLScriptElement::prepare_script()
// 25-31. Let options be a script fetch options for el.
auto fetch_priority = Fetch::Infrastructure::request_priority_from_string(attribute(HTML::AttributeNames::fetchpriority).value_or({})).value_or(Fetch::Infrastructure::Request::Priority::Auto);
- auto options = script_element_fetch_options({
+ ScriptElementFetchAttributes fetch_attributes {
.crossorigin = m_crossorigin,
.cryptographic_nonce = nonce(),
.integrity = get_attribute_ns({}, HTML::AttributeNames::integrity),
.referrer_policy = m_referrer_policy,
.fetch_priority = fetch_priority,
.parser_inserted = is_parser_inserted(),
- });
+ };
+ auto options = script_element_fetch_options(*this, fetch_attributes);
// 32. Let settings object be el's node document's relevant settings object.
auto& settings_object = document().relevant_settings_object();
diff --git a/Libraries/LibWeb/HTML/HTMLScriptElement.h b/Libraries/LibWeb/HTML/HTMLScriptElement.h
index e695a3de5a1..63904b8cacc 100644
--- a/Libraries/LibWeb/HTML/HTMLScriptElement.h
+++ b/Libraries/LibWeb/HTML/HTMLScriptElement.h
@@ -172,7 +172,7 @@ struct ScriptElementFetchAttributes {
bool parser_inserted { false };
};
-ScriptFetchOptions script_element_fetch_options(ScriptElementFetchAttributes const&);
+ScriptFetchOptions script_element_fetch_options(DOM::Element const&, ScriptElementFetchAttributes const&);
}
diff --git a/Libraries/LibWeb/SVG/SVGScriptElement.cpp b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
index 0f3e8145227..de7080b1c83 100644
--- a/Libraries/LibWeb/SVG/SVGScriptElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
@@ -128,12 +128,13 @@ void SVGScriptElement::process_the_script_element()
// NB: An SVG script element has no integrity or fetchpriority attribute.
auto referrer_policy_attribute = get_attribute(HTML::AttributeNames::referrerpolicy).value_or({});
auto referrer_policy = ReferrerPolicy::from_string(referrer_policy_attribute).value_or(ReferrerPolicy::ReferrerPolicy::EmptyString);
- auto options = HTML::script_element_fetch_options({
+ HTML::ScriptElementFetchAttributes fetch_attributes {
.crossorigin = cors_setting,
.cryptographic_nonce = nonce(),
.referrer_policy = referrer_policy,
.parser_inserted = is_parser_inserted(),
- });
+ };
+ auto options = HTML::script_element_fetch_options(*this, fetch_attributes);
// 32. Let settings object be el's node document's relevant settings object.
auto& settings_object = document().relevant_settings_object();
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-nonce-dangling-markup.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-nonce-dangling-markup.txt
new file mode 100644
index 00000000000..2eec4a8e44d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/script-nonce-dangling-markup.txt
@@ -0,0 +1,2 @@
+PASS: external script with a nonce executed
+PASS: external script with markup in an attribute blocked
diff --git a/Tests/LibWeb/Text/input/ContentSecurityPolicy/script-nonce-dangling-markup.html b/Tests/LibWeb/Text/input/ContentSecurityPolicy/script-nonce-dangling-markup.html
new file mode 100644
index 00000000000..bb4721de810
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ContentSecurityPolicy/script-nonce-dangling-markup.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-good'">
+<script nonce=good src="data:text/javascript,globalThis.externalScriptExecuted = true;"></script>
+<script nonce=good data="<script" src="data:text/javascript,globalThis.danglingMarkupScriptExecuted = true;"></script>
+<script nonce=good>
+ test(() => {
+ println(globalThis.externalScriptExecuted ? "PASS: external script with a nonce executed" : "FAIL: external script with a nonce blocked");
+ println(globalThis.danglingMarkupScriptExecuted ? "FAIL: external script with markup in an attribute executed" : "PASS: external script with markup in an attribute blocked");
+ });
+</script>