MCPcopy
hub / github.com/ampproject/amphtml / checkSha384

Method checkSha384

extensions/amp-script/0.1/amp-script.js:624–638  ·  view source on GitHub ↗

* Checks if `sha384(script)` exists in `meta[name="amp-script-src"]` element * in document head. * * @param {string} script The script contents. * @param {string} debugId An element identifier for error messages. * @return {!Promise}

(script, debugId)

Source from the content-addressed store, hash-verified

622 * @return {!Promise}
623 */
624 checkSha384(script, debugId) {
625 const bytes = utf8Encode(script);
626 return this.crypto_.sha384Base64(bytes).then((hash) => {
627 if (!hash || !this.sources_.includes('sha384-' + hash)) {
628 // TODO(#24266): Refactor to %s interpolation when error string
629 // extraction is ready.
630 throw user().createError(
631 TAG,
632 `Script hash not found or incorrect for ${debugId}. You must include <meta name="amp-script-src" content="sha384-${hash}">. ` +
633 `During development, you can disable this check by adding the "data-ampdevmode" attribute to ${debugId}, or the root html node` +
634 'See https://amp.dev/documentation/components/amp-script/#script-hash.'
635 );
636 }
637 });
638 }
639
640 /**
641 * Adds `size` to current total. Returns true if new total is <= size cap.

Callers 3

getAuthorScript_Method · 0.80
fetchAuthorScript_Method · 0.80
test-amp-script.jsFile · 0.80

Calls 5

utf8EncodeFunction · 0.90
userFunction · 0.90
sha384Base64Method · 0.80
thenMethod · 0.45
createErrorMethod · 0.45

Tested by

no test coverage detected