Repository navigation
plumbing: format/config, Refuse names Git refuses - #2607
Merged
Merged
Conversation
This was referenced Nov 18, 2026
hiddeco
force-pushed
the
fix/config-encode-names
branch
2 times, most recently
from
November 19, 2026 03:49
1d24e2a to
85e1201
Compare
pjbgf
previously approved these changes
Nov 19, 2026
Member
|
@hiddeco once the conflicts are fixed we should be good to merge this. |
hiddeco
force-pushed
the
fix/config-encode-names
branch
from
November 19, 2026 06:49
85e1201 to
5270608
Compare
The encoder wrote section, subsection and key names verbatim. A key
containing a newline therefore injected extra options into the file
("x\n\tinjected" encoded as two options), a section name containing
"]" or a newline could open sections of its own, and a subsection
name containing a newline produced a file that no longer parses. A
NUL in a subsection name was written too, and Git then reads the name
as a C string, so `[core "sshcommand\0x"]` yields `core.sshcommand`.
Check every name before writing anything and return an error, so a
refused config never leaves partial output. The rules were checked
against `git config --file` (2.54) and follow git_config_parse_key
and store_create_section in config.c[1]:
- keys must be non-empty, start with a letter and contain only
ASCII letters, digits and "-";
- section names must be non-empty and contain only ASCII letters,
digits and "-". Git's writer splits at the first "." into section
and subsection, so it writes `remote.origin` as
`[remote "origin"]` and can never produce a section of that name.
Git reads `[remote.origin]` the same way, as a subsection;
- subsection names may contain anything but a newline or a NUL.
Subsection names already had `\` and `"` escaped; Git reads that
back unchanged, so the escaping is untouched.
`ConfigStorage.SetConfig` created `.git/config`, truncating it, before
marshalling the config, so a refused name left an empty file. It now
marshals first, as the worktree path already does.
Behaviour change: `Encode` and `Config.Marshal` now fail with the
new `ErrInvalidSectionName`, `ErrInvalidSubsectionName` or
`ErrInvalidKey` for configs that were previously written, usually
corrupt or unreadable by Git. Keys with "_" or non-ASCII characters,
and section names with "." or non-ASCII characters, which Git cannot
write as such, are affected as well. Valid configs encode
exactly as before.
The decoder is unchanged. It still rejects some names Git accepts,
such as a section starting with a digit.
[1]: https://github.com/git/git/blob/v2.54.0/config.c
Assisted-by: Claude Sonnet 5.5
Signed-off-by: Hidde Beydals <[email protected]>
hiddeco
force-pushed
the
fix/config-encode-names
branch
from
November 19, 2026 07:22
5270608 to
f36461f
Compare
pjbgf
approved these changes
Nov 20, 2026
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.
Why
The encoder writes section, subsection and key names as given. A name
that comes from outside, such as a remote name taken from user input,
can then change the file:
"x\n\tinjected"encodes as two options, so a caller could add
core.sshCommand);]or a newline opens sections of its own;parses.
Git refuses such names when it writes them 1. go-git did not.
What
Encoder.Encodechecks every name before it writes anything andreturns an error, so a refused config never leaves partial output. The
rules follow
git_config_parse_keyandstore_create_section:letters, digits and
-(ErrInvalidKey);and
-(ErrInvalidSectionName)..is not allowed: it is thereader's rule for the deprecated
[section.subsection]header, andGit's writer splits a name at the first dot into a section and a
subsection (
store_create_section);(
ErrInvalidSubsectionName). Git keeps a NUL and then uses the nameas a C string, so
[core "sshcommand\0x"]withk = cmdreads ascore.sshcommand=cmd.The three errors are new exported sentinels, wrapped with the quoted
name, so callers can match them with
errors.Is. Subsection namesalready had
\and"escaped, which Git reads back unchanged, sothat is untouched.
The rules were checked against
git config --file(2.54): it rejectsthe keys
1foo,foo_bar,foo bar,foo=bar,föo,foo#,foo", the empty key and a key with a newline, and acceptsfooandfoo-bar. Its file parser reads[a "b\"c\\d"]asb"c\d, rejects anewline in a subsection header, and accepts a tab, a CR and
]there.git config rem.ote.origin.url 2writes[rem "ote.origin"], and a[remote.origin]header reads as remoteorigin.ConfigStorage.SetConfigmarshals the config before it opens.git/configfor writing. It used to truncate the file first, so arefused name left
.git/configempty; for example a.gitmoduleskeysuch as
föread fine and then wiped the config onSubmodule.Init.Compatibility
Encode,Config.Marshaland anything that saves a config now failfor names Git refuses, where they used to write a file. Valid configs
encode exactly as before. Who is affected:
_: go-git's own decoder could never read these back,so the failure moves from read time to write time.
reads and writes them, but Git refuses them. A config with such a key
can no longer be saved by go-git. Reading it is unchanged.
containing
]: these were corrupting or injecting into the file..or a non-ASCII character: Git's writer cannot produce these as written, asstore_create_sectionsplits a name at the first.into a section and a subsection.This is a deliberate strict choice, for
main(v6) only: a configgo-git writes should be one Git can read.
Release note
format/config.Encoder.Encode,config.Config.Marshaland saving aconfig now return
ErrInvalidSectionName,ErrInvalidSubsectionNameorErrInvalidKeyfor names Git refuses, before writing anything: keysmust start with an ASCII letter and hold only ASCII letters, digits and
-; section names only ASCII letters, digits and-(no., nonon-ASCII); subsection names anything but a newline and a NUL.
ConfigStorage.SetConfigno longer truncates.git/configwhen theconfig is refused.
Known gaps
starting with a digit (
[1core]). That is not changed here.Tests
Written first and failing for the right reasons: the injected option,
the newline in a subsection,
]in a section, and_or non-ASCII in akey. Further cases cover a NUL in a subsection and dotted and non-ASCII
section names, and a filesystem
SetConfigtest checks that a refusedname leaves
.git/configuntouched. Another test checks that validnames are accepted and round-trip.
go test ./...passes, excepttests/objectverify, which needsgpg.Ordering
Independent of
Quote values containing a CR(#2606) apart from bothappending tests to
encoder_test.go.AI assistance: Claude Sonnet 5.5 (see the
Assisted-bytrailer).