Skip to content

fix: escape property names in the schema refs the generated code validates against - #882

Merged
mcollina merged 2 commits into
fastify:mainfrom
nigrosimone:fix-property-name-escaping
Sep 25, 2026
Merged

mcollina merged 2 commits into
fastify:mainfrom
nigrosimone:fix-property-name-escaping

Conversation

@nigrosimone

@nigrosimone nigrosimone commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

A property whose subschema has anyOf, oneOf or if is validated at run time through ajv, by a schema ref the generated code carries as a string literal: validator.validate("#/properties/<name>/oneOf/0", ...). The name goes in unescaped, so a property named with a newline, a quote or a backslash makes the build throw Invalid or unexpected token, and the same ref is used unescaped in the TypeError template of a mismatch. The JSON pointer is not escaped either, so a name with a / or a ~ points at nothing and ajv answers no schema with key or ref. The ref also sits on a comment line above the function of a nested object or array, where a newline or a U+2028 in the name ends the comment and the rest of the name becomes code.

The fix embeds the ref and the message with JSON.stringify, escapes the pointer segments as RFC 6901 says, and escapes the line terminators on the comment lines.

The added test builds each of the three keywords with such names, and a nested object under a name with a line terminator: 24 of its 39 cases fail on main, all pass here.

@nigrosimone
nigrosimone marked this pull request as ready for review September 13, 2026 11:30

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina
mcollina merged commit 04a55c4 into fastify:main Sep 25, 2026
17 checks passed
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.

2 participants