* [PATCH 0/3] remote: reject invalid branch names
@ 2026-11-25 18:55 Harald Nordgren via GitGitGadget
2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 UTC (permalink / raw)
To: git; +Cc: Harald Nordgren
Reject invalid branch names in remote add -t, add -m and set-branches before
any configuration is written, instead of silently breaking every future
fetch on that remote.
Harald Nordgren (3):
remote: reject invalid branch names in add -t
remote: reject invalid branch names in set-branches
remote: reject invalid branch names in add -m
builtin/remote.c | 27 +++++++++++++++++++++++++++
t/t5505-remote.sh | 28 ++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2429%2FHaraldNordgren%2Fremote-add-branch-validate-name-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2429/HaraldNordgren/remote-add-branch-validate-name-v1
Pull-Request: https://github.com/git/git/pull/2429
--
gitgitgadget
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/3] remote: reject invalid branch names in add -t 2026-11-25 18:55 [PATCH 0/3] remote: reject invalid branch names Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 ` Harald Nordgren via GitGitGadget 2026-11-26 3:07 ` Junio C Hamano 2026-11-25 18:55 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget 2026-11-25 18:55 ` [PATCH 3/3] remote: reject invalid branch names in add -m Harald Nordgren via GitGitGadget 2 siblings, 1 reply; 7+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 UTC (permalink / raw) To: git; +Cc: Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> A typo in "git remote add -t", or a shell that expands something unexpected like @{u}, silently produced a fetch refspec that could never match anything, or in some cases outright invalid syntax. Every later fetch from that remote then failed with a confusing error and nothing pointed back at the original mistake. Check each branch name given to -t before anything is written, so the command fails without creating the remote at all. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- builtin/remote.c | 15 +++++++++++++++ t/t5505-remote.sh | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/builtin/remote.c b/builtin/remote.c index de989ea3ba..63d2db39fe 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -123,6 +123,19 @@ enum { #define MIRROR_PUSH 2 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) +static void validate_tracked_branchname(const char *branchname, int mirror) +{ + struct strbuf ref = STRBUF_INIT; + + if (mirror) + strbuf_addf(&ref, "refs/%s", branchname); + else + strbuf_addf(&ref, "refs/heads/%s", branchname); + if (check_refname_format(ref.buf, REFNAME_REFSPEC_PATTERN)) + die(_("'%s' is not a valid branch name"), branchname); + strbuf_release(&ref); +} + static void add_branch(const char *key, const char *branchname, const char *remotename, int mirror, struct strbuf *tmp) { @@ -211,6 +224,8 @@ static int add(int argc, const char **argv, const char *prefix, die(_("specifying a master branch makes no sense with --mirror")); if (mirror && !(mirror & MIRROR_FETCH) && track.nr) die(_("specifying branches to track makes sense only with fetch mirrors")); + for (size_t i = 0; i < track.nr; i++) + validate_tracked_branchname(track.items[i].string, mirror); name = argv[0]; url = argv[1]; diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 5fbfcb0848..055fa42e27 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -266,6 +266,13 @@ test_expect_success 'add invalid foreign_vcs remote' ' test_cmp expect actual ' +test_expect_success 'add -t with an invalid branch name' ' + echo "fatal: '\''@{u}'\'' is not a valid branch name" >expect && + test_must_fail git remote add -t "@{u}" badtrack bar 2>actual && + test_cmp expect actual && + test_must_fail git config get remote.badtrack.url +' + test_expect_success 'without subcommand' ' echo origin >expect && git -C test remote >actual && -- gitgitgadget ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] remote: reject invalid branch names in add -t 2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget @ 2026-11-26 3:07 ` Junio C Hamano 0 siblings, 0 replies; 7+ messages in thread From: Junio C Hamano @ 2026-11-26 3:07 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > > A typo in "git remote add -t", or a shell that expands something > unexpected like @{u}, silently produced a fetch refspec that could > never match anything, or in some cases outright invalid syntax. Every > later fetch from that remote then failed with a confusing error and > nothing pointed back at the original mistake. Our commit log messages normally describe the pre-patch behavior in the present tense ("produces", "fails", "points"). Also, syntactic validation only catches invalid refname syntax (like @{u}, which causes refspec_append() inside remote_get() to die on subsequent commands), whereas a "typo" that is still a valid refname will still be accepted. > +static void validate_tracked_branchname(const char *branchname, int mirror) > +{ > + struct strbuf ref = STRBUF_INIT; > + > + if (mirror) > + strbuf_addf(&ref, "refs/%s", branchname); > + else > + strbuf_addf(&ref, "refs/heads/%s", branchname); > + if (check_refname_format(ref.buf, REFNAME_REFSPEC_PATTERN)) > + die(_("'%s' is not a valid branch name"), branchname); > + strbuf_release(&ref); > +} Using "refs/%s" when mirror is true and "refs/heads/%s" otherwise matches add_branch() right below. Passing REFNAME_REFSPEC_PATTERN to check_refname_format() allows single wildcard patterns such as -t "topic/*" while rejecting invalid sequences like @{u}, "..", ":", or multiple asterisks. Using check_refname_format() directly, instead of check_branch_ref(), makes sense. Because check_branch_ref() calls copy_branchname() with INTERPRET_BRANCH_LOCAL and would expand local shorthands like @{-1}, and we do not want that. > static void add_branch(const char *key, const char *branchname, > const char *remotename, int mirror, struct strbuf *tmp) > { > @@ -211,6 +224,8 @@ static int add(int argc, const char **argv, const char *prefix, > die(_("specifying a master branch makes no sense with --mirror")); > if (mirror && !(mirror & MIRROR_FETCH) && track.nr) > die(_("specifying branches to track makes sense only with fetch mirrors")); > + for (size_t i = 0; i < track.nr; i++) > + validate_tracked_branchname(track.items[i].string, mirror); Validating track entries here runs before remote_get() and before repo_config_set() writes remote.<name>.url, so a bad -t argument leaves no configuration behind. The new test in t/t5505-remote.sh verifies both the error message and the absence of remote.badtrack.url. OK. ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] remote: reject invalid branch names in set-branches 2026-11-25 18:55 [PATCH 0/3] remote: reject invalid branch names Harald Nordgren via GitGitGadget 2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 ` Harald Nordgren via GitGitGadget 2026-11-26 5:17 ` Junio C Hamano 2026-11-25 18:55 ` [PATCH 3/3] remote: reject invalid branch names in add -m Harald Nordgren via GitGitGadget 2 siblings, 1 reply; 7+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 UTC (permalink / raw) To: git; +Cc: Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> Giving "git remote set-branches" a name like @{u} wrote a fetch refspec that could never match anything. Worse, without --add the existing remote.<name>.fetch entries were thrown away first, so a single typo left the remote with a broken configuration and no indication of what went wrong. Check every branch name before touching the configuration, so a bad name leaves the existing fetch refspecs as they were. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- builtin/remote.c | 4 ++++ t/t5505-remote.sh | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/builtin/remote.c b/builtin/remote.c index 63d2db39fe..b04cbe70b6 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1777,6 +1777,7 @@ static int set_remote_branches(const char *remotename, const char **branches, { struct strbuf key = STRBUF_INIT; struct remote *remote; + const char **branch; strbuf_addf(&key, "remote.%s.fetch", remotename); @@ -1786,6 +1787,9 @@ static int set_remote_branches(const char *remotename, const char **branches, exit(2); } + for (branch = branches; *branch; branch++) + validate_tracked_branchname(*branch, remote->mirror); + if (!add_mode && remove_all_fetch_refspecs(key.buf)) { strbuf_release(&key); return 1; diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 055fa42e27..4bf97db6af 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -1454,6 +1454,20 @@ test_expect_success 'remote set-branches with --mirror' ' test_cmp expect.replace actual.replace ' +test_expect_success 'remote set-branches with an invalid branch name' ' + git clone .git/ setbranches-invalid && + ( + cd setbranches-invalid && + git config --get-all remote.origin.fetch >expect && + test_must_fail git remote set-branches origin main "@{u}" 2>err && + test_grep "is not a valid branch name" err && + test_must_fail git remote set-branches --add origin "@{u}" 2>err && + test_grep "is not a valid branch name" err && + git config --get-all remote.origin.fetch >actual && + test_cmp expect actual + ) +' + test_expect_success 'new remote' ' git remote add someremote foo && echo foo >expect && -- gitgitgadget ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/3] remote: reject invalid branch names in set-branches 2026-11-25 18:55 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget @ 2026-11-26 5:17 ` Junio C Hamano 0 siblings, 0 replies; 7+ messages in thread From: Junio C Hamano @ 2026-11-26 5:17 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > > Giving "git remote set-branches" a name like @{u} wrote a fetch > refspec that could never match anything. Worse, without --add the > existing remote.<name>.fetch entries were thrown away first, so a > single typo left the remote with a broken configuration and no > indication of what went wrong. The same "make an observation on the current state in the present tense" applies here. In fact, once an invalid refspec like '+refs/heads/@{u}:...' is written to remote.<name>.fetch, even running 'git remote set-branches' again cannot fix it, because remote_get() dies while parsing the existing config. Good catch. > diff --git a/builtin/remote.c b/builtin/remote.c > index 63d2db39fe..b04cbe70b6 100644 > --- a/builtin/remote.c > +++ b/builtin/remote.c > @@ -1786,6 +1787,9 @@ static int set_remote_branches(const char *remotename, const char **branches, > exit(2); > } > > + for (branch = branches; *branch; branch++) > + validate_tracked_branchname(*branch, remote->mirror); > + In the unshown call at the end of set_remote_branches(), add_branches() passes the '.mirror' member to add_branch(): static void add_branches(struct remote *remote, const char **branches, const char *key) { const char *remotename = remote->name; int mirror = remote->mirror; ... for (; *branches; branches++) add_branch(key, *branches, remotename, mirror, &refspec); So passing remote->mirror to validate_tracked_branchname() matches the refspec construction that follows, and checking every element of branches before remove_all_fetch_refspecs() ensures neither replace mode nor --add touches existing config on failure. Looking good. ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] remote: reject invalid branch names in add -m 2026-11-25 18:55 [PATCH 0/3] remote: reject invalid branch names Harald Nordgren via GitGitGadget 2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget 2026-11-25 18:55 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 ` Harald Nordgren via GitGitGadget 2026-11-26 5:17 ` Junio C Hamano 2 siblings, 1 reply; 7+ messages in thread From: Harald Nordgren via GitGitGadget @ 2026-11-25 18:55 UTC (permalink / raw) To: git; +Cc: Harald Nordgren, Harald Nordgren From: Harald Nordgren <haraldnordgren@gmail.com> "git remote add -m" only looked at its argument after the remote had been created and fetched from. A bad name such as @{u} therefore got as far as a network fetch before failing with "Could not setup master", leaving a half configured remote behind. Check the name up front, so the command fails before any remote is created. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> --- builtin/remote.c | 8 ++++++++ t/t5505-remote.sh | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/builtin/remote.c b/builtin/remote.c index b04cbe70b6..35c6a1f7fd 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -226,6 +226,14 @@ static int add(int argc, const char **argv, const char *prefix, die(_("specifying branches to track makes sense only with fetch mirrors")); for (size_t i = 0; i < track.nr; i++) validate_tracked_branchname(track.items[i].string, mirror); + if (master) { + struct strbuf ref = STRBUF_INIT; + + strbuf_addf(&ref, "refs/heads/%s", master); + if (check_refname_format(ref.buf, 0)) + die(_("'%s' is not a valid branch name"), master); + strbuf_release(&ref); + } name = argv[0]; url = argv[1]; diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh index 4bf97db6af..867ada059e 100755 --- a/t/t5505-remote.sh +++ b/t/t5505-remote.sh @@ -273,6 +273,13 @@ test_expect_success 'add -t with an invalid branch name' ' test_must_fail git config get remote.badtrack.url ' +test_expect_success 'add -m with an invalid branch name' ' + echo "fatal: '\''@{u}'\'' is not a valid branch name" >expect && + test_must_fail git remote add -m "@{u}" badmaster bar 2>actual && + test_cmp expect actual && + test_must_fail git config get remote.badmaster.url +' + test_expect_success 'without subcommand' ' echo origin >expect && git -C test remote >actual && -- gitgitgadget ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] remote: reject invalid branch names in add -m 2026-11-25 18:55 ` [PATCH 3/3] remote: reject invalid branch names in add -m Harald Nordgren via GitGitGadget @ 2026-11-26 5:17 ` Junio C Hamano 0 siblings, 0 replies; 7+ messages in thread From: Junio C Hamano @ 2026-11-26 5:17 UTC (permalink / raw) To: Harald Nordgren via GitGitGadget; +Cc: git, Harald Nordgren "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: Harald Nordgren <haraldnordgren@gmail.com> > > "git remote add -m" only looked at its argument after the remote had > been created and fetched from. A bad name such as @{u} therefore got > as far as a network fetch before failing with "Could not setup > master", leaving a half configured remote behind. Again, log messages are written in present tense. Have you perhaps changed your LLM settings or something? Minor factual nit on the commit log. In add(), fetch_remote() only runs when -f is passed, so without -f no network fetch happens before refs_update_symref() fails with "Could not setup master '%s'". Saying "after the remote has been created and (with -f) fetched from" would be more accurate. > diff --git a/builtin/remote.c b/builtin/remote.c > index b04cbe70b6..35c6a1f7fd 100644 > --- a/builtin/remote.c > +++ b/builtin/remote.c > @@ -226,6 +226,14 @@ static int add(int argc, const char **argv, const char *prefix, > die(_("specifying branches to track makes sense only with fetch mirrors")); > for (size_t i = 0; i < track.nr; i++) > validate_tracked_branchname(track.items[i].string, mirror); > + if (master) { > + struct strbuf ref = STRBUF_INIT; > + > + strbuf_addf(&ref, "refs/heads/%s", master); > + if (check_refname_format(ref.buf, 0)) > + die(_("'%s' is not a valid branch name"), master); > + strbuf_release(&ref); > + } Passing 0 instead of REFNAME_REFSPEC_PATTERN here is the right choice, because -m names a single branch rather than a refspec glob pattern. Because check_refname_format() allows "HEAD" as a component, -m HEAD still passes this check. We would then later attempt to point refs/remotes/<name>/HEAD to itself, which would be similar to the crazy configurations this series tries to prevent. ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-26 5:17 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-25 18:55 [PATCH 0/3] remote: reject invalid branch names Harald Nordgren via GitGitGadget 2026-11-25 18:55 ` [PATCH 1/3] remote: reject invalid branch names in add -t Harald Nordgren via GitGitGadget 2026-11-26 3:07 ` Junio C Hamano 2026-11-25 18:55 ` [PATCH 2/3] remote: reject invalid branch names in set-branches Harald Nordgren via GitGitGadget 2026-11-26 5:17 ` Junio C Hamano 2026-11-25 18:55 ` [PATCH 3/3] remote: reject invalid branch names in add -m Harald Nordgren via GitGitGadget 2026-11-26 5:17 ` Junio C Hamano
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox