Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@ampproject/toolbox-script-csp

ampproject213.9kApache-2.02.10.1

A library that calculates the CSP hash for amp-script

amp

readme

AMP-Toolbox Script CSP

npm version

Calculates the Content Security Policy (CSP) hash for the given script in the format expected by amp-script.

CSP is required when using amp-script with inline or cross-origin scripts.

Usage

Install via:

$ npm install @ampproject/toolbox-script-csp

Using the module

The module exposes a single function, calculateHash(src, options?).

src can be either a string (in which case UTF8 encoding is assumed) or a DataView such as a Buffer.

options is used to override the default options.

Options

  • algorithm overrides the hashing algorithm to use. Currently, the only algorithm supported by amp-script is sha384 which is the default value.

Example

Here is an example on how to use the calculateHash function to generate an amp-script CSP header.

Note that both leading and trailing whitespace is included in calculating the hash and must exactly match the whitespace used in the inline script.

const {calculateHash} = require('@ampproject/toolbox-script-csp');

const script = `
    const subject = 'world';
    console.log('Hello, ' + subject);
    `;

const hash = calculateHash(script);

console.log(hash);  // sha384-xRxb5sv13at6tVgZET4JLmf89TSZP10HjCGXVqO9bKWVXB0asV2jLrsDN8v4zX6j

The generated hash can be used in a <meta> tag this way:

<!doctype html>
<html ⚡>
<head>
  ...

  <meta name="amp-script-src" content="sha384-xRxb5sv13at6tVgZET4JLmf89TSZP10HjCGXVqO9bKWVXB0asV2jLrsDN8v4zX6j">
</head>
  <body>
    ...

    <amp-script layout="container" script="example"></amp-script>

    <script id="example" type="text/plain" target="amp-script">
    const subject = 'world';
    console.log('Hello, ' + subject);
    </script>
  </body>
</html>

changelog

v2.8.0 (2021-05-31)

:rocket: Enhancement

:bug: Bug Fix

:memo: Documentation

Committers: 1

v2.7.6 (2021-02-01)

:rocket: Enhancement

:bug: Bug Fix

Committers: 1

v2.7.5 (2021-01-18)

:bug: Bug Fix

Committers: 4

v2.7.4 (2020-12-23)

:bug: Bug Fix

  • cli, core, linter, optimizer, runtime-fetch, validator-rules

Committers: 1

v2.7.3 (2020-12-17)

:rocket: Enhancement

:bug: Bug Fix

Committers: 1

v2.7.2 (2020-12-15)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

  • linter, optimizer

Committers: 5

v2.7.1 (2020-10-26)

:rocket: Enhancement

  • linter
  • optimizer
    • #911 Allow users to specify version numbers for auto imported extensions (@digijin)

:bug: Bug Fix

Committers: 11

v2.7.0-alpha.3 (2020-08-25)

:rocket: Enhancement

Committers: 1

v2.7.0-alpha.2 (2020-08-21)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

  • cache-url, cli, cors, linter, optimizer, runtime-fetch, runtime-version
  • linter
  • lighthouse-plugin-amp, linter, optimizer, update-cache

Committers: 3

v2.6.0 (2020-08-10)

:rocket: Enhancement

:bug: Bug Fix

:memo: Documentation

:house: Internal

Committers: 2

v2.5.14 (2020-07-24)

:rocket: Enhancement

:bug: Bug Fix

Committers: 3

v2.5.13 (2020-07-15)

:bug: Bug Fix

Committers: 1

v2.5.12 (2020-07-15)

:rocket: Enhancement

Committers: 2

v2.5.11 (2020-07-14)

:bug: Bug Fix

Committers: 1

v2.5.9 (2020-07-08)

:bug: Bug Fix

Committers: 1

v2.5.8 (2020-07-08)

:bug: Bug Fix

Committers: 1

v2.5.7 (2020-07-08)

:bug: Bug Fix

Committers: 1

v2.5.6 (2020-07-07)

:rocket: Enhancement

Committers: 1

v2.5.5 (2020-06-22)

:bug: Bug Fix

Committers: 1

v2.5.4 (2020-06-22)

:bug: Bug Fix

Committers: 1

v2.5.3 (2020-06-15)

:bug: Bug Fix

  • optimizer
    • #838 fix(optimizer): add missing dependency node-fetch (@merceyz)

Committers: 1

v2.5.2 (2020-06-11)

:bug: Bug Fix

:house: Internal

Committers: 2

v2.5.1 (2020-06-09)

:rocket: Enhancement

  • linter
  • cache-list, cli, core, cors, lighthouse-plugin-amp, linter, optimizer-express, optimizer, runtime-fetch, runtime-version, update-cache
  • cli, linter

:bug: Bug Fix

:house: Internal

Committers: 2

v2.5.0 (2020-05-29)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 4

v2.4.0 (2020-05-13)

:bug: Bug Fix

:rocket: Enhancement

  • core, optimizer
  • linter
    • #755 Added og:image and alt text checks to linter for Stories (@Dbrown910)

:memo: Documentation

:house: Internal

Committers: 4

2.3.1 (2020-05-05)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 5

2.3.0 (2020-04-15)

:rocket: Enhancement

  • runtime-fetch
    • #711 runtime-fetch: Update amp-geo for subdivision matching (@mdmower)
  • cli, optimizer
  • cli, runtime-version
    • #695 runtime-version: Update for custom hosts and lts (@mdmower)
  • cache-url
  • cache-url, cli
    • #705 Add support for serving types in createCacheUrl (@Enriqe)

:memo: Documentation

  • cors, optimizer-express, optimizer, update-cache
    • #690 Use relative links compatible with github&npmjs (@mdmower)
  • cli

:house: Internal

  • optimizer

Committers: 3

2.2.0 (2020-04-03)

:rocket: Enhancement

  • optimizer
    • #679 Update to use postcss and cssnano for SeparateKeyframes transform (@ijjk)
  • cli

:bug: Bug Fix

:house: Internal

Committers: 3

2.1.0 (2020-04-01)

:rocket: Enhancement

  • cli, download-runtime
    • #663 download-runtime: Add tool to download AMP runtime (@mdmower)
  • cache-url, cli
  • optimizer

:bug: Bug Fix

:memo: Documentation

:house: Internal

Committers: 4

2.0.1

:rocket: Enhancement

  • optimizer
    • #622 optimizer: Add meta tags for singleDoc self-host (@mdmower)

:bug: Bug Fix

:house: Internal

Committers: 3

2.0.0

Migration notes:

  • The SeparateKeyframe transformer's compress option has been renamed to minify. The same option will also be used by the MinifyHtml transformer. The default value continues to be true.
  • The API for implementing a custom transformer has changed. We no longer monkey patch the node class, but instead rely on helper methods defined in NodeUtils:

    const {firstChildByTag, appendChild, createElement} =
    require('@ampproject/toolbox-optimizer').NodeUtils; class CustomTransformer { constructor(config)
    { this.log_ = config.log.tag('CUSTOM'); } transform(tree, params) { this.log_.info('Running custom
    transformation for ', params.filePath); const html = firstChildByTag(tree, 'html'); if (!html)
    return; const head = firstChildByTag(html, 'head'); if (!head) return; const desc =
    createElement('meta', { name: 'description', content: 'this is just a demo', }); appendChild(head,
    desc); } }

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

  • cli, core, cors, optimizer, update-cache
  • linter, optimizer-express
  • cache-list, cache-url, cli, core, cors, lighthouse-plugin-amp, linter, optimizer-express, optimizer, runtime-version, script-csp, update-cache, validator-rules
    • #568 Adds "repository" and "homepage" to every package.json (@fstanis)

Committers: 2

v1.1.2 (2019-10-15)

:bug: Bug Fix

Committers: 1

v1.1.2-alpha.0 (2019-10-09)

:rocket: Enhancement

  • optimizer
    • #518 optimizer: Make dynamic component URL rewrites optional (@mattwomple)

:bug: Bug Fix

  • optimizer

Committers: 2

v1.1.0 (2019-10-08)

:rocket: Enhancement

Committers: 4

v1.1.0-beta.1 (2019-09-19)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 6