symbolic-ref: refuse to set syntactically invalid target

You can feed absolute garbage to symbolic-ref as a target like: git symbolic-ref HEAD refs/heads/foo..bar While this doesn't technically break the repo entirely (our "is it a git directory" detector looks only for "refs/" at the start), we would never resolve such a ref, as the ".." is invalid within a refname. Let's flag these as invalid at creation time to help the caller realize that what they're asking for is bogus. A few notes: - We use REFNAME_ALLOW_ONELEVEL here, which lets: git update-ref refs/heads/foo FETCH_HEAD continue to work. It's unclear whether anybody wants to do something so odd, but it does work now, so this is erring on the conservative side. There's a test to make sure we didn't accidentally break this, but don't take that test as an endorsement that it's a good idea, or something we might not change in the future. - The test in t4202-log.sh checks how we handle such an invalid ref on the reading side, so it has to be updated to touch the HEAD file directly. - We need to keep our HEAD-specific check for "does it start with refs/". The ALLOW_ONELEVEL flag means we won't be enforcing that for other refs, but HEAD is special here because of the checks in validate_headref(). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Aug 1, 2022 at 14:15 UTC 04ede97211c132f4d71c96c61f5124cbb3ebdc77
3 files changed +14 -2
builtin/symbolic-ref.c
+2
@@ -71,6 +71,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
71 if (!strcmp(argv[0], "HEAD") &&
72 !starts_with(argv[1], "refs/"))
73 die("Refusing to point HEAD outside of refs/");
74 + if (check_refname_format(argv[1], REFNAME_ALLOW_ONELEVEL) < 0)
75 + die("Refusing to set '%s' to invalid ref '%s'", argv[0], argv[1]);
76 ret = !!create_symref(argv[0], argv[1], msg);
77 break;
78 default:
t/t1401-symbolic-ref.sh
+10
@@ -165,4 +165,14 @@ test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
165 test_cmp expect actual
166 '
167
168 +test_expect_success 'symbolic-ref refuses invalid target for non-HEAD' '
169 + test_must_fail git symbolic-ref refs/heads/invalid foo..bar
170 +'
171 +
172 +test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
173 + git symbolic-ref refs/heads/top-level FETCH_HEAD &&
174 + git update-ref FETCH_HEAD HEAD &&
175 + test_cmp_rev top-level HEAD
176 +'
177 +
178 test_done
t/t4202-log.sh
+2 -2
@@ -2112,9 +2112,9 @@ test_expect_success REFFILES 'log diagnoses bogus HEAD hash' '
2112 test_i18ngrep broken stderr
2113 '
2114
2115 -test_expect_success 'log diagnoses bogus HEAD symref' '
2115 +test_expect_success REFFILES 'log diagnoses bogus HEAD symref' '
2116 git init empty &&
2117 - git --git-dir empty/.git symbolic-ref HEAD refs/heads/invalid.lock &&
2117 + echo "ref: refs/heads/invalid.lock" > empty/.git/HEAD &&
2118 test_must_fail git -C empty log 2>stderr &&
2119 test_i18ngrep broken stderr &&
2120 test_must_fail git -C empty log --default totally-bogus 2>stderr &&