sri #
Subresource Integrity metadata (W3C SRI).
A script a page loads from elsewhere runs with the page's authority: on a
payment page it can rewrite the form or the redirect to the payment provider.
PCI DSS requirement 6.4.3 asks that every script on a payment page is
authorised and inventoried, and that its integrity is assured. The browser
assures it with Subresource Integrity: the integrity attribute of a
<script> or <link> element names the digest of the bytes the page
expects, and the browser refuses to run a resource whose bytes do not match.
CSP's hash sources use the same metadata.
sri computes that metadata, parses an attribute as a browser does, keeps
the metadata of its strongest algorithm and matches bytes against it. Its
command line prints the attribute of a file or a URL, so a page's script tags
are generated with their integrity, and checks a file or a URL against an
attribute.
Installation #
opam install sri
Usage #
The script alert('Hello, world.'); of section 3.1 has this sha384
metadata:
# let m = Sri.hash Sri.Algorithm.Sha384 "alert('Hello, world.');";;
val m : Sri.t = <abstr>
# Sri.to_string m;;
- : string =
"sha384-H8BRh8j48O9oYatfu5AZzq6A9RINhZO5H16dQZngK7T62em8MUt1FLm52t+eX6xO"
Parsing an attribute skips the metadata of an algorithm outside sha256,
sha384 and sha512 (section 3.3.2), and only the metadata of the strongest
algorithm named are checked (sections 3.3.3 and 3.3.4). Here the sha256
digest is right and the sha384 one is not, so the bytes do not match:
# let ms = Sri.parse "md5-abc sha256-qznLcsROx4GACP2dm0UCKCzCG+HiZ1guq6ZZDob/Tng= sha384-wrong";;
val ms : Sri.t list = [<abstr>; <abstr>]
# Sri.attribute (Sri.strongest ms);;
- : string = "sha384-wrong"
# Sri.matches ms "alert('Hello, world.');";;
- : bool = false
An attribute with no metadata of a known algorithm matches any bytes, as
section 3.3.4 says; a caller that needs an assurance checks for an empty list
first, as sri check does.
Command line #
sri hash SOURCE prints the integrity attribute of a file, or of the body of
a GET of an http or https URL: sha384 by default, one hash expression for each
repeated --algorithm.
$ sri hash https://example.com/checkout.js
sha384-...
$ sri hash -a sha384 -a sha512 checkout.js
sha384-... sha512-...
sri check --integrity METADATA SOURCE checks a file or a URL as a browser
does before it runs it. It prints SOURCE: ok on a match and exits 1 when the
bytes match none of the strongest metadata, or when the attribute names no
sha256, sha384 or sha512 metadata.
Specifications #
- W3C Subresource Integrity: integrity metadata
(section 3.1), the hash algorithms (section 3.2) and the algorithms of
section 3.3, as the editor's draft numbers them: apply algorithm to bytes
(3.3.1), parse metadata (3.3.2), get the strongest metadata (3.3.3) and do
bytes match metadataList (3.3.4). - FIPS 180-4: SHA-256,
SHA-384 and SHA-512. - RFC 4648 section 4: base64.
License #
ISC