-
Notifications
You must be signed in to change notification settings - Fork 16
feat(web-component): add SRI support for web-component-ui loader RELEASE #1333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e2df56
feat(web-component): add sri support for web-component-ui loader
omercnet 00cfe28
fix: address review feedback for SRI implementation
omercnet 5639d75
Merge branch 'main' into shuni/task-20260216142601
omercnet 0d844c7
Merge remote-tracking branch 'origin/main' into shuni/task-2026021614…
omercnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -23,21 +23,25 @@ const hashUrl = (url: URL) => { | |
| return `${Math.abs(hash).toString()}`; | ||
| }; | ||
|
|
||
| const setupScript = (id: string) => { | ||
| const setupScript = (id: string, integrity?: string) => { | ||
| const scriptEle = document.createElement('script'); | ||
| scriptEle.id = id; | ||
|
|
||
| return scriptEle; | ||
| }; | ||
| if (integrity) { | ||
| scriptEle.integrity = integrity; | ||
| scriptEle.crossOrigin = 'anonymous'; | ||
| } | ||
|
|
||
| type ScriptData = { | ||
| id: string; | ||
| url: URL; | ||
| if ((window as any).DESCOPE_NONCE) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who sets this |
||
| scriptEle.setAttribute('nonce', (window as any).DESCOPE_NONCE); | ||
| } | ||
|
|
||
| return scriptEle; | ||
| }; | ||
|
|
||
| const injectScript = (scriptId: string, url: URL) => { | ||
| const injectScript = (scriptId: string, url: URL, integrity?: string) => { | ||
| return new Promise((res, rej) => { | ||
| const scriptEle = setupScript(scriptId); | ||
| const scriptEle = setupScript(scriptId, integrity); | ||
|
|
||
| scriptEle.onerror = (error) => { | ||
| scriptEle.setAttribute('status', 'error'); | ||
|
|
@@ -54,7 +58,21 @@ const injectScript = (scriptId: string, url: URL) => { | |
| }); | ||
| }; | ||
|
|
||
| const handleExistingScript = (existingScript: HTMLScriptElement) => { | ||
| const handleExistingScript = ( | ||
| existingScript: HTMLScriptElement, | ||
| expectedIntegrity?: string, | ||
| ) => { | ||
| if (expectedIntegrity) { | ||
| const actualIntegrity = existingScript.integrity; | ||
| if (actualIntegrity !== expectedIntegrity) { | ||
| return Promise.reject( | ||
| new Error( | ||
| `Integrity mismatch: expected ${expectedIntegrity}, found ${actualIntegrity}`, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (isScriptLoaded(existingScript)) { | ||
| return Promise.resolve(existingScript); | ||
| } | ||
|
|
@@ -74,23 +92,29 @@ const handleExistingScript = (existingScript: HTMLScriptElement) => { | |
| }); | ||
| }; | ||
|
|
||
| export type ScriptData = { | ||
| id: string; | ||
| url: URL; | ||
| integrity?: string; | ||
| }; | ||
|
|
||
| export const injectScriptWithFallbacks = async ( | ||
| scriptsData: ScriptData[], | ||
| onError: (scriptData: ScriptData, existingScript: boolean) => void, | ||
| ) => { | ||
| for (const scriptData of scriptsData) { | ||
| const { id, url } = scriptData; | ||
| const { id, url, integrity } = scriptData; | ||
| const existingScript = getExistingScript(id); | ||
| if (existingScript) { | ||
| try { | ||
| await handleExistingScript(existingScript); | ||
| await handleExistingScript(existingScript, integrity); | ||
| return scriptData; | ||
| } catch (e) { | ||
| onError(scriptData, true); | ||
| } | ||
omercnet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| try { | ||
| await injectScript(id, url); | ||
| await injectScript(id, url, integrity); | ||
| return scriptData; | ||
| } catch (e) { | ||
| onError(scriptData, false); | ||
|
|
@@ -105,6 +129,7 @@ export const generateLibUrls = ( | |
| libName: string, | ||
| version: string, | ||
| path = '', | ||
| integrity?: string, | ||
| ) => | ||
| baseUrls.reduce((prev, curr) => { | ||
| const baseUrl = curr; | ||
|
|
@@ -125,13 +150,16 @@ export const generateLibUrls = ( | |
| url.pathname = `/npm/${libName}@${version}/${path}`; | ||
| } | ||
|
|
||
| return [ | ||
| ...prev, | ||
| { | ||
| url: url, | ||
| id: `npmlib-${libName | ||
| .replaceAll('@', '') | ||
| .replaceAll('/', '_')}-${hashUrl(url)}`, | ||
| }, | ||
| ]; | ||
| const scriptData: ScriptData = { | ||
| url: url, | ||
| id: `npmlib-${libName.replaceAll('@', '').replaceAll('/', '_')}-${hashUrl( | ||
| url, | ||
| )}`, | ||
| }; | ||
|
|
||
| if (integrity) { | ||
| scriptData.integrity = integrity; | ||
| } | ||
|
|
||
| return [...prev, scriptData]; | ||
| }, []); | ||
This file contains hidden or 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
107 changes: 107 additions & 0 deletions
107
packages/libs/sdk-mixins/test/injectNpmLibMixin.sri.test.ts
This file contains hidden or 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,107 @@ | ||
| import { | ||
| generateLibUrls, | ||
| type ScriptData, | ||
| } from '../src/mixins/injectNpmLibMixin/helpers'; | ||
|
|
||
| describe('injectNpmLibMixin - SRI support', () => { | ||
| const baseUrls = ['https://cdn1.example.com', 'https://cdn2.example.com']; | ||
| const libName = '@descope/web-components-ui'; | ||
| const version = '1.0.0'; | ||
| const path = 'dist/umd/index.js'; | ||
|
|
||
| describe('generateLibUrls with SRI', () => { | ||
| it('should include integrity in generated script data when provided', () => { | ||
| const sriHash = | ||
| 'sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC'; | ||
|
|
||
| const result = generateLibUrls(baseUrls, libName, version, path, sriHash); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| result.forEach((scriptData) => { | ||
| expect(scriptData.integrity).toBe(sriHash); | ||
| expect(scriptData.url).toBeDefined(); | ||
| expect(scriptData.id).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not include integrity when not provided', () => { | ||
| const result = generateLibUrls(baseUrls, libName, version, path); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| result.forEach((scriptData) => { | ||
| expect(scriptData.integrity).toBeUndefined(); | ||
| expect(scriptData.url).toBeDefined(); | ||
| expect(scriptData.id).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not include integrity when empty string is provided', () => { | ||
| const result = generateLibUrls(baseUrls, libName, version, path, ''); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| result.forEach((scriptData) => { | ||
| expect(scriptData.integrity).toBeUndefined(); | ||
| expect(scriptData.url).toBeDefined(); | ||
| expect(scriptData.id).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should apply same integrity to all CDN URLs', () => { | ||
| const sriHash = 'sha384-test123'; | ||
| const multipleCdns = [ | ||
| 'https://descopecdn.com', | ||
| 'https://static.descope.com', | ||
| 'https://cdn.jsdelivr.net', | ||
| ]; | ||
|
|
||
| const result = generateLibUrls( | ||
| multipleCdns, | ||
| libName, | ||
| version, | ||
| path, | ||
| sriHash, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(3); | ||
| result.forEach((scriptData) => { | ||
| expect(scriptData.integrity).toBe(sriHash); | ||
| }); | ||
| }); | ||
|
|
||
| it('should generate correct URLs with integrity', () => { | ||
| const sriHash = 'sha256-abc123'; | ||
| const result = generateLibUrls( | ||
| [baseUrls[0]], | ||
| libName, | ||
| version, | ||
| path, | ||
| sriHash, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0].url.toString()).toContain(libName); | ||
| expect(result[0].url.toString()).toContain(version); | ||
| expect(result[0].url.toString()).toContain(path); | ||
| expect(result[0].integrity).toBe(sriHash); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ScriptData type', () => { | ||
| it('should support optional integrity field', () => { | ||
| const scriptDataWithIntegrity = { | ||
| id: 'test-script', | ||
| url: new URL('https://example.com/script.js'), | ||
| integrity: 'sha384-test', | ||
| }; | ||
|
|
||
| const scriptDataWithoutIntegrity: ScriptData = { | ||
| id: 'test-script', | ||
| url: new URL('https://example.com/script.js'), | ||
| }; | ||
|
|
||
| // TypeScript should accept both forms | ||
| expect(scriptDataWithIntegrity.integrity).toBe('sha384-test'); | ||
| expect(scriptDataWithoutIntegrity.integrity).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not to set enabled by default?