Truly independent web browser
LibWeb: Reject an @import whose response is not text/css
Shannon Booth0e61955a3ff8
diff --git a/Libraries/LibWeb/CSS/StyleSheetImport.cpp b/Libraries/LibWeb/CSS/StyleSheetImport.cppindex ac17cd4d186..209c6c21615 100644--- a/Libraries/LibWeb/CSS/StyleSheetImport.cpp+++ b/Libraries/LibWeb/CSS/StyleSheetImport.cpp@@ -270,12 +270,19 @@ void StyleSheetImport::fetch()return;}- // FIXME: 2. If parentStylesheet is in quirks mode and response is CORS-same-origin, let content type be "text/css".- // Otherwise, let content type be the Content Type metadata of response.- auto content_type = "text/css"sv;+ // 2. If parentStylesheet is in quirks mode and response is CORS-same-origin, let content type be "text/css".+ // Otherwise, let content type be the Content Type metadata of response.+ // NB: The Content Type metadata is read from the internal response, as an opaque response has no headers.+ auto extracted_mime_type = Fetch::Infrastructure::extract_mime_type(response->unsafe_response()->header_list());+ Optional<String> content_type;+ if (document->in_quirks_mode() && response->is_cors_same_origin())+ content_type = "text/css"_string;+ else if (extracted_mime_type.has_value())+ content_type = extracted_mime_type->essence();// 3. If content type is not "text/css", return.- if (content_type != "text/css"sv) {+ // AD-HOC: A response without a valid Content-Type is used as CSS, as for a <link> style sheet.+ if (content_type.has_value() && content_type != "text/css"sv) {dbgln_if(CSS_LOADER_DEBUG, "StyleSheetImport: Rejecting loaded style sheet; content type isn't text/css; is: '{}'", content_type);return;}@@ -287,7 +294,6 @@ void StyleSheetImport::fetch()auto url = response->unsafe_response()->url().value();Optional<StringView> mime_type_charset;- auto extracted_mime_type = Fetch::Infrastructure::extract_mime_type(response->header_list());if (extracted_mime_type.has_value()) {if (auto charset = extracted_mime_type->parameters().get("charset"sv); charset.has_value())mime_type_charset = charset->bytes_as_string_view();diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 33a76ead7cb..42924ff293c 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -93,6 +93,8 @@ Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamText/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamic-import/propagate-nonce-inline-module.html; Resource loading tests use WPT Python handlers, substitutions, or .headers files.+Text/input/wpt-import/css/css-cascade/import-mime-type-quirks.html+Text/input/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.htmlText/input/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.htmlText/input/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_without_require_trusted_types_eval.htmlText/input/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_report_only_require_trusted_types_eval.htmldiff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-cascade/import-mime-type-quirks.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-cascade/import-mime-type-quirks.txtnew file mode 100644index 00000000000..81cb486aa0e--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-cascade/import-mime-type-quirks.txt@@ -0,0 +1,12 @@+Harness status: OK++Found 7 tests++7 Pass+Pass Same-origin @import with text/css+Pass Cross-origin @import with text/css+Pass Same-origin @import with wrong MIME type+Pass Cross-origin @import with wrong MIME type+Pass Cross-origin @import with wrong MIME type, with CORS+Pass Cross-origin redirect to same-origin @import with wrong MIME type+Pass Same-origin redirect to cross-origin @import with wrong MIME type\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-01.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-01.txtindex d21c2c2bb0e..3cce02f489c 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-01.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-01.txt@@ -2,8 +2,7 @@ Harness status: OKFound 26 tests-24 Pass-2 Fail+26 PassPass Should get an error event for a 404 error.Pass Should get an error event for an unsupported URL.Pass Stylesheet loading using <link> with no Content-Type, same-origin, and no nosniff@@ -20,13 +19,13 @@ Pass Stylesheet loading using <link> with broken Content-Type, cross-origin, andPass Stylesheet loading using <link> with broken Content-Type, cross-origin, and nosniffPass Stylesheet loading using @import with no Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with no Content-Type, same-origin, and nosniff-Fail Stylesheet loading using @import with wrong Content-Type, same-origin, and no nosniff+Pass Stylesheet loading using @import with wrong Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with wrong Content-Type, same-origin, and nosniffPass Stylesheet loading using @import with broken Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with broken Content-Type, same-origin, and nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and nosniff-Fail Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniff+Pass Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with wrong Content-Type, cross-origin, and nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and nosniff\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-limited-quirks.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-limited-quirks.txtindex 0e42e031c18..cef5e8110ff 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-limited-quirks.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-limited-quirks.txt@@ -2,8 +2,7 @@ Harness status: OKFound 24 tests-22 Pass-2 Fail+24 PassPass Stylesheet loading using <link> with no Content-Type, same-origin, and no nosniffPass Stylesheet loading using <link> with no Content-Type, same-origin, and nosniffPass Stylesheet loading using <link> with wrong Content-Type, same-origin, and no nosniff@@ -18,13 +17,13 @@ Pass Stylesheet loading using <link> with broken Content-Type, cross-origin, andPass Stylesheet loading using <link> with broken Content-Type, cross-origin, and nosniffPass Stylesheet loading using @import with no Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with no Content-Type, same-origin, and nosniff-Fail Stylesheet loading using @import with wrong Content-Type, same-origin, and no nosniff+Pass Stylesheet loading using @import with wrong Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with wrong Content-Type, same-origin, and nosniffPass Stylesheet loading using @import with broken Content-Type, same-origin, and no nosniffPass Stylesheet loading using @import with broken Content-Type, same-origin, and nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and nosniff-Fail Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniff+Pass Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with wrong Content-Type, cross-origin, and nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and nosniff\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-quirks.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-quirks.txtindex 7c767abed7d..cef5e8110ff 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-quirks.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-link-element/link-style-error-quirks.txt@@ -2,8 +2,7 @@ Harness status: OKFound 24 tests-23 Pass-1 Fail+24 PassPass Stylesheet loading using <link> with no Content-Type, same-origin, and no nosniffPass Stylesheet loading using <link> with no Content-Type, same-origin, and nosniffPass Stylesheet loading using <link> with wrong Content-Type, same-origin, and no nosniff@@ -24,7 +23,7 @@ Pass Stylesheet loading using @import with broken Content-Type, same-origin, andPass Stylesheet loading using @import with broken Content-Type, same-origin, and nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with no Content-Type, cross-origin, and nosniff-Fail Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniff+Pass Stylesheet loading using @import with wrong Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with wrong Content-Type, cross-origin, and nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and no nosniffPass Stylesheet loading using @import with broken Content-Type, cross-origin, and nosniff\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.txtnew file mode 100644index 00000000000..da3161b68e9--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass Should get an error event for a text/plain response.\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/import-mime-type-quirks.html b/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/import-mime-type-quirks.htmlnew file mode 100644index 00000000000..f25af15c827--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/import-mime-type-quirks.html@@ -0,0 +1,85 @@+<!-- No DOCTYPE: this test must run in quirks mode. -->+<meta charset="utf-8">+<title>@import: MIME type quirk in quirks mode</title>+<link rel="help" href="https://drafts.csswg.org/css-cascade-5/#content-type">+<link rel="author" title="Simon Pieters" href="mailto:[email protected]">+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<script src="../../common/get-host-info.sub.js"></script>+<div id="log"></div>+<script>+assert_equals(document.compatMode, "BackCompat", "must be in quirks mode");++const CSS_FILE = "/css/css-cascade/resources/quirks-mime-type.css";+const SAME_ORIGIN = location.origin + CSS_FILE;+const CROSS_ORIGIN = get_host_info().REMOTE_ORIGIN + CSS_FILE;+const WRONG_MIME = "?pipe=header(Content-Type,text/plain)";+const CORS = "|header(Access-Control-Allow-Origin,*)";++function redirectTo(origin, target, enableCors) {+ return origin + "/common/redirect.py?" + (enableCors ? "enable-cors&" : "") ++ "location=" + encodeURIComponent(target);+}++// The style element fires load or error once the @import has settled; engines+// disagree about which, so wait for either.+function importStylesheet(href) {+ return new Promise(resolve => {+ const style = document.createElement("style");+ style.textContent = `@import url("${href}");`;+ style.onload = style.onerror = () => resolve(style);+ document.head.appendChild(style);+ });+}++// quirks-mime-type.css sets body { padding: 10px }.+function sheetApplied() {+ return getComputedStyle(document.body).paddingTop == "10px";+}++promise_test(async t => {+ const style = await importStylesheet(SAME_ORIGIN);+ t.add_cleanup(() => style.remove());+ assert_true(sheetApplied(), "styles should apply");+}, "Same-origin @import with text/css");++promise_test(async t => {+ const style = await importStylesheet(CROSS_ORIGIN);+ t.add_cleanup(() => style.remove());+ assert_true(sheetApplied(), "styles should apply");+}, "Cross-origin @import with text/css");++promise_test(async t => {+ const style = await importStylesheet(SAME_ORIGIN + WRONG_MIME);+ t.add_cleanup(() => style.remove());+ assert_true(sheetApplied(), "styles should apply");+}, "Same-origin @import with wrong MIME type");++promise_test(async t => {+ const style = await importStylesheet(CROSS_ORIGIN + WRONG_MIME);+ t.add_cleanup(() => style.remove());+ assert_false(sheetApplied(), "styles should not apply");+}, "Cross-origin @import with wrong MIME type");++promise_test(async t => {+ const style = await importStylesheet(CROSS_ORIGIN + WRONG_MIME + CORS);+ t.add_cleanup(() => style.remove());+ assert_false(sheetApplied(), "styles should not apply");+}, "Cross-origin @import with wrong MIME type, with CORS");++// The response is same-origin with the document, but it is not CORS-same-origin,+// so the quirk must not apply.+promise_test(async t => {+ const href = redirectTo(get_host_info().REMOTE_ORIGIN, SAME_ORIGIN + WRONG_MIME, false);+ const style = await importStylesheet(href);+ t.add_cleanup(() => style.remove());+ assert_false(sheetApplied(), "styles should not apply");+}, "Cross-origin redirect to same-origin @import with wrong MIME type");++promise_test(async t => {+ const href = redirectTo(location.origin, CROSS_ORIGIN + WRONG_MIME, false);+ const style = await importStylesheet(href);+ t.add_cleanup(() => style.remove());+ assert_false(sheetApplied(), "styles should not apply");+}, "Same-origin redirect to cross-origin @import with wrong MIME type");+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/resources/quirks-mime-type.css b/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/resources/quirks-mime-type.cssnew file mode 100644index 00000000000..588ec4aa061--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-cascade/resources/quirks-mime-type.css@@ -0,0 +1 @@+body { padding: 10px }diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.htmlnew file mode 100644index 00000000000..30d3a705f53--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.html@@ -0,0 +1,32 @@+<!DOCTYPE html>+<title>style: error events</title>+<link rel="author" title="Ms2ger" href="mailto:[email protected]">+<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-style-element">+<script src="../../../../resources/testharness.js"></script>+<script src="../../../../resources/testharnessreport.js"></script>+<div id="log"></div>+<div id="test">+<script>+//var t404 = async_test("Should get an error event for a 404 error.")+//t404.step(function() {+// var elt = document.createElement("style");+// elt.onerror = t404.step_func(function() {+// assert_true(true, "Got error event for 404 error.")+// t404.done()+// })+// elt.appendChild(+// document.createTextNode('@import 404 error;'));+// document.getElementsByTagName("head")[0].appendChild(elt);+//})+var tText = async_test("Should get an error event for a text/plain response.")+tText.step(function() {+ var elt = document.createElement("style");+ elt.onerror = tText.step_func(function() {+ assert_true(true, "Got error event for 404 error.")+ tText.done()+ })+ elt.appendChild(+ document.createTextNode('@import "support/css-red.txt";'));+ document.getElementsByTagName("head")[0].appendChild(elt);+})+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/support/css-red.txt b/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/support/css-red.txtnew file mode 100644index 00000000000..9ef04cbd12d--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/document-metadata/the-style-element/support/css-red.txt@@ -0,0 +1 @@+html { color: red; }