Repository navigation
gcfg: Read values the way git does around whitespace and CR - #20
Merged
Merged
Conversation
Git drops unquoted whitespace for as long as a value is still empty, wherever it occurs. gcfg only trimmed it at the very start, so a value that began with a line continuation or an empty `""` pair kept the whitespace that followed: `k = \<LF> v` read as " v" where git reads "v". The difference is not cosmetic, as `allow = \<LF> never` did not read as `never`. `unquote` now skips an unquoted space or tab while its result is still empty. A quote does not count as content, and quoted or escaped whitespace, and whitespace after content, is kept. The new table test also runs each input through `git config --get`, when git is installed, so the expected values stay tied to git.
Git drops a carriage return only when it directly precedes a line feed (`get_next_char`). The scanner stripped every CR in a value and skipped one after a backslash without checking for an LF. A bare CR was therefore lost silently: `k = a<CR>b` read as "ab" where git reads "a<CR>b", and `k = "a<CR>"` read as "a". After a backslash, `a\<CR>b` turned `\b` into a backspace where git rejects the line with "bad config line". `scanValString` now marks a CR for stripping only when an LF follows, `stripCR` removes only a CR that precedes an LF, and a CR after a backslash is skipped only when an LF follows. A trailing unquoted CR is still trimmed as whitespace, and `unquote` drops a leading one while the value is empty, both as git does. Files with CRLF line endings read as before. A value with a bare CR now keeps it, so callers that relied on it being removed see a change.
Four bare-CR rows compare gcfg with `git config`, but git changed how an unquoted value keeps whitespace in 2.45 (`parse_value` gained `trim_len`, absent in 2.44.0). Earlier releases turn each whitespace byte inside such a value into a space, so a bare CR reads as " " and the comparison fails on distro git even though gcfg matches current git. `runValueCasesSince` takes the first git release that reads the rows this way and skips only the comparison with git on older ones; the gcfg side always runs. `versionAtLeast` parses `git --version`, and treats output it cannot parse as older. Assisted-by: Claude Sonnet 5.5
hiddeco
force-pushed
the
hidde/value-whitespace-cr
branch
from
November 18, 2026 07:15
3d1b177 to
fa2dfad
Compare
pjbgf
approved these changes
Nov 18, 2026
hiddeco
added a commit
to go-git/go-git
that referenced
this pull request
Nov 19, 2026
The encoder wrote a value holding a carriage return unquoted. A value ending in CR was then written as `key = foo<CR><LF>`, which Git reads as `foo` because it drops a CR before an LF, and gcfg drops every CR. The value written is not the value read, the mismatch behind CVE-2025-48384, where a submodule path ending in CR was truncated. Quote any value containing `\r`, as Git's `write_pair` does since 2.50.1. Git keeps a CR inside quotes, and so does gcfg v2.0.3, which only drops a CR that is followed by LF. Values with a trailing space, tab or newline were already quoted or escaped, and a form feed or vertical tab is not whitespace to Git, so none of those change. The CR round-trip test needs gcfg v2.0.3 (go-git/gcfg#20), which keeps a quoted CR. The encoder output test fails without this change either way. Assisted-by: Claude Sonnet 5.5 Signed-off-by: Hidde Beydals <[email protected]>
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.
Leading whitespace (
gcfg: Drop leading whitespace in values like git)Git drops unquoted whitespace for as long as a value is still empty,
wherever it occurs (
parse_value). gcfg only trimmed it at the verystart, so a value that began with a line continuation or an empty
""pair kept the whitespace that followed:
k = \<LF> vread as" v"where Git reads
"v". This is not cosmetic:allow = \<LF> neverdidnot read as
never.unquotenow skips an unquoted space, tab or CR while its result is stillempty. A quote does not count as content, and quoted or escaped
whitespace, and whitespace after content, is kept.
Bare CR (
scanner: Drop a CR in a value only before an LF)Git drops a carriage return only when it directly precedes a line feed
(
get_next_char). The scanner stripped every CR in a value andskipped one after a backslash without checking for an LF. A bare CR was
therefore lost silently:
k = a<CR>bread as"ab"where Git reads"a<CR>b", andk = "a<CR>"read as"a". After a backslash,a\<CR>bturned\binto a backspace where Git rejects the line with"bad config line".
The scanner now marks a CR for stripping only when an LF follows,
stripCRremoves only a CR that precedes an LF, and a CR after abackslash is skipped only when an LF follows. A trailing unquoted CR is
still trimmed as whitespace, as in Git.
Behaviour changes
""followed by whitespaceloses that whitespace, as in Git.
removed sees a change. Files with CRLF line endings read as before.
a\<CR>bis now an error, as in Git.=\<CR>atend of input is now 2 (the position of the backslash, as for
=\)instead of 3. It is still an error.
Git version
Git before 2.45.0 turned each whitespace character inside an unquoted value into a plain space, where newer Git keeps it as written (
parse_valuegainedtrim_lenin 2.45.0; I checked the tags 2.43 to 2.53, and not the 2.44.x maintenance releases). So four git-comparison rows, the bare CR inside a value, before a quote, and before and after a CRLF continuation, compare with Git only on 2.45 or newer and skip on older Git. The checks against gcfg's own expectations still run.Release note
After this, a carriage return inside a value is dropped only when a line feed directly follows it, as in Git. A bare CR is kept:
k = a<CR>breads asa<CR>b, where it read asab. Files with CRLF line endings read as before, and a trailing unquoted CR is still trimmed. A backslash followed by a CR without an LF is now an error, as in Git.Downstream encoders need to be ready before they bump. A writer that emits a value containing a CR, or one ending in whitespace, without quoting it can be read back differently from what was written (the CVE-2025-48384 pattern). go-git's encoder is fixed in go-git/go-git#2606.
Testing
Table tests run each input through
git config --getwhengitisinstalled, so the expected values stay tied to Git; without it they skip
visibly. They cover leading whitespace after a continuation and after
"", a bare CR in the middle, at the end of a quoted value and next to aCRLF continuation, a leading CR after a continuation, and
a\<CR>b. Thenew rows fail when the fix is removed.
go vetandgo test -race ./...pass.