Skip to content

fix: keep a member keyed __proto__ as data on the response path (#269) - #279

Merged
thomasfosterau merged 1 commit into
trunkfrom
issue-269-wire-proto-key
Oct 8, 2026
Merged

thomasfosterau merged 1 commit into
trunkfrom
issue-269-wire-proto-key

Conversation

@thomasfosterau

Copy link
Copy Markdown
Owner

Summary

Closes #269

Assemble.toWireSafeResource copied a free-form object member by member with plain assignment (out[key] = …). A value from JSON.parse or 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 on trunk (7d16c9b) with the issue's reproduction: '{"kept":1}', no own __proto__, prototype not Object.prototype, settings.inner === 2 (inherited).

What changed:

  • src/Assemble.ts, toWireValue: every member is now written as an own data property. The doc comment on toWireSafeResource says so.
  • src/Handlers.ts, selectAttributes (behind Handlers.project and the Assemble builders' fields option): 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): the define helper that fix(Query): drop undeclared query parameters when the query declares nothing (#260) #264 added privately to src/internal/codecs.ts (Object.defineProperty, enumerable, writable, configurable) now lives here, so the query codecs, Assemble and Handlers share one implementation. codecs.ts imports it and its behaviour is unchanged.
  • test/entry-points.test.ts: adds internal/define.ts to 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 and included builders use object spread or Object.fromEntries, which define own properties.
  • Document encoding goes through effect's Record and Struct codecs. A probe on trunk showed Document.AnyMeta keeps an own __proto__ member on decode, on encode and under Schema.toCodecJson.
  • Idempotency.canonical uses Object.fromEntries.

Request-path assignments are out of scope here and were not changed: Lid.resolveLinkage, the flat/filter split in src/Query.ts (#261's file, which existing __proto__ cases in Query.test.ts already cover) and internal/filter.ts (whose keys are generated filter[...] strings).

Tests (written first, through the public API)

  • src/Assemble.test.ts: toWireSafeResource on a resource decoded from JSON text. The test checks that a nested __proto__ member in attributes and one in meta stay own data members with Object.prototype as 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 that Object.prototype is untouched.
  • src/Assemble.test.ts: an Assemble.resource document built from that resource with fields: { things: ["__proto__", "title"] }. The projected attributes and the meta keep the member, and the JSON text of data contains it.
  • src/Handlers.test.ts: Handlers.project keeps 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:

Mutation Result
toWireValue back to out[key] = … 2 failed (keeps a member keyed __proto__…, carries a __proto__ member through an assembled document…)
selectAttributes back to projected[name] = … 2 failed (carries a __proto__ member through an assembled document…, keeps a requested attribute keyed __proto__…)
shared define body back to record[key] = value 5 failed (the 3 above plus the 2 existing __proto__ cases in src/internal/codecs.test.ts)
fix restored all pass

Verification

Run in AGENTS.md order in a fresh clone of this branch:

  • pnpm install --frozen-lockfile: pass
  • vp run build: pass
  • vp 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 @example compiled and executed): pass
  • fallow audit --format json --quiet --explain --gate-marker agent: verdict pass (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 of Assemble.toWireSafeResource and sparse-fieldset projection)

Notes

  • No exported signature changes. internal/define.ts is not exported from either entry point.
  • The new tests read their input with Schema.fromJsonString rather than calling JSON.parse directly, so they add no new warn-tier lint sites. effect's JSON decoder makes __proto__ an own key just as JSON.parse does.

🤖 Generated with Claude Code


Generated by Claude Code

`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>
@thomasfosterau
thomasfosterau merged commit fd6e2e4 into trunk Oct 8, 2026
1 check passed
@thomasfosterau
thomasfosterau deleted the issue-269-wire-proto-key branch October 8, 2026 11:27
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>
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.

bug: Assemble.toWireSafeResource drops a member keyed __proto__ and re-parents the copy

2 participants