Fossil SCM

fossil-scm / test / login.test
Blame History Raw 1635 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
# Authentication: how the server in src/login.c reads a request and decides
15
# who the user is. Requests are written to a file and handed to "fossil http
16
# REPO --in", which serves one request and writes the reply to a file, the way
17
# test/cgi.test does. The /test-env page reports g.zLogin, g.isRobot and the
18
# capabilities the server granted. Several command-line test entry points in
19
# login.c are exercised directly.
20
#
21
22
# Fossil sleeps for a second before it refuses a wrong password or a bad
23
# password-reset link. Requests that are refused that way are sent
24
# together with login_send_all, so that they wait at the same time, and
25
# are left out at -assurance smoke.
26
set loginWaits [expr {[test_assurance] ne "smoke"}]
27
if {!$loginWaits} {
28
test_skip_part "the logins that Fossil delays need -assurance ci or stress"
29
}
30
31
require_no_open_checkout
32
33
test_setup
34
35
set loginRepo [file join [pwd] .rep.fossil]
36
37
# A user with Read, Write and Password capability, and a Setup user.
38
fossil user new loginbob [email protected] bobpw
39
fossil user capabilities loginbob i
40
fossil user new loginsue [email protected] suepw
41
fossil user capabilities loginsue s
42
43
# Let any request see the /test-env page, which reports the login the server
44
# settled on, the robot flag and the granted capabilities.
45
fossil sql "REPLACE INTO config(name,value,mtime)
46
VALUES('test_env_enable','1',0)"
47
48
# Send TEXT (a byte string) to the server and return the whole reply. ARGS are
49
# extra "fossil http" options.
50
proc login_send {text args} {
51
return [fossil_http $::loginRepo $text --ipaddr 9.9.9.9 {*}$args]
52
}
53
54
# Send each request in TEXTS to a server of its own, all at the same
55
# time, and return the list of replies in the same order.
56
proc login_send_all {texts} {
57
global fossilexe loginRepo
58
set servers {}
59
foreach text $texts {
60
set n [llength $servers]
61
write_file lreq$n.txt $text
62
file delete lreply$n.txt
63
lappend servers [open |[list $fossilexe http $loginRepo --in lreq$n.txt \
64
--out lreply$n.txt --ipaddr 9.9.9.9]]
65
}
66
set replies {}
67
foreach server $servers {
68
set n [llength $replies]
69
catch {close $server}
70
if {[file exists lreply$n.txt]} {
71
lappend replies [read_file lreply$n.txt]
72
} else {
73
lappend replies ""
74
}
75
}
76
return $replies
77
}
78
79
# A GET request for PATH carrying the header lines in HEADERS, a list.
80
proc login_get {path {headers {}}} {
81
return [login_send [request_get $path $headers]]
82
}
83
84
# The text of a POST of url-encoded BODY to PATH, with a Referer matching
85
# the base URL so that the same-origin CSRF check passes.
86
proc login_post_text {path body {headers {}}} {
87
return [request_post $path $body \
88
[list "Referer: http://x/login" {*}$headers]]
89
}
90
91
# A POST of url-encoded BODY to PATH.
92
proc login_post {path body {headers {}}} {
93
return [login_send [login_post_text $path $body $headers]]
94
}
95
96
# The login name that /test-env reports for a reply. The server writes an
97
# empty value for the "nobody" user, so report that as "nobody".
98
proc login_who {reply} {
99
if {[regexp {g\.zLogin = ([^<]*)<br>} $reply all who]} {
100
if {$who eq ""} { return nobody }
101
return $who
102
}
103
return -
104
}
105
106
# The capability string /test-env reports.
107
proc login_caps {reply} {
108
if {[regexp {capabilities = ([^<]*)<br>} $reply all caps]} { return $caps }
109
return -
110
}
111
112
###############################################################################
113
# HTTP Basic authentication. The "http_authentication_ok" setting must be on.
114
# Correct credentials establish the user; wrong credentials give a 401; a
115
# header that is not well-formed Basic auth is ignored and the request falls
116
# through to the "nobody" user.
117
118
fossil sql "REPLACE INTO config(name,value,mtime)
119
VALUES('http_authentication_ok','1',0)"
120
proc login_basic {creds} {
121
return [login_get /test-env \
122
[list "Authorization: Basic [binary encode base64 $creds]"]]
123
}
124
125
test_eq login-basic-good [login_who [login_basic loginbob:bobpw]] loginbob
126
test login-basic-good-caps \
127
{[string match "*i*" [login_caps [login_basic loginbob:bobpw]]]}
128
129
set loginReply [login_basic loginbob:wrongpw]
130
test login-basic-badpw {[string match "HTTP/1.0 401*" \
131
[reply_status $loginReply]]}
132
133
# A value with no colon names no password: the request is served as "nobody".
134
test_eq login-basic-nocolon [login_who [login_basic nocolonhere]] nobody
135
136
# Header that does not begin with "Basic " is ignored.
137
set loginReply [login_get /test-env {"Authorization: Bearer abc"}]
138
test_eq login-basic-notbasic [login_who $loginReply] nobody
139
140
# Leading whitespace before and after "Basic" is skipped.
141
set loginReply [login_get /test-env \
142
[list "Authorization: Basic [binary encode base64 loginbob:bobpw]"]]
143
test_eq login-basic-leading-space [login_who $loginReply] loginbob
144
145
# An email address in place of the login name resolves to that user.
146
fossil sql "UPDATE user SET info='Bob <[email protected]>' WHERE login='loginbob'"
147
test_eq login-basic-email [login_who [login_basic [email protected]:bobpw]] loginbob
148
fossil sql "DELETE FROM config WHERE name='http_authentication_ok'"
149
150
###############################################################################
151
# The "resid"/"token" query parameters carry an (optionally obscured) login and
152
# password. Both the obscured and the plain forms log the user in.
153
154
fossil test-obscure loginbob
155
regexp -- {-> ([0-9a-f]+)} $::RESULT all loginObUser
156
fossil test-obscure bobpw
157
regexp -- {-> ([0-9a-f]+)} $::RESULT all loginObPw
158
test_eq login-resid-obscured \
159
[login_who [login_get "/test-env?resid=$loginObUser&token=$loginObPw"]] \
160
loginbob
161
test_eq login-resid-plain \
162
[login_who [login_get "/test-env?resid=loginbob&token=bobpw"]] loginbob
163
# A token that names no user leaves the request as "nobody".
164
test_eq login-resid-unknown \
165
[login_who [login_get "/test-env?resid=nosuch&token=nopw"]] nobody
166
167
###############################################################################
168
# A form login with correct credentials sets a login cookie; presenting that
169
# cookie on a later request re-establishes the user. A wrong password gives a
170
# 401 and no cookie.
171
172
proc login_form {user pw {extra ""}} {
173
return [login_post /login "u=$user&p=$pw&in=Login&$extra"]
174
}
175
set loginReply [login_form loginbob bobpw]
176
test login-form-status {[string match "HTTP/1.0 302*" \
177
[reply_status $loginReply]]}
178
set loginCookie [reply_header $loginReply Set-Cookie]
179
test login-form-cookie {[string match "fossil-*=*loginbob;*HttpOnly;*" \
180
$loginCookie]}
181
regexp {(fossil-[0-9a-f]+=[^;]+)} $loginCookie all loginCookiePair
182
test_eq login-cookie-reuse \
183
[login_who [login_get /test-env [list "Cookie: $loginCookiePair"]]] loginbob
184
185
# An email address with a wrong password is refused too.
186
if {$loginWaits} {
187
lassign [login_send_all [list \
188
[login_post_text /login "u=loginbob&p=nope&in=Login&"] \
189
[login_post_text /login "[email protected]&p=wrongpw&in=Login&"]]] \
190
loginReply loginEmailReply
191
test login-form-badpw \
192
{[string match "HTTP/1.0 401*" [reply_status $loginReply]]}
193
test_eq login-form-badpw-nocookie [reply_header $loginReply Set-Cookie] "-"
194
test login-form-email-badpw \
195
{[string match "HTTP/1.0 401*" [reply_status $loginEmailReply]]}
196
}
197
198
###############################################################################
199
# The "g" goto parameter is always rendered relative to the repository root:
200
# an absolute URL given in "g" is prefixed and so cannot leave the site.
201
202
set loginReply [login_form loginbob bobpw "g=/doc/trunk/x"]
203
test_eq login-goto-relative [reply_header $loginReply Location] \
204
http://x//doc/trunk/x
205
206
###############################################################################
207
# Logout. A logged-in user who POSTs "out" with a matching Referer and the
208
# Anti-CSRF token from the form has the login cookie cleared (an empty value
209
# with a past expiry).
210
211
set loginReply [login_form loginbob bobpw]
212
regexp {(fossil-[0-9a-f]+=[^;]+)} [reply_header $loginReply Set-Cookie] \
213
all loginLogoutPair
214
set loginReply [login_get /login [list "Cookie: $loginLogoutPair"]]
215
regexp {name="csrf" value="([^"]+)"} $loginReply all loginCsrf
216
set loginReply [login_post /login "out=Logout&csrf=$loginCsrf" \
217
[list "Cookie: $loginLogoutPair"]]
218
test login-logout-clears \
219
{[string match "fossil-*=; *" [reply_header $loginReply Set-Cookie]]}
220
221
###############################################################################
222
# Robot detection. With no login, g.isRobot is set from the User-Agent: a
223
# self-identified bot is a robot, a current browser is not.
224
225
proc login_isrobot {agent} {
226
set reply [login_get /test-env [list "User-Agent: $agent"]]
227
regexp {g\.isRobot = ([01])<br>} $reply all r
228
return $r
229
}
230
test_eq login-robot-bot [login_isrobot "Googlebot/2.1"] 1
231
test_eq login-robot-browser [login_isrobot "Mozilla/5.0 Firefox/122.0"] 0
232
test_eq login-robot-noagent [login_isrobot ""] 1
233
# The "isrobot" query parameter forces the robot flag on.
234
set loginReply [login_get "/test-env?isrobot=1" \
235
{"User-Agent: Mozilla/5.0 Firefox/122.0"}]
236
test login-robot-forced {[regexp {g\.isRobot = 1<br>} $loginReply]}
237
238
###############################################################################
239
# test-ishuman labels User-Agent strings read from standard input.
240
241
proc login_ishuman {agent} {
242
fossil_raw -input "$agent\n" test-ishuman
243
return [lindex [split [string trim $::RESULT]] 0]
244
}
245
test_eq login-ishuman-firefox [login_ishuman "Mozilla/5.0 Firefox/122"] HUMAN
246
test_eq login-ishuman-spider [login_ishuman "something spider"] ROBOT
247
test_eq login-ishuman-url [login_ishuman "agent http://x"] ROBOT
248
test_eq login-ishuman-lynx [login_ishuman "Lynx/2.9"] HUMAN
249
test_eq login-ishuman-oldmozilla [login_ishuman "Mozilla/3.0"] ROBOT
250
251
###############################################################################
252
# test-resetpw-url builds a /resetpw suffix for a user and verifies that it is
253
# accepted straight away, and that an altered suffix is rejected.
254
255
set loginUid [string trim [fossil sql \
256
"SELECT uid FROM user WHERE login='loginbob'"]]
257
fossil test-resetpw-url $loginUid
258
test login-resetpw-valid \
259
{[regexp "/resetpw/\[0-9a-f-\]+ +$loginUid \\(loginbob\\)" $::RESULT]}
260
261
###############################################################################
262
# test-email-used reports which user owns an email address.
263
264
fossil test-email-used [email protected] [email protected]
265
test login-email-used-known \
266
{[string match "*[email protected]: UID*loginbob*" $::RESULT]}
267
test login-email-used-unknown \
268
{[string match "*[email protected]: not used*" $::RESULT]}
269
270
###############################################################################
271
# Login groups. Two repositories are joined into a group, the membership is
272
# shown, then one leaves.
273
274
set loginRepo2 [file join [pwd] grp2.fossil]
275
fossil new $loginRepo2
276
fossil sql -R $loginRepo2 \
277
"INSERT INTO user(login,pw,cap) VALUES('loginsue','suepw','s')"
278
279
test login-group-none-start \
280
{![catch {fossil login-group} m] && [string match "*Not currently*" \
281
$::RESULT]}
282
fossil login-group join --name loggrp $loginRepo2
283
fossil login-group
284
test login-group-joined {[string match "*login-group \"loggrp\"*grp2.fossil*" \
285
$::RESULT]}
286
test_eq login-group-name-fn [string trim [fossil sql \
287
"SELECT value FROM config WHERE name='login-group-name'"]] loggrp
288
fossil login-group leave
289
fossil login-group
290
test login-group-left {[string match "*Not currently*" $::RESULT]}
291
292
# An unknown subcommand is an error.
293
fossil_raw login-group bogus
294
test login-group-bad {$CODE != 0
295
&& [string match "*unknown command*" $RESULT]}
296
297
###############################################################################
298
# Changing a password through the /login form. A logged-in user with the
299
# Password capability supplies the old password and two copies of a new one,
300
# together with the Anti-CSRF token from the form. A correct change
301
# redirects; afterwards the new password authenticates and the old does not.
302
# A mismatch or a wrong old password leaves the password unchanged.
303
304
fossil user capabilities loginbob op
305
fossil sql "REPLACE INTO config(name,value,mtime)
306
VALUES('http_authentication_ok','1',0)"
307
308
# Log in as loginbob and return a two-element list {cookie-pair csrf-token}.
309
proc login_session {user pw} {
310
set reply [login_form $user $pw]
311
regexp {(fossil-[0-9a-f]+=[^;]+)} [reply_header $reply Set-Cookie] all pair
312
set reply [login_get /login [list "Cookie: $pair"]]
313
regexp {name="csrf" value="([^"]+)"} $reply all tok
314
return [list $pair $tok]
315
}
316
317
set loginSess [login_session loginbob bobpw]
318
set loginPair [lindex $loginSess 0]
319
set loginTok [lindex $loginSess 1]
320
321
set loginReply [login_post /login \
322
"p=bobpw&n1=newbobpw&n2=newbobpw&csrf=$loginTok" \
323
[list "Cookie: $loginPair"]]
324
test login-pwchange-ok {[string match "HTTP/1.0 302*" \
325
[reply_status $loginReply]]}
326
test_eq login-pwchange-newpw-works \
327
[login_who [login_basic loginbob:newbobpw]] loginbob
328
test login-pwchange-oldpw-fails \
329
{[string match "HTTP/1.0 401*" [reply_status [login_basic loginbob:bobpw]]]}
330
331
set loginReply [login_post /login \
332
"p=newbobpw&n1=aaaaaa&n2=bbbbbb&csrf=$loginTok" [list "Cookie: $loginPair"]]
333
test login-pwchange-mismatch {[string match "*do not match*" $loginReply]}
334
335
if {$loginWaits} {
336
set loginReply [login_post /login \
337
"p=totallywrong&n1=cccccc&n2=cccccc&csrf=$loginTok" \
338
[list "Cookie: $loginPair"]]
339
test login-pwchange-badold \
340
{[string match "*incorrect old password*" $loginReply]}
341
}
342
343
###############################################################################
344
# Self-registration through /register. The captcha answer is derived the same
345
# way the server derives it: the first 8 hex digits of sha1("SECRET-SEEDHEX").
346
# A complete, valid form creates the account; each validation rule rejects the
347
# form with its own message.
348
349
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('self-register','1',0)"
350
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('default-perms','u',0)"
351
352
# Return {seed captcha csrf} scraped from a freshly rendered /register form.
353
proc login_register_tokens {} {
354
set reply [login_get /register]
355
regexp {name="captchaseed" value="([0-9]+)"} $reply all seed
356
regexp {name="csrf" value="([^"]+)"} $reply all csrf
357
set secret [string trim [fossil sql \
358
"SELECT value FROM config WHERE name='captcha-secret'"]]
359
set fd [open capin.txt wb]
360
puts -nonewline $fd "$secret-[format %x $seed]"
361
close $fd
362
fossil sha1sum capin.txt
363
set captcha [string range [lindex $::RESULT 0] 0 7]
364
return [list $seed $captcha $csrf]
365
}
366
set loginRegTok [login_register_tokens]
367
set loginSeed [lindex $loginRegTok 0]
368
set loginCap [lindex $loginRegTok 1]
369
set loginRCsrf [lindex $loginRegTok 2]
370
set loginRegBase \
371
"new=1&captchaseed=$loginSeed&captcha=$loginCap&csrf=$loginRCsrf"
372
373
# POST a registration form whose user-supplied fields are FIELDS.
374
proc login_register {fields} {
375
global loginRegBase
376
return [login_post /register "$loginRegBase&$fields"]
377
}
378
379
set loginReply [login_register \
380
"u=realuser&dn=Real&[email protected]&p=longpw9&cp=longpw9"]
381
test login-register-ok {[string match "HTTP/1.0 302*" \
382
[reply_status $loginReply]]}
383
test_eq login-register-created \
384
[string trim [fossil sql "SELECT login FROM user WHERE login='realuser'"]] \
385
"realuser"
386
387
test login-register-short-uid \
388
{[string match "*User ID too short*" [login_register \
389
"u=abc&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
390
test login-register-reserved \
391
{[string match "*reserved*" [login_register \
392
"u=anonymousx&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
393
test login-register-no-dname \
394
{[string match "*Required*" [login_register \
395
"u=gooduser&dn=&[email protected]&p=longpw9&cp=longpw9"]]}
396
test login-register-bad-email \
397
{[string match "*Not a valid email*" [login_register \
398
"u=gooduser&dn=Real&ea=notmail&p=longpw9&cp=longpw9"]]}
399
test login-register-short-pw \
400
{[string match "*at least 6 characters*" [login_register \
401
"u=gooduser&dn=Real&[email protected]&p=sh&cp=sh"]]}
402
test login-register-mismatch \
403
{[string match "*do not match*" [login_register \
404
"u=gooduser&dn=Real&[email protected]&p=longpw9&cp=other99"]]}
405
test login-register-taken \
406
{[string match "*already taken*" [login_register \
407
"u=realuser&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
408
# A bad captcha is rejected before any field is examined.
409
test login-register-bad-captcha \
410
{[string match "*Incorrect CAPTCHA*" [login_post /register \
411
"new=1&captchaseed=$loginSeed&captcha=00000000&csrf=$loginRCsrf&u=whoever&dn=R&[email protected]&p=longpw9&cp=longpw9"]]}
412
413
###############################################################################
414
# Capability letters from the user record. With "nobody" and "anonymous"
415
# stripped of all capability and auto-hyperlink off, the capabilities the
416
# server grants are exactly the expansion of the user's own capability string,
417
# including the recursive inheritance of "reader" (u) and "developer" (v).
418
419
fossil sql "UPDATE user SET cap='' WHERE login IN ('nobody','anonymous')"
420
fossil sql "UPDATE user SET cap='j' WHERE login='reader'"
421
fossil sql "UPDATE user SET cap='b' WHERE login='developer'"
422
fossil sql \
423
"REPLACE INTO config(name,value,mtime) VALUES('auto-hyperlink','0',0)"
424
proc login_capexpand {cap} {
425
fossil sql "UPDATE user SET cap='$cap' WHERE login='loginbob'"
426
return [login_caps [login_basic loginbob:newbobpw]]
427
}
428
test_eq login-cap-wiki [login_capexpand kv] bjkmL
429
test_eq login-cap-tkt [login_capexpand w] cnrwL
430
test_eq login-cap-reader [login_capexpand u] jL
431
test_eq login-cap-read [login_capexpand ojf] fjoL
432
433
###############################################################################
434
# isHuman recognises several current browsers and several bot tells. Each row
435
# is a User-Agent string and the label test-ishuman must give it.
436
foreach {loginIhName loginIhAgent loginIhWant} {
437
chrome "Mozilla/5.0 (X11) Chrome/120.0" HUMAN
438
msie "Mozilla/5.0 (compatible; MSIE 9.0; Windows)" HUMAN
439
trident "Mozilla/5.0 (Windows; Trident/7.0; rv:11.0)" HUMAN
440
webkit "Mozilla/5.0 AppleWebKit/605.1 (KHTML, like)" HUMAN
441
palemoon "Mozilla/5.0 Goanna PaleMoon/32.0" HUMAN
442
opera "Opera/9.80 (Windows)" HUMAN
443
safari "Safari/537.36" HUMAN
444
netsurf "NetSurf/3.10 (Linux)" HUMAN
445
crawler "Mozilla/5.0 compatible crawler" ROBOT
446
googai "Mozilla/5.0 GoogleOther)" ROBOT
447
walker "Mozilla/5.0 Safari/537.36Mozilla/5.0" ROBOT
448
plain5 "Mozilla/5.0 (unknown engine)" ROBOT
449
} {
450
test_eq login-ishuman-$loginIhName [login_ishuman $loginIhAgent] $loginIhWant
451
}
452
453
###############################################################################
454
# Capability expansion for the privilege letters that grant many flags at once:
455
# "s" (Setup) unlocks everything, and the forum digits accumulate.
456
set loginSetupCaps [login_capexpand s]
457
test login-cap-setup {[string match "*a*" $loginSetupCaps]
458
&& [string match "*s*" $loginSetupCaps] && [string match "*D*" \
459
$loginSetupCaps]}
460
test_eq login-cap-forum6 [login_capexpand 6] 23456L
461
test_eq login-cap-forum3 [login_capexpand 3] 23L
462
463
###############################################################################
464
# Login cookies that are not of the form HASH/ARG/USER, or that name a user
465
# with a hash that is not that user's: the page is served as usual and the
466
# visitor is "nobody".
467
468
regexp {^(fossil-[0-9a-f]+)=} $loginCookiePair all loginCookieName
469
foreach {loginCkName loginCkValue} {
470
slash /
471
end-slash abcdef/
472
two-parts a/b/
473
no-slash abcdef
474
empty {}
475
wrong-hash
476
0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01/0123456789abcdef/loginbob
477
special-user
478
0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01/0123456789abcdef/nobody
479
} {
480
set loginReply [login_get /test-env \
481
[list "Cookie: $loginCookieName=$loginCkValue"]]
482
test login-cookie-bad-$loginCkName {$CODE == 0
483
&& [reply_status $loginReply] eq "HTTP/1.0 200 OK"
484
&& [login_who $loginReply] eq "nobody"}
485
}
486
487
###############################################################################
488
# Where a login goes afterwards when the request carries "fossil-goto", as a
489
# query parameter, a form field or a cookie: to that address if it is on this
490
# site, and to the home page if it is anywhere else. A value that is not an
491
# absolute http: or https: address, such as "timeline" or "//evil.example/",
492
# is taken as a page of this site. The same holds for an
493
# anonymous login and for the answer to the robot captcha at /ityaar.
494
495
set loginHome [reply_header [login_form loginbob newbobpw] Location]
496
test login-goto-home {[string match "http://x/*" $loginHome]}
497
fossil sql "UPDATE user SET cap='h' WHERE login='anonymous'"
498
set loginAnon "u=anonymous&p=$loginCap&cs=$loginSeed"
499
set loginRobot "captchaseed=$loginSeed&captcha=$loginCap"
500
501
foreach {loginGoName loginGoTarget loginGoWant} [list \
502
other-site https://evil.example/x $loginHome \
503
host-prefix http://x.evil.example/y $loginHome \
504
no-scheme //evil.example/ http://x//evil.example/ \
505
relative timeline http://x/timeline \
506
same-site http://x/timeline http://x/timeline \
507
] {
508
set loginReply [login_get \
509
"/login?u=loginbob&p=newbobpw&fossil-goto=$loginGoTarget"]
510
test_eq login-goto-query-$loginGoName [reply_header $loginReply Location] \
511
$loginGoWant
512
set loginReply [login_form loginbob newbobpw "fossil-goto=$loginGoTarget"]
513
test_eq login-goto-form-$loginGoName [reply_header $loginReply Location] \
514
$loginGoWant
515
set loginReply [login_post /login "u=loginbob&p=newbobpw&in=Login" \
516
[list "Cookie: fossil-goto=$loginGoTarget"]]
517
test_eq login-goto-cookie-$loginGoName [reply_header $loginReply Location] \
518
$loginGoWant
519
set loginReply [login_get "/login?$loginAnon&fossil-goto=$loginGoTarget"]
520
test login-goto-anonymous-$loginGoName \
521
{[string match "*anonymous*" [reply_header $loginReply Set-Cookie]]
522
&& [reply_header $loginReply Location] eq $loginGoWant}
523
set loginReply [login_get "/ityaar?$loginRobot&fossil-goto=$loginGoTarget"]
524
test_eq login-goto-ityaar-$loginGoName [reply_header $loginReply Location] \
525
$loginGoWant
526
}
527
528
# The captcha page at /honeypot remembers the address asked for in a
529
# "fossil-goto" cookie, and a right answer sent with that cookie goes there.
530
set loginReply [login_get /honeypot?x=1]
531
test login-honeypot-cookie {[string match "*X-Robot: 1*" $loginReply]
532
&& [string match "*Set-Cookie: fossil-goto=http%3A%2F%2Fx%2Fhoneypot*" \
533
$loginReply]}
534
set loginReply [login_get "/ityaar?$loginRobot" \
535
{"Cookie: fossil-goto=http%3A%2F%2Fx%2Fhoneypot%3Fx%3D1"}]
536
test_eq login-honeypot-return [reply_header $loginReply Location] \
537
http://x/honeypot?x=1
538
# A wrong answer shows the captcha again.
539
set loginReply [login_get "/ityaar?captchaseed=$loginSeed&captcha=zzzzzzzz"]
540
test login-ityaar-wrong {[string match "*Prove that you are human*" \
541
$loginReply]}
542
543
###############################################################################
544
# Helpers for the cases below.
545
546
# A GET or POST to the repository REPO instead of the main one.
547
proc login_get_at {repo path {headers {}}} {
548
global loginRepo
549
set saved $loginRepo
550
set loginRepo $repo
551
set reply [login_get $path $headers]
552
set loginRepo $saved
553
return $reply
554
}
555
proc login_post_at {repo path body {headers {}}} {
556
global loginRepo
557
set saved $loginRepo
558
set loginRepo $repo
559
set reply [login_post $path $body $headers]
560
set loginRepo $saved
561
return $reply
562
}
563
564
# The NAME=VALUE of the login cookie set by a reply, or "".
565
proc login_cookie_pair {reply} {
566
if {[regexp {Set-Cookie: (fossil-[0-9a-f]+=[^;]+)} $reply all pair]} {
567
return $pair
568
}
569
return ""
570
}
571
572
# The authentication method /test-env reports: COOKIE, LOCAL, PW, ENV or HTTP.
573
proc login_method {reply} {
574
if {[regexp {g\.eAuthMethod = [0-9]+ \(([A-Z]+)\)} $reply all m]} {
575
return $m
576
}
577
return -
578
}
579
580
581
# Set the repository setting NAME to VALUE, or remove it when VALUE is "".
582
proc login_config {name value} {
583
if {$value eq ""} {
584
fossil sql "DELETE FROM config WHERE name='$name'"
585
} else {
586
fossil sql \
587
"REPLACE INTO config(name,value,mtime) VALUES('$name','$value',0)"
588
}
589
}
590
591
###############################################################################
592
# The capability letters not shown above each grant their own capability.
593
# "anonymous", whose capabilities every logged-in user also has, is given
594
# none for these cases.
595
fossil sql "UPDATE user SET cap='' WHERE login='anonymous'"
596
test_eq login-cap-letters [login_capexpand celmnqtxyzB7ACD] celmnqtxyz7ACDL
597
598
###############################################################################
599
# auto-hyperlink. A logged-in user without the Hyperlink capability is
600
# given it when the setting is 1 (with links written by script) or 2, and
601
# is not given it when the setting is 0.
602
fossil sql "UPDATE user SET cap='o' WHERE login='loginbob'"
603
foreach {loginAlName loginAlValue loginAlCaps loginAlJs} {
604
off 0 oL 0
605
script 1 hoL 1
606
plain 2 hoL 0
607
} {
608
login_config auto-hyperlink $loginAlValue
609
set loginReply [login_basic loginbob:newbobpw]
610
test login-autolink-$loginAlName {[login_caps $loginReply] eq $loginAlCaps
611
&& [string match "*g.jsHref = $loginAlJs<br>*" $loginReply]}
612
}
613
login_config auto-hyperlink 0
614
fossil sql "UPDATE user SET cap='h' WHERE login='anonymous'"
615
616
###############################################################################
617
# public-pages. A request for a page that matches one of its globs is
618
# given the "default-perms" capabilities; a request for another page is
619
# not.
620
login_config default-perms r
621
login_config public-pages /test-env*
622
test_eq login-public-match [login_caps [login_get /test-env]] r
623
login_config public-pages /other*,/x*
624
test_eq login-public-nomatch [login_caps [login_get /test-env]] ""
625
login_config public-pages ""
626
login_config default-perms u
627
628
###############################################################################
629
# With no user named "nobody", a request without credentials is served as
630
# user id -1 with no capabilities.
631
fossil sql "UPDATE user SET login='nobody-saved' WHERE login='nobody'"
632
set loginReply [login_get /test-env]
633
test login-no-nobody {[reply_status $loginReply] eq "HTTP/1.0 200 OK"
634
&& [string match "*g.userUid = -1<br>*" $loginReply]
635
&& [login_caps $loginReply] eq ""}
636
fossil sql "UPDATE user SET login='nobody' WHERE login='nobody-saved'"
637
638
###############################################################################
639
# REMOTE_USER. Under CGI, the REMOTE_USER the web server sets names the
640
# user when the "remote_user_ok" setting is on, and is ignored when it is
641
# off.
642
643
fossil settings backoffice-disable 1
644
write_file login.ctl "repository: [file nativename $loginRepo]\n"
645
proc login_cgi {user} {
646
set vars [list REQUEST_METHOD GET SCRIPT_NAME "" PATH_INFO /test-env \
647
REMOTE_ADDR 9.9.9.9 REMOTE_USER $user]
648
foreach {name value} $vars { set ::env($name) $value }
649
fossil_raw cgi login.ctl
650
foreach {name value} $vars { unset ::env($name) }
651
return $::RESULT
652
}
653
test_eq login-remote-user-off [login_who [login_cgi loginbob]] nobody
654
login_config remote_user_ok 1
655
set loginReply [login_cgi loginbob]
656
test login-remote-user-on {[login_who $loginReply] eq "loginbob"
657
&& [login_method $loginReply] eq "ENV"}
658
test_eq login-remote-user-unknown [login_who [login_cgi nosuchuser]] nobody
659
login_config remote_user_ok ""
660
661
###############################################################################
662
# Local authentication. A request from 127.0.0.1 to "fossil http
663
# --localauth" is served as the first user with Setup capability, or,
664
# when the repository is the open check-out's, as the check-out's default
665
# user. With the "localauth" setting on, the request is served as
666
# "nobody".
667
668
proc login_local {args} {
669
write_file lreq.txt "GET /test-env HTTP/1.1\r\nHost: x\r\n\r\n"
670
file delete lreply.txt
671
fossil_raw http {*}$args --in lreq.txt --out lreply.txt \
672
--ipaddr 127.0.0.1 --localauth
673
if {[file exists lreply.txt]} { return [read_file lreply.txt] }
674
return ""
675
}
676
set loginSetupUser [string trim [fossil sql \
677
"SELECT login FROM user WHERE cap LIKE '%s%' ORDER BY uid LIMIT 1"]]
678
set loginReply [login_local $loginRepo]
679
test login-local {[login_who $loginReply] eq $loginSetupUser
680
&& [login_method $loginReply] eq "LOCAL"
681
&& [string match "*s*" [login_caps $loginReply]]}
682
fossil user default
683
set loginDefaultUser [string trim $RESULT]
684
fossil user default loginbob
685
test_eq login-local-default-user [login_who [login_local]] loginbob
686
fossil user default $loginDefaultUser
687
fossil settings localauth 1
688
test_eq login-local-setting [login_who [login_local $loginRepo]] nobody
689
fossil unset localauth
690
691
###############################################################################
692
# With --httptrace, the login and its capability string are written to
693
# standard error.
694
write_file lreq.txt \
695
[join [list \
696
"GET /test-env HTTP/1.1\r\nHost: x\r\n" \
697
"Authorization: Basic " \
698
"[binary encode base64 loginbob:newbobpw]\r\n\r\n"] ""]
699
fossil_raw http $loginRepo --in lreq.txt --out lreply.txt --ipaddr 9.9.9.9 \
700
--httptrace
701
test login-httptrace \
702
{[string first {# login: [loginbob] with capabilities [o]} $RESULT] >= 0}
703
704
###############################################################################
705
# Anonymous login cookies, HASH/TIME/anonymous. Sent back with the same
706
# User-Agent, the cookie logs in as "anonymous"; with another User-Agent,
707
# past the anonymous cookie lifespan, or with anonymous logins turned off
708
# it is ignored. A cookie or a captcha answer made with the previous
709
# captcha secret is accepted.
710
711
set loginUAText "Mozilla/5.0 Firefox/122.0"
712
set loginUA "User-Agent: $loginUAText"
713
set loginReply [login_get "/login?$loginAnon" [list $loginUA]]
714
set loginAnonPair [login_cookie_pair $loginReply]
715
test login-anon-cookie {[string match "fossil-*=*%2Fanonymous" $loginAnonPair]}
716
test login-anon-cookie-session \
717
{![string match "*max-age=*" [reply_header $loginReply Set-Cookie]]}
718
set loginReply [login_get /test-env [list "Cookie: $loginAnonPair" $loginUA]]
719
test login-anon-cookie-reuse {[login_who $loginReply] eq "anonymous"
720
&& [login_method $loginReply] eq "COOKIE"}
721
test_eq login-anon-cookie-other-agent [login_who [login_get /test-env \
722
[list "Cookie: $loginAnonPair" "User-Agent: Mozilla/5.0 Chrome/120.0"]]] \
723
nobody
724
set loginReply [login_get "/login?$loginAnon&remember=1"]
725
test login-anon-cookie-persistent \
726
{[string match "*%2Fanonymous;*max-age=*" \
727
[reply_header $loginReply Set-Cookie]]}
728
729
set loginSecret [string trim [fossil sql \
730
"SELECT value FROM config WHERE name='captcha-secret'"]]
731
fossil sql "REPLACE INTO config(name,value,mtime)
732
VALUES('captcha-secret-1','oldsecret',unixepoch())"
733
set loginNow [string trim [fossil sql "SELECT julianday('now')"]]
734
set loginOldHash [sha1_text "$loginNow/$loginUAText/oldsecret"]
735
test_eq login-anon-cookie-old-secret [login_who [login_get /test-env \
736
[list "Cookie: $loginCookieName=$loginOldHash/$loginNow/anonymous" \
737
$loginUA]]] \
738
anonymous
739
set loginStaleHash [sha1_text "2400000.5/$loginUAText/$loginSecret"]
740
test_eq login-anon-cookie-expired [login_who [login_get /test-env \
741
[list "Cookie: $loginCookieName=$loginStaleHash/2400000.5/anonymous" \
742
$loginUA]]] \
743
nobody
744
set loginOldPw [string range \
745
[sha1_text "oldsecret-[format %x $loginSeed]"] 0 7]
746
set loginReply [login_get "/login?u=anonymous&p=$loginOldPw&cs=$loginSeed"]
747
test login-anon-old-secret \
748
{[string match "*%2Fanonymous*" [reply_header $loginReply Set-Cookie]]}
749
if {$loginWaits} {
750
set loginReply [login_get "/login?u=anonymous&p=00000000&cs=$loginSeed"]
751
test login-anon-wrong {
752
[string match "HTTP/1.0 401*" [reply_status $loginReply]]
753
&& [reply_header $loginReply Set-Cookie] eq "-"}
754
}
755
fossil sql "DELETE FROM config WHERE name='captcha-secret-1'"
756
757
login_config anon-cookie-lifespan 0
758
test_eq login-anon-cookie-off [login_who [login_get /test-env \
759
[list "Cookie: $loginAnonPair" $loginUA]]] nobody
760
if {$loginWaits} {
761
set loginReply [login_get "/login?$loginAnon"]
762
test login-anon-login-off {
763
[string match "HTTP/1.0 401*" [reply_status $loginReply]]
764
&& [reply_header $loginReply Set-Cookie] eq "-"}
765
}
766
login_config anon-cookie-lifespan ""
767
768
###############################################################################
769
# The /login page. "anon=1" with "g" asks only for the captcha for an
770
# anonymous login; "g" alone names the page asked for, without its query
771
# string; a logged-in user is asked for a login with more privilege;
772
# "anon=2" ignores the login cookie; "noanon" leaves out the anonymous
773
# login; "self" goes to /register.
774
775
set loginReply [login_get "/login?anon=1&g=timeline"]
776
test login-page-anon {[string match {*name="anon" value="1"*} $loginReply]
777
&& [string match "*Verify that you are human*" $loginReply]
778
&& [string match "*Verify that I am human*" $loginReply]}
779
set loginReply [login_get "/login?g=timeline%3fn=5"]
780
test login-page-goto \
781
{[string match "*Login as a named user to access page <b>timeline</b>*" \
782
$loginReply]}
783
set loginSess [login_session loginbob newbobpw]
784
set loginPair [lindex $loginSess 0]
785
set loginReply [login_get "/login?g=timeline" [list "Cookie: $loginPair"]]
786
test login-page-goto-loggedin {[string match \
787
"*greater privilege than <b>loginbob</b>*to access <b>timeline</b>*" \
788
$loginReply]}
789
set loginReply [login_get "/login?anon=2" [list "Cookie: $loginPair"]]
790
test login-page-anon2 {![string match "*Currently logged in*" $loginReply]
791
&& [string match "*Verify that I am human*" $loginReply]}
792
set loginReply [login_get /login]
793
test login-page-visitors {[string match "*Visitors may enter*" $loginReply]
794
&& [string match "*Create A New Account*" $loginReply]}
795
test login-page-noanon \
796
{![string match "*Visitors may enter*" [login_get "/login?noanon=1"]]}
797
test_eq login-page-self [reply_header [login_get "/login?self=1"] Location] \
798
http://x/register
799
800
# A logout sent with "anon=2", which skips the login check, redirects and
801
# clears no cookie.
802
set loginReply [login_post /login "anon=2&out=Logout&csrf="]
803
test login-logout-anon2 {[string match "HTTP/1.0 302*" \
804
[reply_status $loginReply]]
805
&& [reply_header $loginReply Set-Cookie] eq "-"}
806
807
# With "https-login" on, the login form is sent to the https: address of
808
# the site, unless the server is run with --nossl.
809
login_config https-login 1
810
test login-page-https \
811
{[string match "*data-action='https://x/login'*" [login_get /login]]}
812
set loginReply [login_send "GET /login HTTP/1.1\r\nHost: x\r\n\r\n" --nossl]
813
test login-page-nossl {![string match "*'https://x/login'*" $loginReply]}
814
login_config https-login ""
815
816
# With "auto-captcha" on, the page offers a button that fills in the
817
# anonymous password.
818
login_config auto-captcha 1
819
test login-page-autocaptcha \
820
{[string match "*Fill out captcha*" [login_get /login]]}
821
login_config auto-captcha ""
822
823
# "nobody" with the Password capability is refused a password change.
824
fossil sql "UPDATE user SET cap='p' WHERE login='nobody'"
825
regexp {name="csrf" value="([^"]+)"} [login_get /login] all loginNbCsrf
826
set loginReply [login_post /login "p=x&n1=yyyyyy&n2=yyyyyy&csrf=$loginNbCsrf"]
827
test login-pwchange-nobody \
828
{[string match "*cannot be changed for this type of login*" $loginReply]}
829
fossil sql "UPDATE user SET cap='' WHERE login='nobody'"
830
831
###############################################################################
832
# /register: user names with special characters, a missing email address,
833
# addresses not allowed by "auth-sub-email" (compared ignoring case), an
834
# address of an existing user, a "g" parameter kept in the form, and the
835
# page when self-registration is off.
836
837
test login-register-special {[string match "*may not contain spaces*" \
838
[login_register "u=bad+user1&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
839
test login-register-no-email {[regexp {Email Address:.*Required} \
840
[login_register "u=gooduser&dn=Real&ea=&p=longpw9&cp=longpw9"]]}
841
login_config auth-sub-email *@ok.example
842
test login-register-unauthorized {[string match "*Not an authorized email*" \
843
[login_register \
844
"u=gooduser&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
845
test login-register-authorized {[string match "*Passwords do not match*" \
846
[login_register "u=gooduser&dn=Real&[email protected]&p=longpw9&cp=other99"]]}
847
login_config auth-sub-email ""
848
test login-register-email-used \
849
{[string match "*already associated with a user*" \
850
[login_register "u=gooduser&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
851
test login-register-goto \
852
{[string match {*name="g" value="timeline"*} \
853
[login_get "/register?g=timeline"]]}
854
login_config self-register ""
855
test login-register-off \
856
{[string match "*Registration not possible*" [login_get /register]]}
857
login_config self-register 1
858
859
###############################################################################
860
# Email. The alert tables are created and outgoing email is written to
861
# files in the directory lmail.
862
863
file mkdir lmail
864
fossil alerts status
865
fossil settings email-send-method dir
866
fossil settings email-send-dir [file join [pwd] lmail]
867
proc login_mail_count {} {
868
return [llength [glob -nocomplain -directory lmail *]]
869
}
870
proc login_mail_clear {} {
871
foreach f [glob -nocomplain -directory lmail *] { file delete $f }
872
}
873
874
# The registration form asks about email alerts when "default-perms"
875
# includes "7" or "selfreg-verify" is on.
876
test login-register-alerts-none \
877
{![string match "*Email&nbsp;Alerts?*" [login_get /register]]}
878
login_config default-perms 7u
879
test login-register-alerts-perms \
880
{[string match "*Email&nbsp;Alerts?*" [login_get /register]]}
881
login_config default-perms u
882
login_config selfreg-verify 1
883
test login-register-alerts-verify \
884
{[string match "*Email&nbsp;Alerts?*" [login_get /register]]}
885
886
# With "selfreg-verify", a new user starts with capability 7 only and is
887
# made a subscriber, and a verification email is sent. Without an
888
# "email-self" address the email cannot be sent and the page says so.
889
proc login_user_cap {login} {
890
return [string trim [fossil sql "SELECT cap FROM user WHERE login='$login'"]]
891
}
892
proc login_ssub {email} {
893
return [string trim [fossil sql \
894
"SELECT ssub FROM subscriber WHERE semail='$email'"]]
895
}
896
set loginReply [login_register \
897
"u=verify1&dn=V&[email protected]&p=longpw9&cp=longpw9"]
898
test login-register-verify-noself {[string match "*Internal Error*" $loginReply]
899
&& [login_user_cap verify1] eq "7" && [login_ssub [email protected]] eq "aw"}
900
fossil settings email-self [email protected]
901
login_config default-perms o2rj
902
set loginReply [login_register \
903
"u=verify2&dn=V&[email protected]&p=longpw9&cp=longpw9&g=timeline"]
904
test login-register-verify-sent \
905
{[string match {*An email has been sent to "[email protected]"*} $loginReply]
906
&& [string match "*href='timeline'>Continue*" $loginReply]
907
&& [login_mail_count] == 1 && [login_ssub [email protected]] eq "acftw"}
908
login_config default-perms u
909
# With alerts=0 there is no subscription and no email.
910
set loginReply [login_register \
911
"u=verify3&dn=V&[email protected]&p=longpw9&cp=longpw9&alerts=0"]
912
test login-register-noalerts \
913
{[string match "HTTP/1.0 302*" [reply_status $loginReply]]
914
&& [login_ssub [email protected]] eq "" && [login_mail_count] == 1}
915
# An address that is already a verified subscriber needs no verification.
916
fossil sql "INSERT INTO subscriber(semail,sverified,sdonotcall,sdigest,ssub)
917
VALUES('[email protected]',1,0,0,'a')"
918
set loginReply [login_register \
919
"u=verify4&dn=V&[email protected]&p=longpw9&cp=longpw9"]
920
test login-register-verified \
921
{[string match "HTTP/1.0 302*" [reply_status $loginReply]]
922
&& [string trim [fossil sql \
923
"SELECT suname FROM subscriber WHERE semail='[email protected]'"]] eq "verify4"
924
&& [login_mail_count] == 1}
925
login_config selfreg-verify ""
926
927
# The /login page of a user with capability 7 links to the email alerts
928
# page; once the repository has a forum it also links to the user's forum
929
# posts.
930
fossil sql "UPDATE user SET cap='o7' WHERE login='loginbob'"
931
set loginPair [lindex [login_session loginbob newbobpw] 0]
932
set loginReply [login_get /login [list "Cookie: $loginPair"]]
933
test login-page-alerts-link {[string match "*Email Alerts</a>*" $loginReply]
934
&& ![string match "*post timeline</a>*" $loginReply]}
935
write_file lpost.txt "A post.\n"
936
fossil forum new --title Hello --content lpost.txt
937
set loginReply [login_get /login [list "Cookie: $loginPair"]]
938
test login-page-forum-link \
939
{[string match "*post timeline</a> for user <b>loginbob</b>*" $loginReply]}
940
941
###############################################################################
942
# test-email-used: an address of a user with Setup capability, or that is
943
# not the first address in the user's contact information, has no
944
# password reset; a verified subscriber's address names that subscriber's
945
# user. Both test commands need an argument.
946
947
fossil sql "UPDATE user SET info='Sue <[email protected]>' WHERE login='loginsue'"
948
fossil sql "UPDATE user SET info='Ann <[email protected]> <[email protected]>'
949
WHERE login='verify3'"
950
fossil sql \
951
"INSERT INTO subscriber(semail,suname,sverified,sdonotcall,sdigest,ssub)
952
VALUES('[email protected]','verify2',1,0,0,'a'),
953
('[email protected]','loginsue',1,0,0,'a'),
954
('[email protected]','verify2',0,0,0,'a')"
955
fossil test-email-used [email protected] [email protected] [email protected] [email protected] \
956
[email protected]
957
test login-email-used-setup \
958
{[string match "*[email protected]: used but no password reset*" $RESULT]}
959
test login-email-used-second \
960
{[string match "*[email protected]: used but no password reset*" $RESULT]}
961
test login-email-used-subscriber \
962
{[string match "*[email protected]: UID*(verify2)*" $RESULT]}
963
test login-email-used-subscriber-setup \
964
{[string match "*[email protected]: used but no password reset*" $RESULT]}
965
test login-email-used-unverified \
966
{[string match "*[email protected]: not used*" $RESULT]}
967
fossil_raw test-email-used
968
test login-email-used-usage {$CODE != 0 && [string match "*Usage:*" $RESULT]}
969
fossil_raw test-resetpw-url
970
test login-resetpw-url-usage {$CODE != 0 && [string match "*Usage:*" $RESULT]}
971
972
###############################################################################
973
# /reqpwreset. With "self-pw-reset" on, it takes an email address and a
974
# captcha answer and emails a /resetpw link to a user who may reset a
975
# password. Bad input, an unknown address, and users with Setup or Admin
976
# capability (directly or inherited) or without the Password capability
977
# are refused with a message.
978
979
test login-reqpw-off \
980
{[string match "*does not allow users to reset*" [login_get /reqpwreset]]}
981
login_config self-pw-reset 1
982
test login-reqpw-form \
983
{[string match {*name="reqpwreset" value="1"*} [login_get /reqpwreset]]}
984
test login-reqpw-from-login \
985
{[string match {*name="reqpwreset" value="1"*} \
986
[login_get "/login?pwreset=1"]]}
987
test login-reqpw-from-register \
988
{[string match {*name="reqpwreset" value="1"*} \
989
[login_get "/register?pwreset=1"]]}
990
test login-page-reset-button \
991
{[string match "*Reset My Password*" [login_get /login]]}
992
test login-register-reset-button {[string match "*Request Password Reset For*" \
993
[login_register "u=gooduser&dn=Real&[email protected]&p=longpw9&cp=longpw9"]]}
994
995
proc login_reqpw {fields} {
996
global loginSeed loginCap
997
return [login_post /reqpwreset \
998
"reqpwreset=1&captchaseed=$loginSeed&captcha=$loginCap&$fields"]
999
}
1000
test login-reqpw-badcaptcha {[string match "*Incorrect CAPTCHA*" [login_post \
1001
/reqpwreset \
1002
"reqpwreset=1&captchaseed=$loginSeed&captcha=00000000&[email protected]"]]}
1003
fossil sql "UPDATE user SET cap='vp' WHERE login='loginbob'"
1004
fossil sql "UPDATE user SET cap='a' WHERE login='developer'"
1005
foreach {loginRqName loginRqAddr loginRqWant} {
1006
empty {} {*Required*}
1007
invalid notmail {*Not a valid email*}
1008
unknown [email protected] {*not associated with a user*}
1009
setup [email protected] {*not associated with a user*}
1010
inherited [email protected] {*not associated with a user*}
1011
} {
1012
test login-reqpw-$loginRqName \
1013
{[string match $loginRqWant [login_reqpw ea=$loginRqAddr]]}
1014
}
1015
fossil sql "UPDATE user SET cap='b' WHERE login='developer'"
1016
fossil sql "UPDATE user SET cap='o' WHERE login='loginbob'"
1017
test login-reqpw-nopw \
1018
{[string match "*not associated with a user*" [login_reqpw [email protected]]]}
1019
fossil sql "UPDATE user SET cap='op' WHERE login='loginbob'"
1020
login_config auth-sub-email *@ok.example
1021
test login-reqpw-unauthorized \
1022
{[string match "*Not an authorized email*" [login_reqpw [email protected]]]}
1023
login_config auth-sub-email ""
1024
1025
login_mail_clear
1026
fossil unset email-self
1027
test login-reqpw-noself \
1028
{[string match "*Internal Error*" [login_reqpw [email protected]]]}
1029
fossil settings email-self [email protected]
1030
set loginReply [login_reqpw [email protected]]
1031
test login-reqpw-sent {[string match {*has been sent to "[email protected]"*} \
1032
$loginReply]
1033
&& [login_mail_count] == 1}
1034
set loginMail [read_file [lindex [glob -directory lmail *] 0]]
1035
test login-reqpw-mail {[string match "*To: <[email protected]>*" $loginMail]
1036
&& [string match "*Password reset for http://x*" $loginMail]}
1037
1038
###############################################################################
1039
# /resetpw/UID-TIMESTAMP-HASH. A valid suffix gives a form that sets the
1040
# user's password. A suffix that is malformed, names no user, is more than
1041
# an hour old or has the wrong hash is refused, and so is a suffix whose
1042
# first or second field ends in a character other than "-", and a valid suffix
1043
# for "nobody", for a user with Setup capability or for a user without the
1044
# Password capability.
1045
1046
proc login_reset_suffix {login} {
1047
set uid [string trim [fossil sql "SELECT uid FROM user WHERE login='$login'"]]
1048
fossil test-resetpw-url $uid
1049
regexp {/resetpw/([0-9a-f-]+)} $::RESULT all suffix
1050
return $suffix
1051
}
1052
set loginRName [login_reset_suffix loginbob]
1053
test login-resetpw-form {[string match \
1054
"*Change Password for user <b>loginbob</b>*" \
1055
[login_get /resetpw/$loginRName]]}
1056
test login-resetpw-mismatch {[string match "*do not match*" \
1057
[login_post /resetpw "name=$loginRName&n1=aaaaaa&n2=bbbbbb"]]}
1058
test login-resetpw-changed {[string match "*Password changed successfully*" \
1059
[login_post /resetpw "name=$loginRName&n1=resetpw1&n2=resetpw1"]]}
1060
test_eq login-resetpw-works [login_who [login_basic loginbob:resetpw1]] loginbob
1061
1062
set loginHash [string repeat 0 64]
1063
set loginNowHex [format %x [clock seconds]]
1064
set loginBadTail [string repeat 0 70]
1065
set loginRbNames {}
1066
set loginRbRequests {}
1067
foreach {loginRbName loginRbSuffix} [list \
1068
short 1-2-3 \
1069
no-uid [string repeat x 70] \
1070
no-time "1--$loginHash$loginHash" \
1071
uid-zero "0-$loginNowHex-$loginHash" \
1072
no-user "ffff-$loginNowHex-$loginHash" \
1073
expired "[format %x $loginUid]-1-$loginHash" \
1074
wrong-hash "[format %x $loginUid]-$loginNowHex-$loginHash" \
1075
uid-end "1x$loginBadTail" \
1076
time-end "1-2x$loginBadTail" \
1077
] {
1078
lappend loginRbNames $loginRbName
1079
lappend loginRbRequests [request_get /resetpw/$loginRbSuffix]
1080
}
1081
if {$loginWaits} {
1082
foreach loginRbName $loginRbNames \
1083
loginReply [login_send_all $loginRbRequests] {
1084
test login-resetpw-bad-$loginRbName \
1085
{[string match "*URL is invalid*" $loginReply]}
1086
}
1087
}
1088
foreach loginRrUser {loginsue nobody} {
1089
test login-resetpw-refused-$loginRrUser {[string match \
1090
"*Cannot change the password for user*" \
1091
[login_get /resetpw/[login_reset_suffix $loginRrUser]]]}
1092
}
1093
fossil sql "UPDATE user SET cap='o' WHERE login='loginbob'"
1094
test login-resetpw-refused-nopw {[string match \
1095
"*Cannot change the password for user*" \
1096
[login_get /resetpw/[login_reset_suffix loginbob]]]}
1097
fossil sql "UPDATE user SET cap='op' WHERE login='loginbob'"
1098
1099
###############################################################################
1100
# A password change sets the password in every repository of the login
1101
# group. A member that is not a repository database is reported on the
1102
# page; a member whose file is gone is dropped from the group.
1103
1104
write_file ljunk.fossil "This is not a database.\n"
1105
login_config peer-repo-0000000000000000 [file join [pwd] ljunk.fossil]
1106
login_config peer-repo-1111111111111111 [file join [pwd] gone.fossil]
1107
login_config peer-name-1111111111111111 gone
1108
set loginSess [login_session loginbob resetpw1]
1109
set loginReply [login_post /login \
1110
"p=resetpw1&n1=groupnew1&n2=groupnew1&csrf=[lindex $loginSess 1]" \
1111
[list "Cookie: [lindex $loginSess 0]"]]
1112
test login-group-pwchange-error {[string match "*ljunk.fossil: *" $loginReply]
1113
&& ![string match "HTTP/1.0 302*" [reply_status $loginReply]]}
1114
test_eq login-group-gone-dropped [string trim [fossil sql \
1115
"SELECT count(*) FROM config WHERE name GLOB 'peer-*-1111111111111111'"]] 0
1116
set loginRName [login_reset_suffix loginbob]
1117
test login-group-resetpw-error {[string match "*ljunk.fossil: *" \
1118
[login_post /resetpw "name=$loginRName&n1=groupnew2&n2=groupnew2"]]}
1119
login_config peer-repo-0000000000000000 ""
1120
test_eq login-group-resetpw-works \
1121
[login_who [login_basic loginbob:groupnew2]] loginbob
1122
1123
###############################################################################
1124
# Login-group credentials. A login cookie issued by one member of a group
1125
# logs in the same user at another member, which takes over the cookie.
1126
# If the other member has no user of that name, the cookie gives an
1127
# anonymous login. A well-formed group cookie also keeps /ityaar from
1128
# replacing it with an anonymous cookie, and from asking again.
1129
1130
fossil sql -R $loginRepo2 \
1131
"INSERT INTO user(login,pw,cap,info) VALUES('grponly','gopw','o','')
1132
;UPDATE user SET info='' WHERE login='loginsue'"
1133
fossil login-group join --name loggrp2 $loginRepo2
1134
set loginGrpCookie fossil-[string range [string trim [fossil sql \
1135
"SELECT value FROM config WHERE name='login-group-code'"]] 0 15]
1136
set loginReply [login_post_at $loginRepo2 /login "u=loginsue&p=suepw&in=Login"]
1137
set loginSuePair [login_cookie_pair $loginReply]
1138
test login-group-cookie-name \
1139
{[string match "$loginGrpCookie=*%2Floginsue" $loginSuePair]}
1140
set loginReply [login_get /test-env [list "Cookie: $loginSuePair"]]
1141
test login-group-transfer {[login_who $loginReply] eq "loginsue"
1142
&& [login_method $loginReply] eq "COOKIE"}
1143
set loginReply [login_post_at $loginRepo2 /login "u=grponly&p=gopw&in=Login"]
1144
set loginGoPair [login_cookie_pair $loginReply]
1145
test_eq login-group-transfer-anon \
1146
[login_who [login_get /test-env [list "Cookie: $loginGoPair"]]] anonymous
1147
1148
set loginWellFormed "$loginGrpCookie=[string repeat a 50]/x"
1149
set loginReply [login_get "/ityaar?$loginRobot" \
1150
[list "Cookie: $loginWellFormed"]]
1151
test login-group-ityaar-keeps \
1152
{[string match "HTTP/1.0 302*" [reply_status $loginReply]]
1153
&& ![string match "*%2Fanonymous*" $loginReply]}
1154
set loginReply [login_get "/ityaar?captchaseed=$loginSeed&captcha=zzzzzzzz" \
1155
[list "Cookie: $loginWellFormed"]]
1156
test login-group-ityaar-no-captcha \
1157
{![string match "*Prove that you are human*" $loginReply]}
1158
fossil login-group leave
1159
1160
###############################################################################
1161
# login-group command errors: joining this repository itself, a file that
1162
# does not exist or is not a database, a wrong number of arguments.
1163
foreach {loginLgName loginLgArgs loginLgWant} [list \
1164
self [list join $loginRepo] {*same as this one*} \
1165
missing [list join [file join [pwd] nosuch.fossil]] {*does not exist*} \
1166
junk [list join [file join [pwd] ljunk.fossil]] {*not a database*} \
1167
join-args [list join] \
1168
{*unexpected argument count*} \
1169
leave-args [list leave extra] \
1170
{*unknown extra arguments*} \
1171
] {
1172
fossil_raw login-group {*}$loginLgArgs
1173
test login-group-err-$loginLgName {$CODE != 0
1174
&& [string match $loginLgWant $RESULT]}
1175
}
1176
1177
# The /setup_login_group page joins a group only when the login and
1178
# password given are those of a Setup user of the other repository.
1179
proc login_setup_join {pw} {
1180
global loginRepo2
1181
return [login_post /setup_login_group \
1182
"repo=[file nativename $loginRepo2]&login=loginsue&pw=$pw&newname=webgrp&join=1" \
1183
[list "Authorization: Basic [binary encode base64 loginsue:suepw]"]]
1184
}
1185
test login-setup-join-badpw {[string match "*does not correspond to a user*" \
1186
[login_setup_join wrong]]}
1187
login_setup_join suepw
1188
fossil login-group
1189
test login-setup-join {[string match "*login-group \"webgrp\"*" $RESULT]}
1190
fossil login-group leave
1191
1192
###############################################################################
1193
# Pages that need a capability "nobody" lacks redirect to /login, naming
1194
# the page and its query string, and adding "anon=1" when "anonymous" has
1195
# the capability. With "redirect-to-https" set to 2 the redirect goes to
1196
# the https: address.
1197
test_eq login-needed [reply_header [login_get "/tktnew?x=1"] Location] \
1198
http://x/login?g=tktnew%3fx%3D1
1199
fossil sql "UPDATE user SET cap='hn' WHERE login='anonymous'"
1200
test_eq login-needed-anon [reply_header [login_get /tktnew] Location] \
1201
http://x/login?g=tktnew&anon=1
1202
fossil sql "UPDATE user SET cap='h' WHERE login='anonymous'"
1203
1204
# A page that shows hyperlinks only with the Hyperlink capability offers an
1205
# anonymous login when "anonymous" has that capability.
1206
fossil sql "UPDATE user SET cap='o' WHERE login='nobody'"
1207
test login-anonymous-available {[string match "*hyperlinks are disabled*" \
1208
[login_get /leaves [list $loginUA]]]}
1209
1210
# A page on the "robot-restrict" list asks a client without a login to
1211
# show that it is not a robot.
1212
login_config robot-restrict test-env
1213
set loginReply [login_get /test-env [list "User-Agent: Googlebot/2.1"]]
1214
test_eq login-robot-restrict [login_who $loginReply] "-"
1215
login_config robot-restrict ""
1216
fossil sql "UPDATE user SET cap='' WHERE login='nobody'"
1217
1218
# With "redirect-to-https" set to 1, the redirect to /login for such a page
1219
# goes to the https: address.
1220
login_config redirect-to-https 1
1221
test_eq login-needed-https [reply_header [login_get /tktnew] Location] \
1222
https://x/login?g=tktnew
1223
login_config redirect-to-https ""
1224
1225
# A login cookie in a repository that is in no login group does not keep
1226
# /ityaar from asking again after a wrong answer.
1227
set loginReply [login_get "/ityaar?captchaseed=$loginSeed&captcha=zzzzzzzz" \
1228
[list "Cookie: $loginCookieName=[string repeat a 50]/x"]]
1229
test login-ityaar-nogroup {[string match "*Prove that you are human*" \
1230
$loginReply]}
1231
1232
# A sync request whose login card names a user with a NULL capability
1233
# column logs in with no capabilities: its pull is refused. The same
1234
# request for that user with Read capability is answered.
1235
fossil user new loginnocap [email protected] nocappw
1236
proc login_xfer_pull {user} {
1237
set pc [string trim [fossil sql \
1238
"SELECT value FROM config WHERE name='project-code'"]]
1239
set sc [string trim [fossil sql \
1240
"SELECT value FROM config WHERE name='server-code'"]]
1241
set pw [string trim [fossil sql "SELECT pw FROM user WHERE login='$user'"]]
1242
set tail "pull $sc $pc\n"
1243
set nonce [sha1_text $tail]
1244
set sig [sha1_text "$nonce$pw"]
1245
set body "login $user $nonce $sig\n$tail"
1246
return [login_send \
1247
[join [list \
1248
"POST /xfer HTTP/1.1\r\nHost: x\r\n" \
1249
"Content-Type: application/x-fossil-uncompressed\r\n" \
1250
"Content-Length: [string length $body]\r\n\r\n$body"] ""]]
1251
}
1252
fossil sql "UPDATE user SET cap=NULL WHERE login='loginnocap'"
1253
test login-xfer-null-cap \
1254
{[string first {not\sauthorized\sto\sread} [login_xfer_pull loginnocap]] >=
1255
0}
1256
fossil sql "UPDATE user SET cap='o' WHERE login='loginnocap'"
1257
set loginReply [login_xfer_pull loginnocap]
1258
test login-xfer-read-cap {[string match "*igot *" $loginReply]
1259
&& [string first {not\sauthorized} $loginReply] < 0}
1260
1261
# test-resetpw-url for a user id that does not exist reports that the
1262
# suffix it made is not valid.
1263
fossil test-resetpw-url 99999
1264
test login-resetpw-url-nouser {[string match "*/resetpw/* 0 (???)*" $RESULT]}
1265
1266
###############################################################################
1267
# A repository file that cannot be opened: a login-group member is
1268
# reported on a password change, and joining it is an error. Skipped
1269
# where the file stays readable (as for root).
1270
1271
write_file lnoperm.fossil "x\n"
1272
file attributes lnoperm.fossil -permissions 0000
1273
if {![file readable lnoperm.fossil]} {
1274
login_config peer-repo-2222222222222222 [file join [pwd] lnoperm.fossil]
1275
set loginSess [login_session loginbob groupnew2]
1276
set loginReply [login_post /login \
1277
"p=groupnew2&n1=groupnew3&n2=groupnew3&csrf=[lindex $loginSess 1]" \
1278
[list "Cookie: [lindex $loginSess 0]"]]
1279
test login-group-noperm-member \
1280
{[string match "*lnoperm.fossil: *" $loginReply]}
1281
login_config peer-repo-2222222222222222 ""
1282
fossil_raw login-group join [file join [pwd] lnoperm.fossil]
1283
test login-group-noperm-join {$CODE != 0
1284
&& [string match "*unable to open*" $RESULT]}
1285
} else {
1286
test_skip_part \
1287
"login-group-noperm-*: need a file the user running the tests cannot read"
1288
}
1289
file attributes lnoperm.fossil -permissions 0644
1290
1291
###############################################################################
1292
# Pages that depend on the client. A request with a "Mobi" User-Agent is
1293
# shown a unified diff and one with a desktop User-Agent a side-by-side
1294
# diff. A tarball request referred from /login is answered with a
1295
# download button rather than the tarball.
1296
1297
write_file lfile.txt "one\n"
1298
fossil add lfile.txt
1299
fossil commit -m one --no-warnings
1300
write_file lfile.txt "two\n"
1301
fossil commit -m two --no-warnings
1302
proc login_local_get {path headers} {
1303
set text "GET $path HTTP/1.1\r\nHost: x\r\n"
1304
foreach line $headers { append text $line "\r\n" }
1305
write_file lreq.txt "$text\r\n"
1306
file delete lreply.txt
1307
fossil_raw http $::loginRepo --in lreq.txt --out lreply.txt \
1308
--ipaddr 127.0.0.1 --localauth
1309
return [read_file lreply.txt]
1310
}
1311
test login-mobile-diff {[string match {*class="diff udiff"*} \
1312
[login_local_get /info/tip {"User-Agent: Mozilla/5.0 Mobi Firefox/122.0"}]]}
1313
test login-desktop-diff {[string match {*class="diff splitdiff"*} \
1314
[login_local_get /info/tip [list $loginUA]]]}
1315
test login-tarball-from-login {[string match "*Tarball Download*" \
1316
[login_local_get /tarball/tip/x.tar.gz {"Referer: http://x/login"}]]}
1317
test login-tarball-other {![string match "*Tarball Download*" \
1318
[login_local_get /tarball/tip/x.tar.gz {"Referer: http://x/timeline"}]]}
1319
1320
###############################################################################
1321
# Further /login requests, each varying one part of a request above.
1322
1323
fossil user password loginbob bobfinal
1324
fossil sql "UPDATE user SET cap='op' WHERE login='loginbob'"
1325
set loginSess [login_session loginbob bobfinal]
1326
set loginPair [lindex $loginSess 0]
1327
set loginTok [lindex $loginSess 1]
1328
1329
# "fossil-goto" that is empty goes to the home page; one that is the base
1330
# URL itself is followed; one that only starts with the base URL is not.
1331
foreach {loginFgName loginFgTarget loginFgWant} [list \
1332
empty {} $loginHome \
1333
base http://x http://x \
1334
base-more http://xy $loginHome \
1335
other http://y/ $loginHome \
1336
] {
1337
test_eq login-goto-$loginFgName [reply_header [login_get \
1338
"/login?u=loginbob&p=bobfinal&fossil-goto=$loginFgTarget"] Location] \
1339
$loginFgWant
1340
}
1341
1342
# A GET with "out" does not log out.
1343
test login-logout-get {[string match \
1344
"*Currently logged in as <b>loginbob</b>*" \
1345
[login_get "/login?out=1" [list "Cookie: $loginPair"]]]}
1346
1347
# A password change without the second copy of the new password, without
1348
# the first, or without the Anti-CSRF token changes nothing.
1349
foreach {loginPcName loginPcBody} [list \
1350
no-n2 "p=bobfinal&n1=changed1&csrf=$loginTok" \
1351
no-n1 "p=bobfinal&n2=changed1&csrf=$loginTok" \
1352
no-csrf "p=bobfinal&n1=changed1&n2=changed1" \
1353
] {
1354
set loginReply [login_post /login $loginPcBody [list "Cookie: $loginPair"]]
1355
test login-pwchange-$loginPcName \
1356
{![string match "HTTP/1.0 302*" [reply_status $loginReply]]
1357
&& [login_who [login_basic loginbob:bobfinal]] eq "loginbob"}
1358
}
1359
1360
# A user name with no password, or an empty password, shows the form and
1361
# sets no cookie.
1362
foreach {loginNpName loginNpQuery} {no-p u=loginbob empty-p u=loginbob&p=} {
1363
set loginReply [login_get "/login?$loginNpQuery"]
1364
test login-form-$loginNpName {[reply_status $loginReply] eq "HTTP/1.0 200 OK"
1365
&& [reply_header $loginReply Set-Cookie] eq "-"}
1366
}
1367
1368
# "g" with "noanon" does not name the page asked for.
1369
test login-page-goto-noanon {![string match "*access page*" \
1370
[login_get "/login?g=timeline&noanon=1"]]}
1371
1372
# "pwreset" when self password reset is off shows the usual page.
1373
login_config self-pw-reset ""
1374
test login-page-pwreset-off {[string match {*name="u"*} \
1375
[login_get "/login?pwreset=1"]]}
1376
test login-register-pwreset-off {[string match {*name="captchaseed"*} \
1377
[login_get "/register?pwreset=1"]]
1378
&& ![string match {*name="reqpwreset"*} [login_get "/register?pwreset=1"]]}
1379
1380
# A request over https: has no warning about sending the password in the
1381
# clear, and its login form goes to the same address even with
1382
# "https-login" on.
1383
login_config https-login 1
1384
set loginReply [login_send "GET /login HTTP/1.1\r\nHost: x\r\n\r\n" --https]
1385
test login-page-over-https {![string match "*sent in the clear*" $loginReply]
1386
&& ![string match "*data-action='https:*" $loginReply]}
1387
login_config https-login ""
1388
1389
# "auto-captcha" has no effect on the captcha-only page of "anon=1".
1390
login_config auto-captcha 1
1391
test login-page-autocaptcha-anon \
1392
{![string match "*Fill out captcha*" [login_get "/login?anon=1"]]}
1393
login_config auto-captcha ""
1394
1395
# A user with capability 7 is not offered email alerts when email is off.
1396
fossil sql "UPDATE user SET cap='op7' WHERE login='loginbob'"
1397
fossil settings email-send-method off
1398
test login-page-alerts-off {![string match "*Email Alerts</a>*" \
1399
[login_get /login [list "Cookie: $loginPair"]]]}
1400
fossil settings email-send-method dir
1401
fossil sql "UPDATE user SET cap='op' WHERE login='loginbob'"
1402
1403
# An anonymous login is not offered a password change.
1404
set loginAnonPair [login_cookie_pair \
1405
[login_get "/login?$loginAnon" [list $loginUA]]]
1406
set loginReply [login_get /login [list "Cookie: $loginAnonPair" $loginUA]]
1407
test login-page-anonymous {[string match \
1408
"*Currently logged in as <b>anonymous</b>*" $loginReply]
1409
&& ![string match "*Change Password*" $loginReply]}
1410
1411
###############################################################################
1412
# Further credential checks.
1413
1414
# "resid" without "token" logs no one in; "resid" naming a user with no
1415
# password does not match any token.
1416
test_eq login-resid-no-token \
1417
[login_who [login_get "/test-env?resid=loginbob"]] nobody
1418
test_eq login-resid-no-pw \
1419
[login_who [login_get "/test-env?resid=nobody&token=x"]] nobody
1420
1421
# A user whose login name is empty is served with that user's capabilities
1422
# but without "L", the mark of a logged-in user.
1423
fossil sql "INSERT INTO user(login,pw,cap,info) VALUES('','emptypw','o','')"
1424
set loginReply [login_get "/test-env?resid=&token=emptypw"]
1425
test login-empty-name {[string match "*g.zLogin = <br>*" $loginReply]
1426
&& [string match "*o*" [login_caps $loginReply]]
1427
&& ![string match "*L*" [login_caps $loginReply]]}
1428
fossil sql "UPDATE user SET cap='op' WHERE login=''"
1429
test login-empty-name-page {![string match "*Change Password*" \
1430
[login_get "/login?resid=&token=emptypw"]]}
1431
fossil sql "DELETE FROM user WHERE login=''"
1432
1433
# --httptrace writes no login line for a request served with no "nobody"
1434
# user.
1435
fossil sql "UPDATE user SET login='nobody-saved' WHERE login='nobody'"
1436
write_file lreq.txt "GET /test-env HTTP/1.1\r\nHost: x\r\n\r\n"
1437
fossil_raw http $loginRepo --in lreq.txt --out lreply.txt --ipaddr 9.9.9.9 \
1438
--httptrace
1439
fossil sql "UPDATE user SET login='nobody' WHERE login='nobody-saved'"
1440
test login-httptrace-no-nobody {[string first {# login:} $RESULT] < 0}
1441
1442
# A request from 127.0.0.1 without --localauth, or over https: with it, is
1443
# not logged in. A request over the fossil ssh transport with --localauth
1444
# is, whatever the client's address.
1445
fossil_raw http $loginRepo --in lreq.txt --out lreply.txt --ipaddr 127.0.0.1
1446
test_eq login-loopback-no-localauth [login_who [read_file lreply.txt]] nobody
1447
set ::env(REMOTE_HOST) 127.0.0.1
1448
test_eq login-local-https [login_who [login_local $loginRepo --https]] nobody
1449
unset ::env(REMOTE_HOST)
1450
write_file lreq.txt \
1451
"GET /test-env HTTP/1.1\r\nHost: x\r\nX-Fossil-Transport: SSH\r\n\r\n"
1452
set ::env(SSH_CONNECTION) "9.9.9.9 5000 127.0.0.1 22"
1453
fossil_raw http $loginRepo --in lreq.txt --out lreply.txt --localauth
1454
unset ::env(SSH_CONNECTION)
1455
set loginReply [read_file lreply.txt]
1456
test login-local-ssh {[login_method $loginReply] eq "LOCAL"
1457
&& [string match "*REMOTE_ADDR = 9.9.9.9*" $loginReply]}
1458
1459
# Under CGI with no QUERY_STRING, the redirect to /login names only the page.
1460
proc login_cgi_path {path} {
1461
set vars [list REQUEST_METHOD GET SCRIPT_NAME "" PATH_INFO $path \
1462
REMOTE_ADDR 9.9.9.9]
1463
foreach {name value} $vars { set ::env($name) $value }
1464
fossil_raw cgi login.ctl
1465
foreach {name value} $vars { unset ::env($name) }
1466
return $::RESULT
1467
}
1468
test login-needed-cgi {[string match "*\nLocation: http*/login?g=tktnew\r\n*" \
1469
[login_cgi_path /tktnew]]}
1470
1471
# The offer of an anonymous login is not made when "anonymous" lacks the
1472
# Hyperlink capability or anonymous logins are off.
1473
fossil sql "UPDATE user SET cap='o' WHERE login='nobody'"
1474
fossil sql "UPDATE user SET cap='' WHERE login='anonymous'"
1475
test login-anonymous-available-nocap \
1476
{![string match "*hyperlinks are disabled*" \
1477
[login_get /leaves [list $loginUA]]]}
1478
fossil sql "UPDATE user SET cap='h' WHERE login='anonymous'"
1479
login_config anon-cookie-lifespan 0
1480
test login-anonymous-available-off {![string match "*hyperlinks are disabled*" \
1481
[login_get /leaves [list $loginUA]]]}
1482
login_config anon-cookie-lifespan ""
1483
fossil sql "UPDATE user SET cap='' WHERE login='nobody'"
1484
1485
###############################################################################
1486
# Further /register and /reqpwreset requests.
1487
1488
# The reserved names "nobody", "reader" and "developer", with anything
1489
# after them, are refused.
1490
foreach loginRsName {nobody1 reader1 developer1} {
1491
test login-register-reserved-$loginRsName {[string match "*reserved*" \
1492
[login_register "u=$loginRsName&dn=R&[email protected]&p=longpw9&cp=longpw9"]]}
1493
}
1494
# A GET with "new" shows the form and creates no user.
1495
set loginReply [login_get \
1496
"/register?$loginRegBase&u=getuser1&dn=G&[email protected]&p=longpw9&cp=longpw9"]
1497
test login-register-get {[string match {*name="captchaseed"*} $loginReply]
1498
&& [login_user_cap getuser1] eq ""}
1499
# An "auth-sub-email" setting that is empty allows any address.
1500
fossil sql "REPLACE INTO config(name,value,mtime) VALUES('auth-sub-email','',0)"
1501
test login-register-auth-empty {[string match "*Passwords do not match*" \
1502
[login_register \
1503
"u=gooduser&dn=Real&[email protected]&p=longpw9&cp=other99"]]}
1504
login_config auth-sub-email ""
1505
1506
login_config self-pw-reset 1
1507
# The address of the "nobody" user is refused even with the Password
1508
# capability.
1509
fossil sql "UPDATE user SET cap='p', info='N <[email protected]>' WHERE login='nobody'"
1510
test login-reqpw-nobody \
1511
{[string match "*not associated with a user*" [login_reqpw [email protected]]]}
1512
fossil sql "UPDATE user SET cap='', info='' WHERE login='nobody'"
1513
# A POST without "reqpwreset" shows the form.
1514
set loginReply [login_post /reqpwreset \
1515
"captchaseed=$loginSeed&captcha=$loginCap&[email protected]"]
1516
test login-reqpw-no-flag {[string match {*name="reqpwreset"*} $loginReply]
1517
&& ![string match "*has been sent*" $loginReply]}
1518
1519
###############################################################################
1520
# Further /resetpw requests: a POST with only one copy of the password, and
1521
# valid suffixes for a user with Admin capability and for "nobody" with the
1522
# Password capability.
1523
1524
set loginRName [login_reset_suffix loginbob]
1525
test login-resetpw-one-copy {[string match "*Change Password for user*" \
1526
[login_post /resetpw "name=$loginRName&n1=changed1"]]}
1527
fossil sql "UPDATE user SET cap='a' WHERE login='verify3'"
1528
test login-resetpw-refused-admin {[string match \
1529
"*Cannot change the password for user*" \
1530
[login_get /resetpw/[login_reset_suffix verify3]]]}
1531
fossil sql "UPDATE user SET cap='p' WHERE login='nobody'"
1532
test login-resetpw-refused-nobody-p {[string match \
1533
"*Cannot change the password for user*" \
1534
[login_get /resetpw/[login_reset_suffix nobody]]]}
1535
fossil sql "UPDATE user SET cap='' WHERE login='nobody'"
1536
1537
###############################################################################
1538
# Further login-group checks. A cookie naming a peer whose file is not a
1539
# repository, or naming a peer that has no such cookie, logs no one in.
1540
# In a group, /ityaar treats as well-formed only a cookie of more than 48
1541
# hex digits, a slash and at least one more character.
1542
1543
login_config peer-repo-3333333333333333 [file join [pwd] ljunk.fossil]
1544
login_config peer-repo-4444444444444444 $loginRepo2
1545
set loginFakeHash [string repeat A 50]
1546
foreach {loginPeName loginPeCode} \
1547
{junk 3333333333333333 nocookie 4444444444444444} {
1548
test_eq login-transfer-$loginPeName [login_who [login_get /test-env \
1549
[list "Cookie: $loginCookieName=$loginFakeHash/$loginPeCode/loginsue"]]] \
1550
nobody
1551
}
1552
login_config peer-repo-3333333333333333 ""
1553
login_config peer-repo-4444444444444444 ""
1554
1555
fossil login-group join --name loggrp3 $loginRepo2
1556
set loginGrpCookie fossil-[string range [string trim [fossil sql \
1557
"SELECT value FROM config WHERE name='login-group-code'"]] 0 15]
1558
foreach {loginWfName loginWfValue} [list \
1559
short "[string repeat a 48]/x" \
1560
no-slash "[string repeat a 50]x" \
1561
end-slash "[string repeat a 50]/" \
1562
] {
1563
set loginReply [login_get "/ityaar?captchaseed=$loginSeed&captcha=zzzzzzzz" \
1564
[list "Cookie: $loginGrpCookie=$loginWfValue"]]
1565
test login-group-wellformed-$loginWfName \
1566
{[string match "*Prove that you are human*" $loginReply]}
1567
}
1568
fossil login-group leave
1569
1570
# An empty login-group subcommand is unknown.
1571
fossil_raw login-group ""
1572
test login-group-err-empty \
1573
{$CODE != 0 && [string match "*unknown command*" $RESULT]}
1574
1575
###############################################################################
1576
# Further cases, each varying one input of a case above.
1577
1578
# With "access-log" off, a login is not recorded.
1579
login_config access-log 0
1580
set loginLogCount [string trim [fossil sql "SELECT count(*) FROM accesslog"]]
1581
login_form loginbob bobfinal
1582
test_eq login-accesslog-off \
1583
[string trim [fossil sql "SELECT count(*) FROM accesslog"]] $loginLogCount
1584
login_config access-log ""
1585
1586
# A negative "anon-cookie-lifespan" turns anonymous logins off.
1587
if {$loginWaits} {
1588
login_config anon-cookie-lifespan -5
1589
set loginReply [login_get "/login?$loginAnon"]
1590
test login-anon-negative-lifespan \
1591
{[string match "HTTP/1.0 401*" [reply_status $loginReply]]
1592
&& [reply_header $loginReply Set-Cookie] eq "-"}
1593
login_config anon-cookie-lifespan ""
1594
}
1595
1596
# Login cookies that name the special users "developer" and "reader" are
1597
# not accepted.
1598
foreach loginSpUser {developer reader} {
1599
test_eq login-cookie-special-$loginSpUser [login_who [login_get /test-env \
1600
[list \
1601
"Cookie: $loginCookieName=[string repeat A 50]/0123456789abcdef/$loginSpUser"]]] \
1602
nobody
1603
}
1604
1605
# The forum capabilities 5 and 4 grant those below them; "u" or "v" given
1606
# twice inherits once.
1607
fossil user password loginbob newbobpw
1608
fossil sql "UPDATE user SET cap='' WHERE login='anonymous'"
1609
test_eq login-cap-forum5 [login_capexpand 5] 2345L
1610
test_eq login-cap-forum4 [login_capexpand 4] 234L
1611
test_eq login-cap-reader-twice [login_capexpand uu] jL
1612
test_eq login-cap-developer-twice [login_capexpand vv] bL
1613
fossil sql "UPDATE user SET cap='h' WHERE login='anonymous'"
1614
1615
# Self password reset is not offered by a repository without the email
1616
# alert tables.
1617
fossil sql -R $loginRepo2 "REPLACE INTO config(name,value,mtime)
1618
VALUES('self-pw-reset','1',0)"
1619
test login-reqpw-no-tables {[string match "*does not allow users to reset*" \
1620
[login_get_at $loginRepo2 /reqpwreset]]}
1621
1622
# "login-group leave" outside a group shows that there is no group.
1623
fossil login-group leave
1624
test login-group-leave-none {[string match "*Not currently*" $RESULT]}
1625
1626
# With no User-Agent, a diff is shown side by side; a tarball request with
1627
# no Referer is answered with the tarball.
1628
test login-diff-no-agent {[string match {*class="diff splitdiff"*} \
1629
[login_local_get /info/tip {}]]}
1630
test login-tarball-no-referer {![string match "*Tarball Download*" \
1631
[login_local_get /tarball/tip/x.tar.gz {}]]}
1632
1633
###############################################################################
1634
test_cleanup
1635

Keyboard Shortcuts

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