Repository navigation
fix: keep a member keyed __proto__ as data on the response path (#269) - #279
Merged
Merged
Conversation
`Assemble.toWireSafeResource` copied a free-form object member by member with plain assignment, so an own key spelled `__proto__` (as `JSON.parse` or a database driver gives) ran the prototype setter: the member vanished from the wire and the copy was re-parented onto its value. The sparse fieldset projection behind `Handlers.project` (and the `Assemble` builders' `fields` option) copied requested attributes the same way. Both now write own properties through the `define` helper that #264 added to `internal/codecs.ts`, moved to `internal/define.ts` so the query codecs, `Assemble` and `Handlers` share it. The other response-path copies (`stringifyLinks`, `withMeta`, the document and `included` builders, and effect's `Record` decoding under `Document`) already use spreads, `Object.fromEntries` or effect's own-key-safe helpers and need no change. Tests go through the public API: `toWireSafeResource` on a resource decoded from JSON text, an `Assemble.resource` document with a sparse fieldset requesting the member, and `Handlers.project` directly. Each checks own keys, `Object.prototype` as the prototype, the JSON text round-trip, and that `Object.prototype` is untouched. Claude-Session: https://claude.ai/code/session_0187g2QpzaHqEzX8GJxiJBVc Co-Authored-By: Claude <noreply@anthropic.com>
9 tasks done
thomasfosterau
pushed a commit
that referenced
this pull request
Oct 8, 2026
#279 kept a member keyed __proto__ as data by writing every member of toWireValue and selectAttributes with Object.defineProperty, which made Assemble.toWireSafeResource ~60% slower and Handlers.project ~2.2x slower at 10k resources x 20 attributes. Those two copies now use a new `assign` helper that mirrors effect's Record.assignProperty: `__proto__` is defined, every other key plainly assigned. Output is unchanged. The request-path codecs (nest/flatten, #260/#264) keep `define`, which defines every key, so a name such as `toString` is still written as an own member under a frozen Object.prototype. The fast path brings back the pre-#279 behaviour there: a frozen prototype makes assigning `toString`/`constructor` throw (the "override mistake"), documented on `assign`. Tests: constructor/prototype/toString/__proto__ stay own data members through toWireSafeResource and Handlers.project; define/assign unit tests pin the split, with `prototype` now on the frozen stand-in; and nest/ flatten write a name that a read-only Object.prototype member shadows as their own, so switching the codecs to `assign` fails. Refs #269 Claude-Session: https://claude.ai/code/session_0187g2QpzaHqEzX8GJxiJBVc Co-Authored-By: Claude <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #269
Assemble.toWireSafeResourcecopied a free-form object member by member with plain assignment (out[key] = …). A value fromJSON.parseor a database driver can carry__proto__as an ordinary own key, and assigning that key runs the prototype setter, so the member vanished from the wire and the copy was re-parented onto its value. Verified ontrunk(7d16c9b) with the issue's reproduction:'{"kept":1}', no own__proto__, prototype notObject.prototype,settings.inner === 2(inherited).What changed:
src/Assemble.ts,toWireValue: every member is now written as an own data property. The doc comment ontoWireSafeResourcesays so.src/Handlers.ts,selectAttributes(behindHandlers.projectand theAssemblebuilders'fieldsoption): this was the one other response-path copy with the same pattern. A sparse fieldset that requested an own__proto__attribute dropped it the same way. It now writes own properties as well.src/internal/define.ts(new): thedefinehelper that fix(Query): drop undeclared query parameters when the query declares nothing (#260) #264 added privately tosrc/internal/codecs.ts(Object.defineProperty, enumerable, writable, configurable) now lives here, so the query codecs,AssembleandHandlersshare one implementation.codecs.tsimports it and its behaviour is unchanged.test/entry-points.test.ts: addsinternal/define.tsto the HTTP-tier module list. Only HTTP-tier modules reach it, and the schema entry point's module set is unchanged.Other copies on the response path that I checked and left alone, because they were already correct:
stringifyLinks,wireRelationshipLinks,Handlers.withMeta's merge and the document andincludedbuilders use object spread orObject.fromEntries, which define own properties.Documentencoding goes through effect'sRecordandStructcodecs. A probe on trunk showedDocument.AnyMetakeeps an own__proto__member on decode, on encode and underSchema.toCodecJson.Idempotency.canonicalusesObject.fromEntries.Request-path assignments are out of scope here and were not changed:
Lid.resolveLinkage, the flat/filter split insrc/Query.ts(#261's file, which existing__proto__cases inQuery.test.tsalready cover) andinternal/filter.ts(whose keys are generatedfilter[...]strings).Tests (written first, through the public API)
src/Assemble.test.ts:toWireSafeResourceon a resource decoded from JSON text. The test checks that a nested__proto__member inattributesand one inmetastay own data members withObject.prototypeas the prototype, that the output's JSON text equals the input's byte for byte, that a value under the member is still coerced (URL→ href), and thatObject.prototypeis untouched.src/Assemble.test.ts: anAssemble.resourcedocument built from that resource withfields: { things: ["__proto__", "title"] }. The projectedattributesand themetakeep the member, and the JSON text ofdatacontains it.src/Handlers.test.ts:Handlers.projectkeeps a requested own__proto__attribute as data.Mutation checks
Each fix site was reverted on its own to plain assignment, and the relevant suites (
src/Assemble.test.ts,src/Handlers.test.ts) were re-run:toWireValueback toout[key] = …keeps a member keyed __proto__…,carries a __proto__ member through an assembled document…)selectAttributesback toprojected[name] = …carries a __proto__ member through an assembled document…,keeps a requested attribute keyed __proto__…)definebody back torecord[key] = value__proto__cases insrc/internal/codecs.test.ts)Verification
Run in AGENTS.md order in a fresh clone of this branch:
pnpm install --frozen-lockfile: passvp run build: passvp check: pass (0 errors; none of the lines this PR adds raise a warn-tier lint)vp run check(sources, type-level tests, examples, emitted declarations): pass ("check: every type gate green")vp run -r test:unit: pass (root 2315/2315, examples 41/41, 45/45, 54/54)vp run docgen(every JSDoc@examplecompiled and executed): passfallow audit --format json --quiet --explain --gate-marker agent: verdictpass(0 introduced findings)fallow audit --base origin/trunk: pass ("No issues in 8 changed files"; 6 inherited findings excluded)Changeset
.changeset/wire-proto-member.md,patch(a pre-1.0 fix to published behaviour: data fidelity ofAssemble.toWireSafeResourceand sparse-fieldset projection)Notes
internal/define.tsis not exported from either entry point.Schema.fromJsonStringrather than callingJSON.parsedirectly, so they add no new warn-tier lint sites. effect's JSON decoder makes__proto__an own key just asJSON.parsedoes.🤖 Generated with Claude Code
Generated by Claude Code