git-check-ref-format: reject funny ref names.
Update check_ref_format() function to reject ref names that: * has a path component that begins with a ".", or * has a double dots "..", or * has ASCII control character, "~", "^", ":" or SP, anywhere, or * ends with a "/". Use it in 'git-checkout -b', 'git-branch', and 'git-tag' to make sure that newly created refs are well-formed. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 15, 2005 at 17:10 UTC
652d5dc6c0aef842eafcd9d2f73a4836d20734e2
6 files changed
+103
-38
Makefile
+1
-1
@@ -116,7 +116,7 @@ PROGRAMS = \
116
git-ssh-upload git-tar-tree git-unpack-file \
117
git-unpack-objects git-update-index git-update-server-info \
118
git-upload-pack git-verify-pack git-write-tree \
119
- git-update-ref git-symbolic-ref \
119
+ git-update-ref git-symbolic-ref git-check-ref-format \
120
$(SIMPLE_PROGRAMS)
121
122
# Backward compatibility -- to be removed after 1.0
check-ref-format.c
new
+17
@@ -0,0 +1,17 @@
1
+/*
2
+ * GIT - The information manager from hell
3
+ */
4
+
5
+#include "cache.h"
6
+#include "refs.h"
7
+
8
+#include <stdio.h>
9
+
10
+int main(int ac, char **av)
11
+{
12
+ if (ac != 2)
13
+ usage("git-check-ref-format refname");
14
+ if (check_ref_format(av[1]))
15
+ exit(1);
16
+ return 0;
17
+}
git-branch.sh
+35
-28
@@ -13,38 +13,42 @@ If two arguments, create a new branch <branchname> based off of <start-point>.
13
}
14
15
delete_branch () {
16
- option="$1" branch_name="$2"
16
+ option="$1"
17
+ shift
18
headref=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD |
19
sed -e 's|^refs/heads/||')
19
- case ",$headref," in
20
- ",$branch_name,")
21
- die "Cannot delete the branch you are on." ;;
22
- ,,)
23
- die "What branch are you on anyway?" ;;
24
- esac
25
- branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
26
- branch=$(git-rev-parse --verify "$branch^0") ||
27
- die "Seriously, what branch are you talking about?"
28
- case "$option" in
29
- -D)
30
- ;;
31
- *)
32
- mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
33
- case " $mbs " in
34
- *' '$branch' '*)
35
- # the merge base of branch and HEAD contains branch --
36
- # which means that the HEAD contains everything in the HEAD.
20
+ for branch_name
21
+ do
22
+ case ",$headref," in
23
+ ",$branch_name,")
24
+ die "Cannot delete the branch you are on." ;;
25
+ ,,)
26
+ die "What branch are you on anyway?" ;;
27
+ esac
28
+ branch=$(cat "$GIT_DIR/refs/heads/$branch_name") &&
29
+ branch=$(git-rev-parse --verify "$branch^0") ||
30
+ die "Seriously, what branch are you talking about?"
31
+ case "$option" in
32
+ -D)
33
;;
34
*)
39
- echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
40
-If you are sure you want to delete it, run 'git branch -D $branch_name'."
41
- exit 1
35
+ mbs=$(git-merge-base -a "$branch" HEAD | tr '\012' ' ')
36
+ case " $mbs " in
37
+ *' '$branch' '*)
38
+ # the merge base of branch and HEAD contains branch --
39
+ # which means that the HEAD contains everything in the HEAD.
40
+ ;;
41
+ *)
42
+ echo >&2 "The branch '$branch_name' is not a strict subset of your current HEAD.
43
+ If you are sure you want to delete it, run 'git branch -D $branch_name'."
44
+ exit 1
45
+ ;;
46
+ esac
47
;;
48
esac
44
- ;;
45
- esac
46
- rm -f "$GIT_DIR/refs/heads/$branch_name"
47
- echo "Deleted branch $branch_name."
49
+ rm -f "$GIT_DIR/refs/heads/$branch_name"
50
+ echo "Deleted branch $branch_name."
51
+ done
52
exit 0
53
}
54
@@ -52,7 +56,7 @@ while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
56
do
57
case "$1" in
58
-d | -D)
55
- delete_branch "$1" "$2"
59
+ delete_branch "$@"
60
exit
61
;;
62
--)
@@ -93,6 +97,9 @@ branchname="$1"
97
98
rev=$(git-rev-parse --verify "$head") || exit
99
96
-[ -e "$GIT_DIR/refs/heads/$branchname" ] && die "$branchname already exists"
100
+[ -e "$GIT_DIR/refs/heads/$branchname" ] &&
101
+ die "$branchname already exists."
102
+git-check-ref-format "heads/$branchname" ||
103
+ die "we do not like '$branchname' as a branch name."
104
105
echo $rev > "$GIT_DIR/refs/heads/$branchname"
git-checkout.sh
+2
@@ -17,6 +17,8 @@ while [ "$#" != "0" ]; do
17
die "git checkout: -b needs a branch name"
18
[ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
19
die "git checkout: branch $newbranch already exists"
20
+ git-check-ref-format "heads/$newbranch" ||
21
+ die "we do not like '$newbranch' as a branch name."
22
;;
23
"-f")
24
force=1
git-tag.sh
+2
@@ -46,6 +46,8 @@ if [ -e "$GIT_DIR/refs/tags/$name" -a -z "$force" ]; then
46
die "tag '$name' already exists"
47
fi
48
shift
49
+git-check-ref-format "tags/$name" ||
50
+ die "we do not like '$name' as a tag name."
51
52
object=$(git-rev-parse --verify --default HEAD "$@") || exit 1
53
type=$(git-cat-file -t $object) || exit 1
refs.c
+46
-9
@@ -335,17 +335,54 @@ int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
335
return retval;
336
}
337
338
+/*
339
+ * Make sure "ref" is something reasonable to have under ".git/refs/";
340
+ * We do not like it if:
341
+ *
342
+ * - any path component of it begins with ".", or
343
+ * - it has double dots "..", or
344
+ * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
345
+ * - it ends with a "/".
346
+ */
347
+
348
+static inline int bad_ref_char(int ch)
349
+{
350
+ return (((unsigned) ch) <= ' ' ||
351
+ ch == '~' || ch == '^' || ch == ':');
352
+}
353
+
354
int check_ref_format(const char *ref)
355
{
340
- char *middle;
341
- if (ref[0] == '.' || ref[0] == '/')
342
- return -1;
343
- middle = strchr(ref, '/');
344
- if (!middle || !middle[1])
345
- return -1;
346
- if (strchr(middle + 1, '/'))
347
- return -1;
348
- return 0;
356
+ int ch, level;
357
+ const char *cp = ref;
358
+
359
+ level = 0;
360
+ while (1) {
361
+ while ((ch = *cp++) == '/')
362
+ ; /* tolerate duplicated slashes */
363
+ if (!ch)
364
+ return -1; /* should not end with slashes */
365
+
366
+ /* we are at the beginning of the path component */
367
+ if (ch == '.' || bad_ref_char(ch))
368
+ return -1;
369
+
370
+ /* scan the rest of the path component */
371
+ while ((ch = *cp++) != 0) {
372
+ if (bad_ref_char(ch))
373
+ return -1;
374
+ if (ch == '/')
375
+ break;
376
+ if (ch == '.' && *cp == '.')
377
+ return -1;
378
+ }
379
+ level++;
380
+ if (!ch) {
381
+ if (level < 2)
382
+ return -1; /* at least of form "heads/blah" */
383
+ return 0;
384
+ }
385
+ }
386
}
387
388
int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)