From a21b9e78fb89878193b327f4a6a71e8284043632 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 21 Oct 2026 11:12:33 +0200 Subject: [PATCH 1/5] test(sri): rename same-origin fixtures Name the fixtures after the `src` form they exercise (`_relative`, `_https`, `cross_origin_noproto`) instead of numbering them, so the sub-cases in "checks for same origin" are self-describing. The `sameorigin3` fixture loads `//www.mozilla.org` on `mozilla.org`, which is a different origin, so it joins the `cross_origin_` fixtures. --- ...in3.html => test_content_sri_cross_origin_noproto.html} | 0 ...origin2.html => test_content_sri_sameorigin_https.html} | 0 ...gin1.html => test_content_sri_sameorigin_relative.html} | 0 test/subresource-integrity.test.js | 7 ++++--- 4 files changed, 4 insertions(+), 3 deletions(-) rename test/files/{test_content_sri_sameorigin3.html => test_content_sri_cross_origin_noproto.html} (100%) rename test/files/{test_content_sri_sameorigin2.html => test_content_sri_sameorigin_https.html} (100%) rename test/files/{test_content_sri_sameorigin1.html => test_content_sri_sameorigin_relative.html} (100%) diff --git a/test/files/test_content_sri_sameorigin3.html b/test/files/test_content_sri_cross_origin_noproto.html similarity index 100% rename from test/files/test_content_sri_sameorigin3.html rename to test/files/test_content_sri_cross_origin_noproto.html diff --git a/test/files/test_content_sri_sameorigin2.html b/test/files/test_content_sri_sameorigin_https.html similarity index 100% rename from test/files/test_content_sri_sameorigin2.html rename to test/files/test_content_sri_sameorigin_https.html diff --git a/test/files/test_content_sri_sameorigin1.html b/test/files/test_content_sri_sameorigin_relative.html similarity index 100% rename from test/files/test_content_sri_sameorigin1.html rename to test/files/test_content_sri_sameorigin_relative.html diff --git a/test/subresource-integrity.test.js b/test/subresource-integrity.test.js index e61d26b0..fcd99a91 100644 --- a/test/subresource-integrity.test.js +++ b/test/subresource-integrity.test.js @@ -30,7 +30,8 @@ describe("Subresource Integrity", () => { }); it("checks for same origin", function () { - reqs = emptyRequests("test_content_sri_sameorigin1.html"); + // On the same origin with relative path + reqs = emptyRequests("test_content_sri_sameorigin_relative.html"); let result = subresourceIntegrityTest(reqs); assert.equal( result.result, @@ -39,7 +40,7 @@ describe("Subresource Integrity", () => { assert.isTrue(result.pass); // On a different subdomain, without a protocol - reqs = emptyRequests("test_content_sri_sameorigin3.html"); + reqs = emptyRequests("test_content_sri_cross_origin_noproto.html"); result = subresourceIntegrityTest(reqs); assert.equal( result.result, @@ -48,7 +49,7 @@ describe("Subresource Integrity", () => { assert.isFalse(result.pass); // On the same origin, but with https:// specified - reqs = emptyRequests("test_content_sri_sameorigin2.html"); + reqs = emptyRequests("test_content_sri_sameorigin_https.html"); result = subresourceIntegrityTest(reqs); assert.equal( result.result, From 55f89cbbf1f25ed4343abfd57a8d8a71b040e527 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 21 Oct 2026 11:14:00 +0200 Subject: [PATCH 2/5] refactor(sri): resolve script `src` against base URL Replace the regex-based classification into relative, protocol-relative and full URLs with a single `new URL(scriptSrc, baseUrl)`, and derive `sameOrigin` and `secureScheme` from the resolved `scriptUrl`. This drops the `relativeOrigin` and `secureOrigin` flags and the second `new URL(scriptSrc)` parse. Scoring is unchanged: protocol-relative URLs still count as a foreign origin with an insecure scheme, and the resolved scheme of a path-relative URL matches the former `session.url.protocol` check since `baseUrl` is derived from the session. Resolving every `src` means a malformed value such as `src="//"` throws from `new URL`. Since `analyzeScan` runs tests without a per-test `try`/`catch`, that would abort the whole scan, so skip such a `src` instead and cover it with a test. --- src/analyzer/tests/subresource-integrity.js | 69 +++++++------------ .../files/test_content_sri_malformed_src.html | 6 ++ test/subresource-integrity.test.js | 12 ++++ 3 files changed, 43 insertions(+), 44 deletions(-) create mode 100644 test/files/test_content_sri_malformed_src.html diff --git a/src/analyzer/tests/subresource-integrity.js b/src/analyzer/tests/subresource-integrity.js index eb650c27..89a3d601 100644 --- a/src/analyzer/tests/subresource-integrity.js +++ b/src/analyzer/tests/subresource-integrity.js @@ -72,16 +72,14 @@ export function subresourceIntegrityTest( output.result = Expectation.HtmlNotParseable; return output; } - // The origin the page was ultimately served from. Scripts are same-origin - // only when their exact scheme + host + port match this, not merely when - // they share the same registrable domain (e.g. a different subdomain is a - // distinct origin). + // Origin the page was ultimately served from. Same-origin requires an exact + // scheme + host + port match, so a different subdomain is a distinct origin. const baseUrl = requests.session?.redirectHistory.at(-1)?.url ?? requests.session?.url; const baseOrigin = baseUrl?.origin ?? null; - // Track to see if any scripts were on foreign origins. let scriptsOnForeignOrigin = false; + for (const script of scripts) { const scriptSrc = getAttribute(script, "src"); if (!scriptSrc) { @@ -91,50 +89,33 @@ export function subresourceIntegrityTest( const integrity = getAttribute(script, "integrity") || null; const crossorigin = getAttribute(script, "crossorigin") || null; - let relativeOrigin = false; - let relativeProtocol = false; - let sameOrigin; - - const relativeProtocolRegex = /^(\/\/)[^/]/; - const fullUrlRegex = /^https?:\/\//; - - if (relativeProtocolRegex.test(scriptSrc)) { - // relative protocol (src="//example.com/script.js") - relativeProtocol = true; - sameOrigin = false; - } else if (fullUrlRegex.test(scriptSrc)) { - // full URL (src="https://example.com/script.js") - sameOrigin = new URL(scriptSrc).origin === baseOrigin; - } else { - // relative URL (src="/path" etc.) - relativeOrigin = true; - sameOrigin = true; + // Protocol-relative URLs (src="//example.com/script.js") inherit the + // page scheme and are scored as foreign, even on the same origin. + const relativeProtocol = /^(\/\/)[^/]/.test(scriptSrc); + + // Resolving against baseUrl covers relative and protocol-relative URLs; + // without a session there is no base, so treat the script as foreign. + // A src that fails to resolve (e.g. src="//") is not a loadable + // sub-resource, so skip it rather than crashing the whole scan. + let scriptUrl = null; + if (baseUrl) { + try { + scriptUrl = new URL(scriptSrc, baseUrl); + } catch { + continue; + } } - // Protocol-relative URLs are scored as foreign, even on the same origin. - let secureOrigin; - if (sameOrigin && !relativeProtocol) { - secureOrigin = true; - } else { - secureOrigin = false; + const sameOrigin = !relativeProtocol && scriptUrl?.origin === baseOrigin; + if (!sameOrigin) { scriptsOnForeignOrigin = true; } - // Check if it is a secure scheme - let scheme = null; - if (!relativeProtocol && !relativeOrigin) { - scheme = new URL(scriptSrc).protocol; - } - let secureScheme = false; - if ( - scheme === "https:" || - (relativeOrigin && baseUrl?.protocol === "https:") - ) { - secureScheme = true; - } + const secureScheme = + !relativeProtocol && scriptUrl?.protocol === "https:"; - // Add it to the scripts data result, if it's not a relative URI - if (!secureOrigin) { + // Record and score off-origin scripts; same-origin ones are trusted. + if (!sameOrigin) { output.data[scriptSrc] = { crossorigin, integrity }; if (integrity && !secureScheme) { @@ -157,7 +138,7 @@ export function subresourceIntegrityTest( ); } } else { - // Grant bonus even if they use SRI on the same origin + // Reward SRI on same-origin scripts too, even if not required. if (integrity && secureScheme && !output.result) { output.result = Expectation.SriImplementedAndAllScriptsLoadedSecurely; } diff --git a/test/files/test_content_sri_malformed_src.html b/test/files/test_content_sri_malformed_src.html new file mode 100644 index 00000000..e3495191 --- /dev/null +++ b/test/files/test_content_sri_malformed_src.html @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/subresource-integrity.test.js b/test/subresource-integrity.test.js index fcd99a91..0a74dacd 100644 --- a/test/subresource-integrity.test.js +++ b/test/subresource-integrity.test.js @@ -68,6 +68,18 @@ describe("Subresource Integrity", () => { assert.isTrue(result.pass); }); + it("skips a src that fails to resolve instead of crashing", function () { + // A malformed src like "//" cannot be resolved into a URL; it is not a + // loadable sub-resource, so it must be skipped rather than throwing. + reqs = emptyRequests("test_content_sri_malformed_src.html"); + const result = subresourceIntegrityTest(reqs); + assert.equal( + result.result, + Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin + ); + assert.isTrue(result.pass); + }); + for (const { name, file, expected } of [ { name: "treats a different subdomain as a distinct origin", From 1d955b0bf380b24e68db7a2bc38ae4a79d5ae302 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 21 Oct 2026 11:14:18 +0200 Subject: [PATCH 3/5] fix(sri): treat on-origin protocol-relative scripts as same-origin A protocol-relative `src` that resolves to the page's exact origin (same scheme, host and port) loads from the page itself, so any attack on it also applies to the document. Score it like any other same-origin script (0 points) instead of as an external script loaded over HTTP (-50). A different host behind a protocol-relative `src` (`//www.mozilla.org` on `mozilla.org`) is still a distinct origin and keeps the off-origin scoring, so that case takes over the "scores as foreign" test while the `sameorigin_noproto` fixture moves into "checks for same origin". --- src/analyzer/tests/subresource-integrity.js | 4 ++-- test/subresource-integrity.test.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/analyzer/tests/subresource-integrity.js b/src/analyzer/tests/subresource-integrity.js index 89a3d601..82ca9d3a 100644 --- a/src/analyzer/tests/subresource-integrity.js +++ b/src/analyzer/tests/subresource-integrity.js @@ -90,7 +90,7 @@ export function subresourceIntegrityTest( const crossorigin = getAttribute(script, "crossorigin") || null; // Protocol-relative URLs (src="//example.com/script.js") inherit the - // page scheme and are scored as foreign, even on the same origin. + // page scheme and are treated as loaded insecurely when off-origin. const relativeProtocol = /^(\/\/)[^/]/.test(scriptSrc); // Resolving against baseUrl covers relative and protocol-relative URLs; @@ -106,7 +106,7 @@ export function subresourceIntegrityTest( } } - const sameOrigin = !relativeProtocol && scriptUrl?.origin === baseOrigin; + const sameOrigin = scriptUrl?.origin === baseOrigin; if (!sameOrigin) { scriptsOnForeignOrigin = true; } diff --git a/test/subresource-integrity.test.js b/test/subresource-integrity.test.js index 0a74dacd..c5b222a2 100644 --- a/test/subresource-integrity.test.js +++ b/test/subresource-integrity.test.js @@ -39,14 +39,14 @@ describe("Subresource Integrity", () => { ); assert.isTrue(result.pass); - // On a different subdomain, without a protocol - reqs = emptyRequests("test_content_sri_cross_origin_noproto.html"); + // On the same origin, but without a protocol + reqs = emptyRequests("test_content_sri_sameorigin_noproto.html"); result = subresourceIntegrityTest(reqs); assert.equal( result.result, - Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely + Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin ); - assert.isFalse(result.pass); + assert.isTrue(result.pass); // On the same origin, but with https:// specified reqs = emptyRequests("test_content_sri_sameorigin_https.html"); @@ -87,8 +87,8 @@ describe("Subresource Integrity", () => { expected: Expectation.SriNotImplementedButExternalScriptsLoadedSecurely, }, { - name: "scores a same-origin protocol-relative script as foreign", - file: "test_content_sri_sameorigin_noproto.html", + name: "scores a cross-origin protocol-relative script as foreign", + file: "test_content_sri_cross_origin_noproto.html", expected: Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely, }, From 717f01a8f0582431146e9e02b5c058e358921d23 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 21 Oct 2026 11:14:36 +0200 Subject: [PATCH 4/5] fix(sri): score off-origin protocol-relative scripts by HTTP enforcement An off-origin protocol-relative `src` resolves to whatever scheme the visitor reached the page over, so judge it by the scheme a normal visitor ends up with instead of a blanket -50. When there is no HTTP server, or the HTTP redirect chain ends on `https:`, the script always loads over HTTPS and scores like an `https://` external script (-5 without SRI). Otherwise it can load over HTTP and keeps the -50 / -20 penalty. Only the final hop of the chain matters: an intermediate HTTP hop (`http` -> `http` -> `https`) still lands the visitor on HTTPS, and that downgradeable hop is already penalized by the redirection test (`RedirectionNotToHttpsOnInitialRedirection`), so docking it here too would count one flaw twice. The `cross_origin_noproto` fixture (a different host, HTTP redirecting to HTTPS in the test defaults) accordingly scores -5 instead of -50. --- src/analyzer/tests/subresource-integrity.js | 23 +++++- test/subresource-integrity.test.js | 88 ++++++++++++++++++++- 2 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/analyzer/tests/subresource-integrity.js b/src/analyzer/tests/subresource-integrity.js index 82ca9d3a..337f1973 100644 --- a/src/analyzer/tests/subresource-integrity.js +++ b/src/analyzer/tests/subresource-integrity.js @@ -80,6 +80,18 @@ export function subresourceIntegrityTest( let scriptsOnForeignOrigin = false; + // A protocol-relative URL (//cdn.example.com/…) inherits the page's scheme, + // so it only adds risk when an off-origin sub-resource is served over HTTP + // (an attacker could MITM the sub-resource origin) — see issue #464. It is + // safe whenever a visitor lands on HTTPS: no HTTP server, or the HTTP request + // ultimately redirects to HTTPS. A downgradeable intermediate hop + // (http → http → https) still lands on HTTPS and is penalized by the + // redirection test, so it is not docked again here. + const httpRedirects = requests.responses.httpRedirects; + const httpEnforcesHttps = + !requests.responses.http || + httpRedirects.at(-1)?.url.protocol === "https:"; + for (const script of scripts) { const scriptSrc = getAttribute(script, "src"); if (!scriptSrc) { @@ -89,8 +101,8 @@ export function subresourceIntegrityTest( const integrity = getAttribute(script, "integrity") || null; const crossorigin = getAttribute(script, "crossorigin") || null; - // Protocol-relative URLs (src="//example.com/script.js") inherit the - // page scheme and are treated as loaded insecurely when off-origin. + // Protocol-relative URLs inherit the page scheme, so their security is + // judged by httpEnforcesHttps below; other URLs carry a concrete scheme. const relativeProtocol = /^(\/\/)[^/]/.test(scriptSrc); // Resolving against baseUrl covers relative and protocol-relative URLs; @@ -111,8 +123,11 @@ export function subresourceIntegrityTest( scriptsOnForeignOrigin = true; } - const secureScheme = - !relativeProtocol && scriptUrl?.protocol === "https:"; + // Protocol-relative URLs are secure only when httpEnforcesHttps; others + // are secure when their resolved scheme is https. + const secureScheme = relativeProtocol + ? httpEnforcesHttps + : scriptUrl?.protocol === "https:"; // Record and score off-origin scripts; same-origin ones are trusted. if (!sameOrigin) { diff --git a/test/subresource-integrity.test.js b/test/subresource-integrity.test.js index c5b222a2..bbdf6fa4 100644 --- a/test/subresource-integrity.test.js +++ b/test/subresource-integrity.test.js @@ -89,8 +89,7 @@ describe("Subresource Integrity", () => { { name: "scores a cross-origin protocol-relative script as foreign", file: "test_content_sri_cross_origin_noproto.html", - expected: - Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely, + expected: Expectation.SriNotImplementedButExternalScriptsLoadedSecurely, }, ]) { it(name, function () { @@ -158,6 +157,32 @@ describe("Subresource Integrity", () => { assert.isFalse(result.pass); }); + // Only protocol-relative URLs depend on HTTP enforcement. + for (const { name, file, expected, pass } of [ + { + name: "scores an https:// script with SRI as secure when HTTP stays on HTTP", + file: "test_content_sri_impl_external_https1.html", + expected: Expectation.SriImplementedAndExternalScriptsLoadedSecurely, + pass: true, + }, + { + name: "scores an https:// script without SRI as secure when HTTP stays on HTTP", + file: "test_content_sri_notimpl_external_https.html", + expected: Expectation.SriNotImplementedButExternalScriptsLoadedSecurely, + pass: false, + }, + ]) { + it(name, function () { + reqs = emptyRequests(file); + reqs.responses.httpRedirects = [ + { url: new URL("http://mozilla.org/"), status: 200 }, + ]; + const result = subresourceIntegrityTest(reqs); + assert.equal(result.result, expected); + assert.equal(result.pass, pass); + }); + } + it("checks if implemented with external scripts and http", function () { reqs = emptyRequests("test_content_sri_impl_external_http.html"); let result = subresourceIntegrityTest(reqs); @@ -169,8 +194,23 @@ describe("Subresource Integrity", () => { }); it("checks if implemented with external scripts and no protocol", function () { + // When HTTP redirects to HTTPS, //cdn.example.com/script.js always resolves to https://, + // so protocol-relative URLs should be treated the same as https:// (issue #464). reqs = emptyRequests("test_content_sri_impl_external_noproto.html"); let result = subresourceIntegrityTest(reqs); + assert.equal( + result.result, + Expectation.SriImplementedAndExternalScriptsLoadedSecurely + ); + assert.isTrue(result.pass); + + // When HTTP does NOT redirect to HTTPS, //cdn.example.com/script.js can resolve to http:// + // on an HTTP visit, so it must still be penalised. + reqs = emptyRequests("test_content_sri_impl_external_noproto.html"); + reqs.responses.httpRedirects = [ + { url: new URL("http://mozilla.org/"), status: 200 }, + ]; + result = subresourceIntegrityTest(reqs); assert.equal( result.result, Expectation.SriImplementedButExternalScriptsNotLoadedSecurely @@ -189,12 +229,56 @@ describe("Subresource Integrity", () => { }); it("checks if not implemented with external scripts and no protocol", function () { + // When HTTP redirects to HTTPS, //cdn.example.com/script.js always resolves to https://, + // so it should score like https:// (-5), not like http:// (-50) (issue #464). reqs = emptyRequests("test_content_sri_notimpl_external_noproto.html"); let result = subresourceIntegrityTest(reqs); + assert.equal( + result.result, + Expectation.SriNotImplementedButExternalScriptsLoadedSecurely + ); + assert.isFalse(result.pass); + + // When HTTP does NOT redirect to HTTPS, //cdn.example.com/script.js can resolve to http:// + // on an HTTP visit, so it must still be penalised at -50. + reqs = emptyRequests("test_content_sri_notimpl_external_noproto.html"); + reqs.responses.httpRedirects = [ + { url: new URL("http://mozilla.org/"), status: 200 }, + ]; + result = subresourceIntegrityTest(reqs); assert.equal( result.result, Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely ); assert.isFalse(result.pass); + + // When there is no HTTP server at all, //cdn.example.com/script.js can only + // resolve to https://, so it should score like https:// (-5) (issue #464). + reqs = emptyRequests("test_content_sri_notimpl_external_noproto.html"); + reqs.responses.http = null; + reqs.responses.httpRedirects = []; + result = subresourceIntegrityTest(reqs); + assert.equal( + result.result, + Expectation.SriNotImplementedButExternalScriptsLoadedSecurely + ); + assert.isFalse(result.pass); + + // When the redirect to HTTPS goes through an intermediate HTTP hop, a normal + // visitor still lands on HTTPS, so //cdn.example.com/script.js resolves to + // https:// and scores like https:// (-5). The downgradeable hop is penalised + // by the redirection test, not double-counted here (issue #464). + reqs = emptyRequests("test_content_sri_notimpl_external_noproto.html"); + reqs.responses.httpRedirects = [ + { url: new URL("http://mozilla.org/"), status: 301 }, + { url: new URL("http://www.mozilla.org/"), status: 301 }, + { url: new URL("https://www.mozilla.org/"), status: 200 }, + ]; + result = subresourceIntegrityTest(reqs); + assert.equal( + result.result, + Expectation.SriNotImplementedButExternalScriptsLoadedSecurely + ); + assert.isFalse(result.pass); }); }); From a6dc89366ccb6c55b3a13221262341a9922a9e14 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Wed, 21 Oct 2026 11:14:38 +0200 Subject: [PATCH 5/5] docs(grader/sri): clarify protocol-relative URL penalty descriptions The -20 and -50 descriptions stated that protocol-relative URLs are penalized unconditionally. Since the analyzer only penalizes them when they can resolve to HTTP, reword the descriptions so users are not told a `src="//..."` URL is penalized when it scored 0 or -5. --- src/grader/charts.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grader/charts.js b/src/grader/charts.js index 0c988d5b..fff4ec64 100644 --- a/src/grader/charts.js +++ b/src/grader/charts.js @@ -689,7 +689,7 @@ export const SCORE_TABLE = new Map([ Expectation.SriImplementedButExternalScriptsNotLoadedSecurely, { description: `

- Subresource Integrity (SRI) implemented, but external scripts are loaded over HTTP or they use protocol-relative URLs via src="//...". + Subresource Integrity (SRI) implemented, but external scripts are loaded over HTTP, including protocol-relative URLs via src="//..." that can resolve to HTTP.

`, modifier: -20, recommendation: `

@@ -701,7 +701,7 @@ export const SCORE_TABLE = new Map([ Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely, { description: `

- Subresource Integrity (SRI) not implemented, and external scripts are loaded over HTTP or they use protocol-relative URLs via src="//...". + Subresource Integrity (SRI) not implemented, and external scripts are loaded over HTTP, including protocol-relative URLs via src="//..." that can resolve to HTTP.

`, modifier: -50, recommendation: `