Skip to content

gcfg: Read values the way git does around whitespace and CR - #20

Merged
pjbgf merged 3 commits into
mainfrom
hidde/value-whitespace-cr
Nov 18, 2026
Merged

pjbgf merged 3 commits into
mainfrom
hidde/value-whitespace-cr

Conversation

@hiddeco

@hiddeco hiddeco commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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 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". This is not cosmetic: allow = \<LF> never did
not read as never.

unquote now skips an unquoted space, tab or CR while its result is still
empty. 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 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".

The scanner 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, as in Git.

Behaviour changes

  • A value that began with a continuation or "" followed by whitespace
    loses that whitespace, as in Git.
  • A value with a bare CR now keeps it, so a caller that relied on it being
    removed sees a change. Files with CRLF line endings read as before.
  • a\<CR>b is now an error, as in Git.
  • One scanner test expectation changed: the error offset for =\<CR> at
    end 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_value gained trim_len in 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>b reads as a<CR>b, where it read as ab. 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 --get when git is
installed, 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 a
CRLF continuation, a leading CR after a continuation, and a\<CR>b. The
new rows fail when the fix is removed. go vet and go test -race ./...
pass.

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
@pjbgf
pjbgf merged commit 6e9d9a0 into main Nov 18, 2026
20 checks passed
@pjbgf
pjbgf deleted the hidde/value-whitespace-cr branch November 18, 2026 13:01
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]>
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