1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
#!/bin/sh
# Drive git-authz the way git does, without git: identity in the environment,
# "old new ref" on stdin, the repository as cwd. The two facts only git knows are
# supplied with --ff and --head, so no repository or commits are needed.
#
# POSIX sh: runs under dash, busybox ash and bash, because the container it has to
# survive is Alpine.
AUTHZ=${AUTHZ:-./git-authz}
if [ ! -x "$AUTHZ" ]; then
printf 'run.sh: no executable at %s (run it from the package root, or set AUTHZ)\n' "$AUTHZ" >&2
exit 1
fi
# A hook is a symlink to the program, so the link has to be written with a path
# that means the same thing from inside the temporary tree.
AUTHZ_ABS=$(CDPATH= cd -- "$(dirname -- "$AUTHZ")" && pwd)/$(basename -- "$AUTHZ")
TMP=${TMPDIR:-/tmp}/git-authz-run.$$
P=$TMP/authz.conf
passed=0
failed=0
skipped=0
cleanup() {
chmod -R u+rwX "$TMP" 2>/dev/null
rm -rf "$TMP"
}
trap 'cleanup; exit 130' INT TERM
trap cleanup EXIT
mkdir -p "$TMP" || exit 1
ZERO=0000000000000000000000000000000000000000
A=1111111111111111111111111111111111111111
B=2222222222222222222222222222222222222222
C=3333333333333333333333333333333333333333
# ------------------------------------------------------------------ harness
section() { printf '\n# %s\n' "$1"; }
ok() { passed=$((passed + 1)); }
bad() { failed=$((failed + 1)); printf 'not ok: %s\n' "$1"; }
skip_it() {
skipped=$((skipped + 1))
printf 'ok (skipped): %s - %s\n' "$1" "$2"
}
# expect <desc> <want-rc> <want-substring|-> <got-rc> <got-text>
expect() {
if [ "$4" != "$2" ]; then
bad "$1: exit $4, wanted $2 (said: $5)"
elif [ "$3" != "-" ] && ! contains "$5" "$3"; then
bad "$1: said \"$5\", wanted it to contain \"$3\""
else
ok
fi
}
# expect_not <desc> <want-rc> <forbidden-substring> <got-rc> <got-text>
expect_not() {
if [ "$4" != "$2" ]; then
bad "$1: exit $4, wanted $2 (said: $5)"
elif [ "$3" != "-" ] && contains "$5" "$3"; then
bad "$1: said \"$5\", which contains the forbidden \"$3\""
else
ok
fi
}
contains() {
case $1 in
*"$2"*) return 0 ;;
*) return 1 ;;
esac
}
# The policy from a here-document.
policy() {
cat >"$P"
}
# check <user> <repo> <level> [ref]
do_check() {
"$AUTHZ" --policy "$POLICY" check "$@" 2>&1
}
# Overridden per test with "out=$(POLICY=something push ...)", a subshell, so it
# does not leak into the next test.
POLICY=$P
# push <user|none> <repo> [extra authorize args] (ref lines on stdin)
push() {
user=$1
repo=$2
shift 2
if [ "$user" = none ]; then
env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_REPO="$repo" \
"$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
else
GIT_HOST_USER="$user" GIT_HOST_REPO="$repo" \
"$AUTHZ" --policy "$POLICY" authorize --no-git "$@" 2>&1
fi
}
# hook <user|none> <repodir>
# The way core.hooksPath reaches it: by name, repo as cwd, git's own argument in
# hand (not ours, and must not be read as one), no --policy anywhere.
hook() {
user=$1
dir=$2
if [ "$user" = none ]; then
( cd "$dir" && env -u GIT_HOST_USER -u REMOTE_USER GIT_HOST_AUTHZ="$POLICY" \
"$TMP/bin/pre-receive" "$dir" 2>&1 )
else
( cd "$dir" && env GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
"$TMP/bin/pre-receive" "$dir" 2>&1 )
fi
}
# ------------------------------------------------------------------ fixture
section "fixture"
cat >"$TMP/authz.conf" <<'EOF'
# who may move which refs
alice * admin # the one account that can do anything
@all * write refs/heads/feature/* # anyone the policy names: working branches
frank team write refs/heads/main refs/tags/* # releases, and nothing else
bob team write refs/heads/feature/*
carol team force refs/heads/feature/*
dave team admin
erin secrets admin
EOF
# A repository directory, and the hook name this binary is reached under.
mkdir -p "$TMP/root/team.git" "$TMP/bin" "$TMP/empty"
ln -sf "$AUTHZ_ABS" "$TMP/bin/pre-receive"
if [ ! -x "$TMP/bin/pre-receive" ]; then
cp "$AUTHZ" "$TMP/bin/pre-receive"
fi
chmod 755 "$TMP/bin/pre-receive"
if [ ! -L "$TMP/bin/pre-receive" ]; then
skip_it "hook reached by its own name" "cannot make a symlink here"
fi
# No '@all' line: for tests that need a stranger and a specific repository.
cat >"$TMP/plain.conf" <<'EOF'
alice * admin
bob team write refs/heads/feature/*
EOF
out=$("$AUTHZ" --policy "$P" validate 2>&1)
rc=$?
expect "the fixture policy is sound" 0 "is in force" "$rc" "$out"
# ---------------------------------------------------------------- usage
section "usage and exit codes"
out=$("$AUTHZ" --policy "$P" 2>&1); rc=$?
expect "no command is a usage error" 64 "usage:" "$rc" "$out"
out=$("$AUTHZ" --policy "$P" frobnicate 2>&1); rc=$?
expect "unknown command" 64 "unknown command" "$rc" "$out"
out=$("$AUTHZ" --policy "$P" check alice web 2>&1); rc=$?
expect "check wants three arguments" 64 "wants <user> <repo> <level>" "$rc" "$out"
out=$("$AUTHZ" --policy "$P" check alice web read 2>&1); rc=$?
expect "'read' is not a level" 64 "unknown access level" "$rc" "$out"
out=$("$AUTHZ" --help 2>&1); rc=$?
expect "--help" 0 "Exit status" "$rc" "$out"
out=$("$AUTHZ" --policy "$P" authorize --wat 2>&1 </dev/null); rc=$?
expect "unknown authorize argument" 64 "unknown argument" "$rc" "$out"
out=$("$AUTHZ" --policy "$P" check alice web write refs/heads/x 2>&1); rc=$?
expect "a granted admin can write anything" 0 "allow" "$rc" "$out"
# ------------------------------------------------------------------ check
section "check: the ladder"
out=$(do_check bob team write refs/heads/feature/login); rc=$?
expect "write on a listed pattern" 0 "allow" "$rc" "$out"
out=$(do_check bob team force refs/heads/feature/login); rc=$?
expect "write is not force" 1 "needs force" "$rc" "$out"
out=$(do_check carol team force refs/heads/feature/login); rc=$?
expect "force on a listed pattern" 0 "allow" "$rc" "$out"
out=$(do_check carol team admin refs/heads/feature/login); rc=$?
expect "force is not admin" 1 "needs admin" "$rc" "$out"
out=$(do_check dave team admin refs/heads/anything); rc=$?
expect "admin on the whole repository" 0 "allow" "$rc" "$out"
out=$(do_check dave team write refs/heads/anything); rc=$?
expect "admin answers a write too" 0 "allow" "$rc" "$out"
out=$(POLICY="$TMP/plain.conf" do_check erin other write refs/heads/dev); rc=$?
expect "a stranger to the repository" 1 "no rule for other" "$rc" "$out"
out=$(do_check erin other write refs/heads/feature/x); rc=$?
expect "unless '@all' reaches that repository too" 0 "allow" "$rc" "$out"
section "check: names mean what they say"
out=$(do_check bob team.git write refs/heads/feature/x); rc=$?
expect "team.git is team" 0 "allow" "$rc" "$out"
out=$(do_check bob team write main); rc=$?
expect "a bare name is a branch" 1 "refs/heads/main" "$rc" "$out"
out=$(do_check bob team write heads/main); rc=$?
expect "heads/main is the same branch" 1 "refs/heads/main" "$rc" "$out"
out=$(do_check bob team write refs/heads/main); rc=$?
expect "and so is refs/heads/main" 1 "refs/heads/main" "$rc" "$out"
out=$(do_check bob team write v1); rc=$?
expect "a bare v1 is a branch, never a tag" 1 "refs/heads/v1" "$rc" "$out"
out=$(do_check bob team write tags/v1); rc=$?
expect "and it takes tags/v1 to mean a tag" 1 "refs/tags/v1" "$rc" "$out"
out=$(do_check dave team write refs/tags/v1); rc=$?
expect "an admin can tag" 0 "allow" "$rc" "$out"
out=$(do_check carol team write refs/heads/feature/deep/nested); rc=$?
expect "* crosses a slash" 0 "allow" "$rc" "$out"
section "check: a rule covers what it names, and nothing else"
out=$(do_check bob team write refs/heads/main); rc=$?
expect "bob's rules never name main" 1 "but not on refs/heads/main" "$rc" "$out"
out=$(do_check carol team force refs/heads/main); rc=$?
expect "and neither do carol's, at any level" 1 "but not on refs/heads/main" "$rc" "$out"
out=$(do_check frank team write refs/heads/main); rc=$?
expect "frank's rule does name it, so he may move it" 0 "allow" "$rc" "$out"
out=$(do_check frank team write refs/heads/feature/x); rc=$?
expect "and he reaches the '@all' rule like anyone else named" 0 "allow" "$rc" "$out"
out=$(do_check frank team force refs/heads/main); rc=$?
expect "naming a ref is not the same as holding every level on it" 1 "needs force" "$rc" "$out"
out=$(do_check dave team force refs/heads/main); rc=$?
expect "an unscoped admin covers main too" 0 "allow" "$rc" "$out"
out=$(do_check bob team write refs/tags/v1); rc=$?
expect "tags have to be named as well" 1 "refs/tags/v1" "$rc" "$out"
out=$(do_check frank team write refs/tags/v1); rc=$?
expect "and frank named his" 0 "allow" "$rc" "$out"
section "check: refs that are neither branch nor tag"
out=$(do_check bob team write refs/pull/1/head); rc=$?
expect "a fetch ref needs admin" 1 "needs admin" "$rc" "$out"
out=$(do_check dave team write refs/pull/1/head); rc=$?
expect "an admin moves anything" 0 "allow" "$rc" "$out"
out=$(do_check alice secrets write refs/notes/x); rc=$?
expect "notes are for admins" 0 "allow" "$rc" "$out"
section "check: '@all' is everyone the policy names"
out=$(do_check carol web write refs/heads/feature/any); rc=$?
expect "a named user reaches the @all rule" 0 "allow" "$rc" "$out"
out=$(do_check nobody team write refs/heads/dev); rc=$?
expect "a name the policy does not have gets nothing" 1 "no rule" "$rc" "$out"
out=$(do_check '' team write refs/heads/dev); rc=$?
expect "an empty identity is no identity" 1 "no identity" "$rc" "$out"
section "check: the question with no ref"
out=$(do_check dave team force); rc=$?
expect "an unscoped admin has force somewhere" 0 "allow" "$rc" "$out"
out=$(do_check bob team write); rc=$?
expect "a scoped rule is 'somewhere'" 0 "some refs" "$rc" "$out"
out=$(do_check bob team force); rc=$?
expect "but not at a level he lacks" 1 "no force" "$rc" "$out"
out=$(do_check alice web admin); rc=$?
expect "an unscoped rule is 'all of'" 0 "all of web" "$rc" "$out"
# -------------------------------------------------------- authorize (hook)
section "authorize: what git hands the hook"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
push bob team --explain); rc=$?
expect "creating a branch is write" 0 "allow" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/notes/x |
push bob team); rc=$?
expect "a ref that is neither a branch nor a tag is for admins" 1 \
"permission denied [user=bob, obj=refs/notes/x]" "$rc" "$out"
out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
POLICY="$TMP/plain.conf" push bob team); rc=$?
expect "a branch outside the pattern is denied" 1 \
"permission denied [user=bob, obj=refs/heads/release]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
push bob team --ff=yes --explain); rc=$?
expect "a fast-forward is write" 0 "allow" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
push bob team --ff=no); rc=$?
expect "a rewrite is force" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
push carol other --ff=no); rc=$?
expect "a rewrite in a repository she only reaches through @all" 1 \
"permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
push bob team); rc=$?
expect "deleting is force" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
push carol team); rc=$?
expect "carol may delete her own kind of branch" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
push carol team --head=refs/heads/dev); rc=$?
expect "deleting the default branch is admin" 1 \
"permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
push carol team --head=refs/heads/other); rc=$?
expect "deleting a branch she holds force on is allowed" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
push carol team --head=refs/heads/other); rc=$?
expect "and only force on that ref will do, not force somewhere else" 1 \
"permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
push dave team --head=refs/heads/dev); rc=$?
expect "an admin deletes the default branch" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev |
env PATH=/nonexistent GIT_HOST_USER=carol GIT_HOST_REPO=team \
"$AUTHZ" --policy "$P" authorize 2>&1); rc=$?
expect "a HEAD that cannot be read is charged as the default branch" 1 \
"permission denied [user=carol, obj=refs/heads/dev]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
push bob team --ff=auto); rc=$?
expect "with no git to ask, an update is a rewrite" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/pull/7/head |
push dave team); rc=$?
expect "an admin writes a fetch ref" 0 "-" "$rc" "$out"
section "authorize: identity"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | push none team); rc=$?
expect "anonymous is denied" 1 \
"permission denied [obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
env -u GIT_HOST_USER REMOTE_USER=bob GIT_HOST_REPO=team \
"$AUTHZ" --policy "$P" authorize --no-git 2>&1); rc=$?
expect "REMOTE_USER is an identity too (http)" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
env GIT_HOST_USER=carol REMOTE_USER=bob GIT_HOST_REPO=team \
"$AUTHZ" --policy "$P" authorize --no-git --explain 2>&1); rc=$?
expect "GIT_HOST_USER wins over REMOTE_USER" 0 "carol has write" "$rc" "$out"
out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
POLICY="$TMP/plain.conf" push bob team --explain); rc=$?
expect "the named repository is allowed" 0 "on team" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
POLICY="$TMP/plain.conf" push bob other --explain); rc=$?
expect "another repository is not" 1 "no rule for other" "$rc" "$out"
section "authorize: several refs, and the first denial"
out=$(printf '%s %s %s\n%s %s %s\n%s %s %s\n' \
"$ZERO" "$A" refs/heads/feature/one \
"$ZERO" "$B" refs/heads/feature/two \
"$ZERO" "$C" refs/heads/feature/three |
push bob team --explain); rc=$?
expect "a whole push of allowed refs" 0 "feature/three" "$rc" "$out"
out=$(printf '%s %s %s\n%s %s %s\n' \
"$ZERO" "$A" refs/heads/feature/one \
"$ZERO" "$B" refs/heads/main |
push bob team); rc=$?
expect "one ref nobody may stop the whole push" 1 "refs/heads/main" "$rc" "$out"
expect_not "and the refs before it are not reported either" 1 "feature/one" "$rc" "$out"
out=$(printf '%s %s %s\n%s %s %s\n' \
"$ZERO" "$A" refs/heads/main \
"$ZERO" "$B" refs/heads/feature/one |
push bob team); rc=$?
expect "the refusal names the input and the verdict, and nothing else" 1 \
"permission denied [user=bob, obj=refs/heads/main]" "$rc" "$out"
expect_not "and does not quote the policy at the person pushing" 1 "write" "$rc" "$out"
expect_not "and stops at the first refusal" 1 "feature/one" "$rc" "$out"
section "authorize: input that is not a push"
# Input that is not a push is not a refusal of a person either: no git writes
# these lines, so it is exit 3 with the host, not exit 1 with the pusher. And
# none of the line comes back: whoever broke it is reading a log, not a terminal.
out=$(printf 'nonsense\n' | push bob team); rc=$?
expect "a line with too few fields" 3 "invalid operation" "$rc" "$out"
expect_not "and it is not dressed up as a permission problem" 3 "permission" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" heads/feature/x | push bob team); rc=$?
expect "a refname that is not a refname" 3 "invalid operation" "$rc" "$out"
expect_not "and none of the line is echoed back" 3 "$A" "$rc" "$out"
out=$(printf '%s %s %s\n' notanoid "$B" refs/heads/x | push bob team); rc=$?
expect "an object id that is not one" 3 "invalid operation" "$rc" "$out"
expect_not "and the bad id is not echoed either" 3 "notanoid" "$rc" "$out"
out=$(printf '' | push bob team); rc=$?
expect "no refs at all is nothing to deny" 0 "-" "$rc" "$out"
out=$(printf '\n\n' | push bob team); rc=$?
expect "blank lines are skipped" 0 "-" "$rc" "$out"
# --------------------------------------------------------- what a refusal says
#
# One line: the input presented and the verdict. The reason is a sentence about
# a file the person pushing cannot read, and `check` is where whoever can read it
# says what it would have taken; --explain is the same sentence in the hook's own
# seat, for whoever is standing there.
section "what a refusal says to the person who hit it"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push bob team); rc=$?
expect "one line, named by the program that refused" 1 \
"git-authz: permission denied [user=bob, obj=refs/heads/main]" "$rc" "$out"
if [ "$(printf '%s\n' "$out" | wc -l)" != 1 ]; then
bad "a denial is one line, and this was: $out"
else
ok
fi
expect_not "with no advice about levels held or needed" 1 "needs" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push bob team --explain); rc=$?
expect "--explain is where the reason is asked for" 1 \
"bob holds write on team, but not on refs/heads/main" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main | push none team); rc=$?
expect "no identity means no user is named" 1 \
"permission denied [obj=refs/heads/main]" "$rc" "$out"
expect_not "not even an empty one" 1 "user=" "$rc" "$out"
# ------------------------------------------------------------ option forms
# The usage text writes these as two words ("--user U", "--ff auto|yes|no"), and
# validate accepts both spellings, so authorize has to accept them too: a person
# following the help must not be told the option is unknown.
section "authorize: the options as the usage spells them"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
"$AUTHZ" --policy "$P" authorize --user bob --repo team --no-git 2>&1); rc=$?
expect "--user and --repo as two words" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
"$AUTHZ" --policy "$P" authorize --user bob --repo team --ff no 2>&1); rc=$?
expect "--ff no as two words costs force" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$B" "$ZERO" refs/heads/main |
"$AUTHZ" --policy "$P" authorize --user bob --repo team --head none 2>&1); rc=$?
expect "--head none as two words" 1 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
"$AUTHZ" --policy "$P" authorize --user=bob --repo=team --no-git 2>&1); rc=$?
expect "and the = form still works" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
"$AUTHZ" --policy "$P" authorize --user 2>&1); rc=$?
expect "an option with no value says so" 64 "wants a value" "$rc" "$out"
# --------------------------------------------------------- the hook by name
section "authorize: reached as pre-receive"
out=$(POLICY="$TMP/plain.conf" printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
expect "the hook name selects authorize, and git's argument is ignored" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release |
POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$?
expect "and denies what authorize denies, on the repository it found" 1 \
"permission denied [user=bob, obj=refs/heads/release]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
POLICY="$TMP/plain.conf" hook none "$TMP/root/team.git"); rc=$?
expect "an anonymous key is denied at the hook" 1 \
"permission denied [obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
POLICY="$TMP/broken.conf" hook bob "$TMP/root/team.git"); rc=$?
expect "a hook on a policy that does not parse stops the push" 3 \
"invalid policy file" "$rc" "$out"
expect_not "and says nothing else to the person pushing" 3 "error" "$rc" "$out"
# the repository, taken from the working directory under GIT_REPO_ROOT
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
GIT_REPO_ROOT="$TMP/root" "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
expect "GIT_REPO_ROOT plus the cwd names the repository" 0 "on team" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
( cd "$TMP/root/team.git" && env -u GIT_HOST_REPO GIT_HOST_USER=bob \
GIT_REPO_ROOT=/var/lib/git "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
expect "and the last path component works when it is not the root" 0 "team" "$rc" "$out"
# ------------------------------------------- the two facts that belong to git
#
# The two questions the rest of this suite cannot reach, because there is no git
# in it. A stand-in named git answers to order, with the real one kept out of
# PATH: the host's git would answer a different question than the one asked.
mkdir -p "$TMP/fakegit"
cat >"$TMP/fakegit/git" <<'EOF'
#!/bin/sh
case $1 in
symbolic-ref) printf '%s\n' "${FAKE_HEAD:-refs/heads/main}"; exit 0 ;;
merge-base) exit "${FAKE_FF:-1}" ;;
esac
exit 125
EOF
chmod 755 "$TMP/fakegit/git"
# hookgit <user> <dir> [PATH=...] - the hook, with the fake git in front.
hookgit() {
user=$1
dir=$2
shift 2
( cd "$dir" && env "$@" GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
"$TMP/bin/pre-receive" "$dir" 2>&1 )
}
section "what the hook asks git"
POLICY="$TMP/authz.conf"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
expect "a deletion is force, not admin, when HEAD is somewhere else" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main |
hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
expect "but the branch HEAD points at may not go" 1 \
"permission denied [user=carol, obj=refs/heads/main]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main |
hookgit dave "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
expect "which is what an admin is for" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/feature/x FAKE_FF=1); rc=$?
expect "and it is HEAD that decides, not the name of the branch" 1 \
"permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=0); rc=$?
expect "an update git calls a fast-forward costs write" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
expect "and one it does not, force" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
# No git to ask: the hook must not guess in the caller's favour.
out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
hookgit carol "$TMP/root/team.git" PATH="$TMP/empty"); rc=$?
expect "with no git to ask, a deletion is the expensive kind" 1 \
"permission denied [user=carol, obj=refs/heads/feature/x]" "$rc" "$out"
out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
hookgit bob "$TMP/root/team.git" PATH="$TMP/empty"); rc=$?
expect "and an update cannot be shown to be a fast-forward" 1 \
"permission denied [user=bob, obj=refs/heads/feature/x]" "$rc" "$out"
# -------------------------------------------------------------- fail closed
section "fail closed"
printf 'alice web admin\nbroken line\n' >"$TMP/broken.conf"
out=$("$AUTHZ" --policy "$TMP/broken.conf" check alice web admin refs/heads/x 2>&1); rc=$?
expect "a policy that does not parse denies" 3 "invalid policy file" "$rc" "$out"
expect "and says which line, to whoever can read it" 3 ":2: error" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/broken.conf" \
authorize --no-git 2>&1); rc=$?
expect "and the hook denies" 3 "invalid policy file" "$rc" "$out"
expect_not "in that one line and nothing more" 3 "error" "$rc" "$out"
out=$("$AUTHZ" --policy "$TMP/no-such.conf" check alice web admin 2>&1); rc=$?
expect "a policy that is not there denies" 3 "invalid policy file" "$rc" "$out"
expect "and says that too, to whoever can read it" 3 "cannot read the policy" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/x |
env GIT_HOST_USER=alice GIT_HOST_REPO=web "$AUTHZ" --policy "$TMP/no-such.conf" \
authorize 2>&1); rc=$?
expect "and the hook denies" 3 "invalid policy file" "$rc" "$out"
expect_not "without saying why, which is not the pusher's to know" 3 "No such file" "$rc" "$out"
printf 'alice web admin\n' >"$TMP/secret.conf"
chmod 000 "$TMP/secret.conf"
if [ "$(id -u)" = 0 ]; then
skip_it "an unreadable policy denies" "running as root, who can read anything"
else
out=$("$AUTHZ" --policy "$TMP/secret.conf" check alice web admin 2>&1); rc=$?
expect "an unreadable policy denies" 3 "invalid policy file" "$rc" "$out"
fi
chmod 644 "$TMP/secret.conf"
# ---------------------------------------------------------------- validate
section "validate: the policy alone"
POLICY=$P
out=$("$AUTHZ" --policy "$P" validate 2>&1); rc=$?
expect "the fixture passes" 0 "is in force" "$rc" "$out"
expect_not "with nothing to complain about" 0 "warn " "$rc" "$out"
out=$("$AUTHZ" --policy "$TMP/broken.conf" validate 2>&1); rc=$?
expect "a broken policy is reported with its line number" 3 ":2: error" "$rc" "$out"
cat >"$TMP/noise.conf" <<'EOF'
@all web write
bob missing write refs/heads/*
EOF
out=$("$AUTHZ" --policy "$TMP/noise.conf" validate 2>&1); rc=$?
expect "warnings do not fail a validate" 0 "warning(s)" "$rc" "$out"
contains "$out" "@all the whole of web" || bad "an unscoped @all should be warned about"
out=$("$AUTHZ" --policy "$TMP/noise.conf" validate --strict 2>&1); rc=$?
expect "--strict makes warnings fatal" 1 "warning" "$rc" "$out"
out=$("$AUTHZ" --policy "$TMP/nope.conf" validate 2>&1); rc=$?
expect "a missing policy is not a valid policy" 3 "cannot be read" "$rc" "$out"
# ------------------------------------------------------------------ install
section "install"
if ! command -v install >/dev/null 2>&1 || ! command -v make >/dev/null 2>&1; then
skip_it "make install" "no make or install(1) here"
skip_it "the hook the installer leaves" "no make or install(1) here"
skip_it "an existing policy is not overwritten" "no make or install(1) here"
else
S=$TMP/stage
out=$(make install DESTDIR="$S" 2>&1); rc=$?
expect "make install into a DESTDIR" 0 "pre-receive" "$rc" "$out"
# A relative link: an absolute one points at whatever occupies that path on
# whatever machine reads it.
link=$(ls -l "$S/usr/local/share/git-host/hooks/pre-receive")
case $link in
*'= /'*) bad "the installed hook is an absolute symlink: $link" ;;
*'pre-receive -> ../../'*) ok ;;
*) bad "the installed hook is not a relative symlink: $link" ;;
esac
# Nothing here but the paths the installer chose.
mkdir -p "$S/var/lib/git/team.git"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
"$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
expect "the installed hook allows what the policy allows" 0 "-" "$rc" "$out"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/main |
( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \
GIT_REPO_ROOT="$S/var/lib/git" GIT_HOST_AUTHZ="$S/etc/git-host/authz.conf" \
"$S/usr/local/share/git-host/hooks/pre-receive" "." 2>&1 )); rc=$?
expect "and denies what it denies" 1 "permission denied" "$rc" "$out"
contains "$out" "pre-receive:" || bad "the hook should say so in its own name: $out"
printf 'someone * admin\n' >"$S/etc/git-host/authz.conf"
out=$(make install DESTDIR="$S" 2>&1); rc=$?
expect "installing again does not fail" 0 "" "$rc" "$out"
if grep -q 'someone' "$S/etc/git-host/authz.conf" &&
[ -f "$S/etc/git-host/authz.conf.new" ]; then
ok
else
bad "an existing policy was overwritten, or the new example is nowhere"
fi
fi
# ------------------------------------------------------------ the shipped policy
if [ -f authz.conf ]; then
section "the example policy that ships with it"
out=$("$AUTHZ" --policy authz.conf validate 2>&1); rc=$?
expect "it validates" 0 "is in force" "$rc" "$out"
out=$("$AUTHZ" --policy authz.conf check alice anything admin refs/heads/main 2>&1); rc=$?
expect "alice can do anything" 0 "allow" "$rc" "$out"
out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/feature/x 2>&1); rc=$?
expect "bob works on features" 0 "allow" "$rc" "$out"
out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/main 2>&1); rc=$?
expect "bob cannot touch main" 1 "but not on refs/heads/main" "$rc" "$out"
out=$("$AUTHZ" --policy authz.conf check nobody team write refs/heads/x 2>&1); rc=$?
expect "a stranger cannot" 1 "no rule" "$rc" "$out"
fi
# ------------------------------------------------------------------ summary
printf '\n%s\n' "passed $passed, failed $failed, skipped $skipped"
[ "$failed" = 0 ] || exit 1
exit 0
|