Skip to content

Fix stringify of param names with control characters#445

Open
chatman-media wants to merge 1 commit into
pillarjs:masterfrom
chatman-media:fix/stringify-control-char-names
Open

Fix stringify of param names with control characters#445
chatman-media wants to merge 1 commit into
pillarjs:masterfrom
chatman-media:fix/stringify-control-char-names

Conversation

@chatman-media

Copy link
Copy Markdown

I was round-tripping token data and noticed names with a control char don't come back the same: parse(stringify(parse(':"a\nb"'))) gives the name anb instead of a\nb.

The cause is that stringifyName falls back to JSON.stringify when a name can't be emitted bare. JSON escaping uses sequences like \n/\t/\uXXXX, but the parser's quoted-name handling only understands \ as "next char literal" — so it reads \n back as the letter n. The parser happily accepts these names in the first place (e.g. :"a<newline>b"), so stringify should be able to reproduce them.

Switched to quoting that only escapes the two characters the parser treats specially inside quotes (" and \). For names that just needed wrapping (digits, dashes, etc.) the output is unchanged, since JSON already produced the same thing there. Added a stringify case for a name with a newline.

Param names that aren't bare identifiers were quoted with JSON.stringify, which emits escapes like \n that parse() reads back as the literal letter n, so a name containing a control character didn't survive a parse->stringify->parse round trip. Quote with only the escapes the parser understands instead.
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