Skip to content

Commit 5fbd0a4

Browse files
committed
Merge branch 'bc/mktag'
* bc/mktag: mktag.c: tweak validation of tagger field and adjust test script mktag.c: improve verification of tagger field and tests
2 parents e0efa03 + ba26ab9 commit 5fbd0a4

File tree

2 files changed

+232
-15
lines changed

2 files changed

+232
-15
lines changed

mktag.c

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
* message and a signature block that git itself doesn't care about,
99
* but that can be verified with gpg or similar.
1010
*
11-
* The first three lines are guaranteed to be at least 63 bytes:
11+
* The first four lines are guaranteed to be at least 83 bytes:
1212
* "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
13-
* shortest possible type-line, and "tag .\n" at 6 bytes is the
14-
* shortest single-character-tag line.
13+
* shortest possible type-line, "tag .\n" at 6 bytes is the shortest
14+
* single-character-tag line, and "tagger . <> 0 +0000\n" at 20 bytes is
15+
* the shortest possible tagger-line.
1516
*/
1617

1718
/*
@@ -43,9 +44,10 @@ static int verify_tag(char *buffer, unsigned long size)
4344
int typelen;
4445
char type[20];
4546
unsigned char sha1[20];
46-
const char *object, *type_line, *tag_line, *tagger_line;
47+
const char *object, *type_line, *tag_line, *tagger_line, *lb, *rb;
48+
size_t len;
4749

48-
if (size < 64)
50+
if (size < 84)
4951
return error("wanna fool me ? you obviously got the size wrong !");
5052

5153
buffer[size] = 0;
@@ -97,11 +99,51 @@ static int verify_tag(char *buffer, unsigned long size)
9799
/* Verify the tagger line */
98100
tagger_line = tag_line;
99101

100-
if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
101-
return error("char" PD_FMT ": could not find \"tagger\"", tagger_line - buffer);
102-
103-
/* TODO: check for committer info + blank line? */
104-
/* Also, the minimum length is probably + "tagger .", or 63+8=71 */
102+
if (memcmp(tagger_line, "tagger ", 7))
103+
return error("char" PD_FMT ": could not find \"tagger \"",
104+
tagger_line - buffer);
105+
106+
/*
107+
* Check for correct form for name and email
108+
* i.e. " <" followed by "> " on _this_ line
109+
* No angle brackets within the name or email address fields.
110+
* No spaces within the email address field.
111+
*/
112+
tagger_line += 7;
113+
if (!(lb = strstr(tagger_line, " <")) || !(rb = strstr(lb+2, "> ")) ||
114+
strpbrk(tagger_line, "<>\n") != lb+1 ||
115+
strpbrk(lb+2, "><\n ") != rb)
116+
return error("char" PD_FMT ": malformed tagger field",
117+
tagger_line - buffer);
118+
119+
/* Check for author name, at least one character, space is acceptable */
120+
if (lb == tagger_line)
121+
return error("char" PD_FMT ": missing tagger name",
122+
tagger_line - buffer);
123+
124+
/* timestamp, 1 or more digits followed by space */
125+
tagger_line = rb + 2;
126+
if (!(len = strspn(tagger_line, "0123456789")))
127+
return error("char" PD_FMT ": missing tag timestamp",
128+
tagger_line - buffer);
129+
tagger_line += len;
130+
if (*tagger_line != ' ')
131+
return error("char" PD_FMT ": malformed tag timestamp",
132+
tagger_line - buffer);
133+
tagger_line++;
134+
135+
/* timezone, 5 digits [+-]hhmm, max. 1400 */
136+
if (!((tagger_line[0] == '+' || tagger_line[0] == '-') &&
137+
strspn(tagger_line+1, "0123456789") == 4 &&
138+
tagger_line[5] == '\n' && atoi(tagger_line+1) <= 1400))
139+
return error("char" PD_FMT ": malformed tag timezone",
140+
tagger_line - buffer);
141+
tagger_line += 6;
142+
143+
/* Verify the blank line separating the header from the body */
144+
if (*tagger_line != '\n')
145+
return error("char" PD_FMT ": trailing garbage in tag header",
146+
tagger_line - buffer);
105147

106148
/* The actual stuff afterwards we don't care about.. */
107149
return 0;

t/t3800-mktag.sh

Lines changed: 180 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ cat >tag.sig <<EOF
4444
xxxxxx 139e9b33986b1c2670fff52c5067603117b3e895
4545
type tag
4646
tag mytag
47+
tagger . <> 0 +0000
48+
4749
EOF
4850

4951
check_verify_failure '"object" line label check' '^error: char0: .*"object "$'
@@ -55,6 +57,8 @@ cat >tag.sig <<EOF
5557
object zz9e9b33986b1c2670fff52c5067603117b3e895
5658
type tag
5759
tag mytag
60+
tagger . <> 0 +0000
61+
5862
EOF
5963

6064
check_verify_failure '"object" line SHA1 check' '^error: char7: .*SHA1 hash$'
@@ -66,6 +70,8 @@ cat >tag.sig <<EOF
6670
object 779e9b33986b1c2670fff52c5067603117b3e895
6771
xxxx tag
6872
tag mytag
73+
tagger . <> 0 +0000
74+
6975
EOF
7076

7177
check_verify_failure '"type" line label check' '^error: char47: .*"\\ntype "$'
@@ -85,6 +91,8 @@ cat >tag.sig <<EOF
8591
object 779e9b33986b1c2670fff52c5067603117b3e895
8692
type tag
8793
xxx mytag
94+
tagger . <> 0 +0000
95+
8896
EOF
8997

9098
check_verify_failure '"tag" line label check #1' \
@@ -121,6 +129,8 @@ cat >tag.sig <<EOF
121129
object 779e9b33986b1c2670fff52c5067603117b3e895
122130
type tagggg
123131
tag mytag
132+
tagger . <> 0 +0000
133+
124134
EOF
125135

126136
check_verify_failure 'verify object (SHA1/type) check' \
@@ -133,6 +143,8 @@ cat >tag.sig <<EOF
133143
object $head
134144
type commit
135145
tag my tag
146+
tagger . <> 0 +0000
147+
136148
EOF
137149

138150
check_verify_failure 'verify tag-name check' \
@@ -145,10 +157,12 @@ cat >tag.sig <<EOF
145157
object $head
146158
type commit
147159
tag mytag
160+
161+
This is filler
148162
EOF
149163

150164
check_verify_failure '"tagger" line label check #1' \
151-
'^error: char70: could not find "tagger"$'
165+
'^error: char70: could not find "tagger "$'
152166

153167
############################################################
154168
# 12. tagger line label check #2
@@ -158,27 +172,188 @@ object $head
158172
type commit
159173
tag mytag
160174
tagger
175+
176+
This is filler
161177
EOF
162178

163179
check_verify_failure '"tagger" line label check #2' \
164-
'^error: char70: could not find "tagger"$'
180+
'^error: char70: could not find "tagger "$'
165181

166182
############################################################
167-
# 13. create valid tag
183+
# 13. disallow missing tag author name
168184

169185
cat >tag.sig <<EOF
170186
object $head
171187
type commit
172188
tag mytag
173-
tagger another@example.com
189+
tagger <> 0 +0000
190+
191+
This is filler
192+
EOF
193+
194+
check_verify_failure 'disallow missing tag author name' \
195+
'^error: char77: missing tagger name$'
196+
197+
############################################################
198+
# 14. disallow missing tag author name
199+
200+
cat >tag.sig <<EOF
201+
object $head
202+
type commit
203+
tag mytag
204+
tagger T A Gger <
205+
> 0 +0000
206+
207+
EOF
208+
209+
check_verify_failure 'disallow malformed tagger' \
210+
'^error: char77: malformed tagger field$'
211+
212+
############################################################
213+
# 15. allow empty tag email
214+
215+
cat >tag.sig <<EOF
216+
object $head
217+
type commit
218+
tag mytag
219+
tagger T A Gger <> 0 +0000
220+
221+
EOF
222+
223+
test_expect_success \
224+
'allow empty tag email' \
225+
'git-mktag <tag.sig >.git/refs/tags/mytag 2>message'
226+
227+
############################################################
228+
# 16. disallow spaces in tag email
229+
230+
cat >tag.sig <<EOF
231+
object $head
232+
type commit
233+
tag mytag
234+
tagger T A Gger <tag ger@example.com> 0 +0000
235+
236+
EOF
237+
238+
check_verify_failure 'disallow spaces in tag email' \
239+
'^error: char77: malformed tagger field$'
240+
241+
############################################################
242+
# 17. disallow missing tag timestamp
243+
244+
cat >tag.sig <<EOF
245+
object $head
246+
type commit
247+
tag mytag
248+
tagger T A Gger <tagger@example.com>
249+
250+
EOF
251+
252+
check_verify_failure 'disallow missing tag timestamp' \
253+
'^error: char107: missing tag timestamp$'
254+
255+
############################################################
256+
# 18. detect invalid tag timestamp1
257+
258+
cat >tag.sig <<EOF
259+
object $head
260+
type commit
261+
tag mytag
262+
tagger T A Gger <tagger@example.com> Tue Mar 25 15:47:44 2008
263+
264+
EOF
265+
266+
check_verify_failure 'detect invalid tag timestamp1' \
267+
'^error: char107: missing tag timestamp$'
268+
269+
############################################################
270+
# 19. detect invalid tag timestamp2
271+
272+
cat >tag.sig <<EOF
273+
object $head
274+
type commit
275+
tag mytag
276+
tagger T A Gger <tagger@example.com> 2008-03-31T12:20:15-0500
277+
278+
EOF
279+
280+
check_verify_failure 'detect invalid tag timestamp2' \
281+
'^error: char111: malformed tag timestamp$'
282+
283+
############################################################
284+
# 20. detect invalid tag timezone1
285+
286+
cat >tag.sig <<EOF
287+
object $head
288+
type commit
289+
tag mytag
290+
tagger T A Gger <tagger@example.com> 1206478233 GMT
291+
292+
EOF
293+
294+
check_verify_failure 'detect invalid tag timezone1' \
295+
'^error: char118: malformed tag timezone$'
296+
297+
############################################################
298+
# 21. detect invalid tag timezone2
299+
300+
cat >tag.sig <<EOF
301+
object $head
302+
type commit
303+
tag mytag
304+
tagger T A Gger <tagger@example.com> 1206478233 + 30
305+
306+
EOF
307+
308+
check_verify_failure 'detect invalid tag timezone2' \
309+
'^error: char118: malformed tag timezone$'
310+
311+
############################################################
312+
# 22. detect invalid tag timezone3
313+
314+
cat >tag.sig <<EOF
315+
object $head
316+
type commit
317+
tag mytag
318+
tagger T A Gger <tagger@example.com> 1206478233 -1430
319+
320+
EOF
321+
322+
check_verify_failure 'detect invalid tag timezone3' \
323+
'^error: char118: malformed tag timezone$'
324+
325+
############################################################
326+
# 23. detect invalid header entry
327+
328+
cat >tag.sig <<EOF
329+
object $head
330+
type commit
331+
tag mytag
332+
tagger T A Gger <tagger@example.com> 1206478233 -0500
333+
this line should not be here
334+
335+
EOF
336+
337+
check_verify_failure 'detect invalid header entry' \
338+
'^error: char124: trailing garbage in tag header$'
339+
340+
############################################################
341+
# 24. create valid tag
342+
343+
cat >tag.sig <<EOF
344+
object $head
345+
type commit
346+
tag mytag
347+
tagger T A Gger <tagger@example.com> 1206478233 -0500
348+
174349
EOF
175350

176351
test_expect_success \
177352
'create valid tag' \
178353
'git-mktag <tag.sig >.git/refs/tags/mytag 2>message'
179354

180355
############################################################
181-
# 14. check mytag
356+
# 25. check mytag
182357

183358
test_expect_success \
184359
'check mytag' \

0 commit comments

Comments
 (0)