Socket
Socket
Sign inDemoInstall

@ampproject/toolbox-script-csp

Package Overview
Dependencies
0
Maintainers
16
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ampproject/toolbox-script-csp

A library that calculates the CSP hash for amp-script


Version published
Weekly downloads
47K
increased by3.09%
Maintainers
16
Install size
16.4 kB
Created
Weekly downloads
 

Changelog

Source

2.9.0 (2022-06-23)

:rocket: Enhancement
  • cloudflare-optimizer-scripts
    • #1318 Add support for Opaque redirects (@abdulhannanali)
  • optimizer
:bug: Bug Fix
  • optimizer
    • #1319 Fix i-amphtml-sizer responsive issue (@ediamin)
    • #1312 More robust auto extension import (@sebastianbenz)
    • #1303 Fix for issue #1302: "SSR: Duplicated ids in rendered markup, if there are nodes with attributes 'media', 'heights' or 'sizes'" (@DK-Stern)
    • #1305 Fix for issue #1304: "SSR: Transforming of nodes with attribute 'heights' fails, if value contains multiple media queries" (@DK-Stern)
:house: Internal
  • cache-url, cli, cloudflare-optimizer-scripts, linter, optimizer-docker, optimizer, page-experience, update-cache
  • cache-list, cache-url, cli, cloudflare-optimizer-scripts, core, cors, linter, optimizer-docker, optimizer-express, optimizer, page-experience, runtime-fetch, runtime-version, script-csp, update-cache, validator-rules
Committers: 6

Readme

Source

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>

Keywords

FAQs

Last updated on 23 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc