Skip to content

plumbing: format/config, Refuse names Git refuses - #2607

Merged
pjbgf merged 1 commit into
mainfrom
fix/config-encode-names
Nov 20, 2026
Merged

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

Conversation

@hiddeco

@hiddeco hiddeco commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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:

  • a key containing a newline injects extra options ("x\n\tinjected"
    encodes as two options, so a caller could add core.sshCommand);
  • a section name containing ] or a newline opens sections of its own;
  • a subsection name containing a newline produces a file that no longer
    parses.

Git refuses such names when it writes them 1. go-git did not.

What

Encoder.Encode checks every name before it writes anything and
returns an error, so a refused config never leaves partial output. The
rules follow git_config_parse_key and store_create_section:

  • a key is non-empty, starts with a letter, and contains only ASCII
    letters, digits and - (ErrInvalidKey);
  • a section name is non-empty and contains only ASCII letters, digits
    and - (ErrInvalidSectionName). . is not allowed: it is the
    reader's rule for the deprecated [section.subsection] header, and
    Git's writer splits a name at the first dot into a section and a
    subsection (store_create_section);
  • a subsection name contains anything except a newline and a NUL
    (ErrInvalidSubsectionName). Git keeps a NUL and then uses the name
    as a C string, so [core "sshcommand\0x"] with k = cmd reads as
    core.sshcommand=cmd.

The three errors are new exported sentinels, wrapped with the quoted
name, so callers can match them with errors.Is. Subsection names
already had \ and " escaped, which Git reads back unchanged, so
that is untouched.

The rules were checked against git config --file (2.54): it rejects
the keys 1foo, foo_bar, foo bar, foo=bar, föo, foo#,
foo", the empty key and a key with a newline, and accepts foo and
foo-bar. Its file parser reads [a "b\"c\\d"] as b"c\d, rejects a
newline in a subsection header, and accepts a tab, a CR and ] there.
git config rem.ote.origin.url 2 writes [rem "ote.origin"], and a
[remote.origin] header reads as remote origin.

ConfigStorage.SetConfig marshals the config before it opens
.git/config for writing. It used to truncate the file first, so a
refused name left .git/config empty; for example a .gitmodules key
such as fö read fine and then wiped the config on Submodule.Init.

Compatibility

Encode, Config.Marshal and anything that saves a config now fail
for names Git refuses, where they used to write a file. Valid configs
encode exactly as before. Who is affected:

  • Keys containing _: go-git's own decoder could never read these back,
    so the failure moves from read time to write time.
  • Keys containing a non-ASCII letter: these worked before, since go-git
    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.
  • Names containing a newline or, in a subsection, a NUL, or a section
    containing ]: these were corrupting or injecting into the file.
  • Section names containing . or a non-ASCII character: Git's writer cannot produce these as written, as store_create_section splits a name at the first . into a section and a subsection.

This is a deliberate strict choice, for main (v6) only: a config
go-git writes should be one Git can read.

Release note

format/config.Encoder.Encode, config.Config.Marshal and saving a
config now return ErrInvalidSectionName, ErrInvalidSubsectionName or
ErrInvalidKey for names Git refuses, before writing anything: keys
must start with an ASCII letter and hold only ASCII letters, digits and
-; section names only ASCII letters, digits and - (no ., no
non-ASCII); subsection names anything but a newline and a NUL.
ConfigStorage.SetConfig no longer truncates .git/config when the
config is refused.

Known gaps

  • The decoder still rejects some names Git accepts, such as a section
    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 a
key. Further cases cover a NUL in a subsection and dotted and non-ASCII
section names, and a filesystem SetConfig test checks that a refused
name leaves .git/config untouched. Another test checks that valid
names are accepted and round-trip.
go test ./... passes, except tests/objectverify, which needs gpg.

Ordering

Independent of Quote values containing a CR (#2606) apart from both
appending tests to encoder_test.go.

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

pjbgf
pjbgf previously approved these changes Nov 19, 2026
@pjbgf

pjbgf commented Nov 19, 2026

Copy link
Copy Markdown
Member

@hiddeco once the conflicts are fixed we should be good to merge this.

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
hiddeco force-pushed the fix/config-encode-names branch from 5270608 to f36461f Compare November 19, 2026 07:22
@pjbgf
pjbgf merged commit a4179a1 into main Nov 20, 2026
21 checks passed
@pjbgf
pjbgf deleted the fix/config-encode-names branch November 20, 2026 12:23
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