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
|
test_description='Check content on plain page'
. ./setup.sh
test_expect_success 'generate foo/plain/file-1' '
cgit_ok_url "foo/plain/file-1" >tmp
'
test_expect_success 'a text file is served as text/plain' '
head -n 1 tmp | grep "Status: 200 OK" &&
sed "/^$/q" tmp | grep "^Content-Type: text/plain; charset=UTF-8$"
'
test_expect_success 'the body carries the blob byte for byte' '
strip_headers <tmp >body &&
git -C repos/foo show master:file-1 >expect &&
test_cmp expect body
'
test_expect_success 'a plain file is served under the untrusted headers' '
cgit_ok_url "foo/plain/file-1" >tmp &&
sed "/^$/q" tmp >headers &&
test_grep "^X-Content-Type-Options: nosniff$" headers &&
test_grep "^Content-Security-Policy: default-src .none.$" headers &&
cgitrc_copy servingrc enable-html-serving=1 &&
cgit_ok servingrc "url=foo/plain/file-1" >tmp &&
sed "/^$/q" tmp >headers &&
test_grep "^Content-Type: text/plain" headers &&
test_grep ! "^X-Content-Type-Options:" headers &&
test_grep ! "^Content-Security-Policy:" headers
'
test_expect_success 'a missing path is a 404' '
cgit_url "foo/plain/no-such-file" >tmp &&
head -n 1 tmp | grep "Status: 404"
'
test_expect_success 'a directory answers with a listing of links' '
cgit_ok_url "foo/plain" >tmp &&
test_grep "<li><a href=./foo/plain/file-1.>file-1</a></li>" tmp &&
test_grep "<li><a href=./foo/plain/file-5.>file-5</a></li>" tmp
'
test_expect_success 'a folder listing links its entries and its parent' '
test_create_repo repos/folders &&
(
cd repos/folders &&
mkdir -p dir/sub &&
echo a >dir/a.txt &&
echo b >dir/sub/b.txt &&
git add dir &&
git commit -m folders
) &&
cgitrc_repo foldersrc folders &&
cgit_ok foldersrc "url=folders/plain/dir" >tmp &&
test_grep "<title>/dir/</title>" tmp &&
test_grep "<li><a href=./folders/plain/.>\.\./</a></li>" tmp &&
test_grep "<li><a href=./folders/plain/dir/a.txt.>a.txt</a></li>" tmp &&
test_grep "<li><a href=./folders/plain/dir/sub/.>sub</a></li>" tmp &&
cgit_ok foldersrc "url=folders/plain/dir/sub" >tmp &&
test_grep "<li><a href=./folders/plain/dir/.>\.\./</a></li>" tmp &&
test_grep "<li><a href=./folders/plain/dir/sub/b.txt.>b.txt</a></li>" tmp
'
test_expect_success 'a blob whose bytes look binary is served as octet-stream' '
(
cd repos/foo &&
printf "\000\001\002" >blob.bin &&
git add blob.bin &&
git commit -m binary
) &&
cgit_ok_url "foo/plain/blob.bin" >tmp &&
sed "/^$/q" tmp | grep "^Content-Type: application/octet-stream$"
'
test_expect_success 'only text and xml types carry a charset' '
(
cd repos/foo &&
printf "PK" >doc.docx &&
printf "<svg/>" >pic.svg &&
git add doc.docx pic.svg &&
git commit -m types
) &&
docx=application/vnd.openxmlformats-officedocument. &&
docx=${docx}wordprocessingml.document &&
cgitrc_copy typesrc enable-html-serving=1 "mimetype.docx=$docx" \
mimetype.svg=image/svg+xml &&
cgit_ok typesrc "url=foo/plain/doc.docx" >tmp &&
sed "/^$/q" tmp | grep "^Content-Type: $docx$" &&
cgit_ok typesrc "url=foo/plain/pic.svg" >tmp &&
sed "/^$/q" tmp | grep "^Content-Type: image/svg+xml; charset=UTF-8$"
'
test_expect_success 'plain answers once for duplicate tree entries' '
test_create_repo repos/dup &&
(
cd repos/dup &&
blob=$(printf PNGDATA | git hash-object -w --stdin) &&
inner=$(printf "100644 blob %s\\tinside\\n" $blob | git mktree) &&
top=$(printf "100644 blob %s\\tx.png\\n040000 tree %s\\tx.png\\n" $blob $inner | git mktree --missing) &&
c=$(echo dup | git commit-tree $top) &&
git update-ref refs/heads/master $c
) &&
cgitrc_repo duprc dup mimetype.png=image/png &&
cgit_ok duprc "url=dup/plain/x.png" >tmp &&
test $(grep -c "^Status:" tmp) = 1 &&
test $(grep -c "^Content-Type:" tmp) = 1 &&
grep "^Content-Type: image/png" tmp
'
test_expect_success 'a control character in a file name stays out of the headers' '
(
cd repos/dup &&
blob=$(printf x | git hash-object -w --stdin) &&
top=$(printf "100644 blob %s\\tctl\\001name.txt\\n" $blob | git mktree) &&
c=$(echo ctl | git commit-tree $top) &&
git update-ref refs/heads/ctl $c
) &&
cgit_ok duprc "url=dup/plain/ctl%01name.txt&h=ctl" >tmp &&
grep "^Content-Disposition: inline; filename=.ctl?name.txt.$" tmp
'
test_done
|