Skip to content

fix(sri): only penalize off-origin proto-relative URLs on HTTP - #561

Draft
caugner wants to merge 5 commits into
558-sri-same-originfrom
464-protocol-relative-urls-with-https
Draft

caugner wants to merge 5 commits into
558-sri-same-originfrom
464-protocol-relative-urls-with-https

Conversation

@caugner

@caugner caugner commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

Update the subresource-integrity analyzer to score protocol-relative script URLs (src="//cdn.example.com/…") according to the actual risk, instead of applying a blanket −50:

  • On-origin protocol-relative URLs (resolving to the page's exact origin: same scheme, host and port) are treated as a secure origin (0 points), like any same-origin script. A script on a different host (e.g. //www.mozilla.org on mozilla.org) is a distinct origin and is handled by the off-origin case below, not treated as same-origin.
  • Off-origin protocol-relative URLs are treated like the URL a visitor would actually resolve: as https:// (−5 without SRI) when a normal visitor ends up on HTTPS (no HTTP server, or the HTTP request ultimately redirects to HTTPS), and as http:// (−50 / −20 with SRI) otherwise.

This also reworks the −20 / −50 grade descriptions, which previously stated protocol-relative URLs are penalized unconditionally:

-external scripts are loaded over HTTP or they use protocol-relative URLs via `src="//..."`.
+external scripts are loaded over HTTP, including protocol-relative URLs via `src="//..."` that can resolve to HTTP.

Motivation

Only penalize protocol-relative URLs in the one case that carries additional risk, per the security team's analysis in #464.

Given a document that loads a protocol-relative sub-resource, there are four cases:

Case Document Sub-resource Additional risk
1 HTTP on-origin none (any attack on the sub-resource also applies to the document)
2 HTTP off-origin yes (an attacker could MITM the sub-resource origin and serve it over HTTP)
3 HTTPS on-origin none
4 HTTPS off-origin none

Only case 2 warrants a penalty. The observatory distinguishes the document scheme from the redirect chain, so it penalizes a protocol-relative URL only when it is off-origin and a normal visitor is served the document over HTTP.

The document counts as served over HTTPS whenever a visitor ends up there, including through a downgradeable intermediate hop (http → http → https), since the visitor still lands on HTTPS and the script loads over HTTPS. That insecure hop is a redirect flaw already penalized by the redirection test (RedirectionNotToHttpsOnInitialRedirection); scoring it here as well would dock one flaw twice.

Additional details

Example that previously scored −50 despite being served only over HTTPS: https://developer.mozilla.org/en-US/observatory/analyze?host=runephilosof-abtion.github.io%2Fobservatory-test%2F

Since every src is now resolved against the served base URL:

  • A src that cannot be resolved into a URL (e.g. src="//") is skipped rather than throwing and aborting the whole scan.
  • A src that is not loaded over HTTP(S) (e.g. a data: URL) is skipped as well.
  • A protocol-relative src is detected by resolving it, so forms like ///cdn.example.com/… or \\cdn.example.com/… are covered too.

Related issues and pull requests

Fixes #464.

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.
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.
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".
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.
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.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SRI: Why are protocol-relative urls punished so hard

1 participant