fix: escape backslashes in values and keys so they round-trip#307
Open
greymoth-jp wants to merge 1 commit into
Open
fix: escape backslashes in values and keys so they round-trip#307greymoth-jp wants to merge 1 commit into
greymoth-jp wants to merge 1 commit into
Conversation
`unsafe` treats `\` as an escape character and unescapes `\\`, `\;` and `\#`, but `safe` only escaped `;` and `#`. A value or key containing a backslash therefore did not survive stringify -> parse: a value of two backslashes was written as `k=\\` and read back as a single backslash. `safe` now escapes the backslash as well. Section names are left unchanged because they rely on `\.` to escape literal dots in key paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ini.parse(ini.stringify(x))is not stable when a value or key contains a backslash.unsafetreats\as an escape character: in the unquoted branch it unescapes\\,\;and\#.safeonly escaped;and#, never the backslash itself, so a backslash that is written verbatim is read back as an escape sequence. Two backslashes collapse to one, and a backslash placed before;or#gets swallowed.This makes
safeescape the backslash as well, so it is the inverse ofunsafe. Section names are left unchanged: they use\.to escape literal dots in key paths, sosafeis called withescapeBackslash = falsefor the section to avoid doubling those structural backslashes. The existing snapshots are byte-identical.Round-trip tests are added in
test/bar.js(two backslashes, a Windows-style path,\;/\#, and a backslash in a key).npm testpasses at 100% coverage, and reverting the one-line change makes the new cases fail.This is the behaviour #9 had in mind: "a
\\needs to be a\and not an escape".