blob: b62fc62d17dedee12d20870c3cfa6078f8c3985b (plain) (blame)
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
#!/bin/sh

# Collects the regression tests for the security fixes and for the behaviour
# this fork adds on top of upstream cgit. Each case builds the smallest
# repository and config that reproduce the original problem and then asks
# for the page that used to mishandle it.

test_description='Check security fixes and fork-specific behaviour'
. ./setup.sh

# Some cases below share one repository and one config, so the fixture
# carries everything they need at once, readmes holding markup that must not
# reach the page as markup and a subdirectory to sort ahead of the files.
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 &&
		mkdir zsub &&
		printf "inner\n" >zsub/inner &&
		git add -A &&
		git commit -m fixtures
	) &&
	cgitrc_repo seccgitrc sec max-blob-size=1 enable-tree-group-dirs=1
'

# A revision beginning with a dash reaches git as an option rather than as a
# tip, so a request for id=--output=<path> could create or truncate any file
# the server is able to write.
test_expect_success 'log id=--output does not write a file' '
	cgit_url "foo/log&id=--output=$PWD/pwned" >tmp 2>&1 &&
	! test -e pwned
'

test_expect_success 'log id=--output is rejected as an invalid revision' '
	grep -i "invalid revision" tmp
'

test_expect_success 'a valid id= still renders the log' '
	sha=$(git -C repos/foo rev-parse HEAD) &&
	cgit_url "foo/log&id=$sha" >tmp &&
	grep -i "commit 5" tmp
'

test_expect_success 'a small blob is still served' '
	cgit_rc seccgitrc "url=sec/plain/afile" >tmp &&
	grep -F top tmp
'

# A readme is repository content, so with no about filter configured it has
# to reach the page escaped instead of as live markup, whatever its name
# suggests about the format.
test_expect_success 'markdown readme without a filter is escaped as plain text' '
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "repo.url=md" &&
		echo "repo.path=$PWD/repos/sec/.git" &&
		echo "repo.readme=master:README.md" &&
		echo "repo.url=txt" &&
		echo "repo.path=$PWD/repos/sec/.git" &&
		echo "repo.readme=master:readme.txt"
	} >secreadmerc &&
	cgit_rc secreadmerc "url=md/about/" >tmp &&
	grep "pre class=.plaintext." tmp &&
	grep "&lt;script&gt;" tmp &&
	! grep "<script>alert(1)</script>" tmp
'

test_expect_success 'non-markdown readme without a filter is escaped as plain text' '
	cgit_rc secreadmerc "url=txt/about/" >tmp &&
	grep "pre class=.plaintext." tmp &&
	grep "&lt;script&gt;" tmp &&
	! grep "<script>alert(2)</script>" tmp
'

# root-readme is the site's own file rather than repository content, so it
# goes out as written, on an about page of its own.
test_expect_success 'root-readme without a filter is its own page, as written' '
	echo "<b>site</b>" >root.html &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "root-readme=$PWD/root.html"
	} >secrootrc &&
	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
'

# A Content-Security-Policy without unsafe-inline stops an inline onchange
# handler from ever running, so the option form submits through a plain
# button and its selects carry no handlers at all.
test_expect_success 'diff options submit through a button' '
	sha=$(git -C repos/foo rev-parse HEAD) &&
	cgit_url "foo/commit&id=$sha" >tmp &&
	grep "type=.submit. value=.reload." tmp &&
	! grep "onchange" tmp
'

# Grouping directories ahead of files is behaviour this fork adds, so nothing
# upstream covers it.
test_expect_success 'tree groups directories before files' '
	cgit_rc seccgitrc "url=sec/tree/" >tmp &&
	dirline=$(grep -n "tree/zsub" tmp | head -1 | cut -d: -f1) &&
	fileline=$(grep -n "tree/afile" tmp | head -1 | cut -d: -f1) &&
	test -n "$dirline" &&
	test -n "$fileline" &&
	test "$dirline" -lt "$fileline"
'

# A file name is repository content and may hold a quote, which would break
# out of the href attribute on the line number links of a side-by-side diff,
# so the path is percent-encoded on its way into them.
test_expect_success 'ssdiff percent-encodes a quoted file path' '
	mkrepo repos/xss 1 &&
	name=$(printf "x\047y.txt") &&
	(
		cd repos/xss &&
		printf "a\nb\n" >"$name" &&
		git add -A &&
		git commit -m add &&
		printf "a\nc\n" >"$name" &&
		git commit -am change
	) &&
	cgitrc_repo xssrc xss &&
	sha=$(git -C repos/xss rev-parse HEAD) &&
	cgit_rc xssrc "url=xss/diff/&id=$sha&ss=1" >tmp &&
	grep "tree/x%27y.txt" tmp &&
	! grep "href=.[^>]*x.y.txt.[^>]*>" tmp
'

# git itself will write a commit with an empty message, so the log and the
# summary both have to have something to print where the subject goes.
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
'

# A branch need not point at a commit. struct refinfo keeps taginfo and
# commitinfo in a union and fills only the member matching the object type,
# so sorting branches through the commit member read past the end of the
# smaller taginfo, and read NULL for a tree, which crashed. git update-ref
# refuses to create such a ref, hence the loose files written by hand below,
# and a repository is only files on disk so cgit meets whatever is there.
test_expect_success '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 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
'

test_expect_success 'the refs, branch and summary pages sort such branches' '
	for page in refs/ refs/heads/ ""
	do
		cgit_rc oddrefrc "url=oddref/$page" >tmp &&
		grep "points-at-tree" tmp &&
		grep "</html>" tmp || return 1
	done
'

test_expect_success 'name-sorted branches are unaffected' '
	sed "s/^branch-sort=age$/branch-sort=name/" oddrefrc >oddrefnamerc &&
	cgit_rc oddrefnamerc "url=oddref/refs/heads/" >tmp &&
	grep "points-at-tree" tmp &&
	grep "</html>" tmp
'

# module-link is a template and a trusted scan lets a repository set its own
# from a cgitrc in the tree, so handing that string to printf let the owner
# of a scanned repository crash the process, or read stack memory into the
# served page, merely by adding conversions past the two that are filled.
# The tests after the fixture also pin down the templates that must keep
# working.
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 &&
	{
		echo "virtual-root=/" &&
		echo "cache-size=0" &&
		echo "trust-scan-config=1" &&
		echo "scan-path=$PWD/scan"
	} >modlinkrc
'

test_expect_success 'a surplus conversion does not crash the tree view' '
	cgit_rc modlinkrc "url=modlink/tree/" >tmp &&
	grep "ls-mod" tmp
'

test_expect_success 'a surplus conversion is shown literally, not filled' '
	grep "href=./m/submod/[0-9a-f]*/%s/%s/" 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
'

# An unknown page under a repository used to reach the page header with no
# head resolved and crash in the branch switcher.
test_expect_success 'an unknown page under a repository answers 404 with its header' '
	cgit_url "foo/nonsense/" >tmp &&
	grep "^Status: 404 Not Found" tmp &&
	grep "Invalid request" tmp &&
	grep "<select name=.h. " tmp
'

test_expect_success 'a clone endpoint with http clone off answers the same way' '
	{
		echo "enable-http-clone=0" &&
		cat cgitrc
	} >noclonerc &&
	cgit_rc noclonerc "url=foo/info/refs" >tmp &&
	grep "^Status: 404 Not Found" tmp &&
	grep "<select name=.h. " tmp
'

# The .gitmodules file is commit-controlled content, so a derived link may
# carry any scheme and any byte an author can commit. Only http and https
# may reach an href, and everything lands attribute-escaped.
test_expect_success 'set up a hostile .gitmodules' '
	(
		cd repos/modlink &&
		sub=$(git rev-parse HEAD) &&
		git update-index --add --cacheinfo 160000,$sub,quoted &&
		cat >.gitmodules <<-EOF &&
		[submodule "submod"]
			path = submod
			url = javascript:alert(1)
		[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 'a javascript url never reaches an href' '
	cgit_rc modlinkgmrc "url=modlink/tree/" >tmp &&
	! grep "href=.javascript:" tmp &&
	grep "class=.ls-mod. title=.javascript:alert(1).>submod</span>" tmp
'

test_expect_success 'a quote in a web url cannot break out of the href' '
	! grep "<script>alert" tmp &&
	grep "href=.https://example.com/x&#x27;&gt;&lt;script&gt;" tmp
'

# The classes behind cgit's published advisories, each pinned against the
# current code: a newline in a file name splitting the headers, a posted
# length overflowing its buffer, a path climbing out on the dumb transport
# and the about page, a commit with an empty author, a percent sign with no
# digits behind it, script in a file name shown by the diff, and shell
# syntax in a file name handed to a filter.
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 &&
		tree=$(git rev-parse HEAD^{tree}) &&
		printf "tree %s\nparent %s\n" "$tree" "$(git rev-parse HEAD)" >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)
		cat >/dev/null
		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 &&
	grep "^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' '
	echo "username=a&password=b" |
	CGIT_CONFIG="$PWD/cverc" REQUEST_METHOD=POST CONTENT_LENGTH=99999999999999 \
		QUERY_STRING="p=login" cgit >tmp &&
	grep "^Status: 302" tmp
'

test_expect_success 'the dumb transport refuses a path that climbs out' '
	cgit_rc cverc "url=cve/objects/../config" >tmp &&
	grep "^Status: 404" tmp &&
	! grep "repositoryformatversion" tmp &&
	cgit_rc cverc "url=cve/objects/info/../../config" >tmp &&
	grep "^Status: 404" tmp &&
	! grep "repositoryformatversion" tmp
'

test_expect_success 'the about page serves nothing above the readme directory' '
	cgit_rc cverc "url=cve/about/../cverc" >tmp &&
	grep "^Status: 404" tmp &&
	! grep "virtual-root" tmp &&
	cgit_rc cverc "url=cve/about/" >tmp &&
	grep "readme text" tmp
'

test_expect_success 'a commit with an empty author renders everywhere' '
	cgit_rc cverc "url=cve/log/&h=noname" >tmp &&
	grep ">empty author</a>" tmp &&
	cgit_rc cverc "url=cve/commit/&h=noname" >tmp &&
	grep "<div class=.commit-subject.>empty author<" tmp &&
	cgit_rc cverc "url=cve/atom/&h=noname" >tmp &&
	grep "<name>unknown</name>" tmp &&
	! grep "<email></email>" tmp
'

test_expect_success 'a percent sign without digits behind it is read as itself' '
	cgit_rc cverc "url=cve/log/&q=%" >tmp &&
	grep "^Status: 200" tmp &&
	cgit_rc cverc "url=cve/log/&q=%zz%2" >tmp &&
	grep "^Status: 200" tmp &&
	grep "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 "&lt;script&gt;alert(1)x.txt" tmp
'

test_expect_success 'shell syntax in a file name reaches a filter as a plain argument' '
	cgit_rc cverc "url=cve/tree/%24(touch%20pwned).c" >tmp &&
	grep "^Status: 200" tmp &&
	grep "pwned).c" tmp &&
	! test -e pwned &&
	! test -e repos/cve/pwned
'

test_done