Skip to content

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

Open
lazerg wants to merge 1 commit into
vitest-dev:mainfrom
lazerg:fix/issue-10615-snapshot-parse-error
Open

fix(snapshot): support no-unsafe-eval CSP by evaluating snapshot files server-side#10646
lazerg wants to merge 1 commit into
vitest-dev:mainfrom
lazerg:fix/issue-10615-snapshot-parse-error

Conversation

@lazerg

@lazerg lazerg commented Jun 22, 2026

Copy link
Copy Markdown

Description

In browser mode, when the page is served with a no-unsafe-eval CSP policy, getSnapshotData silently fails to parse .snap files because new Function() is blocked. The empty catch {} discards the error, leaving snapshot data empty and producing confusing mismatch failures with no hint about the real cause.

This PR introduces an optional readSnapshotFileData method on SnapshotEnvironment that returns already-evaluated snapshot data. When the environment provides it, SnapshotState.create() uses it instead of readSnapshotFile + new Function, so the browser tester runtime never has to eval. The browser's VitestBrowserSnapshotEnvironment implements the method by delegating to the server via RPC, where the file is read and evaluated safely in Node.js context.

Resolves #10615

@lazerg lazerg force-pushed the fix/issue-10615-snapshot-parse-error branch from ba0b491 to 494b3a0 Compare June 22, 2026 23:51
@lazerg

lazerg commented Jun 23, 2026

Copy link
Copy Markdown
Author

the two windows failures aren't from this PR, both Build&Test and Browsers windows jobs fail the same way on https://github.com/vitest-dev/vitest/actions/runs/27978136425 which is a completely different branch. looks like pre-existing flakiness on windows

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we can go all-in as breaking change like:

  • require readSnapshotFileData
  • surface populate error as hard error and say something like "invalid snapshot file, please manually fix or delete (path to snapshot file)"

new e2e should cover

  • csp wihtout unsave eval
  • new hard error populate behavior

resolveRawPath: (testPath: string, rawPath: string) => Promise<string>
saveSnapshotFile: (filepath: string, snapshot: string) => Promise<void>
readSnapshotFile: (filepath: string) => Promise<string | null>
readSnapshotFileData?: (filepath: string) => Promise<Record<string, string> | null>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

On 2nd thought, it may be fine this is optional for integrator but fill up the default implementation on environment side somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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