Fossil SCM

fossil-scm / test / cgi.test
Blame History Raw 2052 lines
1
#
2
# Copyright (c) 2026 Dan Mestas
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the Simplified BSD License (also
6
# known as the "2-Clause License" or "FreeBSD License".)
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but without any warranty; without even the implied warranty of
10
# merchantability or fitness for a particular purpose.
11
#
12
############################################################################
13
#
14
# How a server reads a request: src/cgi.c. Requests are written to a
15
# file and given to "fossil http --in", which handles one request as a
16
# server does and writes the reply to a file. The page /test-env shows
17
# the parameters the server read from the request.
18
#
19
# Plain HTTP, the same request arriving over ssh, and SCGI.
20
#
21
22
test_setup
23
24
write_file a.txt "an unversioned file\n"
25
fossil uv add a.txt
26
set cgiRepo [file join [pwd] .rep.fossil]
27
28
# Send the request in TEXT, a string of bytes, to the server and return
29
# the reply. ARGS are more options for "fossil http".
30
#
31
proc cgi_send {text args} {
32
return [fossil_http $::cgiRepo $text {*}$args]
33
}
34
35
# The same, as a local administrator.
36
proc cgi_admin {text args} {
37
return [cgi_send $text --ipaddr 127.0.0.1 --localauth {*}$args]
38
}
39
40
# The value of parameter NAME as /test-env shows it, or "-" if it does
41
# not show it.
42
proc cgi_param {reply name} {
43
set start [string first "\n$name = " $reply]
44
if {$start < 0} { return - }
45
incr start [expr {[string length $name] + 4}]
46
set end [string first " <br>" $reply $start]
47
if {$end < 0} { return - }
48
return [string range $reply $start [expr {$end - 1}]]
49
}
50
51
# A POST request with BODY as its content, of the type TYPE, and the
52
# header lines in HEADERS, a list.
53
proc cgi_post {type body {path /test-env} {headers {}}} {
54
return [request_post $path $body $headers $type]
55
}
56
57
# A POST of the form data BODY to PATH, sent from the page REFERER.
58
proc cgi_post_from {referer path body} {
59
return [cgi_post application/x-www-form-urlencoded $body $path \
60
[list "Referer: $referer"]]
61
}
62
63
###############################################################################
64
# Plain HTTP.
65
66
set cgiReply [cgi_admin \
67
"GET /test-env?a=1&b=two+words&c=%41%42&d=&e HTTP/1.1\r\nHost: x\r\n\r\n"]
68
test cgi-get {$CODE == 0}
69
test_eq cgi-get-status [reply_status $cgiReply] "HTTP/1.0 200 OK"
70
test_eq cgi-get-plain [cgi_param $cgiReply a] 1
71
test_eq cgi-get-plus [cgi_param $cgiReply b] "two words"
72
test_eq cgi-get-escape [cgi_param $cgiReply c] AB
73
test_eq cgi-get-empty [cgi_param $cgiReply d] ""
74
test_eq cgi-get-no-value [cgi_param $cgiReply e] ""
75
76
set cgiReply [cgi_admin [cgi_post application/x-www-form-urlencoded \
77
"x=1&y=a%20b&Z=upper"]]
78
test_eq cgi-post-form [cgi_param $cgiReply x] 1
79
test_eq cgi-post-form-escape [cgi_param $cgiReply y] "a b"
80
test_eq cgi-post-form-upper [cgi_param $cgiReply z] upper
81
82
foreach {cgiName cgiRequest} [list \
83
no-method "\r\n\r\n" \
84
bad-method "DELETE /test-env HTTP/1.1\r\nHost: x\r\n\r\n" \
85
] {
86
set cgiReply [cgi_send $cgiRequest --ipaddr 127.0.0.1]
87
test cgi-malformed-$cgiName \
88
{[string match "HTTP/1.0 40*" [reply_status $cgiReply]]}
89
}
90
91
# A request line with no path is a request for the home page.
92
set cgiReply [cgi_send "GET\r\n\r\n" --ipaddr 127.0.0.1]
93
test_eq cgi-no-path [reply_status $cgiReply] "HTTP/1.0 302 Moved Temporarily"
94
95
###############################################################################
96
# Form data in several parts, as a browser sends an upload.
97
98
proc cgi_part {name body {extra ""}} {
99
return \
100
[join [list \
101
"--XB\r\n" \
102
"Content-Disposition: form-data; " \
103
"name=\"$name\"$extra\r\n\r\n$body\r\n"] ""]
104
}
105
set cgiType "multipart/form-data; boundary=XB"
106
107
set cgiReply [cgi_admin [cgi_post $cgiType \
108
"[cgi_part alpha {first value}][cgi_part upload {file body} \
109
{; filename="a.txt"}]--XB--\r\n"]]
110
test_eq cgi-multipart [cgi_param $cgiReply alpha] "first value"
111
test_eq cgi-multipart-file [cgi_param $cgiReply upload] "file body"
112
test_eq cgi-multipart-filename [cgi_param $cgiReply upload:filename] a.txt
113
test_eq cgi-multipart-bytes [cgi_param $cgiReply upload:bytes] 9
114
115
set cgiReply [cgi_admin [cgi_post $cgiType \
116
[join [list \
117
"--XB\r\n" \
118
"Content-Disposition: form-data; name=\"t\"; " \
119
"filename=\"t.bin\"\r\n" \
120
"Content-Type: application/octet-stream\r\n\r\nxyz\r\n" \
121
"--XB--\r\n"] ""]]]
122
test_eq cgi-multipart-mimetype [cgi_param $cgiReply t:mimetype] \
123
application/octet-stream
124
125
# A name that begins with a capital is taken in lower case.
126
set cgiReply [cgi_admin [cgi_post $cgiType \
127
"[cgi_part Upper v {; filename="F.TXT"}]--XB--\r\n"]]
128
test_eq cgi-multipart-upper [cgi_param $cgiReply upper] v
129
test_eq cgi-multipart-upper-filename [cgi_param $cgiReply upper:filename] F.TXT
130
131
# Lines may end with a bare newline.
132
set cgiReply [cgi_admin [cgi_post $cgiType \
133
"--XB\nContent-Disposition: form-data; name=\"lf\"\n\nvalue\n--XB--\n"]]
134
test_eq cgi-multipart-lf [cgi_param $cgiReply lf] value
135
136
# A part with no closing boundary after a long one ends at the end of the
137
# request, and every part is read within the request.
138
set cgiReply [cgi_admin [cgi_post $cgiType \
139
[join [list \
140
"[cgi_part alpha [string repeat x 3000]]--XB\r\n" \
141
"Content-Disposition: form-data; name=\"beta\"\r\n\r\n" \
142
"no closing boundary"] ""]]]
143
test cgi-multipart-unclosed {$CODE == 0}
144
test_eq cgi-multipart-unclosed-first \
145
[string length [cgi_param $cgiReply alpha]] 3000
146
test_eq cgi-multipart-unclosed-second [cgi_param $cgiReply beta] \
147
"no closing boundary"
148
149
# Malformed and unusual bodies are read without a crash.
150
set cgiI 0
151
foreach cgiBody [list \
152
"" \
153
"\r\n\r\nvalue\r\n\r\n" \
154
"--XB" \
155
"--XB\r\n" \
156
"--XB\r\n\r\nno name here\r\n--XB--\r\n" \
157
"--XB\r\nContent-Disposition: form-data; name=\"a\"" \
158
"[cgi_part a "one\000two"]--XB--\r\n" \
159
[join [list \
160
"--XB\r\n" \
161
"Content-Disp\000osition: form-data; name=\"a\"\r\n\r\nv\r\n" \
162
"--XB--\r\n"] ""] \
163
[join [list \
164
"--[string repeat B 3000]\r\n" \
165
"Content-Disposition: form-data; name=\"a\"\r\n\r\nshort\r\n"] ""] \
166
[join [list \
167
"[cgi_part a [string repeat x 2000]]--XB\r\n" \
168
"Content-Disposition: form-data; name=\"b\""] ""] \
169
"[cgi_part {} v]--XB--\r\n" \
170
[join [list \
171
"--XB\r\n" \
172
"Content-Disposition: form-data; filename=\"f\"\r\n\r\nv\r\n" \
173
"--XB--\r\n"] ""] \
174
[join [list \
175
"--XB\r\nContent-Disposition: form-data; name=\"abc\r\n\r\n" \
176
"v\r\n--XB--\r\n"] ""] \
177
"--XB\r\nContent-Disposition: form-data; name=\r\n\r\nv\r\n--XB--\r\n" \
178
[join [list \
179
"--XB\r\n" \
180
"Content-Disposition: form-data; name=\"a\"; " \
181
"filename=\r\n\r\nv\r\n--XB--\r\n"] ""] \
182
[join [list \
183
"--XB\r\nContent-Disposition: form-data; name=\"a\"\r\n" \
184
"content-type:\r\n\r\nv\r\n--XB--\r\n"] ""] \
185
"[cgi_part a "\n--X"]--XB--\r\n" \
186
] {
187
incr cgiI
188
cgi_admin [cgi_post $cgiType $cgiBody]
189
test cgi-multipart-odd-$cgiI {$CODE == 0}
190
}
191
set cgiMany ""
192
for {set cgiI 0} {$cgiI < 400} {incr cgiI} {
193
append cgiMany [cgi_part p$cgiI [string repeat v [expr {$cgiI % 50}]]]
194
}
195
set cgiReply [cgi_admin [cgi_post $cgiType "$cgiMany--XB--\r\n"]]
196
test_eq cgi-multipart-many [cgi_param $cgiReply p399] [string repeat v 49]
197
198
###############################################################################
199
# The date in an If-Modified-Since header. A file that has not changed
200
# since that date is not sent again.
201
202
proc cgi_since {date} {
203
set reply [cgi_send \
204
"GET /uv/a.txt HTTP/1.1\r\nHost: x\r\nIf-Modified-Since: $date\r\n\r\n" \
205
--ipaddr 127.0.0.1]
206
return [reply_status $reply]
207
}
208
test_eq cgi-since-later [cgi_since "Mon, 01 Jan 2099 00:00:00 GMT"] \
209
"HTTP/1.0 304 Not Modified"
210
test_eq cgi-since-earlier [cgi_since "Sat, 01 Jan 2000 00:00:00 GMT"] \
211
"HTTP/1.0 200 OK"
212
test_eq cgi-since-garbage [cgi_since "yesterday"] "HTTP/1.0 200 OK"
213
# Numbers out of range are not a date.
214
foreach {cgiName cgiDate} {
215
year "Mon, 01 Jan 2147483647 00:00:00 GMT"
216
day "Mon, 2147483647 Jan 2026 00:00:00 GMT"
217
hour "Mon, 01 Jan 2026 2147483647:00:00 GMT"
218
minute "Mon, 01 Jan 2026 00:2147483647:00 GMT"
219
} {
220
test_eq cgi-since-huge-$cgiName [cgi_since $cgiDate] "HTTP/1.0 200 OK"
221
}
222
223
###############################################################################
224
# A reply is compressed with gzip when the client accepts it and the type
225
# of the content is text, JavaScript, JSON, WebAssembly, Tcl, tar, XML,
226
# SVG or an icon. Other types, and a client that does not accept gzip,
227
# get the content as it is.
228
229
foreach {cgiName cgiGzip} {
230
g.txt 1 g.js 1 g.json 1 g.wasm 1 g.tcl 1 g.tar 1 g.xml 1 g.svg 1
231
g.ico 1 g.png 0 g.zip 0 g.pdf 0
232
} {
233
write_file $cgiName [string repeat "content to compress\n" 50]
234
fossil uv add $cgiName
235
set cgiReply [cgi_send \
236
[join [list \
237
"GET /uv/$cgiName HTTP/1.1\r\nHost: x\r\n" \
238
"Accept-Encoding: gzip, deflate\r\n\r\n"] ""] --ipaddr 127.0.0.1]
239
test_eq cgi-gzip-$cgiName \
240
[string match "*\nContent-Encoding: gzip\r\n*" $cgiReply] $cgiGzip
241
}
242
set cgiReply [cgi_send "GET /uv/g.txt HTTP/1.1\r\nHost: x\r\n\r\n" \
243
--ipaddr 127.0.0.1]
244
test cgi-gzip-not-accepted {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
245
&& ![string match "*Content-Encoding*" $cgiReply]}
246
247
###############################################################################
248
# A POST whose Referer is another site is not taken as safe from forgery,
249
# and is written to the error log.
250
251
proc cgi_logout {referer} {
252
return [cgi_send [cgi_post_from $referer /login "out=1"] --ipaddr 10.0.0.1 \
253
--errorlog [file join [pwd] err.log]]
254
}
255
file delete err.log
256
cgi_logout http://x/login
257
test cgi-same-origin-not-logged {![file exists err.log]
258
|| ![string match "*different origin*" [read_file err.log]]}
259
cgi_logout http://elsewhere.example/page
260
test cgi-other-origin-logged {[file exists err.log]
261
&& [string match "*POST from different origin*" [read_file err.log]]}
262
263
###############################################################################
264
# SCGI: a header of name and value pairs, each ended by a zero byte,
265
# preceded by its length and followed by a comma.
266
267
proc cgi_scgi {pairs {body ""}} {
268
set h ""
269
foreach {k v} $pairs { append h $k \000 $v \000 }
270
return "[string length $h]:$h,$body"
271
}
272
set cgiReply [cgi_send [cgi_scgi {
273
CONTENT_LENGTH 0 SCGI 1 REQUEST_METHOD GET REQUEST_URI /test-env?s=1
274
PATH_INFO /test-env QUERY_STRING s=1 SERVER_NAME x SERVER_PORT 80
275
REMOTE_ADDR 127.0.0.1
276
}] --scgi --localauth]
277
test_eq cgi-scgi [reply_status $cgiReply] "HTTP/1.0 200 OK"
278
test_eq cgi-scgi-param [cgi_param $cgiReply s] 1
279
280
foreach {cgiName cgiRequest} [list \
281
too-short "15:CONTENT_LENGTH\0000\000," \
282
cut-short "40:CONTENT_LENGTH\0000\000," \
283
no-zero "20:CONTENT_LENGTH0000000000," \
284
huge-length "99999999999999999999:CONTENT_LENGTH\0000\000," \
285
] {
286
set cgiReply [cgi_send $cgiRequest --scgi]
287
test cgi-scgi-$cgiName {$CODE == 0 &&
288
[string match "HTTP/1.0 400*" [reply_status $cgiReply]]}
289
}
290
291
###############################################################################
292
# The same reader for a request that arrives over ssh, which the server
293
# knows from SSH_CONNECTION. It reads the client's probes first.
294
295
set ::env(SSH_CONNECTION) "127.0.0.1 5000 127.0.0.1 22"
296
set cgiReply [cgi_send "GET /test-env?q=1 HTTP/1.1\r\nHost: x\r\n\r\n" \
297
--localauth]
298
test_eq cgi-ssh [reply_status $cgiReply] "HTTP/1.0 200 OK"
299
test_eq cgi-ssh-param [cgi_param $cgiReply q] 1
300
test_eq cgi-ssh-address [cgi_param $cgiReply REMOTE_ADDR] 127.0.0.1
301
302
set cgiReply [cgi_send [cgi_post application/x-www-form-urlencoded \
303
"p=posted"] --localauth]
304
test_eq cgi-ssh-post [cgi_param $cgiReply p] posted
305
306
# An older client opens the connection with "echo" probes, each of which
307
# the server answers with the word it was given, and then names the
308
# command it ran. Any other word in a probe is refused.
309
set cgiReply [cgi_send \
310
[join [list \
311
"echo test1\necho probe-2\nfossil\n" \
312
"GET /test-env?q=3 HTTP/1.1\r\nHost: x\r\n\r\n"] ""] --localauth]
313
test cgi-ssh-probes {[string match "test1\nprobe-2\n*" $cgiReply]
314
&& [string match "*\nHTTP/1.0 200 OK*" $cgiReply]}
315
test_eq cgi-ssh-probes-param [cgi_param $cgiReply q] 3
316
foreach {cgiName cgiRequest} [list \
317
probe-word "echo hello\n" \
318
probe-alone "echo\n" \
319
probe-no-command "echo test1\n" \
320
] {
321
set cgiReply [cgi_send $cgiRequest --localauth]
322
test cgi-ssh-$cgiName {$CODE == 0
323
&& [string match "*400 Bad Request*" $cgiReply]}
324
}
325
326
foreach {cgiName cgiRequest} [list \
327
empty "" \
328
bad-method "PUT /x HTTP/1.1\r\n\r\n" \
329
no-url "GET\r\n\r\n" \
330
long-line "GET /[string repeat a 5000] HTTP/1.1\r\n\r\n" \
331
] {
332
cgi_send $cgiRequest
333
test cgi-ssh-$cgiName {$CODE == 0 || $CODE == 1}
334
}
335
unset ::env(SSH_CONNECTION)
336
337
###############################################################################
338
# The tests above try each kind of request once. The tests below aim
339
# at every branch of cgi.c.
340
341
if {[test_assurance] eq "smoke"} {
342
test_skip "the tests of each branch need -assurance ci or stress"
343
}
344
345
###############################################################################
346
# Names and values in a query string and in a Cookie header. A name is
347
# taken when it begins with a letter and holds only letters, digits,
348
# "_" and "-"; of two values with one name the first is kept; a value
349
# from the query string that holds a control character is dropped.
350
351
set cgiReply [cgi_admin [request_get \
352
"/test-env?e&a=1&=5&&a\$b=6&c=%01x&1a=2&_u=3&a=second&Up=4&zz" \
353
[list "Cookie: ck=1; sp=2; fossil-x=9; Big=3"]]]
354
foreach {cgiName cgiParam cgiValue} {
355
no-value-first e ""
356
plain a 1
357
empty-name "" -
358
strange-name a$b -
359
control-char c -
360
digit-first 1a -
361
underscore _u -
362
upper up 4
363
no-value-last zz ""
364
cookie ck 1
365
cookie-space sp 2
366
cookie-upper big 3
367
cookie-fossil fossil-x -
368
cookie-header HTTP_COOKIE -
369
} {
370
test_eq cgi-names-$cgiName [cgi_param $cgiReply $cgiParam] $cgiValue
371
}
372
373
# With "showall" the page shows the cookies as well.
374
set cgiReply [cgi_admin [request_get /test-env?showall \
375
[list "Cookie: ck=1; fossil-x=9"]]]
376
test_eq cgi-showall-cookie [cgi_param $cgiReply fossil-x] 9
377
test_eq cgi-showall-header [cgi_param $cgiReply HTTP_COOKIE] "ck=1; fossil-x=9"
378
379
# A value in a form may hold a control character.
380
set cgiReply [cgi_admin [cgi_post application/x-www-form-urlencoded \
381
"k=%01x&plain=1"]]
382
test_eq cgi-post-control-char [cgi_param $cgiReply k] "\001x"
383
384
# More than 630 parameters are refused.
385
set cgiMany ""
386
for {set cgiI 0} {$cgiI < 700} {incr cgiI} { append cgiMany "q$cgiI=1&" }
387
set cgiReply [cgi_admin [cgi_post application/x-www-form-urlencoded $cgiMany]]
388
test cgi-too-many {$CODE == 1
389
&& [string match "*Too many query parameters*" $cgiReply$RESULT]}
390
391
###############################################################################
392
# The request line and the header lines.
393
394
set cgiReply [cgi_admin \
395
[join [list \
396
"GET /test-env?showall HTTP/1.1\r\n" \
397
"HOST: named.example:8080\r\nIf-None-Match: tag\r\n" \
398
"Authorization: Basic eDp5\r\nAccept-Language: de \r\n" \
399
"User-Agent: Agent/1\r\nUnknown-Header: ignored\r\n\r\n"] ""]]
400
foreach {cgiParam cgiValue} {
401
HTTP_HOST named.example:8080
402
SERVER_NAME named.example
403
HTTP_IF_NONE_MATCH tag
404
HTTP_AUTHORIZATION {Basic eDp5}
405
HTTP_ACCEPT_LANGUAGE de
406
HTTP_USER_AGENT Agent/1
407
REQUEST_SCHEME http
408
REQUEST_METHOD GET
409
} {
410
test_eq cgi-header-$cgiParam [cgi_param $cgiReply $cgiParam] $cgiValue
411
}
412
413
# An "Https:" header says that the request came by https, so the reply
414
# sends a local user to the https login page.
415
set cgiReply [cgi_admin [request_get /test-env [list "Https: on"]]]
416
test_eq cgi-header-https [reply_header $cgiReply Location] \
417
https://x/login?g=test-env
418
419
# X-Forwarded-For names the client when the request comes from this
420
# machine: the last address in it is taken. From any other machine the
421
# header is ignored. Only a local client sees /test-env here.
422
foreach {cgiName cgiFrom cgiForwarded cgiAddr} {
423
one ::1 127.0.0.1 127.0.0.1
424
comma 127.0.0.1 9.9.9.9,::1 ::1
425
space 127.0.0.1 {9.9.9.9, 127.0.0.1} 127.0.0.1
426
mapped 127.0.0.1 ::ffff:127.0.0.1 ::ffff:127.0.0.1
427
remote 127.0.0.1 {127.0.0.1, 9.9.9.9} -
428
empty 127.0.0.1 {} -
429
not-local 10.0.0.1 127.0.0.1 -
430
} {
431
set cgiReply [cgi_send [request_get /test-env \
432
[list "X-Forwarded-For: $cgiForwarded"]] --ipaddr $cgiFrom --localauth]
433
test_eq cgi-forwarded-$cgiName [cgi_param $cgiReply REMOTE_ADDR] $cgiAddr
434
}
435
436
# HEAD gets the headers of the reply to GET and no content.
437
set cgiReply [cgi_send [request_get /uv/g.png {} HEAD] --ipaddr 127.0.0.1]
438
test_eq cgi-head-status [reply_status $cgiReply] "HTTP/1.0 200 OK"
439
test_eq cgi-head-length [reply_header $cgiReply Content-Length] 1000
440
test_eq cgi-head-body [reply_body $cgiReply] ""
441
442
# A POST must say how long its content is, and send that much.
443
set cgiReply [cgi_admin "POST /test-env HTTP/1.1\r\nHost: x\r\n\r\n"]
444
test cgi-post-no-length {[reply_status $cgiReply] eq "HTTP/1.0 400 Bad Request"
445
&& [string match "*missing CONTENT_LENGTH on a POST method*" $cgiReply]}
446
set cgiReply [cgi_admin \
447
[join [list \
448
"POST /test-env HTTP/1.1\r\nHost: x\r\n" \
449
"Content-Type: application/x-www-form-urlencoded\r\n" \
450
"Content-Length: 50\r\n\r\na=1"] ""]]
451
test cgi-post-short {[reply_status $cgiReply] eq "HTTP/1.0 400 Bad Request"
452
&& [string match "*Wanted 50 bytes but got only 3*" $cgiReply]}
453
# Content with no type is not read.
454
set cgiReply [cgi_admin \
455
"POST /test-env HTTP/1.1\r\nHost: x\r\nContent-Length: 3\r\n\r\na=1"]
456
test cgi-post-no-type {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
457
&& [cgi_param $cgiReply a] eq "-"}
458
foreach {cgiName cgiLength} \
459
{negative -5 zero 0 word abc huge 99999999999999999999} {
460
set cgiReply [cgi_admin \
461
[join [list \
462
"POST /test-env HTTP/1.1\r\nHost: x\r\n" \
463
"Content-Type: application/x-www-form-urlencoded\r\n" \
464
"Content-Length: $cgiLength\r\n\r\na=1"] ""]]
465
test cgi-post-length-$cgiName {$CODE == 0
466
&& [regexp {^HTTP/1.0 (200|400) } $cgiReply]}
467
}
468
469
###############################################################################
470
# Part of a file: a Range header on a GET that succeeds gets the bytes
471
# asked for, not compressed, with status 206.
472
473
write_file r.css 0123456789abcdefghij
474
fossil uv add r.css
475
proc cgi_range {range {method GET} {file r.css}} {
476
return [cgi_send [request_get /uv/$file \
477
[list "Range: $range" "Accept-Encoding: gzip"] $method] \
478
--ipaddr 127.0.0.1]
479
}
480
set cgiReply [cgi_range bytes=2-5]
481
test_eq cgi-range-status [reply_status $cgiReply] "HTTP/1.0 206 Partial Content"
482
test_eq cgi-range-header [reply_header $cgiReply Content-Range] "bytes 2-5/20"
483
test_eq cgi-range-length [reply_header $cgiReply Content-Length] 4
484
test_eq cgi-range-encoding [reply_header $cgiReply Content-Encoding] -
485
test_eq cgi-range-body [reply_body $cgiReply] 2345
486
set cgiReply [cgi_range bytes=0-0]
487
test_eq cgi-range-first-byte [reply_body $cgiReply] 0
488
# A range that is not two numbers in order is ignored.
489
foreach {cgiName cgiRange} {
490
word bytes=abc
491
one-number bytes=5-
492
negative bytes=-5-10
493
backwards bytes=9-3
494
other-unit lines=1-2
495
} {
496
set cgiReply [cgi_range $cgiRange]
497
test cgi-range-ignored-$cgiName {
498
[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
499
&& [reply_header $cgiReply Content-Encoding] eq "gzip"}
500
}
501
# A range that runs past the end of the file stops at the end.
502
set cgiReply [cgi_range bytes=15-2147483646]
503
test_eq cgi-range-past-end [reply_body $cgiReply] fghij
504
test_eq cgi-range-past-end-header [reply_header $cgiReply Content-Range] \
505
"bytes 15-19/20"
506
test_eq cgi-range-past-end-length [reply_header $cgiReply Content-Length] 5
507
# A range that starts at or after the end gets 416 and no content.
508
foreach {cgiName cgiRange} {at-end bytes=20-20 after-end bytes=25-30} {
509
set cgiReply [cgi_range $cgiRange]
510
test cgi-range-unsatisfiable-$cgiName {
511
[reply_status $cgiReply] eq "HTTP/1.0 416 Range Not Satisfiable"
512
&& [reply_header $cgiReply Content-Range] eq "bytes */20"
513
&& [reply_header $cgiReply Content-Length] eq "0"}
514
}
515
# A number too large for an int is not taken as a smaller one.
516
set cgiReply [cgi_range bytes=0-4294967299]
517
test_eq cgi-range-too-large [reply_status $cgiReply] "HTTP/1.0 200 OK"
518
# A reply other than 206 is sent whole, whatever range was asked for.
519
set cgiReply [cgi_send [request_get /uv/nosuch.css [list "Range: bytes=3-9"]] \
520
--ipaddr 127.0.0.1]
521
test cgi-range-not-found {
522
[string match "HTTP/1.0 404 *" [reply_status $cgiReply]]
523
&& [string match "<!DOCTYPE*" [reply_body $cgiReply]]}
524
525
###############################################################################
526
# More shapes of form data in several parts.
527
528
proc cgi_form {body} {
529
global cgiType
530
return [cgi_admin [cgi_post $cgiType $body]]
531
}
532
# A part with no content, where lines end with a bare newline.
533
set cgiReply [cgi_form \
534
"--XB\nContent-Disposition: form-data; name=\"none\"\n\n\n--XB--\n"]
535
test_eq cgi-multipart-empty-value [cgi_param $cgiReply none] ""
536
537
# A name in capitals with no file name is taken in lower case and has no
538
# count of bytes.
539
set cgiReply [cgi_form "[cgi_part Shout loud]--XB--\r\n"]
540
test_eq cgi-multipart-upper-plain [cgi_param $cgiReply shout] loud
541
test_eq cgi-multipart-upper-no-bytes [cgi_param $cgiReply shout:bytes] -
542
543
# A name in capitals with a type of content.
544
set cgiReply [cgi_form \
545
[join [list \
546
"--XB\r\n" \
547
"Content-Disposition: form-data; name=\"Typed\"; " \
548
"filename=\"t\"\r\nContent-Type: text/x-test\r\n\r\nv\r\n" \
549
"--XB--\r\n"] ""]]
550
test_eq cgi-multipart-upper-mimetype [cgi_param $cgiReply typed:mimetype] \
551
text/x-test
552
test_eq cgi-multipart-upper-bytes [cgi_param $cgiReply typed:bytes] 1
553
554
# A name that does not begin with a letter is not taken, with or without
555
# a file name or a type.
556
set cgiReply [cgi_form \
557
[join [list \
558
"--XB\r\n" \
559
"Content-Disposition: form-data; name=\"1st\"; " \
560
"filename=\"f\"\r\nContent-Type: text/plain\r\n\r\nv\r\n" \
561
"[cgi_part after ok]--XB--\r\n"] ""]]
562
test_eq cgi-multipart-digit-name [cgi_param $cgiReply 1st] -
563
test_eq cgi-multipart-digit-filename [cgi_param $cgiReply 1st:filename] -
564
test_eq cgi-multipart-digit-mimetype [cgi_param $cgiReply 1st:mimetype] -
565
test_eq cgi-multipart-digit-next [cgi_param $cgiReply after] ok
566
567
# A type or a file name given before the name belongs to no parameter.
568
set cgiReply [cgi_form \
569
[join [list \
570
"--XB\r\nContent-Type: text/early\r\n" \
571
"Content-Disposition: form-data; filename=\"early\"; " \
572
"name=\"late\"\r\n\r\nv\r\n--XB--\r\n"] ""]]
573
test_eq cgi-multipart-type-first-value [cgi_param $cgiReply late] v
574
test_eq cgi-multipart-type-first-mimetype [cgi_param $cgiReply late:mimetype] -
575
test_eq cgi-multipart-type-first-filename [cgi_param $cgiReply late:filename] -
576
577
# Words in a header line that the reader does not know are passed over.
578
set cgiReply [cgi_form \
579
[join [list \
580
"--XB\r\n" \
581
"Content-Disposition: form-data; nonce=1; foo=2; charset=3; " \
582
"xyz; name=\"known\"\r\n\r\nv\r\n--XB--\r\n"] ""]]
583
test_eq cgi-multipart-unknown-words [cgi_param $cgiReply known] v
584
585
# A quoted word that reaches the end of a header line without a closing
586
# quote. The request must be answered within the time limit.
587
set cgiBody \
588
[join [list \
589
"--XB\r\nContent-Disposition: form-data; name=\"\r\n\r\nv\r\n" \
590
"[cgi_part after ok]--XB--\r\n"] ""]
591
write_file request.txt [cgi_post $cgiType $cgiBody]
592
file delete reply.txt
593
fossil_raw -seconds 15 http $cgiRepo --in request.txt --out reply.txt \
594
--ipaddr 127.0.0.1 --localauth
595
test cgi-multipart-lone-quote {$CODE == 0}
596
597
# Only the first 49 words of a header line are read.
598
set cgiWords ""
599
for {set cgiI 0} {$cgiI < 30} {incr cgiI} \
600
{ append cgiWords " w$cgiI \"q$cgiI\";" }
601
set cgiReply [cgi_form \
602
[join [list \
603
"--XB\r\n" \
604
"Content-Disposition: form-data;$cgiWords " \
605
"name=\"far\"\r\n\r\nv\r\n[cgi_part after ok]--XB--\r\n"] ""]]
606
test_eq cgi-multipart-many-words [cgi_param $cgiReply far] -
607
test_eq cgi-multipart-many-words-next [cgi_param $cgiReply after] ok
608
609
# A quote that ends a header line after a space is a word of no
610
# characters, so the part has no name.
611
set cgiReply [cgi_form \
612
[join [list \
613
"--XB\r\nContent-Disposition: form-data; name= \"\r\n\r\n" \
614
"v\r\n[cgi_part after ok]--XB--\r\n"] ""]]
615
test {cgi-multipart-quote-alone} {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
616
&& [cgi_param $cgiReply after] eq "ok"}
617
618
# Content that begins with a zero byte has no parts.
619
set cgiReply [cgi_form "\000--XB\r\n[cgi_part a v]--XB--\r\n"]
620
test cgi-multipart-zero-first {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
621
&& [cgi_param $cgiReply a] eq "-"}
622
623
###############################################################################
624
# More dates in If-Modified-Since. The file is given the time that the
625
# date stands for and is then not sent again; one second newer and it is.
626
627
write_file d.bin "dated"
628
fossil uv add d.bin
629
proc cgi_since_exact {date seconds} {
630
set result ""
631
foreach mtime [list $seconds [expr {$seconds + 1}]] {
632
fossil sql "UPDATE unversioned SET mtime=$mtime WHERE name='d.bin'"
633
set reply [cgi_send [request_get /uv/d.bin \
634
[list "If-Modified-Since: $date"]] \
635
--ipaddr 127.0.0.1]
636
lappend result [lindex [reply_status $reply] 1]
637
}
638
return $result
639
}
640
foreach {cgiName cgiDate cgiSeconds} {
641
epoch-year "Thu, 01 Jan 1970 00:00:10 GMT" 10
642
two-digit-year "Fri, 31 Dec 99 23:59:59 GMT" 946684799
643
leap-2000-feb "Tue, 29 Feb 2000 12:00:00 GMT" 951825600
644
leap-2000-mar "Wed, 01 Mar 2000 00:00:00 GMT" 951868800
645
leap-2024-jan "Wed, 31 Jan 2024 00:00:00 GMT" 1706659200
646
leap-2024-dec "Tue, 31 Dec 2024 23:59:60 GMT" 1735689600
647
plain-2023-mar "Wed, 01 Mar 2023 01:02:03 GMT" 1677632523
648
not-leap-2100 "Mon, 01 Mar 2100 00:00:00 GMT" 4107456000
649
year-8099 "Thu, 31 Dec 9999 23:59:59 GMT" 253402300799
650
} {
651
test_eq cgi-since-exact-$cgiName [cgi_since_exact $cgiDate $cgiSeconds] \
652
{304 200}
653
}
654
# Each of these is not a date, so the file is sent.
655
fossil sql "UPDATE unversioned SET mtime=1 WHERE name='d.bin'"
656
foreach {cgiName cgiDate} {
657
year-before-1970 "Wed, 01 Jan 69 00:00:00 GMT"
658
year-10000 "Sat, 01 Jan 10000 00:00:00 GMT"
659
day-zero "Mon, 00 Jan 2026 00:00:00 GMT"
660
day-32 "Mon, 32 Jan 2026 00:00:00 GMT"
661
hour-negative "Mon, 01 Jan 2026 -1:00:00 GMT"
662
hour-24 "Mon, 01 Jan 2026 24:00:00 GMT"
663
minute-negative "Mon, 01 Jan 2026 00:-1:00 GMT"
664
minute-60 "Mon, 01 Jan 2026 00:60:00 GMT"
665
second-negative "Mon, 01 Jan 2026 00:00:-1 GMT"
666
second-61 "Mon, 01 Jan 2026 00:00:61 GMT"
667
month "Mon, 01 Foo 2026 00:00:00 GMT"
668
short "Mon, 01 Jan 2026"
669
empty ""
670
} {
671
set cgiReply [cgi_send [request_get /uv/d.bin \
672
[list "If-Modified-Since: $cgiDate"]] --ipaddr 127.0.0.1]
673
test_eq cgi-since-not-a-date-$cgiName [reply_status $cgiReply] \
674
"HTTP/1.0 200 OK"
675
}
676
677
###############################################################################
678
# Redirects. A redirect to an absolute URL is sent as it is; a redirect
679
# to a path that begins with "/" is made relative to the base URL; any
680
# other target is appended to the base URL. The "referer" query
681
# parameter, the Referer header, or a default supplies the target for
682
# the user-edit cancel button.
683
684
proc cgi_redirect_to {query {referer none}} {
685
set headers {}
686
if {$referer ne "none"} { lappend headers "Referer: $referer" }
687
set reply [cgi_admin [request_get "/setup_uedit?$query" $headers]]
688
return [reply_header $reply Location]
689
}
690
# The cancel button redirects to the referer.
691
test_eq cgi-redirect-absolute-http [cgi_redirect_to can=1 http://other/p] \
692
http://other/p
693
test_eq cgi-redirect-absolute-https [cgi_redirect_to can=1 https://other/p] \
694
https://other/p
695
test_eq cgi-redirect-rooted [cgi_redirect_to {can=1&referer=/at-root}] \
696
http://x/at-root
697
test_eq cgi-redirect-relative [cgi_redirect_to {can=1&referer=some/where}] \
698
http://x/some/where
699
# With no referer parameter and no Referer header, the default is used.
700
test_eq cgi-redirect-default [cgi_redirect_to can=1] http://x/setup_ulist
701
# The "referer" query parameter is preferred over the Referer header.
702
test_eq cgi-redirect-param-wins \
703
[cgi_redirect_to {can=1&referer=/from-param} http://x/from-header] \
704
http://x/from-param
705
706
###############################################################################
707
# The anti-forgery check. A GET from the same origin is "same origin"
708
# only; a POST adds "POST"; a POST that also carries the right "csrf"
709
# token adds "CSRF token". The page reports the level it reached.
710
711
proc cgi_csrf_level {reply} {
712
if {[regexp {CSRF safety = ([^<]*)} $reply all level]} { return $level }
713
return -
714
}
715
set cgiToken ""
716
set cgiReply [cgi_admin [request_get /setup_uedit?id=0]]
717
regexp {name="csrf" value="([^"]*)"} $cgiReply all cgiToken
718
719
set cgiReply [cgi_admin [request_get /test-env [list "Referer: http://x/prev"]]]
720
test_eq cgi-csrf-get [cgi_csrf_level $cgiReply] "Same origin"
721
proc cgi_env_post {body {referer http://x/prev}} {
722
return [cgi_admin [cgi_post_from $referer /test-env $body]]
723
}
724
set cgiReply [cgi_env_post "csrf=$cgiToken"]
725
test_eq cgi-csrf-post-token [cgi_csrf_level $cgiReply] \
726
"Same origin, POST, CSRF token"
727
set cgiReply [cgi_env_post "csrf=wrong"]
728
test_eq cgi-csrf-post-bad-token [cgi_csrf_level $cgiReply] "Same origin, POST"
729
set cgiReply [cgi_env_post "x=1" http://elsewhere/p]
730
test_eq cgi-csrf-post-other-origin [cgi_csrf_level $cgiReply] "unsafe"
731
732
# A page that consults the check more than once. A cross-origin POST to
733
# the user-edit page, which asks twice, is ignored without error.
734
set cgiReply [cgi_admin [cgi_post_from http://elsewhere/p /setup_uedit \
735
"id=0&delete=1&"]]
736
test_eq cgi-csrf-twice-cross [reply_status $cgiReply] "HTTP/1.0 200 OK"
737
# A valid same-origin POST with the token creates the user.
738
set cgiBody "id=0&login=newcomer&info=x&pw=secretpw&apply=1&csrf=$cgiToken"
739
set cgiReply [cgi_admin [cgi_post_from http://x/setup_uedit /setup_uedit \
740
$cgiBody]]
741
test cgi-csrf-verify-accepts \
742
{[string match "HTTP/1.0 302*" [reply_status $cgiReply]]}
743
test cgi-csrf-user-created \
744
{[fossil sql "SELECT count(*) FROM user WHERE login='newcomer'"] == 1}
745
746
# The user-edit page needs "login", "info", "pw" and "apply" to make a
747
# change. A request with "login" alone changes nothing.
748
set cgiBody "id=0&login=cgighost&csrf=$cgiToken"
749
cgi_admin [cgi_post_from http://x/setup_uedit /setup_uedit $cgiBody]
750
test cgi-all-needs-every-field \
751
{[fossil sql "SELECT count(*) FROM user WHERE login='cgighost'"] == 0}
752
753
###############################################################################
754
# Request methods and the same reader over ssh.
755
756
set ::env(SSH_CONNECTION) "127.0.0.1 5000 127.0.0.1 22"
757
# A HEAD over ssh.
758
set cgiReply [cgi_send [request_get /test-env?q=9 {} HEAD] --localauth]
759
test_eq cgi-ssh-head [reply_status $cgiReply] "HTTP/1.0 200 OK"
760
unset ::env(SSH_CONNECTION)
761
762
###############################################################################
763
# cgi_from_mobile, used by /forum to pick a default display order. An
764
# iPad is not treated as mobile; another phone is; no agent is not.
765
proc cgi_forum_order {agent} {
766
set headers {}
767
if {$agent ne ""} { lappend headers "User-Agent: $agent" }
768
set reply [cgi_admin [request_get /forum?t=a $headers]]
769
return [reply_status $reply]
770
}
771
foreach {cgiName cgiAgent} {
772
phone "Mozilla/5.0 (iPhone; Mobile) Safari"
773
ipad "Mozilla/5.0 (iPad) Safari"
774
none ""
775
} {
776
test_eq cgi-mobile-$cgiName [cgi_forum_order $cgiAgent] "HTTP/1.0 200 OK"
777
}
778
779
###############################################################################
780
# A spider check. For a request that is not logged in, an unused query
781
# parameter whose value looks like an injection attack gets a 418 reply
782
# and a note in the error log. The whole request environment is written
783
# to the log.
784
785
proc cgi_attack {value} {
786
global cgiRepo
787
write_file request.txt \
788
[join [list \
789
"GET /timeline?inj=$value HTTP/1.1\r\nHost: x\r\n" \
790
"Cookie: fossil-x=secret\r\n\r\n"] ""]
791
file delete reply.txt attack.log
792
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
793
--ipaddr 9.9.9.9 --errorlog [file join [pwd] attack.log]
794
return [expr {[file exists reply.txt] ? [read_file reply.txt] : ""}]
795
}
796
set cgiReply [cgi_attack "1;select"]
797
test_eq cgi-attack-status [reply_status $cgiReply] "HTTP/1.0 418 I'm a teapot"
798
test cgi-attack-logged {[file exists attack.log]
799
&& [string match "*possible hack attempt*" [read_file attack.log]]
800
&& [string match "*QUERY_STRING = inj=1;select*" [read_file attack.log]]}
801
# A value longer than 100 characters is shortened in the log.
802
set cgiReply [cgi_attack "[string repeat a 200];x"]
803
test cgi-attack-long {[reply_status $cgiReply] eq "HTTP/1.0 418 I'm a teapot"
804
&& [string match "*aaa...*" [read_file attack.log]]}
805
# A logged-in request with the same value is not stopped.
806
set cgiReply [cgi_admin [request_get "/timeline?inj=1;select"]]
807
test_eq cgi-attack-admin-ok [reply_status $cgiReply] "HTTP/1.0 200 OK"
808
809
###############################################################################
810
# The same-origin test with crafted Referer headers. The base URL here
811
# is "http://x". A Referer that shares the base but has more path after
812
# it with no "/" boundary is a different origin. A Referer with a "%"
813
# escape is decoded before the comparison. The /test-env page reports
814
# the result.
815
816
proc cgi_origin {referer} {
817
set req "GET /test-env HTTP/1.1\r\nHost: x\r\nReferer: $referer\r\n\r\n"
818
set reply [cgi_admin $req]
819
return [cgi_csrf_level $reply]
820
}
821
test_eq cgi-origin-boundary [cgi_origin http://xtra/page] unsafe
822
test_eq cgi-origin-escaped [cgi_origin http://x/a%2Fb] "Same origin"
823
# A different-origin GET reaches the method test in the same-origin check
824
# without writing a warning (the warning is for POST only).
825
test_eq cgi-origin-get-other [cgi_origin http://elsewhere/p] unsafe
826
827
###############################################################################
828
# cgi_parameter_name walks every parameter: the ticket-entry page copies
829
# each into its interpreter.
830
831
set cgiReply [cgi_admin [request_get "/tktnew?username=someone&extra=1"]]
832
test_eq cgi-param-name-page [reply_status $cgiReply] "HTTP/1.0 200 OK"
833
834
###############################################################################
835
# cgi_param_info walks the parameters for the /cookies page.
836
837
set cgiReply [cgi_admin [request_get /cookies \
838
[list "Cookie: fossil_display_settings=x; extra=y"]]]
839
test cgi-param-info-page {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
840
&& [string match "*fossil_display_settings*" $cgiReply]}
841
842
###############################################################################
843
# Two more destinations for cgi_print_all. With --cgitrace the whole
844
# parameter set is traced before the page runs.
845
846
write_file request.txt [request_get "/test-env?traced=1"]
847
file delete reply.txt
848
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
849
--ipaddr 127.0.0.1 --localauth --cgitrace
850
test cgi-cgitrace {$CODE == 0
851
&& [string match "*Calling*" $RESULT]
852
&& [string match "*traced = 1*" $RESULT]}
853
854
# A CGI control file with "cgi-debug:" writes the environment to a debug
855
# file as the control file is read.
856
write_file cgi.ctl \
857
"repository: [file nativename $cgiRepo]\ncgi-debug: debug.txt\n"
858
file delete debug.txt
859
set ::env(REQUEST_METHOD) GET
860
set ::env(SCRIPT_NAME) ""
861
set ::env(PATH_INFO) /test-env
862
set ::env(QUERY_STRING) "dbg=1"
863
set ::env(REMOTE_ADDR) 127.0.0.1
864
fossil_raw cgi cgi.ctl
865
foreach cgiVar {REQUEST_METHOD SCRIPT_NAME PATH_INFO QUERY_STRING REMOTE_ADDR} {
866
unset ::env($cgiVar)
867
}
868
test cgi-debug-file {[file exists debug.txt]
869
&& [string match "*QUERY_STRING = dbg=1*" [read_file debug.txt]]}
870
871
###############################################################################
872
# Trimmed, boolean and "checked" parameter readers, reached through the
873
# email-alert subscription page. The page exists once email is enabled
874
# and its tables are built.
875
876
fossil settings email-send-method db
877
set cgiNotifyBody "submit=Refresh&esm=db&csrf=$cgiToken"
878
cgi_admin [cgi_post_from http://x/setup_notification /setup_notification \
879
$cgiNotifyBody]
880
test cgi-alert-tables-built \
881
{[fossil sql "SELECT count(*) FROM sqlite_schema WHERE name='subscriber'"]
882
== 1}
883
884
# The form renders the "checked" state of the checkboxes.
885
set cgiReply [cgi_admin [request_get /subscribe]]
886
test cgi-subscribe-form {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
887
&& [string match "*checkbox*" $cgiReply]}
888
889
# A submitted subscription: the name is trimmed and the boxes are read as
890
# booleans. A leading and trailing space on the name is removed.
891
set cgiSubBody \
892
[join [list \
893
"submit=1&[email protected]&suname= trimmed " \
894
"&sa=1&sc=on&csrf=$cgiToken"] ""]
895
cgi_admin [cgi_post_from http://x/subscribe /subscribe $cgiSubBody]
896
test_eq cgi-subscribe-trimmed-name \
897
[fossil sql \
898
"SELECT suname FROM subscriber WHERE semail='[email protected]'"] \
899
"trimmed"
900
test cgi-subscribe-boolean-alert \
901
{[string match "*a*" [fossil sql \
902
"SELECT ssub FROM subscriber WHERE semail='[email protected]'"]]}
903
904
###############################################################################
905
# The HTTP reader is called in a loop for a request arriving over ssh.
906
# When the first request announces the Fossil ssh transport, the reader
907
# is called again for a second request in the same connection, and the
908
# later request updates the parameters that the first one set.
909
910
set ::env(SSH_CONNECTION) "127.0.0.1 5000 127.0.0.1 22"
911
write_file request.txt \
912
[join [list \
913
"GET /test-env?first=1 HTTP/1.1\r\nHost: x\r\n" \
914
"X-Fossil-Transport: ssh\r\nContent-Length: 0\r\n\r\n" \
915
"GET /test-env?second=2 HTTP/1.1\r\nHost: x\r\n" \
916
"User-Agent: Later\r\n\r\n"] ""]
917
file delete reply.txt
918
fossil_raw http $cgiRepo --in request.txt --out reply.txt --localauth
919
set cgiReply [read_file reply.txt]
920
test cgi-ssh-transport-first {[string match "*first = 1*" $cgiReply]}
921
test cgi-ssh-transport-second {[string match "*second = 2*" $cgiReply]}
922
unset ::env(SSH_CONNECTION)
923
924
###############################################################################
925
# The skin query parameter sets a cookie with a default path, and marks
926
# the request "udc" unless "once" is present. A cookie carrying a login
927
# card is taken out of the parameters.
928
929
set cgiReply [cgi_admin [request_get /test-env?skin=darkmode]]
930
test cgi-skin-cookie {[string match "*Set-Cookie: fossil_display_settings=*" \
931
$cgiReply] && [cgi_param $cgiReply udc] eq 1}
932
# With "once" the cookie is not written and "udc" is not forced.
933
set cgiReply [cgi_admin [request_get {/test-env?skin=darkmode&once=1}]]
934
test cgi-skin-once {![string match "*Set-Cookie: fossil_display_settings=*" \
935
$cgiReply] && [cgi_param $cgiReply udc] eq "-"}
936
# A skin cookie is honoured as well as a skin query parameter.
937
set cgiReply [cgi_admin [request_get /test-env \
938
[list "Cookie: fossil_display_settings=skin=ardoise"]]]
939
test_eq cgi-skin-from-cookie [cgi_param $cgiReply fossil_display_settings] \
940
skin=ardoise
941
# A login-card cookie is removed from the visible parameters.
942
set cgiReply [cgi_admin [request_get /test-env \
943
[list "Cookie: x-f-l-c=card-value"]]]
944
test_eq cgi-login-card-hidden [cgi_param $cgiReply x-f-l-c] -
945
946
###############################################################################
947
# The reply content type for an unversioned file comes from the
948
# "mimetypes" setting. Parsing of the type stops at the first character
949
# outside the range "+" through "z", and a text type is compressed.
950
951
fossil settings mimetypes "zw text/x|y"
952
write_file f.zw "other text to send"
953
fossil uv add f.zw
954
set cgiReply [cgi_send [request_get /uv/f.zw] --ipaddr 127.0.0.1]
955
test cgi-content-type-stops \
956
{[string match "text/x;*" [reply_header $cgiReply Content-Type]]}
957
958
###############################################################################
959
# A skin name that is not a known skin produces an error, so the cookie
960
# is not written.
961
962
set cgiReply [cgi_admin [request_get /test-env?skin=no_such_skin_here]]
963
test cgi-skin-unknown {![string match "*Set-Cookie: fossil_display_settings=*" \
964
$cgiReply] && [cgi_param $cgiReply skin] eq "no_such_skin_here"}
965
966
###############################################################################
967
# Deleting a cookie: the reply carries a Set-Cookie with a value of
968
# "null" and a negative age. On an https base the cookie is marked
969
# secure.
970
971
proc cgi_cookie_delete {base} {
972
global cgiToken cgiRepo
973
set body "delfossil_display_settings=Delete&csrf=$cgiToken"
974
set req "POST /cookies HTTP/1.1\r\nHost: x\r\nReferer: $base/cookies\r\n"
975
append req "Cookie: fossil_display_settings=skin=xyz\r\n"
976
append req "Content-Type: application/x-www-form-urlencoded\r\n"
977
append req "Content-Length: [string length $body]\r\n\r\n$body"
978
write_file request.txt $req
979
file delete reply.txt
980
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
981
--ipaddr 127.0.0.1 --localauth --baseurl $base
982
return [read_file reply.txt]
983
}
984
set cgiReply [cgi_cookie_delete http://x]
985
test cgi-cookie-delete {[string match \
986
"*Set-Cookie: fossil_display_settings=null;*max-age=-1;*" $cgiReply]}
987
set cgiReply [cgi_cookie_delete https://x/d]
988
test cgi-cookie-delete-secure {[string match "*max-age=-1;*secure;*" $cgiReply]
989
|| [string match "*max-age=-1;* secure*" $cgiReply]}
990
991
###############################################################################
992
# Compression of several reply content types that are named by the "m"
993
# parameter on a raw artifact. A committed file gives a blob to serve.
994
995
write_file art.txt "the quick brown fox jumps over the lazy dog\n"
996
fossil add art.txt
997
fossil commit -m "add art" --no-warnings
998
set cgiArt [fossil sql \
999
"SELECT uuid FROM blob WHERE rid=(SELECT max(rid) FROM blob)"]
1000
proc cgi_raw_type {mime} {
1001
global cgiArt
1002
set reply [cgi_send [request_get /raw/$cgiArt?m=$mime \
1003
[list "Accept-Encoding: gzip"]] --ipaddr 127.0.0.1 --localauth]
1004
return [reply_header $reply Content-Encoding]
1005
}
1006
foreach {cgiName cgiMime} {
1007
javascript application/javascript
1008
json application/json
1009
wasm application/wasm
1010
xtcl application/x-tcl
1011
xtar application/x-tar
1012
appxml application/customxml
1013
} {
1014
test_eq cgi-raw-gzip-$cgiName [cgi_raw_type $cgiMime] gzip
1015
}
1016
# A type outside the compressible set is sent as-is.
1017
test_eq cgi-raw-no-gzip [cgi_raw_type application/octet-stream] -
1018
###############################################################################
1019
# More content types. A type that begins with "a" but is not an
1020
# application type, and an application type outside the list, is sent as
1021
# it is.
1022
1023
foreach {cgiName cgiMime} {
1024
audio audio/mpeg
1025
java application/java-archive
1026
xother application/x-other
1027
} {
1028
test_eq cgi-raw-no-gzip-$cgiName [cgi_raw_type $cgiMime] -
1029
}
1030
# With --nocompress nothing is compressed.
1031
set cgiFile [fossil sql "SELECT uuid FROM blob, mlink, filename
1032
WHERE blob.rid=mlink.fid AND mlink.fnid=filename.fnid
1033
AND filename.name='art.txt'"]
1034
set cgiReply [cgi_send [request_get /raw/$cgiFile?m=text/plain \
1035
[list "Accept-Encoding: gzip"]] --ipaddr 127.0.0.1 --localauth --nocompress]
1036
test cgi-nocompress {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1037
&& [reply_header $cgiReply Content-Encoding] eq "-"
1038
&& [reply_body $cgiReply] eq
1039
"the quick brown fox jumps over the lazy dog\n"}
1040
1041
###############################################################################
1042
# More about ranges. A Range header counts only for GET, and not for
1043
# content of the type that a sync uses. Part of an artifact named by its
1044
# full hash, sent to a visitor who is not logged in, may be kept by the
1045
# client for ever.
1046
1047
set cgiReply [cgi_range bytes=2-5 HEAD]
1048
test cgi-range-head {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1049
&& [reply_header $cgiReply Content-Range] eq "-"}
1050
set cgiReply [cgi_range bytes=5]
1051
test cgi-range-ignored-no-dash {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1052
&& [reply_header $cgiReply Content-Range] eq "-"}
1053
set cgiReply [cgi_send [request_get /raw/$cgiArt?m=application/x-fossil \
1054
[list "Range: bytes=1-3"]] --ipaddr 127.0.0.1 --localauth]
1055
test cgi-range-sync-type {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1056
&& [reply_header $cgiReply Content-Range] eq "-"}
1057
set cgiReply [cgi_send [request_get /raw/$cgiFile?m=text/plain \
1058
[list "Range: bytes=4-8"]] --ipaddr 9.9.9.9]
1059
test_eq cgi-range-artifact [reply_body $cgiReply] quick
1060
test_eq cgi-range-artifact-cache [reply_header $cgiReply Cache-Control] \
1061
"max-age=315360000, immutable"
1062
1063
###############################################################################
1064
# The "at" parameter of /raw names the file for the client to save the
1065
# content in. Only its last path element is used, and every character
1066
# in it other than a letter, a digit, "." and "-" becomes "_".
1067
1068
set cgiReply [cgi_send [request_get \
1069
"/raw/$cgiArt?at=some/dir/my%20file-1%3B.txt"] \
1070
--ipaddr 127.0.0.1 --localauth]
1071
test_eq cgi-attachment-name [reply_header $cgiReply Content-Disposition] \
1072
{attachment; filename="my_file-1_.txt";}
1073
1074
###############################################################################
1075
# A request that is refused before any repository is open: the server is
1076
# given a directory, and the request has a method it does not know.
1077
1078
file mkdir norepo
1079
write_file request.txt "DELETE /x HTTP/1.1\r\nHost: x\r\n\r\n"
1080
file delete reply.txt
1081
fossil_raw http [file join [pwd] norepo] --in request.txt --out reply.txt \
1082
--ipaddr 127.0.0.1
1083
test cgi-no-repository-bad-method {$CODE == 0
1084
&& [reply_status [read_file reply.txt]] eq "HTTP/1.0 400 Bad Request"}
1085
1086
###############################################################################
1087
# More of the same-origin test: a Referer that is the base URL and
1088
# nothing more is the same origin.
1089
1090
test_eq cgi-origin-exact [cgi_origin http://x] "Same origin"
1091
1092
###############################################################################
1093
# With --httptrace the server reports each parameter as it is set and
1094
# copies the request into a file named httplog-NNNNN.txt in the current
1095
# directory. If it cannot make that file it says so and goes on.
1096
1097
foreach cgiLog [glob -nocomplain httplog-*.txt] { file delete $cgiLog }
1098
write_file request.txt [request_get /test-env?traced=2]
1099
file delete reply.txt
1100
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
1101
--ipaddr 127.0.0.1 --localauth --httptrace
1102
set cgiLogs [glob -nocomplain httplog-*.txt]
1103
test cgi-httptrace {$CODE == 0 &&
1104
[holds_strings $RESULT [list "# cgi: traced = \[2\]" \
1105
"# open log on httplog-"]] && [llength $cgiLogs] == 1}
1106
if {[llength $cgiLogs] == 1} {
1107
set cgiLogText [read_file [lindex $cgiLogs 0]]
1108
test cgi-httptrace-log {
1109
[string match "GET /test-env?traced=2 HTTP/1.1*Host: x*" $cgiLogText]}
1110
file delete [lindex $cgiLogs 0]
1111
}
1112
# The file holds the header and the content of a POST.
1113
write_file request.txt [cgi_post application/x-www-form-urlencoded "body=sent"]
1114
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
1115
--ipaddr 127.0.0.1 --localauth --httptrace
1116
set cgiLogs [glob -nocomplain httplog-*.txt]
1117
set cgiLogText ""
1118
foreach cgiLog $cgiLogs {
1119
append cgiLogText [read_file $cgiLog]
1120
file delete $cgiLog
1121
}
1122
test cgi-httptrace-post-content {[string match "*body=sent" $cgiLogText]}
1123
test cgi-httptrace-post-header \
1124
{[string match "POST /test-env HTTP/1.1*" $cgiLogText]}
1125
write_file request.txt [request_get /test-env?traced=2]
1126
if {$::tcl_platform(platform) eq "unix"} {
1127
set cgiHere [pwd]
1128
file mkdir readonly
1129
file attributes readonly -permissions 0555
1130
cd readonly
1131
fossil_raw http $cgiRepo --in [file join $cgiHere request.txt] \
1132
--out [file join $cgiHere reply.txt] \
1133
--ipaddr 127.0.0.1 --localauth --httptrace
1134
cd $cgiHere
1135
file attributes readonly -permissions 0755
1136
test cgi-httptrace-no-log {$CODE == 0
1137
&& [string first "# failed to open httplog-" $RESULT] >= 0
1138
&& [reply_status [read_file reply.txt]] eq "HTTP/1.0 200 OK"}
1139
}
1140
1141
###############################################################################
1142
# A request for the top of the site is sent on to the index page. A
1143
# login-card cookie in such a request is taken once.
1144
1145
set cgiReply [cgi_admin [request_get / [list "Cookie: x-f-l-c=card-value"]]]
1146
test_eq cgi-login-card-home [reply_header $cgiReply Location] http://x/index
1147
1148
###############################################################################
1149
# The honeypot page offers a visitor who is not logged in a captcha, and
1150
# keeps the URL that was asked for, with its query parameters, in a
1151
# cookie.
1152
1153
set cgiReply [cgi_send [request_get "/honeypot?a=1&b&c=two"] --ipaddr 9.9.9.9]
1154
test cgi-original-url {[string first \
1155
"Set-Cookie: fossil-goto=http%3A%2F%2Fx%2Fhoneypot%3Fa%3D1%26b%26c%3Dtwo;" \
1156
$cgiReply] >= 0}
1157
1158
###############################################################################
1159
# The "ss" parameter of /timeline is checked for an attack as soon as it
1160
# is read, for a visitor who is not logged in.
1161
1162
write_file request.txt [request_get "/timeline?ss=1;select"]
1163
file delete reply.txt attack.log
1164
fossil_raw http $cgiRepo --in request.txt --out reply.txt \
1165
--ipaddr 9.9.9.9 --errorlog [file join [pwd] attack.log]
1166
test cgi-attack-read-parameter {
1167
[reply_status [read_file reply.txt]] eq "HTTP/1.0 418 I'm a teapot"
1168
&& [string match "*418 response on \"ss\"*" [read_file attack.log]]}
1169
set cgiReply [cgi_admin [request_get "/timeline?ss=m"]]
1170
test_eq cgi-attack-read-parameter-admin [reply_status $cgiReply] \
1171
"HTTP/1.0 200 OK"
1172
1173
###############################################################################
1174
# SSH_CONNECTION names the client only when it holds an address followed
1175
# by a space. Otherwise the request is read as plain HTTP from an
1176
# unknown address, which --localauth does not log in.
1177
1178
foreach {cgiName cgiValue} {empty "" one-word nospace} {
1179
set ::env(SSH_CONNECTION) $cgiValue
1180
set cgiReply [cgi_send [request_get /test-env] --localauth]
1181
unset ::env(SSH_CONNECTION)
1182
test_eq cgi-ssh-connection-$cgiName [reply_header $cgiReply Location] \
1183
http://x/login?g=test-env
1184
}
1185
1186
###############################################################################
1187
# SCGI: SCRIPT_NAME, PATH_INFO and REQUEST_URI. The server needs
1188
# SCRIPT_NAME, or both of the others to work it out from; and one of
1189
# PATH_INFO and REQUEST_URI, from which it works out the other. If
1190
# SCRIPT_NAME is not the start of REQUEST_URI, it is cut down to the
1191
# part that is.
1192
1193
# The reply to an SCGI request with the name and value PAIRS added to
1194
# the usual ones: the status and the error for a request that is
1195
# refused, or else the status and what /test-env shows for SCRIPT_NAME,
1196
# PATH_INFO and REQUEST_URI.
1197
proc cgi_scgi_names {pairs} {
1198
set reply [cgi_send [cgi_scgi [concat {
1199
CONTENT_LENGTH 0 SCGI 1 REQUEST_METHOD GET SERVER_NAME x SERVER_PORT 80
1200
REMOTE_ADDR 127.0.0.1
1201
} $pairs]] --scgi --localauth]
1202
set status [lindex [reply_status $reply] 1]
1203
if {$status eq "400"} {
1204
return [list 400 [string trim [reply_body $reply]]]
1205
}
1206
if {$status ne "200"} { return $status }
1207
return [list 200 [cgi_param $reply SCRIPT_NAME] [cgi_param $reply PATH_INFO] \
1208
[cgi_param $reply REQUEST_URI]]
1209
}
1210
foreach {cgiName cgiPairs cgiWant} {
1211
nothing
1212
{}
1213
{400 {Bad SCGI Request: missing SCRIPT_NAME}}
1214
uri-only
1215
{REQUEST_URI /test-env}
1216
{400 {Bad SCGI Request: missing SCRIPT_NAME}}
1217
path-only
1218
{PATH_INFO /test-env}
1219
{400 {Bad SCGI Request: missing SCRIPT_NAME}}
1220
path-not-in-uri
1221
{REQUEST_URI /abc PATH_INFO /test-env}
1222
{400 {Bad SCGI Request: PATH_INFO not found in REQUEST_URI}}
1223
script-from-uri-and-path
1224
{REQUEST_URI /pre/test-env PATH_INFO /test-env}
1225
{200 /pre /test-env /pre/test-env}
1226
script-only
1227
{SCRIPT_NAME {}}
1228
{400 {Bad SCGI Request: missing PATH_INFO and/or REQUEST_URI}}
1229
uri-from-path
1230
{SCRIPT_NAME {} PATH_INFO /test-env}
1231
{200 {} /test-env /test-env}
1232
uri-from-path-empty-query
1233
{SCRIPT_NAME {} PATH_INFO /test-env QUERY_STRING {}}
1234
{200 {} /test-env /test-env}
1235
uri-from-path-and-query
1236
{SCRIPT_NAME {} PATH_INFO /test-env QUERY_STRING s=1}
1237
{200 {} /test-env /test-env?s=1}
1238
uri-from-path-no-slash
1239
{SCRIPT_NAME /pre PATH_INFO test-env/x}
1240
404
1241
path-from-uri
1242
{SCRIPT_NAME {} REQUEST_URI /test-env}
1243
{200 {} /test-env /test-env}
1244
path-from-uri-with-query
1245
{SCRIPT_NAME {} REQUEST_URI /test-env?s=1 QUERY_STRING s=1}
1246
{200 {} /test-env /test-env?s=1}
1247
script-cut-down
1248
{SCRIPT_NAME /abcd REQUEST_URI /abc/test-env}
1249
{200 /abc /test-env /abc/test-env}
1250
uri-is-script
1251
{SCRIPT_NAME /abc REQUEST_URI /abc}
1252
302
1253
} {
1254
test_eq cgi-scgi-names-$cgiName [cgi_scgi_names $cgiPairs] $cgiWant
1255
}
1256
foreach {cgiName cgiRequest cgiError} [list \
1257
empty "" "SCGI header too short" \
1258
length-only "20" "cannot read entire SCGI header" \
1259
] {
1260
set cgiReply [cgi_send $cgiRequest --scgi]
1261
test cgi-scgi-$cgiName {$CODE == 0
1262
&& [reply_status $cgiReply] eq "HTTP/1.0 400 Bad Request"
1263
&& [string match "*$cgiError*" $cgiReply]}
1264
}
1265
1266
###############################################################################
1267
# Page aliases. An alias in the configuration names a page and query
1268
# parameters for it: "NAME=VALUE" sets a parameter, replacing one of the
1269
# same name in the request; a name in capitals is taken in lower case;
1270
# "NAME!" removes a parameter of the request.
1271
1272
fossil sql \
1273
"REPLACE INTO config(name,value,mtime) VALUES('walias:/cgialias','/test-env?a=new&Bb=2&gone!&absent!&fresh',0)"
1274
fossil sql \
1275
[join [list \
1276
"REPLACE INTO config(name,value,mtime) " \
1277
"VALUES('walias:/cgialias2','/test-env?zz!',0)"] ""]
1278
set cgiReply [cgi_admin [request_get "/cgialias?a=old&gone=1&keep=3"]]
1279
test_eq cgi-alias-replace [cgi_param $cgiReply a] new
1280
test_eq cgi-alias-upper [cgi_param $cgiReply bb] 2
1281
test_eq cgi-alias-delete [cgi_param $cgiReply gone] -
1282
test_eq cgi-alias-add [cgi_param $cgiReply fresh] ""
1283
test_eq cgi-alias-keep [cgi_param $cgiReply keep] 3
1284
set cgiReply [cgi_admin [request_get "/cgialias2?m=1&zz=9"]]
1285
test {cgi-alias-delete-last} {[cgi_param $cgiReply zz] eq "-"
1286
&& [cgi_param $cgiReply m] eq "1"}
1287
1288
###############################################################################
1289
# The configuration page saves the text given for the main menu, unless
1290
# the request also asks for the menu to be reset: then the saved menu is
1291
# removed and the text is not looked at.
1292
1293
proc cgi_menu_post {fields} {
1294
global cgiToken
1295
set body "$fields&csrf=$cgiToken"
1296
set reply [cgi_admin [cgi_post_from http://x/setup_config /setup_config \
1297
$body]]
1298
return [list [lindex [reply_status $reply] 1] \
1299
[fossil sql "SELECT value FROM config WHERE name='mainmenu'"]]
1300
}
1301
test_eq cgi-menu-saved [cgi_menu_post "mmenu=cgi-menu-text&zlast=1"] \
1302
{200 cgi-menu-text}
1303
test_eq cgi-menu-reset [cgi_menu_post "mmenu=other-text&resetMenu=1&zlast=1"] \
1304
{200 {}}
1305
test_eq cgi-menu-reset-alone [cgi_menu_post "resetMenu=1"] {200 {}}
1306
1307
###############################################################################
1308
# More of the trimmed, boolean and "checked" readers on the subscription
1309
# page. A name that is missing or all spaces is no name; a line end of
1310
# CR LF inside a name becomes LF, and a CR alone is kept. A box with an
1311
# empty value is on, and one with the value "off" is off.
1312
1313
proc cgi_subscribe {email fields} {
1314
global cgiToken
1315
set body "submit=1&e=$email&$fields&csrf=$cgiToken"
1316
cgi_admin [cgi_post_from http://x/subscribe /subscribe $body]
1317
return [fossil sql \
1318
"SELECT hex(suname), ssub FROM subscriber WHERE semail='$email'"]
1319
}
1320
test_eq cgi-subscribe-no-name [cgi_subscribe [email protected] "sa=&sc=off"] \
1321
"|a"
1322
test_eq cgi-subscribe-blank-name \
1323
[cgi_subscribe [email protected] "suname=+++&sa=0&sc=1"] "|c"
1324
test_eq cgi-subscribe-line-ends \
1325
[cgi_subscribe [email protected] "suname=+one%0D%0Atwo%0Dend&sa=1"] \
1326
"6F6E650A74776F0D656E64|a"
1327
# The form shows a box as checked for a value that is a word for "true".
1328
set cgiReply [cgi_admin [request_get "/subscribe?submit=1&sa=on&sc=off&st=2"]]
1329
test cgi-subscribe-checked-word {
1330
[string match "*name=\"sa\" checked>*" $cgiReply]
1331
&& [string match "*name=\"sc\" >*" $cgiReply]
1332
&& [string match "*name=\"st\" >*" $cgiReply]}
1333
1334
###############################################################################
1335
# A request that is refused when Fossil runs as a CGI program names the
1336
# web server, if the web server gave its name. A CGI request with no
1337
# REQUEST_METHOD is not a POST.
1338
1339
# No work in the background after a reply from here on: its process
1340
# would outlive the test.
1341
fossil settings backoffice-disable 1
1342
write_file cgi2.ctl "repository: [file nativename $cgiRepo]\nlocalauth\n"
1343
proc cgi_cgi {vars} {
1344
global RESULT
1345
foreach {name value} $vars { set ::env($name) $value }
1346
fossil_raw cgi cgi2.ctl
1347
foreach {name value} $vars { unset ::env($name) }
1348
return $RESULT
1349
}
1350
set cgiReply [cgi_cgi {REQUEST_METHOD POST SCRIPT_NAME /s PATH_INFO /test-env
1351
REMOTE_ADDR 127.0.0.1}]
1352
test cgi-cgi-refused {[string match "Status: 400 Bad Request*" $cgiReply]
1353
&& [string match \
1354
"*\nBad CGI Request: missing CONTENT_LENGTH on a POST method*" \
1355
$cgiReply]}
1356
set cgiReply [cgi_cgi {REQUEST_METHOD POST SCRIPT_NAME /s PATH_INFO /test-env
1357
REMOTE_ADDR 127.0.0.1 SERVER_SOFTWARE TestServer/1}]
1358
test cgi-cgi-refused-server-named {
1359
[string match \
1360
"*\nBad CGI Request from \"TestServer/1\": missing CONTENT_LENGTH*" \
1361
$cgiReply]}
1362
set cgiReply [cgi_cgi {SCRIPT_NAME /s PATH_INFO /test-env REMOTE_ADDR 127.0.0.1
1363
HTTP_HOST x HTTP_REFERER http://x/s/prev}]
1364
test_eq cgi-cgi-no-method [cgi_csrf_level $cgiReply] "Same origin"
1365
1366
# A control file that names a debug file which cannot be opened is used
1367
# all the same.
1368
write_file cgi3.ctl \
1369
[join [list \
1370
"repository: [file nativename $cgiRepo]\nlocalauth\n" \
1371
"cgi-debug: [file join [pwd] nosuchdir debug.txt]\n"] ""]
1372
set ::env(REQUEST_METHOD) GET
1373
set ::env(SCRIPT_NAME) /s
1374
set ::env(PATH_INFO) /test-env
1375
set ::env(QUERY_STRING) "nodebug=1"
1376
set ::env(REMOTE_ADDR) 127.0.0.1
1377
fossil_raw cgi cgi3.ctl
1378
foreach cgiVar {REQUEST_METHOD SCRIPT_NAME PATH_INFO QUERY_STRING REMOTE_ADDR} {
1379
unset ::env($cgiVar)
1380
}
1381
test cgi-debug-file-unopened {$CODE == 0
1382
&& [string match "Status: 200 OK*" $RESULT]
1383
&& [cgi_param $RESULT nodebug] eq "1"}
1384
1385
###############################################################################
1386
# The JSON interface, where Fossil has it. Content that is not a JSON
1387
# object is refused with error FOSSIL-1101. Content of a type that is
1388
# not JSON is not read as JSON. A request that the web server marks
1389
# "no_json" is not prepared as a JSON request when it is read.
1390
1391
fossil test-th-eval "hasfeature json"
1392
if {[normalize_result] eq "1"} {
1393
foreach {cgiName cgiBody} {not-json {not json} array {[1]} one-byte x} {
1394
set cgiReply [cgi_admin [cgi_post application/json $cgiBody /json/version]]
1395
test cgi-json-post-$cgiName-exit {$CODE == 0}
1396
test cgi-json-post-$cgiName-reply \
1397
{[string match "*FOSSIL-1101*" $cgiReply]}
1398
}
1399
set cgiReply [cgi_admin [cgi_post text/x-other {[1]} /json/version]]
1400
test cgi-json-post-other-type {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1401
&& [string match "*jsonApiVersion*" $cgiReply]}
1402
set cgiReply [cgi_send [cgi_scgi {
1403
CONTENT_LENGTH 8 CONTENT_TYPE application/json SCGI 1 REQUEST_METHOD POST
1404
SCRIPT_NAME {} PATH_INFO /json/version SERVER_NAME x SERVER_PORT 80
1405
REMOTE_ADDR 127.0.0.1 no_json 1
1406
} "not json"] --scgi --localauth]
1407
test cgi-json-no-json {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1408
&& [string match "*jsonApiVersion*" $cgiReply]}
1409
}
1410
1411
###############################################################################
1412
# More of a connection over ssh. An older client, after its probes,
1413
# sends a blank line twice and the command again before each further
1414
# request. A request may name the repository file before the page.
1415
1416
set ::env(SSH_CONNECTION) "127.0.0.1 5000 127.0.0.1 22"
1417
set cgiFirst \
1418
[join [list \
1419
"echo test1\nfossil\nGET /test-env?first=1 HTTP/1.1\r\n" \
1420
"Host: x\r\nUser-Agent: First\r\n" \
1421
"X-Fossil-Transport: other\r\n\r\n"] ""]
1422
set cgiReply [cgi_send \
1423
[join [list \
1424
"$cgiFirst\n\nfossil\nGET /test-env?second=2 HTTP/1.1\r\n" \
1425
"Host: x\r\nContent-Length: 0\r\nContent-Type: text/plain\r\n" \
1426
"X-Fossil-Transport: ssh\r\n\r\n"] ""] --localauth]
1427
test cgi-ssh-flip {[string match "test1\n*" $cgiReply]
1428
&& [string match "*HTTP_USER_AGENT = First*first = 1*second = 2*" \
1429
$cgiReply]}
1430
foreach {cgiName cgiMore cgiError} [list \
1431
cut-short "\n" "incorrect transport_flip" \
1432
not-blank "\nxyz\n" "transport_flip failed" \
1433
other-command "\n\nother\n" "transport_open failed" \
1434
no-request "\n\nfossil\n" "missing HTTP header" \
1435
] {
1436
set cgiReply [cgi_send $cgiFirst$cgiMore --localauth]
1437
test cgi-ssh-flip-$cgiName {$CODE == 0
1438
&& [string match "*first = 1*400 Bad Request*$cgiError*" $cgiReply]}
1439
}
1440
# With nothing after the two blank lines the connection is over.
1441
set cgiReply [cgi_send "$cgiFirst\n\n" --localauth]
1442
test cgi-ssh-flip-end {$CODE == 0 && [string match "*first = 1*" $cgiReply]
1443
&& ![string match "*400 Bad Request*" $cgiReply]}
1444
foreach {cgiName cgiRequest cgiError} [list \
1445
probes-only "echo test1\nfossil\n" "missing HTTP header" \
1446
blank-first "\nGET /test-env HTTP/1.1\r\nHost: x\r\n\r\n" \
1447
"unsupported HTTP method" \
1448
empty-command \
1449
[join [list \
1450
"echo test1\n\nGET /test-env?first=1 HTTP/1.1\r\n" \
1451
"Host: x\r\n\r\n\n\nfossil\n"] ""] \
1452
"transport_open failed" \
1453
repository-only "GET $cgiRepo HTTP/1.1\r\nHost: x\r\n\r\n" \
1454
"malformed URL in HTTP header" \
1455
] {
1456
set cgiReply [cgi_send $cgiRequest --localauth]
1457
test cgi-ssh-$cgiName {$CODE == 0
1458
&& [string match "*400 Bad Request*$cgiError*" $cgiReply]}
1459
}
1460
set cgiReply [cgi_send [request_get $cgiRepo/test-env?named=1] --localauth]
1461
test cgi-ssh-repository-in-url {[cgi_param $cgiReply named] eq "1"
1462
&& [cgi_param $cgiReply PATH_INFO] eq "/test-env"}
1463
# The name is taken off only when a "/" or the end follows it, and only
1464
# for GET.
1465
set cgiReply [cgi_send [request_get ${cgiRepo}x/test-env] --localauth]
1466
test_eq cgi-ssh-repository-longer [cgi_param $cgiReply PATH_INFO] -
1467
set cgiReply [cgi_send [request_get $cgiRepo/test-env \
1468
[list "Content-Length: 0"] \
1469
POST] --localauth]
1470
test_eq cgi-ssh-repository-post [cgi_param $cgiReply PATH_INFO] -
1471
# Two requests on one ssh connection: the second sends Content-Length
1472
# before a longer header line and must get its length and body.
1473
# knownBug: the second request's Content-Length is read from memory that
1474
# a later header line overwrites.
1475
set cgiFirst \
1476
[join [list \
1477
"POST /test-env HTTP/1.1\r\nX-Fossil-Transport: SSH\r\n" \
1478
"Content-Type: application/x-www-form-urlencoded\r\n" \
1479
"Content-Length: 3\r\n\r\na=1"] ""]
1480
set cgiSecond \
1481
[join [list \
1482
"POST /test-env HTTP/1.1\r\nContent-Length: 3\r\n" \
1483
"Content-Type: " \
1484
"application/x-www-form-urlencoded-and-more\r\n\r\nb=2"] ""]
1485
set cgiReply [cgi_send $cgiFirst$cgiSecond --localauth]
1486
test cgi-ssh-later-content-length {$CODE == 0 && [string match \
1487
[join [list \
1488
"*CONTENT_LENGTH = 3*a = 1*CONTENT_LENGTH = 3*b = " \
1489
"2*"] ""] \
1490
[regsub -all {<[^>]*>} $cgiReply {}]]} knownBug
1491
unset ::env(SSH_CONNECTION)
1492
1493
###############################################################################
1494
# A visitor who is not logged in reaches a page that is closed to robots
1495
# with a token, and gets a cookie that opens the page afterwards. The
1496
# same value given as a query parameter does not.
1497
1498
fossil sql \
1499
[join [list \
1500
"REPLACE INTO config(name,value,mtime) " \
1501
"VALUES('robot-restrict','timeline',0)"] ""]
1502
fossil sql \
1503
[join [list \
1504
"REPLACE INTO config(name,value,mtime) " \
1505
"VALUES('token-cgitoken','{\"user\":\"someone\"}',0)"] ""]
1506
proc cgi_robot {path {cookie ""}} {
1507
set headers [list "User-Agent: Agent/1"]
1508
if {$cookie ne ""} { lappend headers "Cookie: $cookie" }
1509
return [cgi_send [request_get $path $headers] --ipaddr 9.9.9.9]
1510
}
1511
set cgiRobot "*Checking to see if you are a robot*"
1512
test cgi-robot-stopped {[string match $cgiRobot [cgi_robot /timeline]]}
1513
set cgiReply [cgi_robot /timeline?token=cgitoken]
1514
set cgiProof ""
1515
regexp {Set-Cookie: fossil-client-ok=(\d+);} $cgiReply all cgiProof
1516
test cgi-robot-token {$cgiProof ne "" && ![string match $cgiRobot $cgiReply]}
1517
test cgi-robot-cookie {![string match $cgiRobot \
1518
[cgi_robot /timeline fossil-client-ok=$cgiProof]]}
1519
test cgi-robot-query-parameter {[string match $cgiRobot \
1520
[cgi_robot /timeline?fossil-client-ok=$cgiProof]]}
1521
# A parameter that failed as proof is left out of the links of the page
1522
# that a good token opens.
1523
set cgiReply [cgi_robot "/timeline?proof=1&token=cgitoken&n=1"]
1524
test cgi-robot-failed-proof-dropped {
1525
[string match "*timeline?*token=cgitoken*" $cgiReply]
1526
&& ![string match "*proof=1*" $cgiReply]}
1527
fossil sql "DELETE FROM config WHERE name='robot-restrict'"
1528
1529
###############################################################################
1530
# A forum thread is shown in order of time to a phone and as a hierarchy
1531
# to anything else, an iPad included. The page offers the other order.
1532
1533
set cgiFd [open post.txt wb]
1534
puts -nonewline $cgiFd \
1535
"D 2026-01-04T10:00:01.000\nH Cgi\\sthread\nU alice\nW 5\nhello\n"
1536
close $cgiFd
1537
fossil md5sum post.txt
1538
set cgiSum [lindex $RESULT 0]
1539
set cgiFd [open post.txt ab]
1540
puts -nonewline $cgiFd "Z $cgiSum\n"
1541
close $cgiFd
1542
fossil test-content-put --crosslink post.txt
1543
set cgiPost [fossil sql \
1544
"SELECT uuid FROM blob WHERE rid=(SELECT fpid FROM forumpost LIMIT 1)"]
1545
foreach {cgiName cgiAgent cgiOther} {
1546
phone "Mozilla/5.0 (iPhone; Mobile) Safari" Hierarchical
1547
ipad "Mozilla/5.0 (iPad; Mobile) Safari" Chronological
1548
desk "Mozilla/5.0 (X11; Linux)" Chronological
1549
none "" Chronological
1550
} {
1551
set cgiHeaders {}
1552
if {$cgiAgent ne ""} { lappend cgiHeaders "User-Agent: $cgiAgent" }
1553
set cgiReply [cgi_admin [request_get /forumthread/$cgiPost?t=a $cgiHeaders]]
1554
test cgi-forum-order-$cgiName {[string match "*>Cgi thread<*" $cgiReply]
1555
&& [string match "*?t=?\">$cgiOther</a>*" $cgiReply]}
1556
}
1557
1558
###############################################################################
1559
# Logging in without "remember me" gives a cookie that lasts for the
1560
# session: it has no age.
1561
1562
fossil user new cgiuser contact userpw
1563
fossil user capabilities cgiuser o
1564
proc cgi_login {fields} {
1565
set body "u=cgiuser&p=userpw&in=Login&$fields"
1566
return [cgi_send [cgi_post_from http://x/login /login $body] --ipaddr 9.9.9.9]
1567
}
1568
set cgiCookie [reply_header [cgi_login x=1] Set-Cookie]
1569
test cgi-login-session-cookie \
1570
{[string match "fossil-*=*cgiuser; Path=*; HttpOnly;*" $cgiCookie]
1571
&& ![string match "*max-age*" $cgiCookie]}
1572
set cgiCookie [reply_header [cgi_login remember=1] Set-Cookie]
1573
test cgi-login-lasting-cookie \
1574
{[string match "fossil-*=*cgiuser; Path=*; max-age=*" $cgiCookie]}
1575
1576
###############################################################################
1577
# A program under /ext that gives a status of 0 gets the usual "200 OK".
1578
1579
if {$::tcl_platform(platform) eq "unix"} {
1580
file mkdir extroot
1581
write_file extroot/status0.sh \
1582
[join [list \
1583
"#!/bin/sh\n" \
1584
"printf 'Status: 0 Nothing\\r\\nContent-Type: " \
1585
"text/plain\\r\\n\\r\\nfrom the program\\n'\n"] ""]
1586
file attributes extroot/status0.sh -permissions 0755
1587
set cgiReply [cgi_admin [request_get /ext/status0.sh] \
1588
--extroot [file join [pwd] extroot]]
1589
test cgi-ext-status-zero {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1590
&& [string match "*from the program*" $cgiReply]
1591
&& ![string match "*printf*" $cgiReply]}
1592
}
1593
1594
###############################################################################
1595
# A CGI control file that names no repository gets "500", and so does a
1596
# CGI request with no control file at all. A control file with a
1597
# "redirect: * URL" line sends every request on for good: to URL with
1598
# the name asked for in place of "%s", to URL as it is if it holds a
1599
# "?", and otherwise to URL followed by the path and the query.
1600
1601
# Run fossil with the words in ARGS and the environment variables in
1602
# VARS, a list of names and values, and return what it printed.
1603
proc cgi_run_with {vars args} {
1604
global RESULT
1605
foreach {name value} $vars { set ::env($name) $value }
1606
fossil_raw {*}$args
1607
foreach {name value} $vars { unset ::env($name) }
1608
return $RESULT
1609
}
1610
# Run from a directory outside the checkout, as a CGI script would be.
1611
set cgiDir [file join [file dirname [pwd]] cgi-no-checkout]
1612
file mkdir $cgiDir
1613
write_file [file join $cgiDir cgi5.ctl] "#!/usr/bin/fossil\n"
1614
set cgiHere [pwd]
1615
cd $cgiDir
1616
set cgiReply [cgi_run_with {REQUEST_METHOD GET SCRIPT_NAME /s PATH_INFO /home} \
1617
cgi cgi5.ctl]
1618
cd $cgiHere
1619
file delete -force $cgiDir
1620
test cgi-cgi-no-repository {$CODE == 1
1621
&& [string match "Status: 500 Internal Server Error*" $cgiReply]
1622
&& [string match "*Unable to find or open the project repository" \
1623
$cgiReply]}
1624
set cgiReply [cgi_run_with {GATEWAY_INTERFACE CGI/1.1 REQUEST_METHOD GET
1625
SCRIPT_NAME /s PATH_INFO /home}]
1626
test cgi-cgi-no-control-file {$CODE == 1
1627
&& [string match "Status: 500 Internal Server Error*" $cgiReply]
1628
&& [string match "*No CGI control file specified" $cgiReply]}
1629
foreach {cgiName cgiTarget cgiLocation} {
1630
name http://other.example/find/%s http://other.example/find/abc
1631
query http://other.example/find?x=1 http://other.example/find?x=1
1632
path http://other.example/base http://other.example/base/abc?q=1
1633
} {
1634
write_file cgi6.ctl "#!/usr/bin/fossil\nredirect: * $cgiTarget\n"
1635
set cgiReply [cgi_run_with {REQUEST_METHOD GET HTTP_HOST x SCRIPT_NAME /s
1636
PATH_INFO /abc QUERY_STRING q=1} cgi cgi6.ctl]
1637
test cgi-cgi-redirect-$cgiName {$CODE == 0
1638
&& [string match "Status: 301 Moved Permanently*" $cgiReply]
1639
&& [reply_header "$cgiReply\r\n\r\n" Location] eq $cgiLocation}
1640
}
1641
1642
###############################################################################
1643
# The setup page warns of a page header that has no "base" element, and
1644
# does not when the header has one, as the built-in header does.
1645
1646
set cgiReply [cgi_admin [request_get /setup]]
1647
test cgi-setup-base-present {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1648
&& ![string match "*Configuration Error*" $cgiReply]}
1649
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('header',
1650
'<html><head><title>plain</title></head><body>',now())"
1651
set cgiReply [cgi_admin [request_get /setup]]
1652
test cgi-setup-base-missing {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1653
&& [string match "*Configuration Error*base href=*" $cgiReply]}
1654
1655
# The TH1 command "redirect" in a page header sends the client to
1656
# another page: with "302", or with "307" when its second argument is
1657
# true, which tells the client to repeat the request as it made it.
1658
foreach {cgiName cgiScript cgiStatus} {
1659
plain {redirect /stat} {HTTP/1.0 302 Moved Temporarily}
1660
method {redirect /stat 1} {HTTP/1.0 307 Temporary Redirect}
1661
} {
1662
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('header',
1663
'<html><head></head><body><th1>$cgiScript</th1>',now())"
1664
set cgiReply [cgi_admin [request_get /timeline]]
1665
test cgi-th1-redirect-$cgiName {[reply_status $cgiReply] eq $cgiStatus
1666
&& [reply_header $cgiReply Location] eq "http://x/stat"}
1667
}
1668
fossil sql "DELETE FROM config WHERE name='header'"
1669
1670
# The same command run from the command line, where there is no request
1671
# to answer, is an error.
1672
foreach {cgiName cgiUrl} {path /stat url http://other.example/x} {
1673
fossil_raw test-th-eval "redirect $cgiUrl"
1674
test cgi-th1-redirect-no-request-$cgiName {$CODE == 0 && [string match \
1675
"*redirect is only allowed in a web request*" $RESULT]}
1676
}
1677
# With --http the command line makes a reply, and a redirect to a full
1678
# URL is sent as "302".
1679
fossil_raw test-th-eval --http "redirect http://other.example/x"
1680
test cgi-th1-redirect-http-option {$CODE == 0
1681
&& [string match "HTTP/1.0 302 *" $RESULT]
1682
&& [string match "*\r\nLocation: http://other.example/x\r\n*" $RESULT]}
1683
1684
# The TH1 command "verifyCsrf" from the command line: with no request
1685
# there is no origin to compare, so it refuses.
1686
fossil_raw test-th-eval verifyCsrf
1687
test cgi-th1-verifycsrf-no-request {$CODE == 1
1688
&& [string match "*possible CSRF attack*" $RESULT]}
1689
1690
###############################################################################
1691
# An ad unit for the right-hand side is used on a page that allows one
1692
# and has no table in its content; a page with a table gets the ad unit
1693
# for the top of the page.
1694
1695
fossil sql "REPLACE INTO config(name,value,mtime) VALUES
1696
('adunit-right','<b>ADRIGHT</b>',now()),('adunit','<b>ADTOP</b>',now())"
1697
set cgiReply [cgi_admin [request_get /sitemap]]
1698
test cgi-adunit-right {[string match "*ADRIGHT*" $cgiReply]
1699
&& ![string match "*ADTOP*" $cgiReply]}
1700
set cgiReply [cgi_admin [request_get /brlist]]
1701
test cgi-adunit-top-with-table {[string match "*ADTOP*" $cgiReply]
1702
&& ![string match "*ADRIGHT*" $cgiReply]}
1703
fossil sql "DELETE FROM config WHERE name IN ('adunit-right','adunit')"
1704
1705
###############################################################################
1706
# Pages that add a query parameter to the request they were given.
1707
# /file with no file name lists the files of the check-in "tip", and
1708
# its links carry "ci=tip". /timeline with "r=BRANCH" shows the branch
1709
# and what is related to it, and its form keeps that as "t=BRANCH" and
1710
# "rel".
1711
1712
set cgiReply [cgi_admin [request_get /file]]
1713
test cgi-file-no-name {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1714
&& [string match "*Top-level Files of tip*" $cgiReply]
1715
&& [string match "*ci=tip*" $cgiReply]}
1716
set cgiReply [cgi_admin [request_get /timeline?r=trunk]]
1717
test cgi-timeline-r {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1718
&& [string match "*name=\"rel\" value=\"1\"*" $cgiReply]
1719
&& [string match "*name=\"t\" value=\"trunk\"*" $cgiReply]}
1720
1721
###############################################################################
1722
# An unversioned file whose time of change is too far in the future to
1723
# be written as a date is sent all the same, with an empty Last-Modified
1724
# header.
1725
1726
write_file late.css "p{}\n"
1727
fossil uv add late.css
1728
fossil sql "UPDATE unversioned SET mtime=1000000000000000000
1729
WHERE name='late.css'"
1730
set cgiReply [cgi_send [request_get /uv/late.css] --ipaddr 127.0.0.1]
1731
test cgi-last-modified-out-of-range {
1732
[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1733
&& [string match "*\r\nLast-Modified: \r\n*" $cgiReply]
1734
&& [reply_body $cgiReply] eq "p{}\n"}
1735
fossil uv rm late.css
1736
1737
###############################################################################
1738
# "fossil server" itself, where it listens with its own code (not on
1739
# Windows). A server that cannot listen where it was told says why and
1740
# stops.
1741
1742
if {$::tcl_platform(platform) eq "unix" && [loopback_available]} {
1743
# A port on ADDRESS with a listener on it. Returns the listener and
1744
# the port.
1745
proc cgi_busy_port {address} {
1746
set listener [socket -server {apply {{c a p} {close $c}}} \
1747
-myaddr $address 0]
1748
return [list $listener [lindex [fconfigure $listener -sockname] 2]]
1749
}
1750
# A port on 127.0.0.1 that was free a moment ago.
1751
proc cgi_free_port {} {
1752
lassign [cgi_busy_port 127.0.0.1] listener port
1753
close $listener
1754
return $port
1755
}
1756
write_file notsocket "a file\n"
1757
lassign [cgi_busy_port 127.0.0.1] cgiListener cgiPort
1758
foreach {cgiName cgiOptions cgiError} [list \
1759
bad-ipv4 {--port 999.9.9.9:8111} \
1760
"not a valid IPv4 address: 999.9.9.9" \
1761
bad-ipv6 {--port {[zz::1]:8111}} \
1762
"not a valid IPv6 address: zz::1" \
1763
other-ipv4 {--port 192.0.2.1:8111} \
1764
"cannot open a listening socket on 192.0.2.1:8111" \
1765
other-ipv6 {--port {[2001:db8::1]:8111}} \
1766
[join [list \
1767
"cannot open a listening socket on " \
1768
"\\\[2001:db8::1\\\]:8111"] ""] \
1769
port-in-use [list --localhost --port $cgiPort] \
1770
"no available TCP ports in the range $cgiPort..$cgiPort" \
1771
socket-name-long [list --socket-name [string repeat s 200]] \
1772
"name of unix socket too big" \
1773
socket-name-taken {--socket-name notsocket} \
1774
"cannot name socket \"notsocket\" because another object" \
1775
] {
1776
fossil_raw -seconds 30 server {*}$cgiOptions $cgiRepo
1777
test cgi-server-refused-$cgiName {$CODE == 1
1778
&& [string match "*$cgiError*" $RESULT]}
1779
}
1780
close $cgiListener
1781
1782
# Run SCRIPT every ten milliseconds until it returns true, for at most
1783
# five seconds, and return what it last returned.
1784
proc cgi_wait {script} {
1785
for {set i 0} {$i < 500} {incr i} {
1786
if {[uplevel 1 $script]} {return 1}
1787
after 10
1788
}
1789
return 0
1790
}
1791
# True if the process PID has not ended.
1792
proc cgi_running {pid} {
1793
return [expr {![catch {exec kill -0 $pid}]}]
1794
}
1795
# Wait for the server PID to print that it listens, and return true
1796
# once it has. A server that ends without listening ends the wait.
1797
proc cgi_listening {pid} {
1798
cgi_wait {expr {[string match "*Listening for*" [read_file server.out]]
1799
|| ![cgi_running $pid]}}
1800
return [string match "*Listening for*" [read_file server.out]]
1801
}
1802
# Connect to HOST and PORT and return the socket. A server prints
1803
# that it listens before it accepts connections, so a connection that
1804
# fails is tried again.
1805
proc cgi_connect {host port} {
1806
cgi_wait {expr {![catch {socket $host $port} sock]}}
1807
fconfigure $sock -translation binary
1808
return $sock
1809
}
1810
# Send REQUEST to HOST and PORT and return the reply, waiting at most
1811
# ten seconds for the end of it. With "unix" for HOST, PORT is the
1812
# name of a unix socket, and the program "nc" carries the request.
1813
proc cgi_client {host port request} {
1814
if {$host eq "unix"} {
1815
cgi_wait {expr {![catch {exec nc -w 10 -U $port << $request} reply]
1816
|| ![string match "*refused*" $reply]}}
1817
return $reply
1818
}
1819
set sock [cgi_connect $host $port]
1820
fconfigure $sock -blocking 0
1821
puts -nonewline $sock $request
1822
flush $sock
1823
set reply ""
1824
for {set i 0} {$i < 2000 && ![eof $sock]} {incr i} {
1825
set part [read $sock]
1826
append reply $part
1827
if {$part eq ""} {after 5}
1828
}
1829
close $sock
1830
return $reply
1831
}
1832
# Start a server with OPTIONS that answers one request and stops, send
1833
# it REQUEST at HOST and PORT, and return what the server printed and
1834
# the reply. The server is killed if it has not stopped by itself.
1835
# The request is sent IDLE milliseconds after the server starts to
1836
# listen.
1837
proc cgi_serve_once {options host port request {command server} {idle 0}} {
1838
global fossilexe cgiRepo
1839
file delete server.out
1840
set pid [exec $fossilexe $command --debug-nofork --localauth {*}$options \
1841
$cgiRepo >& server.out &]
1842
set reply ""
1843
if {[cgi_listening $pid]} {
1844
after $idle
1845
catch {set reply [cgi_client $host $port $request]}
1846
}
1847
cgi_wait {expr {![cgi_running $pid]}}
1848
catch {exec kill -9 $pid}
1849
return [list [string trim [read_file server.out]] $reply]
1850
}
1851
1852
# An IPv4 address, by number and as "localhost".
1853
foreach {cgiName cgiHost} {ipv4 127.0.0.1 localhost localhost} {
1854
set cgiPort [cgi_free_port]
1855
lassign [cgi_serve_once [list --port $cgiHost:$cgiPort] 127.0.0.1 \
1856
$cgiPort [request_get /test-env]] cgiPrinted cgiReply
1857
test_eq cgi-server-$cgiName-printed $cgiPrinted \
1858
"Listening for HTTP requests on TCP port 127.0.0.1:$cgiPort"
1859
test cgi-server-$cgiName-reply {
1860
[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1861
&& [cgi_param $cgiReply REMOTE_ADDR] eq "127.0.0.1"}
1862
}
1863
# "fossil ui" starts the browser on the address it listens on. The
1864
# browser here is "echo".
1865
fossil settings web-browser echo
1866
fossil settings web-browser
1867
if {[string match "*echo*" $RESULT]} {
1868
set cgiPort [cgi_free_port]
1869
lassign [cgi_serve_once [list --port 127.0.0.1:$cgiPort] 127.0.0.1 \
1870
$cgiPort [request_get /test-env] ui] cgiPrinted cgiReply
1871
test cgi-server-ui-browser {
1872
[string match "*\nhttp://127.0.0.1:$cgiPort/*" \n$cgiPrinted]
1873
&& [reply_status $cgiReply] eq "HTTP/1.0 200 OK"}
1874
}
1875
# SCGI in place of HTTP.
1876
set cgiPort [cgi_free_port]
1877
lassign [cgi_serve_once [list --scgi --port 127.0.0.1:$cgiPort] 127.0.0.1 \
1878
$cgiPort [cgi_scgi {
1879
CONTENT_LENGTH 0 SCGI 1 REQUEST_METHOD GET SCRIPT_NAME {}
1880
REQUEST_URI /test-env?viascgi=1 QUERY_STRING viascgi=1 SERVER_NAME x
1881
SERVER_PORT 80 REMOTE_ADDR 127.0.0.1
1882
}]] cgiPrinted cgiReply
1883
test_eq cgi-server-scgi-printed $cgiPrinted \
1884
"Listening for SCGI requests on TCP port 127.0.0.1:$cgiPort"
1885
test_eq cgi-server-scgi-reply [cgi_param $cgiReply viascgi] 1
1886
1887
if {![catch {cgi_busy_port ::1} cgiBusy]} {
1888
close [lindex $cgiBusy 0]
1889
# An IPv6 address.
1890
set cgiPort [cgi_free_port]
1891
lassign [cgi_serve_once [list --port \[::1\]:$cgiPort] ::1 $cgiPort \
1892
[request_get /test-env]] cgiPrinted cgiReply
1893
test_eq cgi-server-ipv6-printed $cgiPrinted \
1894
"Listening for HTTP requests on \[::1\]:$cgiPort"
1895
test cgi-server-ipv6-reply {[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1896
&& [cgi_param $cgiReply REMOTE_ADDR] eq "::1"}
1897
# With no address the server listens on both kinds of loopback
1898
# address, or on IPv4 alone if the port is taken for IPv6. The
1899
# server waits a tenth of a second at a time for a connection, and
1900
# this request leaves it waiting for two of them with none.
1901
set cgiPort [cgi_free_port]
1902
lassign [cgi_serve_once [list --localhost --port \[\]:$cgiPort] ::1 \
1903
$cgiPort [request_get /test-env] server 200] cgiPrinted cgiReply
1904
test_eq cgi-server-both-printed $cgiPrinted \
1905
[join [list \
1906
"Listening for HTTP requests on TCP port " \
1907
"localhost:$cgiPort, IPv4 and IPv6"] ""]
1908
test_eq cgi-server-both-reply [cgi_param $cgiReply REMOTE_ADDR] ::1
1909
lassign [cgi_busy_port ::1] cgiListener cgiPort
1910
lassign [cgi_serve_once [list --localhost --port $cgiPort] 127.0.0.1 \
1911
$cgiPort [request_get /test-env]] cgiPrinted cgiReply
1912
close $cgiListener
1913
test_eq cgi-server-ipv4-only-printed $cgiPrinted \
1914
"Listening for HTTP requests on TCP port localhost:$cgiPort, IPv4 only"
1915
test_eq cgi-server-ipv4-only-reply [reply_status $cgiReply] \
1916
"HTTP/1.0 200 OK"
1917
# With a port and neither an address nor --localhost the server
1918
# listens on every address of the machine.
1919
set cgiPort [cgi_free_port]
1920
lassign [cgi_serve_once [list --port $cgiPort] 127.0.0.1 \
1921
$cgiPort [request_get /login]] cgiPrinted cgiReply
1922
test_eq cgi-server-any-address-printed $cgiPrinted \
1923
"Listening for HTTP requests on TCP port $cgiPort, IPv4 and IPv6"
1924
test_eq cgi-server-any-address-reply [reply_status $cgiReply] \
1925
"HTTP/1.0 200 OK"
1926
}
1927
1928
# A unix socket in place of a port. The server makes the socket, and
1929
# takes the place of a socket of the same name that is left from an
1930
# earlier server. --socket-owner names the owner of the socket.
1931
catch {exec nc -h} cgiNcHelp
1932
if {[string match "*-U*" $cgiNcHelp]} {
1933
set cgiUser [exec id -un]
1934
foreach {cgiName cgiOptions} [list \
1935
new {--socket-name sock1} \
1936
replaced {--socket-name sock1} \
1937
owner [list --socket-owner $cgiUser --socket-name sock1] \
1938
] {
1939
lassign [cgi_serve_once $cgiOptions unix sock1 [request_get /login]] \
1940
cgiPrinted cgiReply
1941
test cgi-server-unix-$cgiName {
1942
$cgiPrinted eq "Listening for HTTP requests on unix socket sock1"
1943
&& [reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1944
&& [file type sock1] eq "socket"
1945
&& [file attributes sock1 -owner] eq $cgiUser}
1946
}
1947
# --socket-mode gives the permissions of the socket; without it they
1948
# are 0660.
1949
foreach {cgiName cgiOptions cgiMode} {
1950
mode {--socket-mode 0600 --socket-name sock2} 600
1951
default {--socket-name sock2} 660
1952
} {
1953
file delete sock2
1954
lassign [cgi_serve_once $cgiOptions unix sock2 [request_get /login]] \
1955
cgiPrinted cgiReply
1956
test cgi-server-unix-$cgiName {
1957
$cgiPrinted eq "Listening for HTTP requests on unix socket sock2"
1958
&& [reply_status $cgiReply] eq "HTTP/1.0 200 OK"
1959
&& [format %o [expr {[file attributes sock2 -permissions] & 0o777}]]
1960
eq $cgiMode}
1961
}
1962
fossil_raw -seconds 30 server --socket-mode rw --socket-name sock3 $cgiRepo
1963
test cgi-server-unix-mode-not-a-number {$CODE == 1 && [string match \
1964
"*cannot change permissions on sock3 to \"rw\"*" $RESULT]}
1965
} else {
1966
test_skip_part "no nc program for unix sockets: the unix socket tests"
1967
}
1968
1969
# A server that gives each request to a process of its own. Start
1970
# one with OPTIONS, as COMMAND, and return its process id once it
1971
# listens. What it prints goes to server.out and server.err. WRAP,
1972
# if given, is a list of words to put before the command.
1973
proc cgi_serve_start {options {command server} {wrap {}}} {
1974
global fossilexe cgiRepo
1975
file delete server.out server.err
1976
set pid [exec {*}$wrap $fossilexe $command --localauth {*}$options \
1977
$cgiRepo > server.out 2> server.err &]
1978
cgi_listening $pid
1979
return $pid
1980
}
1981
# Stop the server with process id PID: ask it to end, and kill it if
1982
# it has not ended after five seconds.
1983
proc cgi_serve_stop {pid} {
1984
catch {exec kill $pid}
1985
cgi_wait {expr {![cgi_running $pid]}}
1986
catch {exec kill -9 $pid}
1987
}
1988
# The process ids of the processes that the server PID has started
1989
# for requests and has not yet found to have ended.
1990
proc cgi_children {pid} {
1991
if {[catch {exec pgrep -P $pid} children]} {return {}}
1992
return $children
1993
}
1994
1995
# A process that handles a request is killed before the request is
1996
# complete. The server goes on to answer the next request, and one
1997
# that traces its work reports the signal that ended the process.
1998
if {[auto_execok pgrep] ne ""} {
1999
foreach {cgiName cgiOptions cgiReport} {
2000
traced --httptrace {*Child * exited on signal 9 *}
2001
quiet {} {}
2002
} {
2003
set cgiPort [cgi_free_port]
2004
set cgiPid [cgi_serve_start [list --port 127.0.0.1:$cgiPort \
2005
{*}$cgiOptions]]
2006
set cgiReply ""
2007
catch {
2008
set cgiSock [cgi_connect 127.0.0.1 $cgiPort]
2009
puts -nonewline $cgiSock "GET /login HTTP/1.1\r\n"
2010
flush $cgiSock
2011
cgi_wait {llength [cgi_children $cgiPid]}
2012
exec kill -9 {*}[cgi_children $cgiPid]
2013
cgi_wait {expr {[cgi_children $cgiPid] eq ""}}
2014
close $cgiSock
2015
set cgiReply [cgi_client 127.0.0.1 $cgiPort [request_get /login]]
2016
cgi_wait {expr {[cgi_children $cgiPid] eq ""}}
2017
}
2018
cgi_serve_stop $cgiPid
2019
test cgi-server-child-killed-$cgiName {
2020
[reply_status $cgiReply] eq "HTTP/1.0 200 OK"
2021
&& ($cgiReport eq ""
2022
? ![string match "*exited on signal*" [read_file server.err]]
2023
: [string match $cgiReport [read_file server.err]])}
2024
}
2025
} else {
2026
test_skip_part "no pgrep program: the killed server process tests"
2027
}
2028
2029
# A server that is not allowed another process cannot start the
2030
# browser and says so, and closes a connection without a reply
2031
# because it cannot start a process for the request. It goes on
2032
# listening all the same.
2033
fossil settings web-browser
2034
if {[string match "*echo*" $RESULT] && [exec id -u] != 0
2035
&& [auto_execok bash] ne ""} {
2036
set cgiPort [cgi_free_port]
2037
set cgiPid [cgi_serve_start [list --port 127.0.0.1:$cgiPort] ui \
2038
[list bash -c {ulimit -u 1 && exec "$@"} bash]]
2039
set cgiReply unset
2040
catch {set cgiReply [cgi_client 127.0.0.1 $cgiPort [request_get /login]]}
2041
set cgiAlive [expr {![catch {exec kill -0 $cgiPid}]}]
2042
cgi_serve_stop $cgiPid
2043
test cgi-server-no-process-browser {[string match \
2044
"*cannot start browser: echo*" [read_file server.err]]}
2045
test cgi-server-no-process-request {$cgiReply eq "" && $cgiAlive}
2046
}
2047
}
2048
2049
###############################################################################
2050
2051
test_cleanup
2052

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button