Skip to content
Draft
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
84 changes: 40 additions & 44 deletions src/analyzer/tests/subresource-integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@ 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;

// 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) {
Expand All @@ -91,50 +101,36 @@ 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 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;
// 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 = 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;
}
// Protocol-relative URLs are secure only when httpEnforcesHttps; others
// are secure when their resolved scheme is https.
const secureScheme = relativeProtocol
? httpEnforcesHttps
: 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) {
Expand All @@ -157,7 +153,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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/grader/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ export const SCORE_TABLE = new Map([
Expectation.SriImplementedButExternalScriptsNotLoadedSecurely,
{
description: `<p>
Subresource Integrity (SRI) implemented, but external scripts are loaded over HTTP or they use protocol-relative URLs via <code>src="//..."</code>.
Subresource Integrity (SRI) implemented, but external scripts are loaded over HTTP, including protocol-relative URLs via <code>src="//..."</code> that can resolve to HTTP.
</p>`,
modifier: -20,
recommendation: `<p>
Expand All @@ -701,7 +701,7 @@ export const SCORE_TABLE = new Map([
Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely,
{
description: `<p>
Subresource Integrity (SRI) not implemented, and external scripts are loaded over HTTP or they use protocol-relative URLs via <code>src="//..."</code>.
Subresource Integrity (SRI) not implemented, and external scripts are loaded over HTTP, including protocol-relative URLs via <code>src="//..."</code> that can resolve to HTTP.
</p>`,
modifier: -50,
recommendation: `<p>
Expand Down
6 changes: 6 additions & 0 deletions test/files/test_content_sri_malformed_src.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<script src="//"></script>
</head>
<body></body>
</html>
117 changes: 107 additions & 10 deletions test/subresource-integrity.test.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: It makes sense to split up the test cases more systematically, but I'll do this in a separate PR, stacked on top.

Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ 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,
Expectation.SriNotImplementedButAllScriptsLoadedFromSecureOrigin
);
assert.isTrue(result.pass);

// On a different subdomain, without a protocol
reqs = emptyRequests("test_content_sri_sameorigin3.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_sameorigin2.html");
reqs = emptyRequests("test_content_sri_sameorigin_https.html");
result = subresourceIntegrityTest(reqs);
assert.equal(
result.result,
Expand All @@ -67,17 +68,28 @@ 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",
file: "test_content_sri_cross_origin_subdomain.html",
expected: Expectation.SriNotImplementedButExternalScriptsLoadedSecurely,
},
{
name: "scores a same-origin protocol-relative script as foreign",
file: "test_content_sri_sameorigin_noproto.html",
expected:
Expectation.SriNotImplementedAndExternalScriptsNotLoadedSecurely,
name: "scores a cross-origin protocol-relative script as foreign",
file: "test_content_sri_cross_origin_noproto.html",
expected: Expectation.SriNotImplementedButExternalScriptsLoadedSecurely,
},
]) {
it(name, function () {
Expand Down Expand Up @@ -145,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);
Expand All @@ -156,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
Expand All @@ -176,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);
});
});