fix(util): escape null byte unambiguously in quote()#1492
Open
spokodev wants to merge 1 commit into
Open
Conversation
…merge
In bash ANSI-C quoting ($'...'), \0 is a variable-length octal escape that
greedily consumes up to three following octal digits. Emitting a NUL as \0
therefore merges with subsequent digits: quote('\0' + '123') produced
$'\0123', which bash reads as octal \012 (newline) followed by '3' instead of
NUL followed by '123'. Emitting a fixed three-digit \000 keeps the escape
unambiguous, since bash octal escapes are at most three digits.
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.
Description
quote()escapes a NUL byte as\0. In bash ANSI-C quoting ($'...'),\0is a variable-length octal escape that greedily absorbs up to three following octal digits. So a NUL followed by octal digits is misdecoded.Bash reads
$'\0123'as\012(octal for newline,0x0a) followed by3. The intended NUL is silently turned into a newline and the following123is corrupted. Confirmed end to end through zx:$`printf '%s' ${'a\0123'}`yields the bytesa,0x0a,3instead of a clean value.Fix
Emit a fixed three-digit octal
\000instead of\0. Bash octal escapes are at most three digits, so\000cannot absorb following digits and the value stays intact.Test
Extended the
quote()null-byte case intest/util.test.js. It fails before the change ($'\0123', ambiguous) and passes after ($'\000123').