Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/analyzer/tests/subresource-integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/files/test_content_sri_impl_sameorigin_and_external.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<script
src="/static/js/react-0.14.7.min.js"
integrity="sha384-zTm/dblzLXQNp3CgY+hfaC/WJ6h4XtNrePh2CW2+rO9GPuNiPb9jmthvAL+oI/dQ"
crossorigin="anonymous"
></script>
<script
src="https://fb.me/react-0.14.7.min.js"
integrity="sha384-zTm/dblzLXQNp3CgY+hfaC/WJ6h4XtNrePh2CW2+rO9GPuNiPb9jmthvAL+oI/dQ"
crossorigin="anonymous"
></script>
</head>
<body></body>
</html>
11 changes: 11 additions & 0 deletions test/files/test_content_sri_impl_sameorigin_partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<script
src="/static/js/react-0.14.7.min.js"
integrity="sha384-zTm/dblzLXQNp3CgY+hfaC/WJ6h4XtNrePh2CW2+rO9GPuNiPb9jmthvAL+oI/dQ"
crossorigin="anonymous"
></script>
<script src="/static/js/foo.js"></script>
</head>
<body></body>
</html>
34 changes: 25 additions & 9 deletions test/subresource-integrity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down