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