Skip to content

fix: ignore options inherited from Object.prototype - #239

Open
kuldeepsinh19 wants to merge 1 commit into
yahoo:mainfrom
kuldeepsinh19:fix/ignore-inherited-options
Open

kuldeepsinh19 wants to merge 1 commit into
yahoo:mainfrom
kuldeepsinh19:fix/ignore-inherited-options

Conversation

@kuldeepsinh19

Copy link
Copy Markdown

Fixes #230. serialize() reads its options with plain property access, so a value inherited from Object.prototype counts the same as one the caller passed. If anything else in the process sets Object.prototype.unsafe, every call skips HTML escaping, including calls with no options. The same applies to isJSON, ignoreFunction and space.

serialize() now copies the four options it knows into a null-prototype object, and only when they are own properties of what the caller passed. Everything after that, including the recursive calls for Maps, Sets, arrays and URLs and the function-body escaping, reads from that copy. Explicit options ({unsafe: true}, a null-prototype options object, space given as a number or string) behave as before.

One behaviour change: an options object that gets its values only through a custom prototype (for example Object.create({unsafe: true})) is no longer honoured. Neither the README nor the tests use options that way.

The new test serializes a value containing </script> in a string, a function body and a Map, with all four options set on Object.prototype, and checks the output is identical to the unpolluted output. It fails without the change in index.js (3 of 3 runs) and passes with it. All 120 tests pass (npm test, Node 24).

This change was written with help from an AI coding assistant (Claude Code).

serialize() read its options with plain property access, so a value set
on Object.prototype (e.g. unsafe) applied to every call and could turn
off HTML escaping. Only own properties of the options object are used now.

Fixes yahoo#230
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.

Security Advisory: Prototype pollution of Object.prototype.unsafe silently disables serialize-javascript's documented automatic XSS escaping

1 participant