|
1
|
# |
|
2
|
# Copyright (c) 2026 Dan Shearer |
|
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
|
# Tests of fossil's HTTP and HTTPS client. A clone must work when the |
|
15
|
# reply is framed in any way HTTP/1.1 allows. It must fail with a |
|
16
|
# message, and leave no repository, when the reply is malformed or the |
|
17
|
# connection is closed or reset. Other tests check the request line |
|
18
|
# fossil sends and which certificates it trusts. |
|
19
|
# |
|
20
|
# The HTTPS tests need openssl and a fossil built with TLS support. The |
|
21
|
# tests that reset a connection, and the HTTPS tests that check what |
|
22
|
# fossil does with a reply, need python3 to run http-relay.py. |
|
23
|
# |
|
24
|
|
|
25
|
require_no_open_checkout |
|
26
|
|
|
27
|
if {$::is_windows} { |
|
28
|
test_skip "http.test stops its servers with kill" |
|
29
|
} |
|
30
|
if {![loopback_available]} { |
|
31
|
test_skip "the tests need a TCP connection over the loopback interface" |
|
32
|
} |
|
33
|
|
|
34
|
# fossil must not use a certificate store from the environment of the |
|
35
|
# test run, so SSL_CERT_FILE and SSL_CERT_DIR are unset until the end of |
|
36
|
# this file. |
|
37
|
# |
|
38
|
foreach var {SSL_CERT_FILE SSL_CERT_DIR} { |
|
39
|
if {[info exists env($var)]} { |
|
40
|
set httpSavedEnv($var) $env($var) |
|
41
|
unset env($var) |
|
42
|
} |
|
43
|
} |
|
44
|
test_setup "" |
|
45
|
fossil test-th-eval {hasfeature ssl} |
|
46
|
set haveTls [expr {[normalize_result] eq "1" && [auto_execok openssl] ne ""}] |
|
47
|
set havePython [expr {[auto_execok python3] ne ""}] |
|
48
|
set haveIpv6 [expr {![catch {close [socket -server list -myaddr ::1 0]}]}] |
|
49
|
set httpPids [list] |
|
50
|
set httpFossilServers [list] |
|
51
|
set httpAnswer "" |
|
52
|
|
|
53
|
# Clones URL into c.fossil with the options in ARGS, answering fossil's |
|
54
|
# prompts with the lines in httpAnswer. Returns "cloned" when the clone |
|
55
|
# is a complete copy of r.fossil. When fossil exits with status 1 and |
|
56
|
# does not leave c.fossil, returns its output, and otherwise says what |
|
57
|
# went wrong. |
|
58
|
# |
|
59
|
proc http_clone {url args} { |
|
60
|
file delete c.fossil |
|
61
|
fossil_raw -seconds 60 -input $::httpAnswer clone $url c.fossil {*}$args |
|
62
|
set ::RESULT [string trim $::RESULT] |
|
63
|
if {$::CODE == 0} { |
|
64
|
fossil sql -R c.fossil "SELECT count(*) FROM blob" |
|
65
|
if {$::RESULT == $::httpBlobs} {return cloned} |
|
66
|
return "cloned $::RESULT of $::httpBlobs blobs" |
|
67
|
} |
|
68
|
if {$::CODE ne "1"} {return "exit $::CODE"} |
|
69
|
if {[file exists c.fossil]} {return "c.fossil is left"} |
|
70
|
return $::RESULT |
|
71
|
} |
|
72
|
|
|
73
|
# Listens on a port on 127.0.0.1 that the system chooses, and returns |
|
74
|
# the port. Calls HANDLER with each connection and the request on it, |
|
75
|
# once the whole request has arrived, and then closes the connection. |
|
76
|
# |
|
77
|
proc http_listen {handler} { |
|
78
|
return [test_listen [list http_receive $handler]] |
|
79
|
} |
|
80
|
|
|
81
|
proc http_receive {handler chan request} { |
|
82
|
if {[string first "\r\n\r\n" $request] >= 0} { |
|
83
|
{*}$handler $chan $request |
|
84
|
} |
|
85
|
} |
|
86
|
|
|
87
|
proc http_listen_close {} { |
|
88
|
set server [socket -server {apply {{chan address port} {close $chan}}} \ |
|
89
|
-myaddr 127.0.0.1 0] |
|
90
|
lappend ::testListeners $server |
|
91
|
return [lindex [fconfigure $server -sockname] 2] |
|
92
|
} |
|
93
|
|
|
94
|
# Starts http-relay.py with ARGS and returns the port it listens on. Its |
|
95
|
# output goes to relay-PORT.log. |
|
96
|
# |
|
97
|
proc http_relay {args} { |
|
98
|
set log relay-[getSeqNo].log |
|
99
|
lappend ::httpPids {*}[exec python3 -I \ |
|
100
|
[file join $::testfiledir http-relay.py] {*}$args >& $log &] |
|
101
|
for {set i 0} {$i < 100} {incr i} { |
|
102
|
after 100 |
|
103
|
if {[regexp {listening (\d+)} [read_file $log] -> port]} { |
|
104
|
file rename $log relay-$port.log |
|
105
|
return $port |
|
106
|
} |
|
107
|
} |
|
108
|
error "http-relay.py did not start: [read_file $log]" |
|
109
|
} |
|
110
|
|
|
111
|
# Returns the TLS server names that clients sent to the relay on PORT |
|
112
|
# since the last call, sorted and without duplicates. |
|
113
|
# |
|
114
|
proc http_server_names {port} { |
|
115
|
set names [regexp -all -inline -line {^SNI: .*$} \ |
|
116
|
[read_file relay-$port.log]] |
|
117
|
set seen 0 |
|
118
|
if {[info exists ::httpNamesSeen($port)]} {set seen $::httpNamesSeen($port)} |
|
119
|
set ::httpNamesSeen($port) [llength $names] |
|
120
|
return [join [lsort -unique [lrange $names $seen end]] \n] |
|
121
|
} |
|
122
|
|
|
123
|
# Starts "fossil server" with the options in ARGS on REPOSITORY, and |
|
124
|
# returns the port it listens on. |
|
125
|
# |
|
126
|
proc http_server {repository args} { |
|
127
|
set server [test_start_server $repository "" {*}$args] |
|
128
|
lappend ::httpFossilServers $server |
|
129
|
return [lindex $server 1] |
|
130
|
} |
|
131
|
|
|
132
|
# A repository of eight check-ins, which anyone can clone. Each check-in |
|
133
|
# adds a file of 20000 random characters, so that a clone takes several |
|
134
|
# round trips and each reply is large enough to split. |
|
135
|
# |
|
136
|
fossil new r.fossil |
|
137
|
fossil user capabilities nobody gio -R r.fossil |
|
138
|
fossil settings backoffice-disable 1 -R r.fossil |
|
139
|
file mkdir co |
|
140
|
cd co |
|
141
|
fossil open ../r.fossil |
|
142
|
for {set i 1} {$i <= 8} {incr i} { |
|
143
|
write_file f$i.txt [rand_str 20000] |
|
144
|
fossil add f$i.txt |
|
145
|
fossil commit -m "check-in $i" |
|
146
|
} |
|
147
|
fossil close |
|
148
|
cd .. |
|
149
|
fossil sql -R r.fossil "SELECT count(*) FROM blob" |
|
150
|
set httpBlobs [normalize_result] |
|
151
|
set upstream [http_server r.fossil] |
|
152
|
set closePort [http_listen_close] |
|
153
|
|
|
154
|
# Writes the reply of the fossil server to REQUEST to CHAN, with its |
|
155
|
# framing changed as the test NAME says. RFC 9112 sections 6 and 7 say |
|
156
|
# what HTTP/1.1 allows. |
|
157
|
# |
|
158
|
proc http_proxy {name chan request} { |
|
159
|
set server [socket 127.0.0.1 $::upstream] |
|
160
|
fconfigure $server -translation binary |
|
161
|
puts -nonewline $server $request |
|
162
|
flush $server |
|
163
|
set reply [read $server] |
|
164
|
close $server |
|
165
|
set end [string first "\r\n\r\n" $reply] |
|
166
|
set header [split [string map {"\r\n" \n} \ |
|
167
|
[string range $reply 0 [expr {$end - 1}]]] \n] |
|
168
|
set body [string range $reply [expr {$end + 4}] end] |
|
169
|
set length [string length $body] |
|
170
|
set half [expr {$length / 2}] |
|
171
|
set cl [lsearch -regexp $header {(?i)^content-length:}] |
|
172
|
set ct [lsearch -regexp $header {(?i)^content-type:}] |
|
173
|
switch -- $name { |
|
174
|
trunc { |
|
175
|
set body [string range $body 0 [string last \n $body $half]] |
|
176
|
} |
|
177
|
content-length-negative {lset header $cl "Content-Length: -1"} |
|
178
|
content-length-no-space {lset header $cl "Content-Length:$length"} |
|
179
|
content-length-letters {lset header $cl "Content-Length: abc"} |
|
180
|
content-length-huge {lset header $cl "Content-Length: 99999999999"} |
|
181
|
content-length-overflow { |
|
182
|
lset header $cl "Content-Length: 99999999999999999999999" |
|
183
|
} |
|
184
|
content-length-two-values { |
|
185
|
lappend header "Content-Length: [expr {$length + 1}]" |
|
186
|
} |
|
187
|
content-length-repeated {lappend header "Content-Length: $length"} |
|
188
|
content-length-folded {set header [linsert $header $cl+1 " 0"]} |
|
189
|
content-type-no-space { |
|
190
|
regexp {:\s*(.*)} [lindex $header $ct] -> type |
|
191
|
lset header $ct "Content-Type:$type" |
|
192
|
} |
|
193
|
bytes-after-reply { |
|
194
|
append body "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\njunk\n" |
|
195
|
} |
|
196
|
} |
|
197
|
if {[string match chunk* $name] || $name eq "interim-100-continue" || |
|
198
|
$name eq "transfer-encoding-twice"} { |
|
199
|
lset header $cl "Transfer-Encoding: chunked" |
|
200
|
if {$name eq "transfer-encoding-twice"} { |
|
201
|
lappend header "Transfer-Encoding: chunked" |
|
202
|
} |
|
203
|
set size $half |
|
204
|
if {$name eq "chunk-short"} {incr size -1} |
|
205
|
set first [format %x $size] |
|
206
|
switch -- $name { |
|
207
|
chunk-extension-empty {append first ";"} |
|
208
|
chunk-size-cr {append first "\rX"} |
|
209
|
chunk-size-nul {append first "\0"} |
|
210
|
} |
|
211
|
set last "0\r\n\r\n" |
|
212
|
if {$name eq "chunk-no-final-crlf"} {set last "0\r\n"} |
|
213
|
set body [string cat $first \r\n \ |
|
214
|
[string range $body 0 [expr {$half - 1}]] \r\n \ |
|
215
|
[format %x [expr {$length - $half}]] \r\n \ |
|
216
|
[string range $body $half end] \r\n $last] |
|
217
|
} |
|
218
|
if {$name eq "interim-100-continue"} { |
|
219
|
puts -nonewline $chan "HTTP/1.1 100 Continue\r\n\r\n" |
|
220
|
} |
|
221
|
puts -nonewline $chan [join $header \r\n]\r\n\r\n$body |
|
222
|
} |
|
223
|
|
|
224
|
foreach {name expected} { |
|
225
|
pass ^cloned$ |
|
226
|
trunc {response truncated} |
|
227
|
content-length-negative {bad content-length} |
|
228
|
content-length-no-space ^cloned$ |
|
229
|
content-length-letters {bad content-length} |
|
230
|
content-length-huge {bad content-length} |
|
231
|
content-length-overflow {bad content-length} |
|
232
|
content-length-two-values {two different content-length values} |
|
233
|
content-length-repeated ^cloned$ |
|
234
|
content-length-folded {folded header line} |
|
235
|
content-type-no-space ^cloned$ |
|
236
|
chunked ^cloned$ |
|
237
|
chunk-short {no line end after chunk data} |
|
238
|
interim-100-continue ^cloned$ |
|
239
|
transfer-encoding-twice {two transfer-encoding headers} |
|
240
|
chunk-extension-empty {malformed chunk size line} |
|
241
|
chunk-size-cr {malformed chunk size line} |
|
242
|
chunk-no-final-crlf ^cloned$ |
|
243
|
bytes-after-reply ^cloned$ |
|
244
|
} { |
|
245
|
set port [http_listen [list http_proxy $name]] |
|
246
|
test_match http-proxy-$name [http_clone http://127.0.0.1:$port/] $expected |
|
247
|
} |
|
248
|
# A chunk size line that ends in a zero byte is malformed. Fossil accepts |
|
249
|
# it and the clone succeeds. |
|
250
|
set port [http_listen [list http_proxy chunk-size-nul]] |
|
251
|
test_match http-proxy-chunk-size-nul [http_clone http://127.0.0.1:$port/] \ |
|
252
|
{malformed chunk size line} knownBug |
|
253
|
|
|
254
|
# The request line. RFC 3986 section 2.1 requires a space or a byte |
|
255
|
# outside ASCII in a path to be percent-encoded, and RFC 9112 section |
|
256
|
# 3.2.2 requires the absolute URL in a request sent to a proxy. |
|
257
|
# |
|
258
|
proc http_record_request {chan request} { |
|
259
|
set ::httpRequestLine [lindex [split $request \r] 0] |
|
260
|
puts -nonewline $chan "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n" |
|
261
|
} |
|
262
|
set recordPort [http_listen http_record_request] |
|
263
|
|
|
264
|
# Test NAME passes when a clone of URL through the HTTP proxy PROXY sends |
|
265
|
# the request line EXPECTED. |
|
266
|
# |
|
267
|
proc http_request_line {name url expected {proxy off} {constraints ""}} { |
|
268
|
set ::httpRequestLine "" |
|
269
|
http_clone $url --proxy $proxy |
|
270
|
test_eq $name $::httpRequestLine $expected $constraints |
|
271
|
} |
|
272
|
foreach {name path expected} { |
|
273
|
path-space {my repo} {POST /my%20repo HTTP/1.1} |
|
274
|
path-escaped-space my%20repo {POST /my%20repo HTTP/1.1} |
|
275
|
path-utf8 répo {POST /r%C3%A9po HTTP/1.1} |
|
276
|
path-brackets {a[b]} {POST /a%5Bb%5D HTTP/1.1} |
|
277
|
path-bad-escape a%zz {POST /a%25zz HTTP/1.1} |
|
278
|
path-fragment a#frag {POST /a HTTP/1.1} |
|
279
|
} { |
|
280
|
http_request_line http-rfc-$name http://127.0.0.1:$recordPort/$path \ |
|
281
|
$expected |
|
282
|
} |
|
283
|
set proxy http://127.0.0.1:$recordPort |
|
284
|
http_request_line http-rfc-proxy-path http://localhost:9/plain \ |
|
285
|
{POST http://localhost:9/plain HTTP/1.1} $proxy |
|
286
|
# Sent to a proxy, Fossil writes the space as "+" (POST |
|
287
|
# http://localhost:9/my%2Brepo HTTP/1.1) and encodes the "%" of each escape |
|
288
|
# of a UTF-8 byte again (POST http://localhost:9/r%25C3%25A9po HTTP/1.1). |
|
289
|
http_request_line http-rfc-proxy-path-space {http://localhost:9/my repo} \ |
|
290
|
{POST http://localhost:9/my%20repo HTTP/1.1} $proxy knownBug |
|
291
|
http_request_line http-rfc-proxy-path-utf8 http://localhost:9/répo \ |
|
292
|
{POST http://localhost:9/r%C3%A9po HTTP/1.1} $proxy knownBug |
|
293
|
|
|
294
|
# URLs with an upper-case scheme or host, a percent-encoded host, and a |
|
295
|
# file: URL with a space. |
|
296
|
# |
|
297
|
file copy r.fossil "my repo.fossil" |
|
298
|
foreach {name url} [list \ |
|
299
|
file-url-escaped-space file://[pwd]/my%20repo.fossil \ |
|
300
|
file-url-space "file://[pwd]/my repo.fossil" \ |
|
301
|
upper-case-scheme HTTP://127.0.0.1:$upstream/ \ |
|
302
|
upper-case-host http://LOCALHOST:$upstream/ \ |
|
303
|
percent-encoded-host http://loc%61lhost:$upstream/] { |
|
304
|
test_eq http-rfc-$name [http_clone $url] cloned |
|
305
|
} |
|
306
|
|
|
307
|
# A redirect to a relative reference, which RFC 9110 section 10.2.2 |
|
308
|
# allows in Location. |
|
309
|
# |
|
310
|
proc http_redirect {chan request} { |
|
311
|
if {[string match "* /old *" [lindex [split $request \r] 0]]} { |
|
312
|
puts -nonewline $chan "HTTP/1.1 302 Found\r\nLocation: /new\r\n" |
|
313
|
puts -nonewline $chan "Content-Length: 0\r\nConnection: close\r\n\r\n" |
|
314
|
} else { |
|
315
|
puts -nonewline $chan "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n" |
|
316
|
puts -nonewline $chan "Connection: close\r\n\r\nnew" |
|
317
|
} |
|
318
|
} |
|
319
|
set port [http_listen http_redirect] |
|
320
|
# Fossil does not follow the relative Location: it fails with "unknown |
|
321
|
# repository: /new" and fetches nothing from /new. |
|
322
|
fossil_raw -seconds 60 test-httpmsg http://127.0.0.1:$port/old --out new.txt |
|
323
|
test_eq http-rfc-relative-redirect \ |
|
324
|
[expr {[file exists new.txt] ? [read_file new.txt] : ""}] new knownBug |
|
325
|
|
|
326
|
# xfer lines with more tokens than any card has. The clone must fail |
|
327
|
# with "unknown command" and not crash. |
|
328
|
# |
|
329
|
proc http_xfer_reply {body chan request} { |
|
330
|
puts -nonewline $chan "HTTP/1.1 200 OK\r\n" |
|
331
|
puts -nonewline $chan "Content-Type: application/x-fossil-uncompressed\r\n" |
|
332
|
puts -nonewline $chan "Content-Length: [string length $body]\r\n\r\n$body" |
|
333
|
} |
|
334
|
foreach {name body} [list eight-tokens "a b c d e f g h\n" \ |
|
335
|
many-tokens "[string repeat {x } 100000]\n"] { |
|
336
|
set port [http_listen [list http_xfer_reply $body]] |
|
337
|
test_match http-tokens-$name [http_clone http://127.0.0.1:$port/] \ |
|
338
|
{unknown command} |
|
339
|
} |
|
340
|
|
|
341
|
# Servers that close or reset the connection at once. fossil must fail |
|
342
|
# with a message, not end on a signal such as SIGPIPE. Each clone runs |
|
343
|
# three times. |
|
344
|
# |
|
345
|
if {$havePython} { |
|
346
|
set resetPort [http_relay 127.0.0.1 reset] |
|
347
|
} |
|
348
|
for {set i 1} {$i <= 3} {incr i} { |
|
349
|
test_match http-sigpipe-http-close-$i \ |
|
350
|
[http_clone http://127.0.0.1:$closePort/] {server did not reply} |
|
351
|
if {$havePython} { |
|
352
|
test_match http-sigpipe-http-reset-$i \ |
|
353
|
[http_clone http://127.0.0.1:$resetPort/] {server did not reply} |
|
354
|
} |
|
355
|
if {$haveTls} { |
|
356
|
test_match http-sigpipe-https-close-$i \ |
|
357
|
[http_clone https://127.0.0.1:$closePort/] \ |
|
358
|
{Error establishing SSL connection} |
|
359
|
} |
|
360
|
if {$haveTls && $havePython} { |
|
361
|
test_match http-sigpipe-https-reset-$i \ |
|
362
|
[http_clone https://127.0.0.1:$resetPort/] \ |
|
363
|
{Error establishing SSL connection} |
|
364
|
} |
|
365
|
} |
|
366
|
if {$haveTls && $havePython} { |
|
367
|
# The message for a reset during the TLS handshake includes the reason. |
|
368
|
# Fossil prints empty parentheses: "Error establishing SSL connection |
|
369
|
# 127.0.0.1:PORT ()". |
|
370
|
set output [http_clone https://127.0.0.1:$resetPort/] |
|
371
|
test http-sigpipe-https-reset-reason \ |
|
372
|
{![string match "*SSL connection*()*" $output]} knownBug |
|
373
|
} |
|
374
|
test_eq http-sigpipe-clone-after-failures \ |
|
375
|
[http_clone http://127.0.0.1:$upstream/] cloned |
|
376
|
|
|
377
|
# Users and passwords in the URL, in raw UTF-8 and percent-encoded, with |
|
378
|
# "fossil server --repolist", for a repository anonymous users cannot |
|
379
|
# read. |
|
380
|
# |
|
381
|
file mkdir repos |
|
382
|
file copy r.fossil repos/répo.fossil |
|
383
|
set repo repos/répo.fossil |
|
384
|
fossil user capabilities nobody "" -R $repo |
|
385
|
fossil user capabilities anonymous "" -R $repo |
|
386
|
fossil user new jérôme "" pässwörd -R $repo |
|
387
|
fossil user capabilities jérôme gio -R $repo |
|
388
|
fossil user new a@b:c "" p@ss:w/rd -R $repo |
|
389
|
fossil user capabilities a@b:c gio -R $repo |
|
390
|
set schemes [list http [http_server repos --repolist]] |
|
391
|
|
|
392
|
# Certificates signed by a private CA: for localhost, for another host, |
|
393
|
# for 127.0.0.1, for ::1, and for all of localhost, 127.0.0.1 and ::1. |
|
394
|
# |
|
395
|
if {$haveTls} { |
|
396
|
if {[catch { |
|
397
|
exec openssl req -x509 -newkey rsa:2048 -nodes -days 2 -subj /CN=TestCA \ |
|
398
|
-keyout ca.key -out ca.pem >& /dev/null |
|
399
|
foreach {cert names} { |
|
400
|
localhost DNS:localhost |
|
401
|
otherhost DNS:otherhost |
|
402
|
ip IP:127.0.0.1 |
|
403
|
ip6 IP:::1 |
|
404
|
all DNS:localhost,IP:127.0.0.1,IP:::1 |
|
405
|
} { |
|
406
|
write_file $cert.ext subjectAltName=$names\n |
|
407
|
exec openssl req -newkey rsa:2048 -nodes -subj /CN=$cert \ |
|
408
|
-keyout $cert.key -out $cert.csr >& /dev/null |
|
409
|
exec openssl x509 -req -in $cert.csr -CA ca.pem -CAkey ca.key \ |
|
410
|
-CAcreateserial -days 2 -extfile $cert.ext -out $cert.pem \ |
|
411
|
>& /dev/null |
|
412
|
} |
|
413
|
} msg]} { |
|
414
|
protOut "SKIPPED the HTTPS tests: openssl failed: $msg" 1 |
|
415
|
set haveTls 0 |
|
416
|
} |
|
417
|
} |
|
418
|
if {$haveTls} { |
|
419
|
set ca [file join [pwd] ca.pem] |
|
420
|
lappend schemes https [http_server repos --repolist --cert all.pem \ |
|
421
|
--pkey all.key] |
|
422
|
} |
|
423
|
|
|
424
|
# Test NAME passes when a clone of URL gives EXPECTED: "cloned", or |
|
425
|
# "failed" when fossil fails without leaving a repository. |
|
426
|
# |
|
427
|
proc http_expect {name url expected} { |
|
428
|
set result [http_clone $url] |
|
429
|
if {$result ne "cloned" && $::CODE == 1 && ![file exists c.fossil]} { |
|
430
|
set result failed |
|
431
|
} |
|
432
|
test_eq $name $result $expected |
|
433
|
} |
|
434
|
if {$haveTls} {set env(SSL_CERT_FILE) $ca} |
|
435
|
foreach {scheme port} $schemes { |
|
436
|
set host localhost:$port |
|
437
|
http_expect http-userinfo-$scheme-raw-utf8 \ |
|
438
|
$scheme://jérôme:pässwörd@$host/répo cloned |
|
439
|
http_expect http-userinfo-$scheme-percent-encoded-utf8 \ |
|
440
|
$scheme://j%C3%A9r%C3%B4me:p%C3%A4ssw%C3%B6rd@$host/répo cloned |
|
441
|
http_expect http-userinfo-$scheme-percent-encoded-delimiters \ |
|
442
|
$scheme://a%40b%3Ac:p%40ss%3Aw%2Frd@$host/répo cloned |
|
443
|
http_expect http-userinfo-$scheme-percent-encoded-path \ |
|
444
|
$scheme://jérôme:pässwörd@$host/r%C3%A9po cloned |
|
445
|
http_expect http-userinfo-$scheme-wrong-password \ |
|
446
|
$scheme://jérôme:wrong@$host/répo failed |
|
447
|
http_expect http-userinfo-$scheme-no-password \ |
|
448
|
$scheme://jérôme@$host/répo failed |
|
449
|
} |
|
450
|
unset -nocomplain env(SSL_CERT_FILE) |
|
451
|
|
|
452
|
# Returns the SHA-256 fingerprint of the certificate in the file CERT, as |
|
453
|
# fossil stores it. |
|
454
|
# |
|
455
|
proc http_fingerprint {cert} { |
|
456
|
set line [exec openssl x509 -in $cert -noout -fingerprint -sha256] |
|
457
|
return [string tolower [string map {: ""} [lindex [split $line =] 1]]] |
|
458
|
} |
|
459
|
|
|
460
|
# Makes fossil remember the certificate with FINGERPRINT as one it may |
|
461
|
# trust for HOST, or forget the certificate for HOST when FINGERPRINT is |
|
462
|
# empty. fossil keeps it as the global setting cert:HOST. |
|
463
|
# |
|
464
|
proc http_remember {host fingerprint} { |
|
465
|
set config [file join $::env(FOSSIL_HOME) .fossil] |
|
466
|
if {$fingerprint eq ""} { |
|
467
|
fossil sqlite3 --no-repository $config \ |
|
468
|
"DELETE FROM global_config WHERE name='cert:$host'" |
|
469
|
} else { |
|
470
|
fossil sqlite3 --no-repository $config \ |
|
471
|
"REPLACE INTO global_config VALUES('cert:$host','$fingerprint')" |
|
472
|
} |
|
473
|
} |
|
474
|
|
|
475
|
# Which certificates fossil trusts: an untrusted CA, a certificate for |
|
476
|
# another host, a CA trusted through SSL_CERT_FILE, SSL_CERT_DIR or the |
|
477
|
# ssl-ca-location setting, IP addresses in the certificate, a remembered |
|
478
|
# exception, an answer to the certificate prompt, and --accept-any-cert. |
|
479
|
# |
|
480
|
if {$haveTls} { |
|
481
|
set localhostPort [http_server r.fossil --cert localhost.pem \ |
|
482
|
--pkey localhost.key] |
|
483
|
set otherhostPort [http_server r.fossil --cert otherhost.pem \ |
|
484
|
--pkey otherhost.key] |
|
485
|
set ipPort [http_server r.fossil --cert ip.pem --pkey ip.key] |
|
486
|
set ip6Port [http_server r.fossil --cert ip6.pem --pkey ip6.key] |
|
487
|
set localhost https://localhost:$localhostPort/ |
|
488
|
set otherhost https://localhost:$otherhostPort/ |
|
489
|
set declined {SSL cert declined} |
|
490
|
test_match http-tls-ca-untrusted [http_clone $localhost] $declined |
|
491
|
test_match http-tls-wrong-host [http_clone $otherhost] $declined |
|
492
|
set env(SSL_CERT_FILE) $ca |
|
493
|
test_eq http-tls-ssl-cert-file [http_clone $localhost] cloned |
|
494
|
test_match http-tls-ssl-cert-file-wrong-host [http_clone $otherhost] \ |
|
495
|
$declined |
|
496
|
test_eq http-tls-ip-in-certificate [http_clone https://127.0.0.1:$ipPort/] \ |
|
497
|
cloned |
|
498
|
test_match http-tls-ip-not-in-certificate \ |
|
499
|
[http_clone https://127.0.0.1:$localhostPort/] $declined |
|
500
|
if {$haveIpv6} { |
|
501
|
test_eq http-tls-ipv6-in-certificate \ |
|
502
|
[http_clone https://\[::1\]:$ip6Port/] cloned |
|
503
|
} |
|
504
|
unset env(SSL_CERT_FILE) |
|
505
|
|
|
506
|
file mkdir certdir |
|
507
|
file copy ca.pem certdir |
|
508
|
if {![catch {exec openssl rehash certdir >& /dev/null}]} { |
|
509
|
set env(SSL_CERT_DIR) [file join [pwd] certdir] |
|
510
|
test_eq http-tls-ssl-cert-dir [http_clone $localhost] cloned |
|
511
|
unset env(SSL_CERT_DIR) |
|
512
|
} |
|
513
|
fossil settings -g ssl-ca-location $ca |
|
514
|
test_eq http-tls-ssl-ca-location [http_clone $localhost] cloned |
|
515
|
fossil unset -g ssl-ca-location |
|
516
|
|
|
517
|
# A certificate that fossil has remembered does not need a CA. |
|
518
|
set fingerprint [http_fingerprint localhost.pem] |
|
519
|
http_remember localhost $fingerprint |
|
520
|
test_eq http-tls-remembered-exception [http_clone $localhost] cloned |
|
521
|
http_remember localhost 00$fingerprint |
|
522
|
test_match http-tls-exception-wrong-fingerprint [http_clone $localhost] \ |
|
523
|
$declined |
|
524
|
http_remember localhost "" |
|
525
|
if {$haveIpv6} { |
|
526
|
http_remember ::1 [http_fingerprint ip6.pem] |
|
527
|
test_eq http-tls-remembered-exception-ipv6 \ |
|
528
|
[http_clone https://\[::1\]:$ip6Port/] cloned |
|
529
|
http_remember ::1 "" |
|
530
|
} |
|
531
|
|
|
532
|
# The certificate prompt: accept once, accept and remember, and a |
|
533
|
# fingerprint that is not the certificate's. |
|
534
|
set httpAnswer "y\nn\n" |
|
535
|
test_eq http-tls-prompt-yes [http_clone $localhost] cloned |
|
536
|
set httpAnswer "$fingerprint\ny\n" |
|
537
|
test_eq http-tls-prompt-fingerprint-remembered [http_clone $localhost] \ |
|
538
|
cloned |
|
539
|
fossil sqlite3 --no-repository [file join $env(FOSSIL_HOME) .fossil] \ |
|
540
|
"SELECT value FROM global_config WHERE name='cert:localhost'" |
|
541
|
test_eq http-tls-prompt-fingerprint-stored [normalize_result] $fingerprint |
|
542
|
http_remember localhost "" |
|
543
|
set httpAnswer "00$fingerprint\n" |
|
544
|
test_match http-tls-prompt-wrong-fingerprint [http_clone $localhost] \ |
|
545
|
$declined |
|
546
|
set httpAnswer "" |
|
547
|
test_eq http-tls-accept-any-cert \ |
|
548
|
[http_clone $localhost --accept-any-cert] cloned |
|
549
|
} |
|
550
|
|
|
551
|
# HTTPS through http-relay.py, which ends the TLS session with or |
|
552
|
# without close_notify, and records the TLS server name fossil sends. |
|
553
|
# RFC 6066 section 3 requires the name without a trailing dot, and no |
|
554
|
# name for an IP address. |
|
555
|
# |
|
556
|
if {$haveTls && $havePython} { |
|
557
|
set env(SSL_CERT_FILE) $ca |
|
558
|
set pass [http_listen [list http_proxy pass]] |
|
559
|
set relay [http_relay 127.0.0.1 $pass all.pem all.key] |
|
560
|
test_eq http-proxy-tls-ipv4-literal [http_clone https://127.0.0.1:$relay/] \ |
|
561
|
cloned |
|
562
|
test_eq http-proxy-tls-host-name [http_clone https://localhost:$relay/] \ |
|
563
|
cloned |
|
564
|
set notify [http_relay 127.0.0.1 $pass all.pem all.key notify] |
|
565
|
test_eq http-proxy-tls-close-notify \ |
|
566
|
[http_clone https://localhost:$notify/] cloned |
|
567
|
if {$haveIpv6} { |
|
568
|
set relay6 [http_relay ::1 $pass all.pem all.key] |
|
569
|
test_eq http-proxy-tls-ipv6-literal \ |
|
570
|
[http_clone https://\[::1\]:$relay6/] cloned |
|
571
|
} |
|
572
|
set trunc [http_relay 127.0.0.1 [http_listen [list http_proxy trunc]] \ |
|
573
|
all.pem all.key] |
|
574
|
test_match http-proxy-tls-trunc [http_clone https://localhost:$trunc/] \ |
|
575
|
{response truncated} |
|
576
|
set closed [http_relay 127.0.0.1 $closePort all.pem all.key] |
|
577
|
test_match http-proxy-tls-close-after-handshake \ |
|
578
|
[http_clone https://localhost:$closed/] {did not reply} |
|
579
|
|
|
580
|
http_server_names $relay |
|
581
|
foreach {name host expected} { |
|
582
|
https-trailing-dot-proxy localhost. {SNI: localhost} |
|
583
|
https-mixed-case-host LocalHost {SNI: localhost} |
|
584
|
https-ip-literal 127.0.0.1 {SNI: None} |
|
585
|
} { |
|
586
|
test_eq http-rfc-$name [http_clone https://$host:$relay/] cloned |
|
587
|
test_eq http-rfc-$name-server-name [http_server_names $relay] $expected |
|
588
|
} |
|
589
|
set tlsPort [http_server r.fossil --cert all.pem --pkey all.key] |
|
590
|
test_eq http-rfc-https-trailing-dot \ |
|
591
|
[http_clone https://localhost.:$tlsPort/] cloned |
|
592
|
unset env(SSL_CERT_FILE) |
|
593
|
} |
|
594
|
|
|
595
|
test_listen_stop |
|
596
|
foreach pid $httpPids {catch {exec kill $pid}} |
|
597
|
foreach server $httpFossilServers { |
|
598
|
test_stop_server [lindex $server 0] [lindex $server 0] [lindex $server 2] |
|
599
|
} |
|
600
|
array set env [array get httpSavedEnv] |
|
601
|
test_cleanup |
|
602
|
|