From 4c4b191a97f723581bbdbb5151996f2d64d2549a Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Tue, 24 Nov 2026 16:54:49 +0100 Subject: [PATCH] fix(sri): grant same-origin bonus only if all scripts use SRI A single same-origin script with `integrity` earned `SriImplementedAndAllScriptsLoadedSecurely` (+5), even when other same-origin scripts lacked SRI. It also mislabeled pages whose external scripts all use SRI over HTTPS as loading all scripts from a similar origin. Decide the result after the loop: require SRI on every same-origin script for the bonus, and report `SriImplementedAndExternalScriptsLoadedSecurely` whenever foreign scripts are present. --- src/analyzer/tests/subresource-integrity.js | 11 ++++-- ...tent_sri_impl_sameorigin_and_external.html | 15 ++++++++ ...t_content_sri_impl_sameorigin_partial.html | 11 ++++++ test/subresource-integrity.test.js | 34 ++++++++++++++----- 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 test/files/test_content_sri_impl_sameorigin_and_external.html create mode 100644 test/files/test_content_sri_impl_sameorigin_partial.html diff --git a/src/analyzer/tests/subresource-integrity.js b/src/analyzer/tests/subresource-integrity.js index 337f1973..b9b58a65 100644 --- a/src/analyzer/tests/subresource-integrity.js +++ b/src/analyzer/tests/subresource-integrity.js @@ -79,6 +79,8 @@ export function subresourceIntegrityTest( const baseOrigin = baseUrl?.origin ?? null; let scriptsOnForeignOrigin = false; + let scriptsOnSameOrigin = false; + let allSameOriginScriptsUseSri = true; // 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 @@ -153,9 +155,9 @@ export function subresourceIntegrityTest( ); } } else { - // Reward SRI on same-origin scripts too, even if not required. - if (integrity && secureScheme && !output.result) { - output.result = Expectation.SriImplementedAndAllScriptsLoadedSecurely; + scriptsOnSameOrigin = true; + if (!integrity || !secureScheme) { + allSameOriginScriptsUseSri = false; } } } @@ -167,6 +169,9 @@ export function subresourceIntegrityTest( if (scriptsOnForeignOrigin) { output.result = Expectation.SriImplementedAndExternalScriptsLoadedSecurely; + } else if (scriptsOnSameOrigin && allSameOriginScriptsUseSri) { + // Reward SRI on same-origin scripts too, even if not required. + output.result = Expectation.SriImplementedAndAllScriptsLoadedSecurely; } else { output.result = Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin; diff --git a/test/files/test_content_sri_impl_sameorigin_and_external.html b/test/files/test_content_sri_impl_sameorigin_and_external.html new file mode 100644 index 00000000..8ef2fe2c --- /dev/null +++ b/test/files/test_content_sri_impl_sameorigin_and_external.html @@ -0,0 +1,15 @@ + + + + + + + diff --git a/test/files/test_content_sri_impl_sameorigin_partial.html b/test/files/test_content_sri_impl_sameorigin_partial.html new file mode 100644 index 00000000..d1c2cd63 --- /dev/null +++ b/test/files/test_content_sri_impl_sameorigin_partial.html @@ -0,0 +1,11 @@ + + + + + + + diff --git a/test/subresource-integrity.test.js b/test/subresource-integrity.test.js index bbdf6fa4..8763eb59 100644 --- a/test/subresource-integrity.test.js +++ b/test/subresource-integrity.test.js @@ -137,15 +137,31 @@ describe("Subresource Integrity", () => { assert.isTrue(result.pass); }); - it("checks if implemented with same origin", function () { - reqs = emptyRequests("test_content_sri_impl_sameorigin.html"); - let result = subresourceIntegrityTest(reqs); - assert.equal( - result.result, - Expectation.SriImplementedAndAllScriptsLoadedSecurely - ); - assert.isTrue(result.pass); - }); + for (const { name, file, expected } of [ + { + name: "grants the bonus when all same-origin scripts use SRI", + file: "test_content_sri_impl_sameorigin.html", + expected: Expectation.SriImplementedAndAllScriptsLoadedSecurely, + }, + { + name: "withholds the bonus when only some same-origin scripts use SRI", + file: "test_content_sri_impl_sameorigin_partial.html", + expected: + Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin, + }, + { + name: "reports external scripts when same-origin and external scripts use SRI", + file: "test_content_sri_impl_sameorigin_and_external.html", + expected: Expectation.SriImplementedAndExternalScriptsLoadedSecurely, + }, + ]) { + it(name, function () { + reqs = emptyRequests(file); + const result = subresourceIntegrityTest(reqs); + assert.equal(result.result, expected); + assert.isTrue(result.pass); + }); + } it("checks if not implemented with external scripts and https", function () { reqs = emptyRequests("test_content_sri_notimpl_external_https.html");