Update Subresource Integrity Hash Values for Scripts in the Buy with Prime Cartridge
Buy with Prime API is now available for early access
Sign up for early access to the Buy with Prime API using the 'Sign Up' button below. The API may change as Amazon receives feedback and iterates on it.
To help prevent script tampering and ensure that checkout processes work correctly, when you modify any file in the Buy with Prime cartridge, generate and update the Subresource Integrity (SRI) hash values for the scripts in the cartridge.
Automatically generate new SRI hash values when building the Buy with Prime cartridge
To automatically generate new SRI hash values when you build the Buy with Prime cartridge, take the following steps.
-
Install the following packages. In version 0.13.0 and later, the Buy with Prime cartridge includes the packages by default.
npm install [email protected] --save npm install [email protected] --save -
In your
webpack.config.jsconfiguration file, require theSubresourceIntegrityPluginandWebpackAssetsManifestplugins. In version 0.13.0 and later, the Buy with Prime cartridge includes the plugins by default.var path = require('path'); const SubresourceIntegrityPlugin = require('webpack-subresource-integrity'); const WebpackAssetsManifest = require('webpack-assets-manifest'); -
In the
pluginsarray of the Javascript configuration object, add instances of theSubresourceIntegrityPluginandWebpackAssetsManifestplugins as follows. In version 0.13.0 and later, the Buy with Prime cartridge includes the instances by default.plugins: [ new SubresourceIntegrityPlugin({ hashFuncNames: ['sha384'] }), new WebpackAssetsManifest({ integrity: true, integrityHashes: ['sha384'], writeToDisk: true, output: 'js-asset-manifest.json' }) ]
Generate the js-asset-manifest.json file
js-asset-manifest.json fileThe webpack-assets-manifest package generates js-asset-manifest.json, which includes SHA-384 SRI hash values for each affected script. To generate the js-asset-manifest.json file, do the following.
- At a command prompt, open your Buy with Prime cartridge directory and build the cartridge.
- After you build the cartridge, find the
js-asset-manifest.jsonfile inlink_amazon_bwp/cartridges/int_buywithprime/cartridge/static/.
Update SRI hash values for scripts in the Buy with Prime cartridge
When the Buy with Prime cartridge loads a checkout script, it automatically verifies the SRI of the script by checking whether the new SRI hash value matches the expected SRI hash value. The following sections show how to update the SRI hash values for scripts in the Buy with Prime cartridge to ensure that checkout processes work correctly.
Update the SRI hash value for bwpSummary.js
bwpSummary.jsTo update the SRI hash value for the bwpSummary.js script, take the following steps.
-
Open the following file in a text editor:
link_amazon_bwp/cartridges/int_buywithprime_sfra/cartridge/templates/default/checkout/checkout.isml -
In the Load Static Assets section, find the line that references the
bwpSummary.jsscript.assets.addJs('/js/bwpSummary.js', 'sha384-xyZ123dEf456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG'); -
From the
js-asset-manifest.jsonfile, get the new SRI hash value for thebwpSummary.jsentry under theintegritykey. The following example contains a placeholder SRI hash value."default/js/bwpSummary.js": { "src": "default/js/bwpSummary.js", "integrity": "sha384-def123abC456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG" }, -
In the
checkout.ismlfile, replace the existing SRI hash value with the new SRI hash value from thejs-asset-manifest.jsonfile and keep thesha384-prefix. -
Save the
checkout.ismlfile.
Update the SRI hash values for clickstreamLoader.js and buywithprime.js
clickstreamLoader.js and buywithprime.jsTo update the SRI hash values for the clickstreamLoader.js and buywithprime.js scripts, take the following steps.
-
Open the following file in a text editor:
link_amazon_bwp/cartridges/int_buywithprime/cartridge/scripts/amazon/hooks/htmlHooks.js -
Find the following script references in the
htmlHeadfunction:// clickstreamLoader.js hash Velocity.render('<script defer src="$url" integrity="sha384-abC123dEf456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG"></script>', { url: URLUtils.staticURL('/js/clickstreamLoader.js') }); // buywithprime.js hash Velocity.render('<script defer src="$url" integrity="sha384-abC123dEf456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG"></script>', { url: URLUtils.staticURL('/js/buywithprime.js').toString() }); -
From the
js-asset-manifest.jsonfile, get the new SRI hash values for theclickstreamLoader.jsandbuywithprime.jsentries under theintegritykey. The following example contains placeholder SRI hash values."default/js/clickstreamLoader.js": { "src": "default/js/clickstreamLoader.js", "integrity": "sha384-abC123dEf456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG" }, "default/js/buywithprime.js": { "src": "default/js/buywithprime.js", "integrity": "sha384-xyZ123aBC456GhI789jKl+012MnO345pQr678sTu901vWx234yZa567bCd890EfG" }, -
In the
htmlHooks.jsfile, replace both SRI hash values with their corresponding new SRI hash values from thejs-asset-manifest.jsonfile and keep thesha384-prefix. -
Save the
htmlHooks.jsfile.
Generate new SRI hash values manually
If your configuration requires you to build the Buy with Prime cartridge separately, you may have to generate new SRI hash values manually. For details about how to generate new SRI hash values manually, in the Mozilla documentation see Subresource Integrity.
Updated 6 months ago
