amp.dev icon indicating copy to clipboard operation
amp.dev copied to clipboard

amp-script documentation for inline scripts needs updating

Open alankent opened this issue 7 years ago • 12 comments

📖 Missing or out-of-date documentation

Describe the content that is missing or should be up dated The page https://amp.dev/documentation/components/amp-script/?format=websites contains an example of amp-script where the script is inlined. The example needs updating to match the final released version of amp-script.

@fstanis said "You need a CSP with the hash to use amp-script inline."

Also the version where the script is in an external file works, but using textContent += ... has a side effect of destroying the <button> element so you can only click on it once. Keeps the example short though, so not end of the world.

Which part of the site would that content live at? URL given above.

What's the type of content? Referenced docs out of date due to changes made to amp-script before release.

alankent avatar Aug 21 '19 21:08 alankent

To clarify on CSP: this requirement was introduced in ampproject/amphtml#23691.

When using <amp-script> with either:

  • An inline script via script="some-id"
  • src attribute set to a cross-origin URL

...then your document must contain a CSP-line <meta name="amp-script-src" content="..."> entry in <head>. The content attribute works in a similar way to script-src CSP, specifically the '<hash-algorithm>-<base64-value>' type (the only algorithm supported is sha384). It can contain multiple space-separated hashes (one for each script).

For example, if you have <script id="hello" type="text/plain" target="amp-script">console.log('hello')</script>, you would:

  1. Extract the script content: console.log('hello')
  2. Calculate the SHA384 sum for it: 2449d3149fb10f687794aeb1f9794d9b07dc9f585743439ca7d2339ef2bf75a419e8e887a5862efabe724746c29ae83c
  3. Use base64url encoding: JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8
  4. Add sha384- prefix: sha384-JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8
  5. Put it in a <meta> tag: <meta name="amp-script-src" content="sha384-JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8">
  6. Add this <meta> tag to head.

fstanis avatar Aug 24 '19 17:08 fstanis

If using node.js, this function will perform steps 2-4 for you:

const crypto = require('crypto');

function generateCSPHash(script) {
  const hash = crypto.createHash('sha384');
  const data = hash.update(script, 'utf-8');
  return 'sha384-' + data.digest('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');
}

generateCSPHash("console.log('hello')");

fstanis avatar Aug 24 '19 17:08 fstanis

Hi @fstanis When I base64 encode the SHA384 in your example:

2449d3149fb10f687794aeb1f9794d9b07dc9f585743439ca7d2339ef2bf75a419e8e887a5862efabe724746c29ae83c

it doesn't output: JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8

Instead, I get:

MjQ0OWQzMTQ5ZmIxMGY2ODc3OTRhZWIxZjk3OTRkOWIwN2RjOWY1ODU3NDM0MzljYTdkMjMzOWVmMmJmNzVhNDE5ZThlODg3YTU4NjJlZmFiZTcyNDc0NmMyOWFlODNj

Any idea as to why I don't get the same output when converting? Thanks in advance.

webdevmike avatar Aug 28 '19 17:08 webdevmike

You need to base64 encode the raw bytes, not the hex string - sorry that I wasn't clear.

So you need to do hex -> binary -> base64 instead of hex -> base64. In a terminal, you can use xxd to decode the hex:

echo "2449d3149fb10f687794aeb1f9794d9b07dc9f585743439ca7d2339ef2bf75a419e8e887a5862efabe724746c29ae83c" | xxd -r -p - | base64

This will give you JEnTFJ+xD2h3lK6x+XlNmwfcn1hXQ0Ocp9IznvK/daQZ6OiHpYYu+r5yR0bCmug8 so you'd still need to replace + with - and / with _ to turn it into URL format.

(of course, ideally you can use something that just goes directly to base64 instead of hex, like the node.js snippet I posted)

fstanis avatar Aug 28 '19 18:08 fstanis

@fstanis I'm so glad you are spelling all of this out. I'm having the same issue with SHA384.

mdvb1001 avatar Aug 28 '19 18:08 mdvb1001

Thanks @fstanis Appreciate the response. I got this working, and I hope others find this helpful: The reason I tried this approach is because in the Chrome console it shows the base64 code needed to use in the meta tag ( sha384- ) and when I copied from the console and pasted into my text editor (Sublime) it added odd characters at the end, so it would error and never run the script. I found this out when pasting into https://www.diffchecker.com/ I could see the extra characters at the end. I removed the characters and it works!

webdevmike avatar Aug 28 '19 18:08 webdevmike

ampproject/amp-toolbox#465 adds support for generating this automatically by the amp-optimizer.

fstanis avatar Aug 28 '19 18:08 fstanis

https://github.com/ampproject/amphtml/pull/24242 makes amp-script documentation a bit clearer.

dreamofabear avatar Aug 30 '19 15:08 dreamofabear

@webdevmike Yea, copy/pasting can inadvertently include three zero-width spaces that we use to identify "user errors" at runtime. The fixed string substitutions in the above PR should avoid this now. Thanks for your patience!

dreamofabear avatar Aug 30 '19 15:08 dreamofabear

@choumx That makes sense, Thanks for the explanation!

webdevmike avatar Sep 05 '19 14:09 webdevmike

If using node.js, this function will perform steps 2-4 for you:

const crypto = require('crypto');

function generateCSPHash(script) {
  const hash = crypto.createHash('sha384');
  const data = hash.update(script, 'utf-8');
  return 'sha384-' + data.digest('base64')
    .replace(/=/g, '')
    .replace(/\+/g, '-')
    .replace(/\//g, '_');
}

generateCSPHash("console.log('hello')");

vishnu323 avatar Feb 26 '20 05:02 vishnu323

what about when script is more than online.its works when the script is one line.but more than one line the generated sha384 is not correct.may i know on more example is the script is more than one line

vishnu323 avatar Feb 26 '20 05:02 vishnu323