Truly independent web browser
LibWeb: Support the trusted-types-eval source expression
Andreas Klingbdc18b752987
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cppindex 22fe7be6d58..74e87beaf33 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp@@ -6,7 +6,6 @@*/#include <AK/AnyOf.h>-#include <LibGC/Heap.h>#include <LibWeb/Bindings/PrincipalHostDefined.h>#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.h>@@ -26,7 +25,6 @@#include <LibWeb/HTML/Window.h>#include <LibWeb/Page/Page.h>#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>-#include <LibWeb/TrustedTypes/TrustedScript.h>#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>#include <LibWeb/WebAssembly/WebAssembly.h>#include <LibWebCommon/Fetch/Infrastructure/URL.h>@@ -326,31 +324,30 @@ Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by// https://w3c.github.io/webappsec-csp/#can-compile-stringsJS::ThrowCompletionOr<void> ensure_csp_does_not_block_string_compilation(JS::Realm& realm, ReadonlySpan<Utf16String> parameter_strings, Utf16View body_string, Utf16View code_string, JS::CompilationType compilation_type, ReadonlySpan<JS::Value> parameter_args, JS::Value body_arg){- Utf16String source_string;+ // 1. Let sourceString be codeString.+ auto source_string = Utf16String::from_utf16(code_string);- // 1. If compilationType is "TIMER", then:- if (compilation_type == JS::CompilationType::Timer) {- // 1. Let sourceString be codeString.- source_string = Utf16String::from_utf16(code_string);- }- // 2. Else:- else {+ // 2. If compilationType is not "TIMER", then:+ if (compilation_type != JS::CompilationType::Timer) {// 1. Let compilationSink be "Function" if compilationType is "FUNCTION", and "eval" otherwise.auto const compilation_sink = compilation_type == JS::CompilationType::Function ? TrustedTypes::InjectionSink::Function : TrustedTypes::InjectionSink::Eval;// 2. Let isTrusted be true if bodyArg implements TrustedScript, and false otherwise.auto is_trusted = TrustedTypes::trusted_script_value_matches(body_arg, body_string.to_utf8_but_should_be_ported_to_utf16());- // 3. If isTrusted is true then:- // NOTE: trusted_script_value_matches() also performs the data equality check from this step.-- // 4. If isTrusted is true, then:+ // 3. If isTrusted is true, then:if (is_trusted) {- // 1. Assert: parameterArgs’ [list/size=] is equal to [parameterStrings]' size.+ // 1. Assert: bodyString is equal to bodyArg’s data.+ // NB: trusted_script_value_matches() also performs the data equality check from this step.++ // 2. Assert: parameterArgs’ size is equal to parameterStrings’ size.VERIFY(parameter_args.size() == parameter_strings.size());- // 2. For each index of the range 0 to |parameterArgs]' [list/size=], verify that the argument implements- // TrustedScript and parameterStrings[index] is equal to arg's data.+ // 3. For each index of the range 0 to parameterArgs’ size:+ // 1. Let arg be parameterArgs[index].+ // 2. If arg implements TrustedScript, then:+ // 1. Assert: parameterStrings[index] is equal to arg’s data.+ // 3. Otherwise, set isTrusted to false.Vector<String> utf8_parameter_strings;utf8_parameter_strings.ensure_capacity(parameter_strings.size());for (auto const& parameter_string : parameter_strings)@@ -358,30 +355,26 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_string_compilation(JS::Reais_trusted = TrustedTypes::trusted_script_values_match(parameter_args, utf8_parameter_strings);}- // 5. Let sourceToValidate be a new TrustedScript object created in realm whose data is set to codeString- // if isTrusted is true, and codeString otherwise.- auto const source_to_validate = is_trusted- ? TrustedTypes::TrustedScriptOrString(GC::Heap::the().allocate<TrustedTypes::TrustedScript>(Utf16String::from_utf16(code_string)))- : Utf16String::from_utf16(code_string);-- // 6. Let sourceString be the result of executing the Get Trusted Type compliant string algorithm,- // with TrustedScript, realm, sourceToValidate, compilationSink, and 'script'.- auto maybe_source_string = TrustedTypes::get_trusted_type_compliant_string(- TrustedTypes::TrustedTypeName::TrustedScript,- realm.global_object(),- source_to_validate,- compilation_sink,- TrustedTypes::Script.view());-- // 7. If the algorithm throws an error, throw an EvalError.- if (maybe_source_string.is_error()) {- return realm.vm().throw_completion<JS::EvalError>("Blocked by Content Security Policy"_utf16);+ // 4. If isTrusted is false, then:+ if (!is_trusted) {+ // 1. Set sourceString to the result of executing the get trusted type compliant string algorithm,+ // with TrustedScript, realm, codeString, compilationSink, and 'script'.+ auto maybe_source_string = TrustedTypes::get_trusted_type_compliant_string(+ TrustedTypes::TrustedTypeName::TrustedScript,+ realm.global_object(),+ source_string,+ compilation_sink,+ TrustedTypes::Script.view());++ // 2. If the algorithm throws an error, throw an EvalError.+ if (maybe_source_string.is_error())+ return realm.vm().throw_completion<JS::EvalError>("Blocked by Content Security Policy"_utf16);+ source_string = maybe_source_string.release_value();++ // 3. If sourceString is not equal to codeString, throw an EvalError.+ if (source_string != code_string)+ return realm.vm().throw_completion<JS::EvalError>("Blocked by Content Security Policy"_utf16);}- source_string = maybe_source_string.release_value();-- // 8. If sourceString is not equal to codeString, throw an EvalError.- if (source_string != code_string)- return realm.vm().throw_completion<JS::EvalError>("Blocked by Content Security Policy"_utf16);}// 3. Let result be "Allowed".@@ -414,43 +407,49 @@ JS::ThrowCompletionOr<void> ensure_csp_does_not_block_string_compilation(JS::Reamaybe_source_list = maybe_default_src->value();}- // 3. If source-list is not null, and does not contain a source expression which is an ASCII case-insensitive- // match for the string "'unsafe-eval'", then:+ // 3. If source-list is not null:if (maybe_source_list.has_value()) {auto const& source_list = maybe_source_list.value();- auto maybe_unsafe_eval = source_list.find_if([](auto const& directive_value) {- return directive_value.equals_ignoring_ascii_case(Directives::KeywordSources::UnsafeEval.view());- });+ // 1. Let trustedTypesRequired be the result of executing does sink type require trusted types?, with realm, 'script', and false.+ auto const trusted_types_required = TrustedTypes::does_sink_require_trusted_types(global, TrustedTypes::Script.view(), TrustedTypes::IncludeReportOnlyPolicies::No);- if (maybe_unsafe_eval.is_end()) {- // 1. Let violation be the result of executing § 2.4.1 Create a violation object for global, policy,- // and directive on global, policy, and "script-src".- auto script_src_string = Directives::Names::ScriptSrc.view().to_utf8_but_should_be_ported_to_utf16();- auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(global, policy, script_src_string);+ // 2. If trustedTypesRequired is true and source-list contains a source expression which is an ASCII case-insensitive match for the string "'trusted-types-eval'", then skip the following steps.+ if (trusted_types_required && any_of(source_list, [](auto const& value) {+ return value.equals_ignoring_ascii_case(Directives::KeywordSources::TrustedTypesEval.view());+ }))+ continue;- // 2. Set violation’s resource to "eval".- violation->set_resource(Violation::Resource::Eval);+ // 3. If source-list contains a source expression which is an ASCII case-insensitive match for the string "'unsafe-eval'", then skip the following steps.+ if (any_of(source_list, [](auto const& value) {+ return value.equals_ignoring_ascii_case(Directives::KeywordSources::UnsafeEval.view());+ }))+ continue;- // 3. If source-list contains the expression "'report-sample'", then set violation’s sample to the- // substring of sourceString containing its first 40 characters.- // FIXME: Should this be case insensitive?- auto maybe_report_sample = source_list.find_if([](auto const& directive_value) {- return directive_value.equals_ignoring_ascii_case(Directives::KeywordSources::ReportSample.view());- });+ // 4. Let violation be the result of executing § 2.4.1 Create a violation object for global, policy, and directive on global, policy, and "script-src".+ auto script_src_string = Directives::Names::ScriptSrc.view().to_utf8_but_should_be_ported_to_utf16();+ auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(global, policy, script_src_string);- if (!maybe_report_sample.is_end()) {- auto source_view = source_string.substring_view(0, min(source_string.length_in_code_units(), 40));- violation->set_sample(Utf16String::from_utf16(source_view));- }+ // 5. Set violation’s resource to "eval".+ violation->set_resource(Violation::Resource::Eval);- // 4. Execute § 5.5 Report a violation on violation.- violation->report_a_violation(realm);+ // 6. If source-list contains the expression "'report-sample'", then set violation’s sample to the substring of sourceString containing its first 40 characters.+ // FIXME: Should this be case insensitive?+ auto maybe_report_sample = source_list.find_if([](auto const& directive_value) {+ return directive_value.equals_ignoring_ascii_case(Directives::KeywordSources::ReportSample.view());+ });- // 5. If policy’s disposition is "enforce", then set result to "Blocked".- if (policy->disposition() == Policy::Disposition::Enforce)- result = Directives::Directive::Result::Blocked;+ if (!maybe_report_sample.is_end()) {+ auto source_view = source_string.substring_view(0, min(source_string.length_in_code_units(), 40));+ violation->set_sample(Utf16String::from_utf16(source_view));}++ // 7. Execute § 5.5 Report a violation on violation.+ violation->report_a_violation(realm);++ // 8. If policy’s disposition is "enforce", then set result to "Blocked".+ if (policy->disposition() == Policy::Disposition::Enforce)+ result = Directives::Directive::Result::Blocked;}}diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h b/Libraries/LibWeb/ContentSecurityPolicy/Directives/KeywordSources.hindex b8f7262451d..6cba7be1a64 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/KeywordSources.h@@ -19,6 +19,7 @@ namespace Web::ContentSecurityPolicy::Directives::KeywordSources {__ENUMERATE_KEYWORD_SOURCE(UnsafeHashes, "'unsafe-hashes'") \__ENUMERATE_KEYWORD_SOURCE(ReportSample, "'report-sample'") \__ENUMERATE_KEYWORD_SOURCE(UnsafeAllowRedirects, "'unsafe-allow-redirects'") \+ __ENUMERATE_KEYWORD_SOURCE(TrustedTypesEval, "'trusted-types-eval'") \__ENUMERATE_KEYWORD_SOURCE(WasmUnsafeEval, "'wasm-unsafe-eval'")#define __ENUMERATE_KEYWORD_SOURCE(name, value) extern Utf16FlyString const& name;diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 287705abae5..bf8abbae5b7 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -12447,7 +12447,7 @@ static WebIDL::ExceptionOr<Utf16String> document_write_compliant_string(DOM::DocHTML::relevant_global_object(document),string.to_string(),sink,- "script"_utf16));+ TrustedTypes::Script.view()));string.clear();string.append(new_string);}diff --git a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cppindex dbb4afcb111..d2950be482f 100644--- a/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp+++ b/Libraries/LibWeb/TrustedTypes/RequireTrustedTypesForDirective.cpp@@ -30,18 +30,9 @@ ENUMERATE_REQUIRE_KEYWORD_TRUSTED_TYPES_FORstatic bool sink_group_matches(Utf16String const& directive_value, Utf16View sink_group){- auto strip_quotes = [](Utf16View value) {- if (value.length_in_code_units() >= 2) {- auto first = value.code_unit_at(0);- auto last = value.code_unit_at(value.length_in_code_units() - 1);- if ((first == u'\'' && last == u'\'') || (first == u'"' && last == u'"'))- return value.substring_view(1, value.length_in_code_units() - 2);- }- return value;- };- auto value = strip_quotes(directive_value.utf16_view());- sink_group = strip_quotes(sink_group);- return value.equals_ignoring_ascii_case(sink_group);+ // INTEROP: Match the complete quoted token case-sensitively, as Chromium and Firefox do.+ // The Trusted Types draft uses case-insensitive ABNF for this keyword.+ return directive_value == sink_group;}static ContentSecurityPolicy::PolicyList const* csp_list_of_global(JS::Object& global)diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.txtindex 90c2ded3274..91b0a436413 100644--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.txt@@ -2,9 +2,9 @@ Harness status: OKFound 5 tests-5 Fail-Fail Script injected via direct `eval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.-Fail Script injected via indirect `eval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.-Fail Script injected via `new Function` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.-Fail Script injected via `setTimeout` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.-Fail Script injected via `setInterval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.\ No newline at end of file+5 Pass+Pass Script injected via direct `eval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.+Pass Script injected via indirect `eval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.+Pass Script injected via `new Function` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.+Pass Script injected via `setTimeout` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.+Pass Script injected via `setInterval` is allowed with `trusted-types-eval` and `require-trusted-types-for 'script'`.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]index 79d68b2558b..86713fc2b7d 100644--- a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]@@ -2,29 +2,28 @@ Harness status: OKFound 24 tests-1 Pass-23 Fail-Fail eval of TrustedScript works.-Fail indirect eval of TrustedScript works.-Fail eval of string works.+24 Pass+Pass eval of TrustedScript works.+Pass indirect eval of TrustedScript works.+Pass eval of string works.Pass eval of !TrustedScript and !string works.-Fail Function constructor of TrustedScript works.-Fail Function constructor of string works.-Fail Function constructor of all TrustedScripts works.-Fail Function constructor of all strings works.-Fail Function constructor of string and TrustedScript works.-Fail Async Function constructor of TrustedScript works.-Fail AsyncFunction constructor of string works.-Fail AsyncFunction constructor of all TrustedScripts works.-Fail AsyncFunction constructor of all strings works.-Fail AsyncFunction constructor of string and TrustedScript works.-Fail GeneratorFunction constructor of TrustedScript works.-Fail GeneratorFunction constructor of string works.-Fail GeneratorFunction constructor of all TrustedScripts works.-Fail GeneratorFunction constructor of all strings works.-Fail GeneratorFunction constructor of string and TrustedScript works.-Fail AsyncGeneratorFunction constructor of TrustedScript works.-Fail AsyncGeneratorFunction constructor of string works.-Fail AsyncGeneratorFunction constructor of all TrustedScripts works.-Fail AsyncGeneratorFunction constructor of all strings works.-Fail AsyncGeneratorFunction constructor of string and TrustedScript works.\ No newline at end of file+Pass Function constructor of TrustedScript works.+Pass Function constructor of string works.+Pass Function constructor of all TrustedScripts works.+Pass Function constructor of all strings works.+Pass Function constructor of string and TrustedScript works.+Pass Async Function constructor of TrustedScript works.+Pass AsyncFunction constructor of string works.+Pass AsyncFunction constructor of all TrustedScripts works.+Pass AsyncFunction constructor of all strings works.+Pass AsyncFunction constructor of string and TrustedScript works.+Pass GeneratorFunction constructor of TrustedScript works.+Pass GeneratorFunction constructor of string works.+Pass GeneratorFunction constructor of all TrustedScripts works.+Pass GeneratorFunction constructor of all strings works.+Pass GeneratorFunction constructor of string and TrustedScript works.+Pass AsyncGeneratorFunction constructor of TrustedScript works.+Pass AsyncGeneratorFunction constructor of string works.+Pass AsyncGeneratorFunction constructor of all TrustedScripts works.+Pass AsyncGeneratorFunction constructor of all strings works.+Pass AsyncGeneratorFunction constructor of string and TrustedScript works.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]index 9a603f7605e..e31a961c5a3 100644--- a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/[email protected]@@ -2,16 +2,15 @@ Harness status: OKFound 13 tests-9 Pass-4 Fail-Fail eval of TrustedScript works.-Fail indirect eval of TrustedScript works.+13 Pass+Pass eval of TrustedScript works.+Pass indirect eval of TrustedScript works.Pass eval of string fails.Pass indirect eval of string fails.Pass eval of !TrustedScript and !string works.-Fail Function constructor of TrustedScript works.+Pass Function constructor of TrustedScript works.Pass Function constructor of string fails.-Fail Function constructor of all TrustedScripts works.+Pass Function constructor of all TrustedScripts works.Pass Function constructor of all strings fails.Pass Function constructor of string and TrustedScript fails.Pass AsyncFunction constructor of string fails.diff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-default-policy@trusted-types-eval.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-default-policy@trusted-types-eval.txtindex a457fb535ef..d163620090a 100644--- a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-default-policy@trusted-types-eval.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-default-policy@trusted-types-eval.txt@@ -2,13 +2,12 @@ Harness status: OKFound 8 tests-4 Pass-4 Fail-Fail setTimeout of string accepted by the default policy runs.-Fail setInterval of string accepted by the default policy runs.+8 Pass+Pass setTimeout of string accepted by the default policy runs.+Pass setInterval of string accepted by the default policy runs.Pass setTimeout of string rejected by the default policy throws.Pass setInterval of string rejected by the default policy throws.Pass setTimeout of string where the default policy throws rethrows the exception.Pass setInterval of string where the default policy throws rethrows the exception.-Fail setTimeout of string changed by the default policy runs the changed string.-Fail setInterval of string changed by the default policy runs the changed string.\ No newline at end of file+Pass setTimeout of string changed by the default policy runs the changed string.+Pass setInterval of string changed by the default policy runs the changed string.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-no-default-policy@trusted-types-eval.txt b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-no-default-policy@trusted-types-eval.txtindex 9d38ed37c0b..6e76f5e3a94 100644--- a/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-no-default-policy@trusted-types-eval.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/trusted-types/setTimeout-setInterval-csp-tt-no-default-policy@trusted-types-eval.txt@@ -2,9 +2,8 @@ Harness status: OKFound 4 tests-2 Pass-2 Fail+4 PassPass setTimeout of string throws.Pass setInterval of string throws.-Fail setTimeout of TrustedScript runs.-Fail setInterval of TrustedScript runs.\ No newline at end of file+Pass setTimeout of TrustedScript runs.+Pass setInterval of TrustedScript runs.\ No newline at end of file