@hint/hint-sri
Require scripts and styles to use Subresource Integrity
Last updated 6 years ago by alrra .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @hint/hint-sri 
SYNC missed versions from official npm registry.

Use subresource integrity (sri)

sri warns about requesting scripts or stylesheets without using subresource integrity.

Why is this important?

A common practice in modern web development is to use third party resources from CDNs or different services (analytics, ads, etc.). However, doing so can increase the attack surface of your web site/app.

While there are techniques to verify the agent is talking with the right server (TLS, HSTS, etc.), an attacker (or administrator) with access to the server can manipulate the content with impunity.

If you want to load a crypto miner on 1,000+ websites you don't attack 1,000+ websites, you attack the 1 website that they all load content from. (Scott Helme)

Subresource integrity is a standard that mitigates this by ensuring that an exact representation of a resource, and only that representation, loads and executes.

What does the hint check?

This hint checks that a website correctly uses SRI, more specifically:

  • All the downloaded resources by an <script> or <link rel="stylesheet"> have an integrity attribute.
  • The integrity attribute needs to be valid. I.e.: it should contain something in the form of sha(256|384|512)-HASH, where HASH is the hashed value of the downloaded body's response using the previously specified algorithm (sha256, sha384, or sha512).
  • The minimum cryptographic hash function used is sha384. If multiple ones are provided, the highest one will be used to determine if the baseline is met.
  • When using a cross-origin resource (e.g.: using a script hosted in a third party CDN), the <script> or <link> tag needs to have a valid crossorigin attribute.
  • The resource is served on a secure context (i.e.: HTTPS) to guarantee the HTML and resource haven't been tampered during the delivery.
  • The hash from the integrity attribute needs to be the same as the one calculated using the response's body.
  • If multiple hashes are provided, at least one needs to be valid.

Examples that trigger the hint

Cross-origin resource with no crossorigin attribute:

<script src="https://example.com/example-framework.js"
  integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7">
</script>

Cross-origin resource with invalid crossorigin attribute:

<script src="https://example.com/example-framework.js"
  integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7"
  crossorigin="invalid">
</script>

Cross-origin resource loaded over HTTP:

<script src="http://example.com/example-framework.js"
  integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7"
  crossorigin="invalid">
</script>

Same-origin resource with no integrity and originCriteria set to all:

<link rel="stylesheet" href="/style.css">

Examples that pass the hint

Cross-origin resource with multiple hashes and sha384 is one of them:

<script src="https://example.com/script.js"
  integrity="sha256-validHashHere
             sha384-validHashHere">
</script>

Cross-origin resource with valid crossorigin attribute:

<script src="https://example.com/example-framework.js"
  integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7"
  crossorigin="anonymous">
</script>

Can the hint be configured?

Yes, you can define the baseline algorithm and the origin of the resources to analyze.

Baseline algorithm

By default the baseline algorithm is sha384 but you can change it to sha256, or sha512 by specifying that in the .hintrc file:

{
    "connector": {...},
    "formatters": [...],
    "hints": {
        "sri": ["warning", {
            "baseline": "sha512"
        }],
        ...
    },
    ...
}

The above will validate that the integrity of all scripts and styles use sha512.

Origin criteria

By default, this hint will analyze only resources with a different origin. To change this behavior you will have to set the originCriteria property to one of the following:

  • all: All resources will be analyzed
  • crossOrigin: Only cross-origin resources will be analyzed

The following will analyze all the resources:

{
    "connector": {...},
    "formatters": [...],
    "hints": {
        "sri": ["warning", {
            "originCriteria": "all"
        }],
        ...
    },
    ...
}

How to use this hint?

This package is installed automatically by webhint:

npm install hint --save-dev

To use it, activate it via the .hintrc configuration file:

{
    "connector": {...},
    "formatters": [...],
    "hints": {
        "sri": "error",
        ...
    },
    "parsers": [...],
    ...
}

Note: The recommended way of running webhint is as a devDependency of your project.

Further Reading

Current Tags

  • 4.0.23                                ...           latest (3 months ago)

57 Versions

  • 4.0.23                                ...           3 months ago
  • 4.0.22                                ...           a year ago
  • 4.0.21                                ...           a year ago
  • 4.0.20                                ...           2 years ago
  • 4.0.19                                ...           2 years ago
  • 4.0.18                                ...           2 years ago
  • 4.0.17                                ...           2 years ago
  • 4.0.16                                ...           2 years ago
  • 4.0.15                                ...           2 years ago
  • 4.0.14                                ...           2 years ago
  • 4.0.13                                ...           3 years ago
  • 4.0.12                                ...           3 years ago
  • 4.0.11                                ...           3 years ago
  • 4.0.10                                ...           3 years ago
  • 4.0.9                                ...           3 years ago
  • 4.0.8                                ...           3 years ago
  • 4.0.7                                ...           3 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.5                                ...           4 years ago
  • 4.0.4                                ...           4 years ago
  • 4.0.3                                ...           4 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           4 years ago
  • 3.3.3                                ...           5 years ago
  • 3.3.2                                ...           5 years ago
  • 3.3.1                                ...           5 years ago
  • 3.3.0                                ...           5 years ago
  • 3.2.11                                ...           5 years ago
  • 3.2.10                                ...           5 years ago
  • 3.2.9                                ...           5 years ago
  • 3.2.8                                ...           5 years ago
  • 3.2.7                                ...           5 years ago
  • 3.2.6                                ...           5 years ago
  • 3.2.5                                ...           5 years ago
  • 3.2.4                                ...           5 years ago
  • 3.2.3                                ...           5 years ago
  • 3.2.2                                ...           5 years ago
  • 3.2.1                                ...           5 years ago
  • 3.2.0                                ...           5 years ago
  • 3.1.2                                ...           5 years ago
  • 3.1.1                                ...           6 years ago
  • 3.1.0                                ...           6 years ago
  • 3.0.5                                ...           6 years ago
  • 3.0.4                                ...           6 years ago
  • 3.0.3                                ...           6 years ago
  • 3.0.2                                ...           6 years ago
  • 3.0.1                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.1.1                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 1.0.0-beta.0                                ...           6 years ago
Maintainers (2)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (13)

© 2010 - cnpmjs.org x YWFE | Home | YWFE