Skip to content

Commit 31f1c37

Browse files
committed
object-name: be more strict in parsing describe-like output
From Documentation/revisions.txt: '<describeOutput>', e.g. 'v1.7.4.2-679-g3bee7fb':: Output from `git describe`; i.e. a closest tag, optionally followed by a dash and a number of commits, followed by a dash, a 'g', and an abbreviated object name. which means that output of the format ${REFNAME}-${INTEGER}-g${HASH} should parse to fully expand ${HASH}. This is fine. However, we currently don't validate any of ${REFNAME}-${INTEGER}, we only parse -g${HASH} and assume the rest is valid. That is problematic, since it breaks things like git cat-file -p branchname:path/to/file/named/i-gaffed which, when commit affed exists, will not return us information about a file we are looking for but will instead tell us about commit affed. Similarly, we should probably not treat refs/tags/invalid/./../...../// ~^:/?*\\&[}/busted.lock-g049e0ef6 as a request for commit 050e0ef either. Tighten up the parsing to make sure ${REFNAME} and ${INTEGER} are present and valid. Reported-by: Gabriel Amaral <[email protected]> Signed-off-by: Elijah Newren <[email protected]>
1 parent 13f68be commit 31f1c37

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

object-name.c

+54-1
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,58 @@ static int peel_onion(struct repository *r, const char *name, int len,
12711271
return 0;
12721272
}
12731273

1274+
/*
1275+
* Documentation/revisions.txt says:
1276+
* '<describeOutput>', e.g. 'v1.7.4.2-679-g3bee7fb'::
1277+
* Output from `git describe`; i.e. a closest tag, optionally
1278+
* followed by a dash and a number of commits, followed by a dash, a
1279+
* 'g', and an abbreviated object name.
1280+
*
1281+
* which means that the stuff before '-g${HASH}' needs to be a valid
1282+
* refname, a dash, and a non-negative integer. This function verifies
1283+
* that.
1284+
*
1285+
* In particular, we do not want to treat
1286+
* branchname:path/to/file/named/i-gaffed
1287+
* as a request for commit affed.
1288+
*
1289+
* More generally, we should probably not treat
1290+
* 'refs/heads/./../.../ ~^:/?*[////\\\&}/busted.lock-g050e0ef6ead'
1291+
* as a request for object 050e0ef6ead either.
1292+
*
1293+
* We are called with name[len] == '-' and name[len+1] == 'g', i.e.
1294+
* we are verifying ${REFNAME}-{INTEGER} part of the name.
1295+
*/
1296+
static int ref_and_count_parts_valid(const char *name, int len)
1297+
{
1298+
struct strbuf sb;
1299+
const char *cp;
1300+
int flags = REFNAME_ALLOW_ONELEVEL;
1301+
int ret = 1;
1302+
1303+
/* Ensure we have at least one digit */
1304+
if (!isxdigit(name[len-1]))
1305+
return 0;
1306+
1307+
/* Skip over digits backwards until we get to the dash */
1308+
for (cp = name + len - 2; name < cp; cp--) {
1309+
if (*cp == '-')
1310+
break;
1311+
if (!isxdigit(*cp))
1312+
return 0;
1313+
}
1314+
/* Ensure we found the leading dash */
1315+
if (*cp != '-')
1316+
return 0;
1317+
1318+
len = cp - name;
1319+
strbuf_init(&sb, len);
1320+
strbuf_add(&sb, name, len);
1321+
ret = !check_refname_format(name, flags);
1322+
strbuf_release(&sb);
1323+
return ret;
1324+
}
1325+
12741326
static int get_describe_name(struct repository *r,
12751327
const char *name, int len,
12761328
struct object_id *oid)
@@ -1284,7 +1336,8 @@ static int get_describe_name(struct repository *r,
12841336
/* We must be looking at g in "SOMETHING-g"
12851337
* for it to be describe output.
12861338
*/
1287-
if (ch == 'g' && cp[-1] == '-') {
1339+
if (ch == 'g' && cp[-1] == '-' &&
1340+
ref_and_count_parts_valid(name, cp - 1 - name)) {
12881341
cp++;
12891342
len -= cp - name;
12901343
return get_short_oid(r,

t/t6120-describe.sh

+22
Original file line numberDiff line numberDiff line change
@@ -725,4 +725,26 @@ test_expect_success '--exact-match does not show --always fallback' '
725725
test_must_fail git describe --exact-match --always
726726
'
727727

728+
test_expect_success 'avoid being fooled by describe-like filename' '
729+
test_when_finished rm out &&
730+
731+
git rev-parse --short HEAD >out &&
732+
FILENAME=filename-g$(cat out) &&
733+
touch $FILENAME &&
734+
git add $FILENAME &&
735+
git commit -m "Add $FILENAME" &&
736+
737+
git cat-file -t HEAD:$FILENAME >actual &&
738+
739+
echo blob >expect &&
740+
test_cmp expect actual
741+
'
742+
743+
test_expect_success 'do not be fooled by invalid describe format ' '
744+
test_when_finished rm out &&
745+
746+
git rev-parse --short HEAD >out &&
747+
test_must_fail git cat-file -t "refs/tags/super-invalid/./../...../ ~^:/?*[////\\\\\\&}/busted.lock-42-g"$(cat out)
748+
'
749+
728750
test_done

0 commit comments

Comments
 (0)