Skip to content

fix(snapshot): support no-unsafe-eval CSP by evaluating snapshot files on server - #10665

Merged
sheremet-va merged 16 commits into
vitest-dev:mainfrom
hi-ogawa:snapshot-csp-server-eval
Aug 12, 2026
Merged

sheremet-va merged 16 commits into
vitest-dev:mainfrom
hi-ogawa:snapshot-csp-server-eval

Conversation

@hi-ogawa

@hi-ogawa hi-ogawa commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Description

Currently the snapshot utils always evaluates snapshot file key values with new Function in the test runtime, but this would fails on browser mode with stricter CSP.

This PR adds SnapshotEnvironment.readSnapshotFileData, so browser environment can evaluate the file on the server so that browser runtime can directly receive napshot key value data.

Also as a drive-by change, this PR now surfaces snapshot files evaluation errors as an actionable errors instead of silently ignoring it.

TODO

  • test

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

…s outside the tester

Add an optional `readSnapshotFileData` to `SnapshotEnvironment` so environments
that cannot evaluate snapshot content in the test runtime (e.g. the browser under
a `no-unsafe-eval` CSP) can evaluate it elsewhere. `SnapshotState.create` provides
the general default inline: it reads and evaluates the file in the current runtime
when the environment does not implement the method. The browser is the only
environment that overrides it, delegating to the server over RPC.

Replace the silent `getSnapshotData` eval with `evaluateSnapshotFile`, which throws
a hard error on corrupted snapshot files instead of swallowing it.

Co-authored-by: OpenCode (claude-opus-4-8) <noreply@opencode.ai>
@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 00148da
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a7c40fbab392400073ee7b1
😎 Deploy Preview https://deploy-preview-10665--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment thread packages/browser/package.json Outdated
Comment thread packages/snapshot/src/environment.ts
hi-ogawa and others added 4 commits August 10, 2026 18:57
Co-authored-by: OpenCode <noreply@opencode.ai>
Co-authored-by: OpenCode <noreply@opencode.ai>
Co-authored-by: OpenCode <noreply@opencode.ai>
"@types/node": "catalog:",
"@types/pngjs": "^6.0.5",
"@types/ws": "catalog:",
"@vitest/snapshot": "workspace:*",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a dev dep to bundle a small piece of code evaluateSnapshotFile about 20 loc. Technically this duplicates main vitest package's snapshot bundled dep, but should be fine since small enough.

Co-authored-by: OpenCode <noreply@opencode.ai>
@hi-ogawa hi-ogawa changed the title fix(snapshot): support no-unsafe-eval CSP by evaluating snapshot files outside the tester fix(snapshot): support no-unsafe-eval CSP by evaluating snapshot files on server Aug 12, 2026
hi-ogawa and others added 2 commits August 12, 2026 12:12
Co-authored-by: OpenCode <noreply@opencode.ai>
Co-authored-by: OpenCode <noreply@opencode.ai>
Comment thread test/test-utils/index.ts
Comment on lines +322 to +324
const root = ctx?.config.root
if (errors && errors.length > 0 && root) {
tree.__unhandled_errors__ = errors.map((e: any) => replaceRoot(e.message, root))

@hi-ogawa hi-ogawa Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked error tree to normalize root:

"__unhandled_errors__": [
  "Invalid snapshot file, please manually fix or delete it: <root>/__snapshots__/basic.test.ts.snap",
]

@hi-ogawa
hi-ogawa marked this pull request as ready for review August 12, 2026 03:24
return fs.readFile(snapshotPath, 'utf-8')
},
async readSnapshotFileData(snapshotPath) {
checkFileAccess(snapshotPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this also needs api.allowExec check

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated 👌

hi-ogawa and others added 4 commits August 12, 2026 18:38
Co-authored-by: OpenCode <noreply@opencode.ai>
Co-authored-by: OpenCode <noreply@opencode.ai>
Co-authored-by: OpenCode <noreply@opencode.ai>
@hi-ogawa
hi-ogawa requested a review from sheremet-va August 12, 2026 09:46
Co-authored-by: OpenCode <noreply@opencode.ai>
@sheremet-va
sheremet-va merged commit 667c139 into vitest-dev:main Aug 12, 2026
25 of 28 checks passed
@hi-ogawa
hi-ogawa deleted the snapshot-csp-server-eval branch August 12, 2026 23:17
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getSnapshotData swallows the parse error, making snapshot failures impossible to debug

2 participants

Sponsor
SponsoredKunjungi sekarang
Promo