Skip to content

fix: do not serialize Object.prototype members inherited by the input - #923

Closed
yxshee wants to merge 1 commit into
fastify:mainfrom
yxshee:inherited-object-prototype-properties
Closed

yxshee wants to merge 1 commit into
fastify:mainfrom
yxshee:inherited-object-prototype-properties

Conversation

@yxshee

@yxshee yxshee commented Nov 9, 2026

Copy link
Copy Markdown

Checklist

  • run npm run test and npm run benchmark — the test suite, lint and type tests pass; the change only adds an Object.hasOwn check for the handful of property names that exist on Object.prototype, so other properties compile to the same code as before
  • tests and/or benchmarks are included
  • documentation is changed or added — no documented behaviour changes
  • commit message and code follows the Developer's Certification of Origin and the Code of conduct

Problem

A property whose name is also a member of Object.prototype is serialized from the prototype when the input does not own it:

const stringify = fastJson({
  type: 'object',
  properties: {
    constructor: { type: 'boolean' },
    toString: { type: 'string' },
    valueOf: { type: 'integer' }
  }
})

stringify({ toString: 't' })
// {"constructor":true,"toString":"t","valueOf":...}  → throws:
// Error: The value "function valueOf() { [native code] }" cannot be converted to an integer.

With valueOf left out of the schema the empty object serializes as {"constructor":true,"toString":"function toString() { [native code] }"}; JSON.stringify({}) is {}. The same happens with objectMechanism: 'native'.

buildInnerObject emits const value = obj["constructor"]; if (value !== undefined) …, and the inherited function passes that check. I found this with a generator that builds random schemas and conforming values and compares the output with JSON.stringify; property names such as constructor turned up in the key pool.

While adding the native case I noticed that an own __proto__ property is dropped by the projection: the scratch literal { "__proto__": undefined } does not create a slot (a non-computed __proto__ key in a literal sets the prototype, and undefined is ignored), so scratch["__proto__"] = value later sets the prototype instead of a property and JSON.stringify never sees it.

Fix

For the property names that Object.hasOwn(Object.prototype, name) reports at build time, the generated code and the native projection read Object.hasOwn(obj, name) ? obj[name] : undefined. Every other property is read exactly as before, so getters on a class prototype keep working (covered by a test). The scratch literal uses a computed key for __proto__.

Tests

test/inherited-properties.test.js covers both mechanisms and standalone mode: {}, { x: 1 } and a null-prototype object serialize without the inherited members; own values for all those names (including __proto__) round-trip; required still sees own values and still throws when they are absent; a prototype getter is still read. The new assertions fail on main (the valueOf case throws) and pass with the change.

A schema property named constructor, toString, valueOf, hasOwnProperty, ...
was read with obj[key] and checked against undefined, so a plain object that
does not own the property contributed the inherited function: {} came out as
{"constructor":true} or {"toString":"function toString() { [native code] }"},
and an integer property named valueOf made stringify({}) throw. Read such
properties only when the input owns them, in the generated serializer and in
the native projection. Other properties keep going through the prototype
chain, so getters on a class prototype still serialize.

The native projection also lost an own __proto__ property: the scratch literal
{"__proto__": undefined} creates no slot, so the later assignment set the
prototype instead. Use a computed key for that slot.
@yxshee yxshee closed this Nov 9, 2026
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.

1 participant