Skip to content

lib,src: fix web storage crash in snapshot builder - #66884

Open
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:webstorage-snapshot
Open

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:webstorage-snapshot

Conversation

@christianaurichzm

Copy link
Copy Markdown
Contributor

A snapshot builder script that reads globalThis.localStorage, sessionStorage or Storage aborts the build:

$ echo 'try { globalThis.localStorage } catch {}' > entry.js
$ node --snapshot-blob snapshot.blob --build-snapshot entry.js
Unknown external reference 0x5dfce95473e0.
<unresolved>

The webstorage binding never registers its C++ callbacks as external references, so once the getters load the binding the snapshot can't serialize the Storage template. Fixing only that is not enough for sessionStorage, because the Storage object itself can't be serialized either.

This registers the callbacks and makes creating localStorage or sessionStorage while building a snapshot throw ERR_NOT_SUPPORTED_IN_SNAPSHOT, like new Worker() does. lib/internal/webstorage.js also read --localstorage-file and the warning flag at load time, so a snapshot that touched localStorage ignored the file passed at run time and did not warn again. The options are now read in the getters, and the flag is reset when the snapshot is taken.

test/parallel/test-snapshot-webstorage.js covers both errors and the run-time case. It aborts without this change and passes with it.

#66740 adds a webstorage BindingData that is not serializable, so whichever of the two lands second will need to make it serializable. I haven't built them together yet.

Refs: #66740

The webstorage binding did not register its C++ callbacks as external
references. A snapshot builder script that only reads
`globalThis.localStorage` loads the binding, and serializing the
snapshot then aborted with "Unknown external reference".

Register them. Storage objects cannot be serialized, so creating
`localStorage` or `sessionStorage` while building a snapshot now throws
`ERR_NOT_SUPPORTED_IN_SNAPSHOT`, as creating a worker does. The web
storage module also reads its options when a Storage is created rather
than at load time, so a snapshot that loaded it still honors
`--localstorage-file` passed at run time. The flag that limits the
missing `--localstorage-file` warning to once is reset after the user's
serialize callbacks, so a run from the snapshot still warns.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.49%. Comparing base (56da329) to head (df0e001).
⚠️ Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66884      +/-   ##
==========================================
- Coverage   90.51%   90.49%   -0.02%     
==========================================
  Files         797      797              
  Lines      278221   278399     +178     
  Branches    53463    53493      +30     
==========================================
+ Hits       251844   251951     +107     
- Misses      16763    16811      +48     
- Partials     9614     9637      +23     
Files with missing lines Coverage Δ
lib/internal/webstorage.js 100.00% <100.00%> (ø)
src/node_external_reference.h 100.00% <ø> (ø)
src/node_webstorage.cc 78.71% <100.00%> (+1.07%) ⬆️

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants