Repository navigation
fix(wallet): ship the security headers config in the build output #683
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
+93
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs'; | ||
| import { tmpdir } from 'os'; | ||
| import { join, resolve } from 'path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { withIcAssetsFile } from './with-ic-assets.plugin'; | ||
|
|
||
| type IcAssetsRule = { match: string; headers?: Record<string, string> }; | ||
|
|
||
| const CANISTER_IDS_PATH = resolve(__dirname, '../../../../canister_ids.json'); | ||
|
|
||
| const INDEX_HTML = '<script>window.inline = true;</script><script src="/main.js"></script>'; | ||
| // sha256 of the inline script above, base64 encoded. | ||
| const INLINE_SCRIPT_HASH = "'sha256-5n2DSRDGaWy2SggN68iONM84vDSa6opmut5FktFmjLE='"; | ||
|
|
||
| const runWriteBundle = (dir: string): void => { | ||
| const plugin = withIcAssetsFile({ isProduction: true }); | ||
| const writeBundle = plugin.writeBundle as unknown as (options: { dir: string }) => void; | ||
|
|
||
| writeBundle.call({}, { dir }); | ||
| }; | ||
|
|
||
| const readHeadersForAllAssets = (dir: string): Record<string, string> => { | ||
| const rules: IcAssetsRule[] = JSON.parse(readFileSync(join(dir, '.ic-assets.json'), 'utf-8')); | ||
|
|
||
| return rules.find(rule => rule.match === '**/*')?.headers ?? {}; | ||
| }; | ||
|
|
||
| describe('with-ic-assets plugin', () => { | ||
| let outDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| outDir = mkdtempSync(join(tmpdir(), 'orbit-wallet-dist-')); | ||
| writeFileSync(join(outDir, 'index.html'), INDEX_HTML); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(outDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('writes .ic-assets.json into the build output', () => { | ||
| runWriteBundle(outDir); | ||
|
|
||
| expect(existsSync(join(outDir, '.ic-assets.json'))).toBe(true); | ||
| }); | ||
|
|
||
| it('sets the security headers on every asset', () => { | ||
| runWriteBundle(outDir); | ||
| const headers = readHeadersForAllAssets(outDir); | ||
|
|
||
| expect(headers['X-Frame-Options']).toBe('DENY'); | ||
| expect(headers['X-Content-Type-Options']).toBe('nosniff'); | ||
| expect(headers['Content-Security-Policy']).toContain("default-src 'none'"); | ||
| expect(headers['Content-Security-Policy']).toContain("frame-ancestors 'none'"); | ||
| }); | ||
|
|
||
| it('allows the inline scripts of index.html by their hash', () => { | ||
| runWriteBundle(outDir); | ||
|
|
||
| expect(readHeadersForAllAssets(outDir)['Content-Security-Policy']).toContain( | ||
| INLINE_SCRIPT_HASH, | ||
| ); | ||
| }); | ||
|
|
||
| it('allows downloads from every wasm chunk store in canister_ids.json', () => { | ||
| runWriteBundle(outDir); | ||
| const csp = readHeadersForAllAssets(outDir)['Content-Security-Policy'] ?? ''; | ||
| const connectSrc = csp.split('; ').find(directive => directive.startsWith('connect-src ')); | ||
| const canisterIds = JSON.parse(readFileSync(CANISTER_IDS_PATH, 'utf-8')); | ||
| const stores = Object.values(canisterIds.wasm_chunk_store ?? {}) as string[]; | ||
|
|
||
| expect(stores.length).toBeGreaterThan(0); | ||
| for (const store of stores) { | ||
| expect(connectSrc?.split(' ')).toContain(`https://${store}.icp0.io`); | ||
| } | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.