Skip to content

Truly independent web browser

LibWeb: Don't split surrogate pairs in Trusted Types violation samples

Shannon Booth299800364be3
diff --git a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
index fae19dd0622..3604619d859 100644
--- a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
+++ b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp
@@ -37,6 +37,16 @@ static bool sink_group_matches(Utf16String const& directive_value, Utf16View sin
return directive_value == sink_group;
}
+static Utf16View first_40_characters(Utf16View sample)
+{
+ // INTEROP: Count UTF-16 code units, but don't split a surrogate pair, as Firefox and WPT do.
+ // https://github.com/w3c/webappsec-csp/issues/704
+ auto length = min(sample.length_in_code_units(), 40uz);
+ if (length < sample.length_in_code_units() && AK::UnicodeUtils::is_utf16_high_surrogate(sample.code_unit_at(length - 1)) && AK::UnicodeUtils::is_utf16_low_surrogate(sample.code_unit_at(length)))
+ ++length;
+ return sample.substring_view(0, length);
+}
+
static ContentSecurityPolicy::PolicyList const* csp_list_of_global(JS::Object& global)
{
if (auto* window = Bindings::window_from_global_object(global))
@@ -206,7 +216,7 @@ ContentSecurityPolicy::Directives::Directive::Result should_sink_type_mismatch_v
violation->set_resource(ContentSecurityPolicy::Violation::Resource::TrustedTypesSink);
// 6. Let trimmedSample be the substring of sample, containing its first 40 characters.
- auto const trimmed_sample = sample.substring_view(0, min(sample.length_in_code_points(), 40));
+ auto const trimmed_sample = first_40_characters(sample);
// 7. Set violation’s sample to be the result of concatenating the list « sink, trimmedSample « using "|" as a separator.
Utf16StringBuilder sample_builder;
@@ -281,8 +291,7 @@ ContentSecurityPolicy::Directives::Directive::Result should_trusted_type_policy_
violation->set_resource(ContentSecurityPolicy::Violation::Resource::TrustedTypesPolicy);
// 10. Set violation’s sample to the substring of policyName, containing its first 40 characters.
- auto sample = policy_name.substring_view(0, min(policy_name.length_in_code_points(), 40));
- violation->set_sample(Utf16String::from_utf16(sample));
+ violation->set_sample(Utf16String::from_utf16(first_40_characters(policy_name)));
// 11. Execute Report a violation on violation.
violation->report_a_violation(realm);
diff --git a/Tests/LibWeb/Text/expected/TrustedTypes/violation-sample-clipping.txt b/Tests/LibWeb/Text/expected/TrustedTypes/violation-sample-clipping.txt
new file mode 100644
index 00000000000..70a1c0931ab
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/TrustedTypes/violation-sample-clipping.txt
@@ -0,0 +1,7 @@
+case 0: createPolicy true, innerHTML true
+case 1: createPolicy true, innerHTML true
+case 2: createPolicy true, innerHTML true
+case 3: createPolicy true, innerHTML true
+case 4: createPolicy true, innerHTML true
+case 5: createPolicy true, innerHTML true
+case 6: createPolicy true, innerHTML true
diff --git a/Tests/LibWeb/Text/input/TrustedTypes/violation-sample-clipping.html b/Tests/LibWeb/Text/input/TrustedTypes/violation-sample-clipping.html
new file mode 100644
index 00000000000..5988fc535d2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/TrustedTypes/violation-sample-clipping.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<meta charset="UTF-8">
+<meta http-equiv="Content-Security-Policy" content="trusted-types 'none'; require-trusted-types-for 'script'">
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const length = 40;
+ const cases = [
+ ["A".repeat(length - 1), "A".repeat(length - 1)],
+ ["B".repeat(length + 1), "B".repeat(length)],
+ ["℉".repeat(length + 1), "℉".repeat(length)],
+ ["\u{1D406}".repeat(length / 2), "\u{1D406}".repeat(length / 2)],
+ ["H" + "\u{1D407}".repeat(length / 2), "H" + "\u{1D407}".repeat(length / 2)],
+ ["\u{1D408}".repeat(length / 2) + "I", "\u{1D408}".repeat(length / 2)],
+ ["\u{1D409}".repeat(length / 2 + 1), "\u{1D409}".repeat(length / 2)],
+ ];
+
+ for (const [index, [input, clipped]] of cases.entries()) {
+ const violations = [];
+ const reported = new Promise(resolve => {
+ const handler = event => {
+ violations.push(event.sample);
+ if (violations.length === 2) {
+ document.removeEventListener("securitypolicyviolation", handler);
+ resolve();
+ }
+ };
+ document.addEventListener("securitypolicyviolation", handler);
+ });
+ try {
+ trustedTypes.createPolicy(input, {});
+ } catch {}
+ try {
+ document.createElement("div").innerHTML = input;
+ } catch {}
+ await reported;
+ println(`case ${index}: createPolicy ${violations[0] === clipped}, innerHTML ${violations[1] === `Element innerHTML|${clipped}`}`);
+ }
+ });
+</script>