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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
test_description='Check security fixes and fork-specific behaviour'
. ./setup.sh
cgitrc_site() {
cgitrc_site_file=$1
shift
printf '%s\n' virtual-root=/ cache-size=0 "$@" >"$cgitrc_site_file"
}
expect_status() {
sed -n 1p "$2" >status-line &&
test_grep "^Status: $1 " status-line &&
grep "^Status:" "$2" >status-lines &&
test_line_count = 1 status-lines
}
cgit_path() {
CGIT_CONFIG="$PWD/$1" PATH_INFO=$2 cgit
}
cgit_ok_path() {
(
PATH_INFO=$2 &&
export PATH_INFO &&
cgit_ok "$1" ""
)
}
test_expect_success 'set up security fixtures' '
mkrepo repos/sec 1 &&
(
cd repos/sec &&
printf "# Title\n<script>alert(1)</script>\n" >README.md &&
printf "<script>alert(2)</script>\n" >readme.txt &&
printf "top\n" >afile &&
git add -A &&
git commit -m fixtures
) &&
cgitrc_repo seccgitrc sec max-blob-size=1
'
test_expect_success 'log id=--output is refused without writing a file' '
cgitrc_copy nocacherc &&
cgit_rc nocacherc "url=foo/log&id=--output=$PWD/log-output" >tmp &&
expect_status 400 tmp &&
test_grep "<div class=.error.>Invalid revision</div>" tmp &&
test_path_is_missing log-output
'
test_expect_success 'a valid id= still renders the log' '
sha=$(git -C repos/foo rev-parse HEAD) &&
cgit_ok_url "foo/log&id=$sha" >tmp &&
test_grep ">commit 5</a>" tmp
'
test_expect_success 'a small blob is still served' '
cgit_ok seccgitrc "url=sec/plain/afile" >tmp &&
strip_headers <tmp >body &&
echo top >expect &&
test_cmp expect body
'
test_expect_success 'markdown readme without a filter is escaped as plain text' '
cgitrc_site secreadmerc \
repo.url=md "repo.path=$PWD/repos/sec/.git" repo.readme=master:README.md \
repo.url=txt "repo.path=$PWD/repos/sec/.git" repo.readme=master:readme.txt &&
cgit_ok secreadmerc "url=md/about/" >tmp &&
test_grep "pre class=.plaintext.># Title$" tmp &&
test_grep "^<script>alert(1)</script>$" tmp &&
test_grep ! "<script>alert(1)</script>" tmp
'
test_expect_success 'non-markdown readme without a filter is escaped as plain text' '
cgit_ok secreadmerc "url=txt/about/" >tmp &&
test_grep "pre class=.plaintext.><script>alert(2)</script>$" tmp &&
test_grep ! "<script>alert(2)</script>" tmp
'
test_expect_success 'root-readme without a filter is its own page, as written' '
echo "<b>site</b>" >root.html &&
cgitrc_site secrootrc "root-readme=$PWD/root.html" &&
cgit_rc secrootrc "url=" >tmp &&
grep "<li><a title=.About this site. href=./?p=about.>about</a>" tmp &&
! grep "<b>site</b>" tmp &&
cgit_rc secrootrc "p=about" >tmp &&
grep "^<b>site</b>$" tmp
'
test_expect_success 'diff options submit through a button' '
sha=$(git -C repos/foo rev-parse HEAD) &&
cgit_ok_url "foo/commit&id=$sha" >tmp &&
test_grep "type=.submit. value=.reload." tmp &&
test_grep ! " on[a-z]*=" tmp
'
test_expect_success 'no page carries an inline handler' '
for page in repolist: summary:foo/ log:foo/log/ tree:foo/tree/ diff:foo/diff/ \
refs:foo/refs/
do
cgit_ok_url "${page#*:}" >tmp &&
test_grep "data-page=.${page%%:*}." tmp &&
test_grep ! " on[a-z]*=" tmp || return 1
done
'
test_expect_success 'ssdiff percent-encodes the file path in its line links' '
mkrepo repos/xss 1 &&
quoted=$(printf "x\047y.txt") &&
(
cd repos/xss &&
printf "a\nb\n" >"$quoted" &&
printf "a\nb\n" >"p#q?.txt" &&
git add -A &&
git commit -m add &&
printf "a\nc\n" >"$quoted" &&
printf "a\nc\n" >"p#q?.txt" &&
git commit -am change
) &&
cgitrc_repo xssrc xss &&
old=$(git -C repos/xss rev-parse HEAD~1) &&
new=$(git -C repos/xss rev-parse HEAD) &&
cgit_ok xssrc "url=xss/diff/&id=$new&ss=1" >tmp &&
for path in x%27y.txt p%23q%3F.txt
do
test_grep "<td class=.lineno.><a href=./xss/tree/$path?id=$old#n2.>2</a></td>" tmp &&
test_grep "<td class=.lineno.><a href=./xss/tree/$path?id=$new#n2.>2</a></td>" tmp || return 1
done
'
test_expect_success 'a message-less commit renders without crashing' '
mkrepo repos/nomsg 1 &&
(
cd repos/nomsg &&
tree=$(git write-tree) &&
printf "tree %s\nauthor a <a@b> 1735689600 +0000\ncommitter a <a@b> 1735689600 +0000\n" "$tree" >raw &&
cid=$(git hash-object -t commit -w raw) &&
git update-ref refs/heads/master "$cid"
) &&
cgitrc_repo nomsgrc nomsg &&
cgit_rc nomsgrc "url=nomsg/log/&showmsg=1" >tmp &&
grep "no commit message" tmp &&
cgit_rc nomsgrc "url=nomsg/" >tmp &&
grep "no commit message" tmp
'
test_expect_success REFFILES 'set up a repo whose branches point at odd objects' '
mkrepo repos/oddref 2 &&
(
cd repos/oddref &&
git tag -a annotated -m note &&
git rev-parse annotated >.git/refs/heads/points-at-tag &&
git rev-parse HEAD^{tree} >.git/refs/heads/points-at-tree &&
git checkout -q -b newer &&
test_tick &&
git commit -q --allow-empty -m newer &&
git checkout -q master &&
git for-each-ref refs/heads/ >refs.out &&
grep -q "tree.refs/heads/points-at-tree" refs.out &&
grep -q "tag.refs/heads/points-at-tag" refs.out
) &&
cgitrc_repo oddrefrc oddref branch-sort=age &&
cgitrc_repo oddrefnamerc oddref branch-sort=name
'
branch_row() {
grep -n "<tr class=.branch.><td><a href=[^>]*>$1</a>" "$2" | cut -d: -f1
}
test_expect_success REFFILES 'the refs, branch and summary pages sort such branches' '
for page in refs/ refs/heads/ ""
do
cgit_ok oddrefrc "url=oddref/$page" >tmp &&
test_grep "<option value=.points-at-tree.>" tmp &&
test_grep "<tr class=.branch.><td><a href=./oddref/log/?h=points-at-tag.>" tmp &&
test $(branch_row newer tmp) -lt $(branch_row master tmp) || return 1
done
'
test_expect_success REFFILES 'name-sorted branches are unaffected' '
cgit_ok oddrefnamerc "url=oddref/refs/heads/" >tmp &&
test_grep "<option value=.points-at-tree.>" tmp &&
test $(branch_row master tmp) -lt $(branch_row newer tmp) &&
test $(branch_row newer tmp) -lt $(branch_row points-at-tag tmp)
'
test_expect_success 'set up a submodule fixture with a hostile module-link' '
mkrepo repos/modlink 1 &&
sub=$(git -C repos/modlink rev-parse HEAD) &&
(
cd repos/modlink &&
git update-index --add --cacheinfo 160000,$sub,submod &&
git commit -m gitlink
) &&
mkdir -p scan &&
cp -R repos/modlink scan/modlink &&
printf "module-link=/m/%%s/%%s/%%s/%%s/%%s/%%s/%%s/%%s/%%s/%%s\n" >scan/modlink/.git/cgitrc &&
cgitrc_site modlinkrc trust-scan-config=1 "scan-path=$PWD/scan"
'
test_expect_success 'a surplus conversion is shown literally, not filled' '
sub=$(git -C scan/modlink rev-parse HEAD:submod) &&
cgit_ok modlinkrc "url=modlink/tree/" >tmp &&
test_grep "href=./m/submod/$sub/%s/%s/%s/%s/%s/%s/%s/%s.>submod</a>" tmp
'
test_expect_success 'a well-formed module-link still takes name and sha1' '
cgitrc_repo modlinkokrc modlink "module-link=/mod/%s/commit/?id=%s" &&
cgit_rc modlinkokrc "url=modlink/tree/" >tmp &&
grep "href=./mod/submod/commit/?id=$sub." tmp
'
test_expect_success 'a per-path module-link takes only the sha1' '
cgitrc_repo modlinkpathrc modlink "repo.module-link.submod=https://example.com/s/?id=%s" &&
cgit_rc modlinkpathrc "url=modlink/tree/" >tmp &&
grep "href=.https://example.com/s/?id=$sub." tmp
'
test_expect_success 'a doubled percent in a module-link renders as one' '
cgitrc_repo modlinkpctrc modlink "module-link=/m/%s/%%/%s" &&
cgit_rc modlinkpctrc "url=modlink/tree/" >tmp &&
grep "href=./m/submod/%/$sub." tmp
'
test_expect_success 'an unknown page under a repository answers 404 with its header' '
cgitrc_copy nocacherc &&
cgit_rc nocacherc "url=foo/nonsense/" >tmp &&
expect_status 404 tmp &&
test_grep "Invalid request" tmp &&
test_grep "<select name=.h. " tmp
'
test_expect_success 'a clone endpoint with http clone off answers the same way' '
cgitrc_copy noclonerc enable-http-clone=0 &&
cgit_rc noclonerc "url=foo/info/refs" >tmp &&
expect_status 404 tmp &&
test_grep "Invalid request" tmp &&
test_grep "<select name=.h. " tmp
'
test_expect_success 'set up a hostile .gitmodules' '
(
cd repos/modlink &&
tip=$(git rev-parse HEAD) &&
for path in quoted upper data vbs file
do
git update-index --add --cacheinfo 160000,$tip,$path || return 1
done &&
cat >.gitmodules <<-EOF &&
[submodule "submod"]
path = submod
url = javascript:alert(1)
[submodule "upper"]
path = upper
url = JavaScript:alert(1)
[submodule "data"]
path = data
url = data:text/html,x
[submodule "vbs"]
path = vbs
url = vbscript:x
[submodule "file"]
path = file
url = file:///etc/passwd
[submodule "quoted"]
path = quoted
url = https://example.com/x'\''><script>alert(1)</script>
EOF
git add .gitmodules &&
git commit -m hostile
) &&
cgitrc_repo modlinkgmrc modlink enable-gitmodules-links=1
'
test_expect_success 'only a web url reaches an href, escaped' '
cgit_ok modlinkgmrc "url=modlink/tree/" >tmp &&
test_grep "class=.ls-mod. title=.javascript:alert(1).>submod</span>" tmp &&
test_grep "class=.ls-mod. title=.JavaScript:alert(1).>upper</span>" tmp &&
test_grep "class=.ls-mod. title=.data:text/html,x.>data</span>" tmp &&
test_grep "class=.ls-mod. title=.vbscript:x.>vbs</span>" tmp &&
test_grep "class=.ls-mod. title=.file:///etc/passwd.>file</span>" tmp &&
test_grep ! -i -e "href=.javascript:" -e "href=.data:" -e "href=.vbscript:" -e "href=.file:" tmp &&
test_grep "href=.https://example.com/x'><script>alert(1)</script>.>quoted</a>" tmp &&
test_grep ! "<script>alert" tmp
'
test_expect_success 'set up the advisory fixtures' '
mkrepo repos/cve 1 &&
(
cd repos/cve &&
printf "x\n" >"$(printf "crlf\r\nX-Injected: 1.txt")" &&
printf "x\n" >"<script>alert(1)x.txt" &&
printf "x\n" >"\$(touch pwned).c" &&
git add -A &&
git commit -m names &&
parent=$(git rev-parse HEAD) &&
echo noname >noname.txt &&
git add noname.txt &&
tree=$(git write-tree) &&
git rm -q --cached noname.txt &&
rm noname.txt &&
printf "tree %s\nparent %s\n" "$tree" "$parent" >raw &&
printf "author <> 1735689600 +0000\ncommitter <> 1735689600 +0000\n\nempty author\n" >>raw &&
cid=$(git hash-object --literally -t commit -w raw) &&
git update-ref refs/heads/noname "$cid"
) &&
mkdir -p readme &&
printf "readme text\n" >readme/README &&
write_script auth.sh <<-\EOF &&
case "$1" in
authenticate-post)
wc -c >received
printf "Status: 302 Found\nLocation: /\n\n"
;;
esac
exit 1
EOF
cgitrc_repo cverc cve "auth-filter=exec:$PWD/auth.sh" "repo.readme=$PWD/readme/README" "repo.source-filter=exec:$FILTER_DIRECTORY/dump.sh"
'
test_expect_success 'a newline in a file name cannot split the headers' '
cgit_rc cverc "url=cve/plain/crlf%0d%0aX-Injected:%201.txt" >tmp &&
expect_status 200 tmp &&
! grep "^X-Injected" tmp &&
grep "^Content-Disposition: inline; filename=.crlf\\\\r\\\\nX-Injected: 1.txt.$" tmp
'
test_expect_success 'a posted length past any buffer is clamped' '
test_seq 2000 >body &&
for length in 99999999999999 -1
do
rm -f received &&
CGIT_CONFIG="$PWD/cverc" REQUEST_METHOD=POST CONTENT_LENGTH=$length \
QUERY_STRING="p=login" cgit <body >tmp &&
expect_status 302 tmp &&
test $(cat received) = 4096 || return 1
done &&
CGIT_CONFIG="$PWD/cverc" REQUEST_METHOD=POST CONTENT_LENGTH=10 \
QUERY_STRING="p=login" cgit <body >tmp &&
test $(cat received) = 10
'
test_expect_success 'the dumb transport serves nothing but objects and packs' '
echo "$PWD/repos/foo/.git/objects" >repos/cve/.git/objects/info/alternates &&
test_when_finished "rm repos/cve/.git/objects/info/alternates" &&
for path in ../config info/../../config info/alternates info//alternates \
info/./alternates ./info/alternates info/Alternates /info/alternates
do
cgit_rc cverc "url=cve/objects/$path" >tmp &&
expect_status 404 tmp &&
test_grep ! -e repositoryformatversion -e repos/foo tmp || return 1
done &&
cgit_ok cverc "url=cve/HEAD" >tmp &&
test_grep "^ref: refs/heads/master$" tmp &&
blob=$(git -C repos/cve rev-parse HEAD:file-1) &&
cgit_ok cverc "url=cve/objects/$(test_oid_to_path $blob)" >tmp &&
test_grep "^Content-Type: application/octet-stream$" tmp
'
test_expect_success 'a path below the about page answers 404 for every kind of readme' '
mkrepo repos/about 1 &&
(
cd repos/about &&
echo "readme in the tree" >README &&
echo "beside the readme" >notes.txt &&
git add README notes.txt &&
git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
git commit -q -m readme
) &&
echo "readme in the repository" >repos/about/.git/README &&
mkdir ondisk &&
echo "readme on the disk" >ondisk/README &&
cgitrc_site aboutrc mimetype.png=image/png "root-readme=$PWD/ondisk/README" \
repo.url=disk "repo.path=$PWD/repos/about/.git" "repo.readme=$PWD/ondisk/README" \
repo.url=gitdir "repo.path=$PWD/repos/about/.git" repo.readme=README \
repo.url=branch "repo.path=$PWD/repos/about/.git" repo.readme=master:README \
repo.url=head "repo.path=$PWD/repos/about/.git" repo.readme=:README &&
while read about path readme
do
cgit_ok aboutrc "$about" >tmp &&
test_grep "$readme" tmp &&
cgit_rc aboutrc "$about$path" >tmp 2>err &&
expect_status 404 tmp &&
test_grep "^Content-Type: text/html" tmp &&
test_grep ! -e virtual-root -e repositoryformatversion -e "beside the readme" tmp &&
test_must_be_empty err || return 1
done <<-\EOF
url=disk/about/ ../aboutrc readme on the disk
url=gitdir/about/ config readme in the repository
url=branch/about/ notes.txt readme in the tree
url=branch/about/ sub readme in the tree
url=branch/about/ missing.png readme in the tree
url=head/about/ notes.txt readme in the tree
p=about&path= ../aboutrc readme on the disk
EOF
'
test_expect_success 'a commit with an empty author renders everywhere' '
cgit_ok cverc "url=cve/log/&h=noname" >tmp &&
test_grep ">empty author</a>" tmp &&
cgit_ok cverc "url=cve/commit/&h=noname" >tmp &&
test_grep "<div class=.commit-subject.>empty author<" tmp &&
for page in "" refs/
do
cgit_ok cverc "url=cve/$page" >tmp &&
test_grep "<a href=./cve/commit/?h=noname.>empty author</a></td><td class=.col-author.></td>" tmp || return 1
done &&
cgit_ok cverc "url=cve/patch/&h=noname" >tmp &&
test_grep "^From: <>$" tmp &&
test_grep "^Subject: empty author$" tmp &&
cgit_ok cverc "url=cve/atom/&h=noname" >tmp &&
test_grep "<name>unknown</name>" tmp &&
test_grep ! "<email></email>" tmp
'
test_expect_success 'a percent sign without digits behind it is read as itself' '
cgit_ok cverc "url=cve/log/&q=%" >tmp &&
test_grep "name=.q. aria-label=.Search. value=.%.>" tmp &&
cgit_ok cverc "url=cve/log/&q=%zz%2" >tmp &&
test_grep "name=.q. aria-label=.Search. value=.%zz%2.>" tmp
'
test_expect_success 'script in a file name is escaped on the diff page' '
cgit_rc cverc "url=cve/diff/" >tmp &&
! grep "<script>alert" tmp &&
grep "<script>alert(1)x.txt" tmp
'
test_expect_success 'shell syntax in a file name reaches a filter as a plain argument' '
cgit_ok cverc "url=cve/tree/%24(touch%20pwned).c" >tmp &&
test_grep -F "\$(touch pwned).c X" tmp &&
test_path_is_missing pwned &&
test_path_is_missing repos/cve/pwned
'
test_expect_success 'the advisory payloads fail the same way through PATH_INFO' '
cgit_ok_path cverc "/cve/plain/$(printf "crlf\r\nX-Injected: 1.txt")" >tmp &&
test_grep ! "^X-Injected" tmp &&
test_grep "^Content-Disposition: inline; filename=.crlf\\\\r\\\\nX-Injected: 1.txt.$" tmp &&
for path in /cve/objects/../config /cve/objects/info/../../config /cve/about/../cverc
do
cgit_path cverc $path >tmp &&
expect_status 404 tmp &&
test_grep ! -e repositoryformatversion -e virtual-root tmp || return 1
done &&
cgit_ok_path cverc /cve/HEAD >tmp &&
test_grep "^ref: refs/heads/master$" tmp &&
cgit_ok_path cverc /cve/about/ >tmp &&
test_grep "readme text" tmp &&
cgit_ok_path cverc "/cve/tree/\$(touch pwned).c" >tmp &&
test_grep -F "\$(touch pwned).c X" tmp &&
test_path_is_missing pwned &&
test_path_is_missing repos/cve/pwned
'
test_expect_success 'a url opening with slashes links only below the site' '
cgitrc_copy slashrc &&
cgit_rc slashrc "url=//evil.example/" >tmp &&
test_grep "action=./evil.example/.>" tmp &&
test_grep ! -e "href=.//" -e "action=.//" tmp &&
cgit_path slashrc //evil.example/ >tmp &&
test_grep "action=./evil.example/.>" tmp &&
test_grep ! -e "href=.//" -e "action=.//" tmp &&
cgit_ok slashrc "url=//f" >tmp &&
test_grep "action=./f.>" tmp &&
cgit_ok_path slashrc //f >tmp &&
test_grep "action=./f.>" tmp
'
test_expect_success 'repository bytes go out under a policy that loads nothing' '
mkrepo repos/svg 1 &&
test_commit -C repos/svg --no-tag svg x.svg \
"<svg xmlns=\"http://www.w3.org/2000/svg\"><script>alert(1)</script></svg>" &&
cgitrc_repo svgrc svg mimetype.svg=image/svg+xml &&
cgit_ok svgrc "url=svg/plain/x.svg" >tmp &&
test_grep "^Content-Type: image/svg+xml;" tmp &&
test_grep "<script>alert(1)</script>" tmp &&
for page in plain/x.svg blob/x.svg
do
cgit_ok svgrc "url=svg/$page" >tmp &&
test_grep "^X-Content-Type-Options: nosniff$" tmp &&
test_grep "^Content-Security-Policy: default-src .none.$" tmp || return 1
done &&
cgitrc_copy lscacherc &&
cgit_ok lscacherc "p=ls_cache" >tmp &&
test_grep "^Content-Type: text/plain;" tmp &&
test_grep "^X-Content-Type-Options: nosniff$" tmp &&
test_grep "^Content-Security-Policy: default-src .none.$" tmp
'
test_expect_success 'set up a repository whose names and messages hold markup' '
mkrepo repos/mk 1 &&
(
cd repos/mk &&
echo hi >"$(printf "f\047\"<x>&.txt")" &&
git add -A &&
GIT_AUTHOR_NAME="au & \"q\" '\''s" GIT_COMMITTER_NAME="co & \"q\" '\''s" \
git commit -m "subject <x> & \"q\" '\''s" -m "body <x> & \"q\" '\''s" &&
git branch "br'\''\"<x>&" &&
GIT_COMMITTER_NAME="tagger & \"q\" t" git tag -a -m "tag <x> & \"q\"" "tg'\''\"<x>&" &&
git notes add -m "note <x> & \"q\""
) &&
cgitrc_repo mkrc mk snapshots=tar.gz
'
test_expect_success 'names and messages holding markup are escaped on every page' '
cgit_ok mkrc "url=mk/" >tmp &&
test_grep "<option value=.br'"<x>&.>br.\"<x>&</option>" tmp &&
test_grep "<a href=./mk/log/?h=br%27%22%3Cx%3E%26.>br.\"<x>&</a>" tmp &&
test_grep "<a href=./mk/tag/?h=tg%27%22%3Cx%3E%26.>tg.\"<x>&</a>" tmp &&
test_grep "<a href=./mk/snapshot/mk-tg%27%22%3Cx%3E%26.tar.gz.>mk-tg.\"<x>&.tar.gz</a>" tmp &&
test_grep "<td class=.col-author.>au & \"q\" .s</td>" tmp &&
test_grep "<td class=.col-author.>tagger & \"q\" t</td>" tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/log/" >tmp &&
test_grep ">subject <x> & \"q\" .s</a>" tmp &&
test_grep "<a class=.branch-deco. href=./mk/log/?h=br%27%22%3Cx%3E%26.>br.\"<x>&</a>" tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/commit/" >tmp &&
test_grep "<div class=.commit-subject.>subject <x> & \"q\" .s<" tmp &&
test_grep "<div class=.commit-msg.>body <x> & \"q\" .s" tmp &&
test_grep "<div class=.notes.>note <x> & \"q\"" tmp &&
test_grep "co & \"q\" .s" tmp &&
test_grep "<div class=.file. data-path=.f'"<x>&.txt.>" tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/commit/&dt=1" >tmp &&
test_grep "<tbody class=.file. data-path=.f'"<x>&.txt.>" tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/tag/&h=tg%27%22%3Cx%3E%26" >tmp &&
test_grep "<div class=.commit-subject.>tag <x> & \"q\"</div>" tmp &&
test_grep "tagger & \"q\" t" tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/atom/" >tmp &&
test_grep "<title>subject <x> & \"q\" .s</title>" tmp &&
test_grep "<content type=.text.>body <x> & \"q\" .s" tmp &&
test_grep "<name>au & \"q\" .s</name>" tmp &&
test_grep ! "<x>" tmp
'
test_expect_success 'request values echoed into a page are escaped' '
payload=%27%3E%3Cs%3E &&
escaped="'><s>" &&
cgit_ok mkrc "url=mk/log/&q=$payload&qt=$payload&id=$payload" >tmp &&
test_grep "name=.q. aria-label=.Search. value=.$escaped.>" tmp &&
for name in q qt id
do
test_grep "<input type=.hidden. name=.$name. value=.$escaped.>" tmp || return 1
done &&
test_grep ! "<s>" tmp &&
cgit_ok mkrc "url=mk/log/&h=br%27%22%3Cx%3E%26" >tmp &&
test_grep "<input type=.hidden. name=.h. value=.br'"<x>&.>" tmp &&
test_grep ! "<x>" tmp &&
cgit_rc mkrc "url=mk/log/&h=%3Cb%3E" >tmp &&
test_grep "<div class=.error.>Invalid branch: <b></div>" tmp &&
cgit_rc mkrc "url=mk/snapshot/%3Cb%3E.zzz" >tmp &&
test_grep "<div class=.error.>Unsupported snapshot format: <b>.zzz</div>" tmp &&
cgit_rc mkrc "url=mk/blob/%3Cb%3E" >tmp &&
test_grep "<div class=.error.>Path not found: <b></div>" tmp &&
test_grep ! "<b>" tmp &&
(
HTTP_HOST="h'\''\"<x>" &&
export HTTP_HOST &&
cgit_ok mkrc "url=mk/" >tmp &&
test_grep "<link rel=.alternate. title=.Atom feed. href=.http://h'"<x>/mk/atom/?h=master." tmp &&
test_grep ! "<x>" tmp &&
cgit_ok mkrc "url=mk/atom/" >tmp &&
test_grep "<id>http://h..<x>/mk/atom/</id>" tmp &&
test_grep "<link rel=.alternate. type=.text/html. href=.http://h'"<x>/mk/./>" tmp &&
test_grep ! "<x>" tmp
)
'
test_expect_success 'a requested download name cannot split the headers' '
crlf=crlf%0d%0aX-Injected:%201 &&
cgit_ok mkrc "url=mk/snapshot/$crlf.tar.gz&h=master" >tmp &&
sed -n "1,/^\$/p" tmp >headers &&
test_grep "^Content-Type: application/gzip$" headers &&
test_grep "^Content-Disposition: inline; filename=.crlf\\\\r\\\\nX-Injected: 1.tar.gz.$" headers &&
test_grep ! "^X-Injected" headers &&
blob=$(git -C repos/mk rev-parse HEAD:file-1) &&
cgit_ok mkrc "url=mk/blob/&id=$blob&path=$crlf" >tmp &&
sed -n "1,/^\$/p" tmp >headers &&
test_grep "^Content-Disposition: inline; filename=.crlf\\\\r\\\\nX-Injected: 1.$" headers &&
test_grep ! "^X-Injected" headers
'
test_expect_success 'a redirect keeps every query byte inside its Location' '
cgitrc_copy redirectrc &&
query=$(printf "x=a b\tc\r\nX-Injected: 1&y=\303\251\177") &&
cgit_rc redirectrc "url=foo/about&$query" >tmp &&
sed -n "1,/^\$/p" tmp >headers &&
expect_status 302 headers &&
test_grep "^Location: /foo/?x=a%20b%09c%0D%0AX-Injected:%201&y=%C3%A9%7F$" headers &&
test_grep ! "^X-Injected" headers
'
test_expect_success 'robots.txt turns crawlers away from snapshots and blame' '
grep -v "^#" "$TEST_OUTPUT_DIRECTORY/../assets/robots.txt" >rules &&
cat >expect <<-\EOF &&
User-agent: *
Disallow: /*/snapshot/
Disallow: /*/blame/
EOF
test_cmp expect rules
'
test_expect_success 'a file name of stray bytes is linked percent-encoded' '
mkrepo repos/bytes 1 &&
blob=$(git -C repos/bytes rev-parse HEAD:file-1) &&
git -C repos/bytes update-index --add --cacheinfo "100644,$blob,$(printf "bad\377name")" &&
git -C repos/bytes update-index --add --cacheinfo "100644,$blob,$(printf "ctl\001name")" &&
git -C repos/bytes commit -q -m bytes &&
cgitrc_repo bytesrc bytes &&
cgit_ok bytesrc "url=bytes/tree/" >tmp &&
test_grep "href=./bytes/tree/bad%FFname.>" tmp &&
test_grep "href=./bytes/tree/ctl%01name.>" tmp &&
cgit_ok bytesrc "url=bytes/tree/bad%FFname" >tmp &&
test_grep "<div class=.blob-header.>blob: $blob " tmp
'
test_expect_success 'a UTF-8 file name is shown as it is and linked percent-encoded' '
mkrepo repos/utf8 1 &&
name=$(printf "caf\303\251.txt") &&
encoded=caf%C3%A9.txt &&
for content in bonjour "au revoir"
do
blob=$(echo "$content" | git -C repos/utf8 hash-object -w --stdin) &&
git -C repos/utf8 update-index --add --cacheinfo "100644,$blob,$name" &&
git -C repos/utf8 commit -q -m "$content" || return 1
done &&
old=$(git -C repos/utf8 rev-parse HEAD~1) &&
new=$(git -C repos/utf8 rev-parse HEAD) &&
cgitrc_repo utf8rc utf8 &&
cgit_ok utf8rc "url=utf8/tree/" >tmp &&
test_grep "<a class=.ls-blob ext-txt. href=./utf8/tree/$encoded.>$name</a>" tmp &&
test_grep "<a class=.button. href=./utf8/plain/$encoded.>plain</a>" tmp &&
test_grep ! "href=.[^>]*$name" tmp &&
cgit_ok utf8rc "url=utf8/tree/$encoded" >tmp &&
test_grep "<title>$name - utf8 - " tmp &&
test_grep "/<a href=./utf8/tree/$encoded.>$name</a></span></nav>" tmp &&
test_grep "(<a href=./utf8/plain/$encoded.>plain</a>)</div>" tmp &&
test_grep "<code>au revoir$" tmp &&
test_grep ! "href=.[^>]*$name" tmp &&
cgit_ok utf8rc "url=utf8/plain/$encoded" >tmp &&
test_grep "^Content-Disposition: inline; filename=.$name.$" tmp &&
strip_headers <tmp >body &&
echo "au revoir" >expect &&
test_cmp expect body &&
cgit_ok utf8rc "url=utf8/diff/" >tmp &&
test_grep "<td class=.upd.><a href=./utf8/diff/$encoded.>$name</a>" tmp &&
test_grep "<div class=.file. data-path=.$name.><div class=.head.>diff --git a/$name b/$name<br>" tmp &&
test_grep "<br>--- a/<a href=./utf8/tree/$encoded?id=$old.>$name</a><br>+++ b/<a href=./utf8/tree/$encoded?id=$new.>$name</a>" tmp &&
test_grep ! "href=.[^>]*$name" tmp
'
test_expect_success 'a UTF-8 repository url is shown as it is and linked percent-encoded' '
url=$(printf "d\303\251p\303\264t") &&
encoded=d%C3%A9p%C3%B4t &&
cgitrc_site utf8urlrc "repo.url=$url" "repo.path=$PWD/repos/foo/.git" repo.desc=accented &&
cgit_ok utf8urlrc "url=" >tmp &&
test_grep "<td class=.col-name toplevel-repo.><a href=./$encoded/.>$url</a></td><td class=.col-desc.><a href=./$encoded/.>accented</a>" tmp &&
test_grep ! "href=.[^>]*$url" tmp &&
cgit_ok utf8urlrc "url=$encoded/" >tmp &&
test_grep "<title>$url - Git repository browser</title>" tmp &&
test_grep "<div id=.cgit. data-page=.summary. data-repo=.$url.>" tmp &&
test_grep "<h1 class=.main.><a href=./.>index</a> : <a href=./$encoded/.>$url</a></h1>" tmp &&
test_grep "<a title=.Commit history. href=./$encoded/log/.>log</a>" tmp &&
test_grep "<tr class=.branch.><td><a href=./$encoded/log/.>master</a>" tmp &&
test_grep ! "href=.[^>]*$url" tmp &&
cgit_ok_path utf8urlrc "/$url/" >tmp &&
test_grep "<h1 class=.main.><a href=./.>index</a> : <a href=./$encoded/.>$url</a></h1>" tmp
'
test_done
|