Skip to content

Make the members of the web interfaces configurable - #48540

Open
robobun wants to merge 2 commits into
mainfrom
robobun/110698b9/web-idl-members-configurable
Open

robobun wants to merge 2 commits into
mainfrom
robobun/110698b9/web-idl-members-configurable

Conversation

@robobun

@robobun robobun commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • No member of TextDecoder, Blob, File, Request, Response or Crypto can be redefined or deleted. Object.defineProperty(TextDecoder.prototype, "decode", { value, configurable: true }) throws TypeError: Attempting to change configurable attribute of unconfigurable property. Under vitest, vi.spyOn(crypto, "randomUUID") stays after vi.restoreAllMocks().
  • Web IDL and Node.js make them configurable. Bun locked them in ed72bee and f947987 (bun-v0.5.6), no reason recorded. Reversing that needs a maintainer's yes.

Fix

  • Set configurable: true on the five bindgen schemas, and drop DontDelete from three hand-written accessors. 70 properties change.
  • Define Crypto.prototype.subtle with the class. Before, creating crypto defined it over a program's own definition.
  • cachedFieldOf keeps the cached getters of a locked prototype: the --aot table stays byte-identical.
  • Verified: test/js/web/web-globals.test.js and four more files: 12 new tests fail on main. Self-reviewed: 20 concerns raised, 19 addressed (notes).

Background

  • bindgen generates a C++ class from a .bind.ts schema. By default its rows get DontDelete, which means not configurable (src/codegen/bindgen-lib-internal.ts:3879).
  • Code compiled with --aot reads a cached getter (request.url) from a field. lockedMembers: ["prototype"] makes an executable refuse such changes.
  • Considered flipping the bindgen default: about 1,300 rows on Bun's own classes move.

Downsides

  • Code that relied on the TypeError changes. Bun's own code reads a replaced getter in the places that the notes list. One of them, new Request(request, init), differs from Node.js. It must be closed before this merges.
  • Spies on Bun's own classes and on Worker.prototype.threadId still stay.
  • Release binary: .text +256 B, .rodata +0 B. Getter reads: 1,353.08 M instructions, 1,353.09 M on main.
Notes

Supersedes #40617, which did this for Crypto alone against the generator that #46329 deleted. Related to #40612.

Questions for a maintainer

  1. Is it right to reverse ed72bee for the web interfaces? That commit ("Mark some things as not configurable", 2023-02-06, empty body) wrote configurable: false on TextDecoder, Request, Response and Blob, and f947987 made it the default of the generator. The members were configurable through bun-v0.5.5. fix(web): make URLSearchParams.prototype.size configurable #25762 lifted the lock for URLSearchParams.prototype.size with Web IDL as the reason.
  2. The configurable test in cachedFieldOf looks more careful than the compiler needs: in every run below, an executable refuses or follows each change to a member that compiled code reads. This PR keeps the test and adds && !isLocked(type), so the table does not move. Without the test, SocketAddress gains two cached getters (107 to 109) and five table files change. That is for the owner of the compiler to decide.
  3. A flip of the bindgen default is the fix at the generator. It is not in this PR: it moves about 1,300 rows on Bun's own classes, and with today's cachedFieldOf the table generator fails.
  4. Where do the reads of new Request(request, init) get closed? See "Members that Bun's own code reads by name". They can be three input-gated arms in the rewrite of http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528, whose construct_into passes request: None and still reads url, signal and a null or used body by name. Or they can be a change to main's loop that lands with this PR. This PR must not merge before one of them.

Who meets the lock (main 98cda0d, this branch, Node v26.3.0)

  • vitest 4.1.9, the real CLI, three cases (vi.spyOn(crypto, "randomUUID"), vi.spyOn(response, "ok", "get"), vi.spyOn(TextDecoder.prototype, "encoding", "get"), each with vi.restoreAllMocks()): main 0 of 3, this branch 3 of 3, Node 3 of 3. @vitest/spy defines the spy on the object with the attributes of the inherited member and removes it with Reflect.deleteProperty(), which answers false for a property that is not configurable.
  • node:test, t.mock.getter() then t.mock.restoreAll() on TextDecoder#encoding, Blob#size, Request#url, Response#ok: main 0 of 4 (TypeError: Attempting to change the getter of an unconfigurable property.), this branch 4 of 4, Node 4 of 4.
  • The report's script: main prints the TypeError and false false false false | true. This branch prints wrapped a and true true true true | true, as Node does.
  • Not affected on main: plain assignment, Object.defineProperty() with value alone, and spyOn of bun:test, which installs natively.

Crypto.prototype.subtle

  • On main the initializer of the lazy crypto global defines it, with DontDelete. JSC runs that initializer when it links a code block that names crypto, so the accessor can arrive before or after a definition of the program.
  • A definition that is there first: a release build replaces it, and a debug build aborts with ASSERTION FAILED: !hadProperty in JSObject::putDirectCustomAccessor.
  • An --aot executable on main has no subtle on the prototype until crypto is read.
  • The AllowLazyMaterializationOfImmutableProperties scope is needed. A build without it gives crypto.subtle === undefined in an --aot executable that only reads it.
  • The no-op setter stays. Node has a getter only.

What stays locked on a web interface

  • FormData.from and FormData.prototype.toJSON. They are read-only functions that Bun calls by name: Bun.readableStreamToFormData() the first, console.log() of a FormData the second.
  • Worker.prototype.threadId. node:worker_threads reads it from the web Worker that it wraps, a private field, and routes the messages between threads by it (src/js/node/worker_threads.ts:962). The first revision of this PR unlocked it, and a review found that a program could then redirect that routing.
  • No standard has these three. The walk test expects exactly them.
  • Under --aot, what the prototypes of Request, Response and Blob have stays read-only, as docs/compiler/compatibility.mdx says. bun run and an executable now differ for them as they do for Headers and URL.
  • params and cookies of the request that a Bun.serve route gets belong to a class of Bun's own.

Members that Bun's own code reads by name

A getter that a program redefines or deletes reaches these readers. None of them is closed in this PR.

  • The input of new Request(request, init): url, signal and an empty or used body (src/runtime/webcore/Request.rs:1182, :1216, :1260). This is the one place where this PR moves Bun away from Node.js. After delete Request.prototype.url the copy throws Failed to construct 'Request': url is required., a url getter gives the copy its own answer, a signal getter that answers a string throws, and a body getter that throws makes the copy throw. Node v26.3.0 copies the real request in all four. new Request(request) and request.clone() are not affected.
    • The read is reachable without this PR. A bun:test spy on Request.prototype.url gives the copy the mock's URL on main. A keepalive getter that a program adds to Request.prototype makes the copy of a request with an empty body throw TypeError: keepalive on 1.4.3-canary.
    • A draft took the three from native state when an exact Request is the input. Getter calls on the input went from 3 to 0. In a table of 14 call forms and 144 prototype changes, a change steered the result in 68 rows before and in 28 after, all 28 with a Request or Response in the place of init, where Node.js reads by name too (42 rows). The 14 rows without a change stayed the same.
    • The draft is on branch robobun/110698b9/request-input-own-state. It is not a PR. Its self-review found two things. A native copy of the signal loses the abort reason after a collection: fetch() of the copy rejects with undefined where main gives TimeoutError, because only the JS object of the signal keeps the reason alive. And http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528 rewrites this function. So the fix waits for question 4.
  • A Request with an own property or of a subclass: new Request() and fetch() read every member by name, the rule of SvelteKit fetch issue #4718. A getter that is redefined on Request.prototype now reaches them too. webcore: copy internal state in new Request(request) instead of calling getters #37033 covers them.
  • A Request or Response in the place of init: url, signal and a null or empty body are read by name. Node.js reads init by name too.
  • undici.request() reads status, headers, body and bodyUsed of its own Response (src/js/thirdparty/undici.ts:68, :71, :233). With a status getter that answers 500, a 201 response gives statusCode 500.
  • node:crypto reads crypto.subtle once when it loads (src/js/node/crypto.ts:233). A getter that throws stops require("node:crypto"). An own property on crypto does the same on 1.4.3-canary.
  • req.cookies of a Bun.serve route reads headers of a request that the program holds (src/jsc/bindings/JSBunRequest.cpp:242).
  • Node.js reads members by name too. On Node v26.3.0, after a signal getter on Request.prototype, request.clone() throws TypeError: Invalid value used as weak map key. Bun gives the real request.

bun test --isolate=modules

The runner puts back what a test file changed on the shared global object (src/js/internal/test/globals.ts). These members now take part in that.

  • A member that a file deletes is back for the next file, at the end of the key order.
  • A Response.json that a file replaces and makes read-only is put back. On main the replacement stays, because the runner skips a property that is neither configurable nor writable.
  • The runner compares the value, the getter and the setter. A change of a flag alone stays for the next file, as it does for Headers.prototype.get on main.

Not in this PR

  • The classes that Node.js writes in JavaScript: Timeout, Immediate, net.BlockList, module.SourceMap, the zlib handles. Node makes their members configurable and most of them non-enumerable, so they need more than this key. 40 members on 12 holders still differ from Node v26.3.0 in configurable (93 on 18 on main).
  • Bun's own classes. A vitest spy on server.publish still stays.

Measurements (linux-x64)

The size, instruction and attribute numbers are from this branch on 98cda0d. The row counts and the table comparison were taken again after the rebase onto 89452d2.

  • Generated rows with DontDelete: 1428 to 1361. Four generated files change, and each changed line loses only that token. Three rows in tables written by hand lose it too.
  • Table of built-ins for --aot: cached getters 107 to 107 (101 without the isLocked clause). 13 of 13 generated table files have the sha256 of main's, also after own-properties.txt was regenerated (8 lines).
  • A dump of the attributes of 7,641 properties (globals, exports of the Node modules, prototypes of instances), taken when this PR also unlocked Worker.prototype.threadId: 71 became configurable (70 without threadId), Crypto.prototype.subtle is new before the first read of crypto, nothing else changed.
  • Release binary, size -A: .text +256 B, .rodata +0 B, file +4,096 B (one page). No host function added.
  • Instructions in user space, 7 runs each, BUN_JSC_useConcurrentJIT=0. A loop of 5,000,000 iterations that reads six getters: main median 1,353,088,356 (range 42,836), this branch 1,353,079,184 (range 35,895). An empty script: main 3,695,758 (range 22,077), this branch 3,697,512 (range 24,393). perf and valgrind are not on the machine, so a small program opened the counter with perf_event_open.
  • CustomGetterSetter cells: +1 after Crypto is touched (main +0), +1 after the first read of crypto (main +1), 37 at start on both.

Ahead-of-time compiler

  • test/aot/parallel/host-classes/replaced-prototype-getters.ts gains rows. TextDecoder.encoding: the executable reads every redefinition, deletion and restore as plain Bun does. Request.url and Response.statusText: each change throws Cannot assign to Request.prototype.url because built-in objects are read-only in a compiled executable. TextDecoder.fatal and Crypto.subtle: a change under a name that the build cannot read is refused.
  • replaced-methods-and-statics.ts (new) does the same for decode, randomUUID, Response.json, Response.redirect and the locked Response.prototype.clone. spelled-out-changes-of-host-members.mjs (new) writes the object and the name out. All three pass as written, with types erased, with failing speculations and with proof checks, and fail on main.
  • test/aot/fixed-runs/closed/getters-kept-in-a-field-redefined-by-a-test.txt (new): under bun test --aot, typed reads of Request.url, Response.statusText and TextDecoder.encoding follow a definition, a deletion and a restore as under bun test. It fails on main.
  • By hand: the grid test/aot/fuzz/grids/tests-write-on-every-accessor-of-built-in-classes.ts, with Crypto no longer left out, prints the same 639 lines under bun test --isolate and bun test --aot --isolate.
  • Changed under bun test --aot: a getter spy on TextDecoder.prototype.encoding through an object that the build cannot read is now taken, and the compiled read sees it. main refuses it with Cannot spy on TextDecoder.prototype.encoding.
  • A debug build with ASAN aborts now and then while it compiles (AOT: debug/ASAN compiler crash in LinkFlow::findWhatSomeLaneWasGiven #48169). That happens on main too.

Self-review

20 concerns, 19 addressed. The scope changed with it: Timeout, Immediate and net.BlockList left this PR, and the accessors of Headers, FormData and URLSearchParams came in. Not done: a stored test-run row that fails on main for the getter spy above. Inside the shared projects of test/aot/fixed-runs main takes that spy too, so the row would prove nothing.

Tests

  • test/js/web/web-globals.test.js: every function and accessor of every global interface is configurable but for the three that stay locked, and a matrix of redefine, replace, delete and restore on eight interfaces.
  • test/js/web/crypto/web-crypto.test.ts: six orders of reading and writing subtle.
  • test/js/node/test_runner/node-test.test.ts with a fixture that also passes under Node.
  • test/js/third_party/vitest/vitest.test.ts: the three vitest cases.
  • test/internal/builtins-table/direct-entries-of-getters.test.ts: the rule of cachedFieldOf.
  • Also run: own-properties.test.ts, fixed-runs-closed.test.ts, mock-fn.test.js, visible-surface.test.ts, the fetch, FormData, Headers, URLSearchParams and worker suites, isolation.test.ts.

no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/web/web-globals.test.js

Web IDL makes the operations and attributes of an interface configurable.
The members of TextDecoder, Blob, File, Request, Response and Crypto were
not, so a program could not redefine or delete one, or replace a getter.
They have been locked since ed72bee and f947987 (bun-v0.5.6).
Today the lock is the default of a bindgen class, which a schema lifts
with `configurable: true`.

- Set `configurable: true` on the schemas of TextDecoder, Blob, Request,
  Response and Crypto.
- Drop DontDelete from the four accessors that tables written by hand
  add to a web interface: Headers.prototype.count,
  FormData.prototype.length, URLSearchParams.prototype.length and
  Worker.prototype.threadId.
- Define `Crypto.prototype.subtle` when the class is set up, without
  DontDelete. It was defined when the `crypto` object was made, over any
  definition that a program had made before.
- Keep the cached getters of a locked prototype in the table of
  built-ins that `bun build --compile --aot` reads. The table stays as
  it is.
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor
Pre-merge checks: Inconclusive 2

Failed Failed checks: 2 inconclusive

Check name Status Explanation Resolution
Title check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
Description check Pending This assessment has not completed. The check will be retried on the next pre-merge check run.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@robobun

robobun commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:48 PM PT - Nov 11th, 2026

✅ @robobun, your commit d55c1d644b12f1470240f78670cd19904a330e81 passed in Build #140390! 🎉


🧪   To try this PR locally:

bunx bun-pr 48540

That installs a local version of the PR into your bun-48540 executable, so you can run:

bun-48540 --bun

@robobun

robobun commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

Reproduced on main (98cda0d and 89452d2), release and debug builds, linux-x64:

const real = TextDecoder.prototype.decode;
Object.defineProperty(TextDecoder.prototype, "decode", {
  value(...args) { return "wrapped " + real.apply(this, args); },
  writable: true,
  configurable: true,
});
// main: TypeError: Attempting to change configurable attribute of unconfigurable property.
// this PR and Node v26.3.0: new TextDecoder().decode(new Uint8Array([97])) === "wrapped a"

The same script lists configurable for decode, encoding, fatal and ignoreBOM: false false false false on main, true true true true with this PR and in Node.

With vitest 4.1.9 on main, vi.spyOn(crypto, "randomUUID") is still in place after vi.restoreAllMocks().

The new tests fail on main and pass on this branch.

The description asks a maintainer two things. First, to confirm that the lock from ed72bee can go for the web interfaces. Second, where the reads of new Request(request, init) get closed (question 4): with the getters configurable, a program that deletes or redefines Request.prototype.url changes that copy, and Node.js copies the real request. This PR must not merge before those reads are closed.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the initLater override for JSCrypto in src/jsc/bindings/ZigGlobalObject.cpp: it is the statement directly after m_bindgenAdditions.initialize(), which only stores a per-class callback (src/jsc/bindings/Bindgen/AdditionsToGlobalObject.cpp), nothing calls get on that structure in between, and src/runtime/webcore/Crypto.bind.ts declares no subtle row, so there is no duplicate definition or ordering hazard. The regenerated own-properties.txt matches the four hand-written table edits (length/count flip to ac; FormData from/toJSON stay locked). I did not run the test suite or the --aot table generator, so the "byte-identical table" claim is unverified here.

Extended reasoning...

The change flips configurable: true on five bindgen schemas, removes DontDelete from four hand-written WebCore accessors, moves the Crypto.prototype.subtle custom accessor into a LazyClassStructure initializer override, and adjusts the AOT builtins table rule in collect-bindgen.ts; no injection, auth or data-exposure surface is touched. Two inline findings (Worker.threadId reliance in node:worker_threads, a typecheck error in the vitest test) are posted, and the PR itself reverses a 2023 maintainer decision on property attributes, so a human decision is needed regardless.

Comment thread src/jsc/bindings/webcore/JSWorker.cpp Resolved
node:worker_threads reads threadId from the web Worker that it wraps and
routes the messages between threads by it. No standard has the member,
so it stays as it is on main. The walk in web-globals.test.js expects
exactly the three members that stay locked.

Type the file map that createVitestProject takes.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I re-reviewed the PR after the second commit and found no bugs; both points from my earlier review are addressed (Worker.prototype.threadId stays DontDelete and the walk test lists it, and createVitestProject now takes Record<string, string>). Since the PR itself asks a maintainer to decide whether to reverse ed72bee for the web interfaces, a human look is still the right next step.

What was reviewed:

  • The JSCrypto initializer override in src/jsc/bindings/ZigGlobalObject.cpp: it replaces the initLater that AdditionsToGlobalObject::initialize() installs, calls the generated setupCryptoClassStructure (which runs init.setPrototype) before touching init.prototype, and the getter still resolves the singleton through cryptoObject() outside the initializer, so no re-entrancy.
  • cachedFieldOf in collect-bindgen.ts against isLocked/lockedMembers: Blob, Request and Response keep their cached fields; TextDecoder and Crypto are unlocked and follow redefinition, matching the new fixed-run and parallel AOT fixtures.
  • The four hand-written hash-table rows and own-properties.txt/snapshot updates line up with the .bind.ts changes; FormData.from and FormData.prototype.toJSON remain locked as stated.
  • Not run here: the codegen and the AOT builtins-table comparison, so the claim that the 13 table files stay byte-identical rests on the PR description.
Extended reasoning...

The change flips configurable: true on five bindgen schemas (Blob, Crypto, TextDecoder, Request, Response), drops DontDelete from four hand-written WebCore accessor rows, moves the Crypto.prototype.subtle custom accessor from the lazy crypto global initializer into the JSCrypto lazy class-structure initializer, and adjusts cachedFieldOf so locked prototypes keep cached-getter reads under --aot. It touches no auth or injection surface; the crypto-adjacent change is only property attributes on Crypto.prototype. Both findings from the earlier review were addressed in commit d55c1d6, the bug hunt ran to a dry streak, and the test additions span node:test, vitest, web-crypto subprocess cases and AOT fixtures. Approval is withheld because the PR explicitly asks a maintainer to ratify reversing a 2023 decision that locked these members, and because the JSC binding and codegen edits are owned areas where the generated output and table-identity claims were not reproduced in this run.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants