Skip to contentSkip to conversation

push: do not turn --delete '' into a matching push

PATCH In review Junio C Hamano 3 participants 3 messages Feb 23, 2021 – Feb 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 push: do not turn --delete '' into a matching push

When we added a syntax sugar "git push remote --delete <ref>" to "git push" as a synonym to the canonical "git push remote :<ref>" syntax at f517f1f2 (builtin-push: add --delete as syntactic sugar for :foo, 2009-12-30), we weren't careful enough to make sure that <ref> is not empty.

Blindly rewriting "--delete <ref>" to ":<ref>" means that an empty string <ref> results in refspec ":", which is the syntax to ask for "matching" push that does not delete anything.

Worse yet, if there were matching refs that can be fast-forwarded, they would have been published prematurely, even if the user feels that they are not ready yet to be pushed out, which would be a real disaster.

---

* So this time with an obvious test. It is somewhat surprising that this has been left unnoticed for the past 10 years.

 builtin/push.c        | 2 +-
 t/t5516-fetch-push.sh | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
builtin/push.c +1 -1
index 03adb58602..194967ed79 100644
@@ -115,7 +115,7 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
 			else
 				refspec_appendf(&rs, "refs/tags/%s", ref);
 		} else if (deleterefs) {
-			if (strchr(ref, ':'))
+			if (strchr(ref, ':') || !*ref)
 				die(_("--delete only accepts plain target ref names"));
 			refspec_appendf(&rs, ":%s", ref);
 		} else if (!strchr(ref, ':')) {
t/t5516-fetch-push.sh +5 -0
index 3ed121d0ce..7eee4e782f 100755
@@ -824,6 +824,11 @@ test_expect_success 'push --delete refuses src:dest refspecs' '
 	test_must_fail git push testrepo --delete master:foo
 '
 
+test_expect_success 'push --delete refuses empty string' '
+	mk_test testrepo heads/master &&
+	test_must_fail git push testrepo --delete ""
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
 	mk_test testrepo heads/master &&
 	(
Signature · 1 line
2.30.1-824-gddfeb442a8
#1 lore raw
Quoted 15 lines · Junio C Hamano

When we added a syntax sugar "git push remote --delete <ref>" to "git push" as a synonym to the canonical "git push remote :<ref>" syntax at f517f1f2 (builtin-push: add --delete as syntactic sugar for :foo, 2009-12-30), we weren't careful enough to make sure that <ref> is not empty.

Blindly rewriting "--delete <ref>" to ":<ref>" means that an empty string <ref> results in refspec ":", which is the syntax to ask for "matching" push that does not delete anything.

Worse yet, if there were matching refs that can be fast-forwarded, they would have been published prematurely, even if the user feels that they are not ready yet to be pushed out, which would be a real disaster.

Quoted 6 lines

---

* So this time with an obvious test. It is somewhat surprising that this has been left unnoticed for the past 10 years.

Thanks, this looks like the obviously correct fix. I'm not too surprised that nobody noticed. You have to add quoting to get an empty-string argument in shell, and the non-delete case does notice and complain.

I did wonder if this was related to recent refactoring around the refspec code (like negative refspecs), but I think this function is independent of that anyway. Thanks for tracking down the original culprit.

builtin/push.c static void set_refspecs(const char **refs, int nr, const char *repo) · line 121

Quoted 13 lines
builtin/push.c +1 -1
index 03adb58602..194967ed79 100644
@@ -115,7 +115,7 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
 			else
 				refspec_appendf(&rs, "refs/tags/%s", ref);
 		} else if (deleterefs) {
-			if (strchr(ref, ':'))
+			if (strchr(ref, ':') || !*ref)
 				die(_("--delete only accepts plain target ref names"));
 			refspec_appendf(&rs, ":%s", ref);
 		} else if (!strchr(ref, ':')) {

Just wondering if there are other similar bugs lurking:

- could we see other strings that aren't empty, but also aren't plain target ref names? E.g., other punctuation like a "^" negative refspec? I guess that would produce ":^", which the refspec code will catch.

- likewise, do any of these other else clauses blindly pass around an empty string? Yes, the final one does, but we catch it later in the refspec code.

So I think it really is this "blindly prepend a colon" part that is the real bug. Anything else would create a nonsense refspec that will be caught by the refspec code.

-Peff

Reviewed-by on v1 1/1
#2 lore raw
In reply to Junio C Hamano
Quoted 57 lines · Junio C Hamano

When we added a syntax sugar "git push remote --delete <ref>" to "git push" as a synonym to the canonical "git push remote :<ref>" syntax at f517f1f2 (builtin-push: add --delete as syntactic sugar for :foo, 2009-12-30), we weren't careful enough to make sure that <ref> is not empty.

Blindly rewriting "--delete <ref>" to ":<ref>" means that an empty string <ref> results in refspec ":", which is the syntax to ask for "matching" push that does not delete anything.

Worse yet, if there were matching refs that can be fast-forwarded, they would have been published prematurely, even if the user feels that they are not ready yet to be pushed out, which would be a real disaster.

---

* So this time with an obvious test. It is somewhat surprising that this has been left unnoticed for the past 10 years.

 builtin/push.c        | 2 +-
 t/t5516-fetch-push.sh | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
builtin/push.c +1 -1
index 03adb58602..194967ed79 100644
@@ -115,7 +115,7 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
                        else
                                refspec_appendf(&rs, "refs/tags/%s", ref);
                } else if (deleterefs) {
-                       if (strchr(ref, ':'))
+                       if (strchr(ref, ':') || !*ref)
                                die(_("--delete only accepts plain target ref names"));
                        refspec_appendf(&rs, ":%s", ref);
                } else if (!strchr(ref, ':')) {
t/t5516-fetch-push.sh +5 -1
index 3ed121d0ce..7eee4e782f 100755
@@ -824,6 +824,11 @@ test_expect_success 'push --delete refuses src:dest refspecs' '
        test_must_fail git push testrepo --delete master:foo
 '
 
+test_expect_success 'push --delete refuses empty string' '
+       mk_test testrepo heads/master &&
+       test_must_fail git push testrepo --delete ""
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
        mk_test testrepo heads/master &&
        (
--
Quoted 1 line

2.30.1-824-gddfeb442a8