amp-script documentation for inline scripts needs updating
📖 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.
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" srcattribute 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:
- Extract the script content:
console.log('hello') - Calculate the SHA384 sum for it:
2449d3149fb10f687794aeb1f9794d9b07dc9f585743439ca7d2339ef2bf75a419e8e887a5862efabe724746c29ae83c - Use
base64urlencoding:JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8 - Add
sha384-prefix:sha384-JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8 - Put it in a
<meta>tag:<meta name="amp-script-src" content="sha384-JEnTFJ-xD2h3lK6x-XlNmwfcn1hXQ0Ocp9IznvK_daQZ6OiHpYYu-r5yR0bCmug8"> - Add this
<meta>tag to head.
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')");
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.
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 I'm so glad you are spelling all of this out. I'm having the same issue with SHA384.
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-
ampproject/amp-toolbox#465 adds support for generating this automatically by the amp-optimizer.
https://github.com/ampproject/amphtml/pull/24242 makes amp-script documentation a bit clearer.
@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!
@choumx That makes sense, Thanks for the explanation!
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')");
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