Skip to content

plumbing: format/config, Quote values containing a CR - #2606

Merged
pjbgf merged 1 commit into
mainfrom
fix/config-encode-cr
Nov 19, 2026
Merged

pjbgf merged 1 commit into
mainfrom
fix/config-encode-cr

Conversation

@hiddeco

@hiddeco hiddeco commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Why

The encoder wrote a value holding a carriage return unquoted. A value
ending in a CR was then written as key = foo<CR><LF>, which Git reads
as foo, because it drops a CR directly before an LF, and which gcfg
reads as foo, because it drops every CR. The value written is not the
value read. This is the mismatch behind CVE-2025-48384, where a
submodule path ending in a CR was truncated on read; Git fixed it in
2.50.1 by quoting values that contain a CR 1.

It matters for go-git before gcfg's CR handling changes. After
go-git/gcfg#20, a CR inside quotes is kept, so a quoted "foo\r" reads
back as foo<CR> while an unquoted trailing CR is still dropped. The
encoder must quote it for the value to survive a write and read cycle.

What

Add \r to the characters that make encodeOptions quote a value. The
other cases Git quotes were already covered: a leading or trailing
space, ; and # (and go-git also quotes ", \, tab and newline,
which Git backslash-escapes). A form feed or vertical tab is not
whitespace to Git and is left alone.

Compatibility

A value containing a CR is now written inside double quotes. Git reads
that back unchanged. Values without a CR encode exactly as before.

Release note

The config encoder now quotes values that contain a carriage return, as
Git does since 2.50.1. Previously key = foo<CR> was written unquoted,
and both Git and gcfg read it back as foo.

Tests

  • TestEncodeQuotesValuesGitQuotes checks the encoder output for three
    CR cases. It fails on main.
  • TestEncodeRoundTripsValuesWithCR encodes and decodes seven CR
    values and asserts the value comes back unchanged. It needs gcfg
    v2.0.3 (gcfg: Read values the way git does around whitespace and CR gcfg#20), which keeps a quoted CR and which main
    already pins; against v2.0.2 it fails.

Verified against git 2.54: git config x.a $'foo\r' writes the value in
quotes and reads it back unchanged.

Ordering

main already pins gcfg v2.0.3, which contains go-git/gcfg#20, so the
encoder and the reader agree on a quoted CR once this lands. Both this PR and #2607 (Refuse names Git refuses) add
tests at the end of encoder_test.go, so the second to merge needs a
trivial rebase.

AI assistance: Claude Sonnet 5.5 (see the Assisted-by trailer).

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]>
@hiddeco
hiddeco force-pushed the fix/config-encode-cr branch from 2e2b9e9 to e27cf95 Compare November 19, 2026 04:26
@pjbgf
pjbgf merged commit 1ac7327 into main Nov 19, 2026
21 checks passed
@pjbgf
pjbgf deleted the fix/config-encode-cr branch November 19, 2026 06:30
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