-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: [DX-3334] Add the Subresource Integrity attribute to Checkout …
…files (#2314) Co-authored-by: Ubuntu <runner@runner.z3atkystfzyuthwl1ys0jzl12d.cx.internal.cloudapp.net>
- Loading branch information
1 parent
c84a52e
commit aa44226
Showing
10 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable max-len */ | ||
export async function generateSHA512Hash(url: string): Promise<string> { | ||
// Fetch the content of the remote JavaScript file | ||
const response = await fetch(url); | ||
const content = await response.text(); | ||
|
||
// Convert the content to an ArrayBuffer | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(content); | ||
|
||
// Use the Browser WebCrypto SubtleCrypto API to generate a SHA-512 hash | ||
const hashBuffer = await window.crypto.subtle.digest('SHA-512', data); | ||
|
||
// Convert the hash to a Base64 string | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashBase64 = btoa(String.fromCharCode(...hashArray)); | ||
|
||
return `sha512-${hashBase64}`; | ||
} | ||
|
||
async function getLatestGitTag(): Promise<string> { | ||
const response = await fetch('https://api.github.com/repos/immutable/ts-immutable-sdk/tags'); | ||
const tags = await response.json(); | ||
return tags[0].name; | ||
} | ||
|
||
export async function validatedHashesUrl(version: string): Promise<string> { | ||
if (version !== 'latest') { | ||
return `https://raw.githubusercontent.com/immutable/ts-immutable-sdk/refs/tags/${version}/packages/checkout/widgets-lib/hashes.json`; | ||
} | ||
|
||
const latestGitTag = await getLatestGitTag(); | ||
return `https://raw.githubusercontent.com/immutable/ts-immutable-sdk/refs/tags/${latestGitTag}/packages/checkout/widgets-lib/hashes.json`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,35 +2,55 @@ | |
import { SDK_VERSION_MARKER } from '../env'; | ||
import { getWidgetsEsmUrl, loadUnresolvedBundle } from './load'; | ||
|
||
const SDK_VERSION = SDK_VERSION_MARKER; | ||
|
||
jest.mock('./hashUtils', () => ({ | ||
generateSHA512Hash: jest.fn(async () => 'sha512-abc123'), | ||
// eslint-disable-next-line max-len | ||
validatedHashesUrl: jest.fn(async () => `https://raw.githubusercontent.com/immutable/ts-immutable-sdk/refs/tags/${SDK_VERSION}/packages/checkout/widgets-lib/hashes.json`), | ||
})); | ||
|
||
describe('load', () => { | ||
const SDK_VERSION = SDK_VERSION_MARKER; | ||
const scriptId = 'immutable-checkout-widgets-bundle'; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(console, 'warn').mockImplementation(() => { }); | ||
}); | ||
|
||
describe('load unresolved bundle', () => { | ||
it('should validate the versioning', () => { | ||
it('should validate the versioning', async () => { | ||
const tag = document.createElement('script'); | ||
loadUnresolvedBundle(tag, scriptId, SDK_VERSION); | ||
await loadUnresolvedBundle(tag, scriptId, SDK_VERSION); | ||
|
||
expect(document.head.innerHTML).toBe( | ||
'<script id="immutable-checkout-widgets-bundle" ' | ||
'<script ' | ||
+ 'integrity="sha512-abc123" ' | ||
+ 'crossorigin="anonymous" ' | ||
+ 'id="immutable-checkout-widgets-bundle" ' | ||
+ 'data-version="__SDK_VERSION__" ' | ||
+ `src="https://cdn.jsdelivr.net/npm/@imtbl/sdk@${SDK_VERSION}/dist/browser/checkout/widgets.js"></script>`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('get widgets esm url', () => { | ||
it('should validate the versioning', () => { | ||
expect(getWidgetsEsmUrl(SDK_VERSION)).toEqual( | ||
beforeEach(() => { | ||
// @ts-expect-error mocking only json value of fetch response | ||
global.fetch = jest.fn(async () => ({ | ||
json: async () => ({ 'dist/index.js': 'sha512-abc123' }), | ||
})); | ||
}); | ||
|
||
it('should validate the versioning', async () => { | ||
const widgetsEsmUrl = await getWidgetsEsmUrl(SDK_VERSION); | ||
expect(widgetsEsmUrl).toEqual( | ||
`https://cdn.jsdelivr.net/npm/@imtbl/sdk@${SDK_VERSION}/dist/browser/checkout/widgets-esm.js`, | ||
); | ||
}); | ||
|
||
it('should change version', () => { | ||
expect(getWidgetsEsmUrl('1.2.3')).toEqual( | ||
it('should change version', async () => { | ||
const widgetsEsmUrl = await getWidgetsEsmUrl('1.2.3'); | ||
expect(widgetsEsmUrl).toEqual( | ||
'https://cdn.jsdelivr.net/npm/@imtbl/[email protected]/dist/browser/checkout/widgets-esm.js', | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"dist/widgets.js": "sha512-WMrmg+Xs0GRr3rQ7dOxLMHXw1Co+WH9+LHzhVcV1mh7MIgacX5jmFtWM0DbXszQGBj6KlOe+3W5HiyPvGylGiQ==", | ||
"dist/index.js": "sha512-dQi34NMUtZhRBuxK3a36z3abkfbfX6+BWvmvILVMRzTQRdxq1w+X+DvkNdiFfRyAAAuAvCDGhk1XETGfE0cKKQ==" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// @ts-check | ||
import { readFileSync, writeFileSync } from "fs" | ||
import { createHash } from "crypto" | ||
|
||
const filesToHash = ["dist/widgets.js", "dist/index.js"] | ||
|
||
filesToHash.forEach(file => { | ||
try { | ||
readFileSync(file) | ||
} catch (e) { | ||
console.error(`File ${file} not found`) | ||
console.error('Please build the Checkout Widgets package') | ||
process.exit(1) | ||
} | ||
}) | ||
|
||
const hashes = filesToHash.reduce((acc, file) => { | ||
const hash = `sha512-${createHash("sha512").update(readFileSync(file | ||
)).digest("base64")}` | ||
acc[file] = hash | ||
return acc | ||
}, {}) | ||
|
||
writeFileSync("hashes.json", JSON.stringify(hashes, null, 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,4 +178,4 @@ | |
}, | ||
"type": "module", | ||
"types": "./dist/index.d.ts" | ||
} | ||
} |