-
Notifications
You must be signed in to change notification settings - Fork 0
test: Storybook browser smoke test + CSP-safety gate #16
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
3 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import type { TestRunnerConfig } from '@storybook/test-runner'; | ||
|
|
||
| /** | ||
| * Storybook test-runner config — a browser smoke test over every story. | ||
| * | ||
| * For each story page it asserts two things: | ||
| * 1. No JavaScript errors — uncaught exceptions (`pageerror`) or app-level | ||
| * `console.error` calls. Network/resource failures (e.g. the YouTube | ||
| * thumbnails and example.com images used in stories, which 404 in CI) | ||
| * are deliberately ignored — they aren't component bugs. | ||
| * 2. The component actually painted — `#storybook-root` has at least one | ||
| * visible descendant with a non-zero box. Catches the failure mode where | ||
| * a component throws or renders nothing and the page is silently blank. | ||
| * | ||
| * Runs against both `storybook dev` and the static `storybook build` output | ||
| * (see the `test-storybook` / `test-storybook:ci` scripts). | ||
| */ | ||
|
|
||
| const ERRORS_KEY = '__unlayerStoryErrors'; | ||
| const LISTENERS_KEY = '__unlayerListenersAttached'; | ||
|
|
||
| // Resource-load / network noise that is not a component regression. | ||
| const RESOURCE_NOISE = | ||
| /(Failed to load resource|net::ERR|ERR_[A-Z_]+|the server responded with a status of [45]\d\d|favicon)/i; | ||
|
|
||
| const config: TestRunnerConfig = { | ||
| async preVisit(page) { | ||
| const anyPage = page as any; | ||
|
|
||
| // The test-runner reuses one page per worker across stories, so attach the | ||
| // listeners exactly once and just reset the buffer on each visit. | ||
| if (!anyPage[LISTENERS_KEY]) { | ||
| anyPage[LISTENERS_KEY] = true; | ||
|
|
||
| page.on('console', (msg) => { | ||
| if (msg.type() !== 'error') return; | ||
| const text = msg.text(); | ||
| if (RESOURCE_NOISE.test(text)) return; | ||
| (anyPage[ERRORS_KEY] ??= []).push(`console.error: ${text}`); | ||
| }); | ||
|
|
||
| page.on('pageerror', (err) => { | ||
| (anyPage[ERRORS_KEY] ??= []).push(`pageerror: ${err.message}`); | ||
| }); | ||
| } | ||
|
|
||
| anyPage[ERRORS_KEY] = []; | ||
| }, | ||
|
|
||
| async postVisit(page, context) { | ||
| const id = `${context.title} › ${context.name}`; | ||
|
|
||
| const hasVisibleContent = await page.evaluate(() => { | ||
| const root = document.querySelector('#storybook-root'); | ||
| if (!root) return false; | ||
| // Media elements count as rendered even when their remote asset 404s in | ||
| // CI (no network) and the box collapses to 0×0 — the component still | ||
| // emitted its UI. Everything else must actually paint a non-zero box. | ||
| const MEDIA = new Set(['IMG', 'IFRAME', 'VIDEO', 'PICTURE', 'SVG', 'CANVAS']); | ||
| for (const el of Array.from(root.querySelectorAll('*'))) { | ||
| const style = getComputedStyle(el); | ||
| if (style.visibility === 'hidden' || style.display === 'none') continue; | ||
| if (MEDIA.has(el.tagName)) return true; | ||
| const rect = el.getBoundingClientRect(); | ||
| if (rect.width > 0 && rect.height > 0) return true; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| if (!hasVisibleContent) { | ||
| throw new Error( | ||
| `[${id}] rendered nothing visible — #storybook-root has no painted content`, | ||
| ); | ||
| } | ||
|
|
||
| const errors: string[] = (page as any)[ERRORS_KEY] ?? []; | ||
| if (errors.length > 0) { | ||
| throw new Error(`[${id}] browser errors:\n ${errors.join('\n ')}`); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env node | ||
| // Run with: node --disallow-code-generation-from-strings scripts/csp-probe.mjs | ||
| // | ||
| // CSP safety gate for @unlayer/react-elements. | ||
| // | ||
| // renderToHtml() pulls in @unlayer/exporters. If any reachable code evaluates a | ||
| // string (eval / new Function) at import or render time, a strict | ||
| // Content-Security-Policy without 'unsafe-eval' blocks it and the package throws | ||
| // in browsers, iframes, and CSP'd SSR — before any render runs. V8's | ||
| // --disallow-code-generation-from-strings is the engine-level equivalent of such | ||
| // a CSP, so importing + rendering the built bundle under it proves the shipped | ||
| // output is CSP-safe. | ||
| // | ||
| // This is a HARD gate. It fails if importing or rendering the built `dist/` | ||
| // evaluates a string anywhere — whether in this package's own code OR in its | ||
| // pinned @unlayer/exporters dependency. It will stay red until the workspace | ||
| // catalog pins an @unlayer/exporters release that precompiles its templates | ||
| // (no `new Function` at import / render). That is intentional: a green check | ||
| // must mean "@unlayer/react-elements is genuinely CSP-safe." | ||
|
|
||
| const CSP_ERROR = /Code generation from strings disallowed/; | ||
|
|
||
| try { | ||
| const React = (await import('react')).default; | ||
| const { | ||
| renderToHtml, | ||
| renderToPlainText, | ||
| Body, | ||
| Row, | ||
| Column, | ||
| Button, | ||
| Heading, | ||
| Image, | ||
| Divider, | ||
| Paragraph, | ||
| Html, | ||
| Social, | ||
| Menu, | ||
| Video, | ||
| Table, | ||
| ColumnLayouts, | ||
| } = await import('../dist/index.js'); | ||
|
|
||
| // Exercise every component, including the template-backed paths (links, menu, | ||
| // social) that historically compiled lodash templates at render time. | ||
| const tree = React.createElement( | ||
| Body, | ||
| null, | ||
| React.createElement( | ||
| Row, | ||
| { layout: ColumnLayouts.OneColumn }, | ||
| React.createElement( | ||
| Column, | ||
| null, | ||
| React.createElement(Heading, { mode: 'web' }, 'Heading'), | ||
| React.createElement(Paragraph, { mode: 'web' }, 'Paragraph'), | ||
| React.createElement(Button, { mode: 'web', href: 'https://example.com' }, 'Button'), | ||
| React.createElement(Divider, { mode: 'web' }), | ||
| React.createElement(Video, { | ||
| mode: 'web', | ||
| videoUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', | ||
| }), | ||
| React.createElement(Image, { mode: 'web', src: { url: 'https://example.com/i.png' } }), | ||
| React.createElement(Table, { mode: 'web' }), | ||
| React.createElement(Social, { | ||
| mode: 'web', | ||
| icons: [{ name: 'Facebook', url: 'https://example.com' }], | ||
| }), | ||
| React.createElement(Menu, { | ||
| mode: 'web', | ||
| items: [{ text: 'Home', link: { name: 'web', values: { href: 'https://example.com' } } }], | ||
| }), | ||
| React.createElement(Html, { mode: 'web' }, React.createElement('p', null, 'x')), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| for (const mode of ['web', 'email', 'document']) { | ||
| const html = renderToHtml(tree, { mode }); | ||
| if (typeof html !== 'string' || html.length === 0) { | ||
| console.error(`CSP_FAIL: renderToHtml(${mode}) produced no output`); | ||
| process.exit(2); | ||
| } | ||
| } | ||
| renderToPlainText(tree); | ||
|
|
||
| console.info( | ||
| `CSP_OK: @unlayer/react-elements imports + renders every component (web / ` + | ||
| `email / document / plaintext) under strict CSP — no eval / new Function`, | ||
| ); | ||
| } catch (err) { | ||
| const message = String((err && err.message) || err); | ||
| if (CSP_ERROR.test(message)) { | ||
| console.error( | ||
| `CSP_FAIL: @unlayer/react-elements (or its pinned @unlayer/exporters ` + | ||
| `dependency) evaluates a string (eval / new Function) at import or render — ` + | ||
| `it would throw under a Content-Security-Policy without 'unsafe-eval'.\n${message}`, | ||
| ); | ||
| } else { | ||
| console.error(`CSP probe failed to run: ${message}`); | ||
| } | ||
| process.exit(3); | ||
| } |
Oops, something went wrong.
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.