askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_signup.py · 26.3 KB · 513 lines
| 1 | """Self-service signup, email verification and password reset (object_signup + the browser routes).""" |
| 2 | |
| 3 | import os |
| 4 | import re |
| 5 | import stat |
| 6 | import urllib.parse |
| 7 | from datetime import datetime, timedelta, timezone |
| 8 | from pathlib import Path |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | import object_credentials |
| 13 | import object_identity |
| 14 | import object_packages |
| 15 | import object_permission_store |
| 16 | import object_permissions |
| 17 | import object_records |
| 18 | import object_server |
| 19 | import object_signup |
| 20 | |
| 21 | from test_object_server import raw_request |
| 22 | |
| 23 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 24 | NOW = datetime(2026, 9, 28, 12, 0, tzinfo=timezone.utc) |
| 25 | PASSWORD = "correct horse battery" |
| 26 | |
| 27 | |
| 28 | # ---- tokens --------------------------------------------------------------- |
| 29 | |
| 30 | |
| 31 | def test_token_is_single_use_and_stored_only_as_a_hash(tmp_path): |
| 32 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, "u1", "a@example.org", base_dir=tmp_path, now=NOW) |
| 33 | |
| 34 | stored = object_signup.tokens_path(tmp_path).read_text() |
| 35 | assert token not in stored |
| 36 | assert object_identity.hash_token(token) in stored |
| 37 | assert stat.S_IMODE(os.stat(object_signup.tokens_path(tmp_path)).st_mode) == 0o600 |
| 38 | |
| 39 | assert object_signup.peek_token(token, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) is not None |
| 40 | row = object_signup.consume_token(token, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) |
| 41 | assert row["user_id"] == "u1" |
| 42 | assert object_signup.consume_token(token, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) is None |
| 43 | # the used row is gone, not kept around |
| 44 | assert object_identity.hash_token(token) not in object_signup.tokens_path(tmp_path).read_text() |
| 45 | |
| 46 | |
| 47 | def test_token_expires_and_only_works_for_its_purpose(tmp_path): |
| 48 | token = object_signup.issue_token(object_signup.PASSWORD_RESET, "u1", "a@example.org", base_dir=tmp_path, now=NOW) |
| 49 | |
| 50 | assert object_signup.consume_token(token, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) is None |
| 51 | later = NOW + object_signup.TOKEN_TTL[object_signup.PASSWORD_RESET] + timedelta(seconds=1) |
| 52 | assert object_signup.consume_token(token, object_signup.PASSWORD_RESET, base_dir=tmp_path, now=later) is None |
| 53 | assert object_signup.consume_token("", object_signup.PASSWORD_RESET, base_dir=tmp_path, now=NOW) is None |
| 54 | assert object_signup.consume_token("nope", object_signup.PASSWORD_RESET, base_dir=tmp_path, now=NOW) is None |
| 55 | |
| 56 | |
| 57 | def test_a_new_link_retires_the_previous_one_and_the_file_stays_small(tmp_path): |
| 58 | first = object_signup.issue_token(object_signup.VERIFY_EMAIL, "u1", "a@example.org", base_dir=tmp_path, now=NOW) |
| 59 | second = object_signup.issue_token(object_signup.VERIFY_EMAIL, "u1", "a@example.org", base_dir=tmp_path, now=NOW) |
| 60 | object_signup.issue_token(object_signup.PASSWORD_RESET, "u1", "a@example.org", base_dir=tmp_path, now=NOW) |
| 61 | |
| 62 | assert object_signup.peek_token(first, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) is None |
| 63 | assert object_signup.peek_token(second, object_signup.VERIFY_EMAIL, base_dir=tmp_path, now=NOW) is not None |
| 64 | |
| 65 | # a write long after everything expired keeps only the new row |
| 66 | much_later = NOW + timedelta(days=30) |
| 67 | object_signup.issue_token(object_signup.VERIFY_EMAIL, "u2", "b@example.org", base_dir=tmp_path, now=much_later) |
| 68 | rows = object_signup._read_tokens(object_signup.tokens_path(tmp_path)) |
| 69 | assert [r["user_id"] for r in rows] == ["u2"] |
| 70 | |
| 71 | |
| 72 | # ---- accounts --------------------------------------------------------------- |
| 73 | |
| 74 | |
| 75 | def test_register_creates_a_pending_user_in_its_own_account(tmp_path): |
| 76 | result = object_signup.register("Ann@Example.org", base_dir=tmp_path, now=NOW) |
| 77 | |
| 78 | assert result["outcome"] == "created" |
| 79 | user = result["user"] |
| 80 | assert user["status"] == object_identity.PENDING_STATUS |
| 81 | assert user["roles"] == [] |
| 82 | assert user["display_name"] == "" |
| 83 | assert object_identity.get_account(user["account_id"], base_dir=tmp_path)["status"] == "active" |
| 84 | # no password exists until the person who holds the mailbox chooses one |
| 85 | assert not object_credentials.has_password(user["user_id"], base_dir=tmp_path) |
| 86 | # pending means no session, through the same gate every login uses |
| 87 | with pytest.raises(ValueError): |
| 88 | object_identity.create_session({"user_id": user["user_id"]}, base_dir=tmp_path, require_known_user=True) |
| 89 | |
| 90 | |
| 91 | def test_register_again_does_not_create_a_second_user(tmp_path): |
| 92 | first = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW) |
| 93 | again = object_signup.register("ANN@example.org", base_dir=tmp_path, now=NOW) |
| 94 | assert again["outcome"] == "pending" |
| 95 | assert again["user"]["user_id"] == first["user"]["user_id"] |
| 96 | |
| 97 | object_identity.set_user_status(first["user"]["user_id"], "active", base_dir=tmp_path) |
| 98 | exists = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW) |
| 99 | assert exists["outcome"] == "exists" |
| 100 | assert len(object_identity.list_users(base_dir=tmp_path)) == 1 |
| 101 | assert not object_credentials.has_password(first["user"]["user_id"], base_dir=tmp_path) |
| 102 | |
| 103 | |
| 104 | @pytest.mark.parametrize("email", ["not-an-email", "a@b", "a b@example.org"]) |
| 105 | def test_register_refuses_bad_input_before_creating_anything(tmp_path, email): |
| 106 | with pytest.raises(object_signup.SignupError): |
| 107 | object_signup.register(email, base_dir=tmp_path) |
| 108 | assert object_identity.list_users(base_dir=tmp_path) == [] |
| 109 | assert object_identity.list_accounts(base_dir=tmp_path) == [] |
| 110 | |
| 111 | |
| 112 | def test_confirming_sets_the_chosen_password_and_activates_once(tmp_path): |
| 113 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 114 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, user["user_id"], user["email"], base_dir=tmp_path) |
| 115 | |
| 116 | with pytest.raises(object_signup.SignupError): |
| 117 | object_signup.confirm_signup(token, "short", base_dir=tmp_path) |
| 118 | # a password that won't do leaves the link usable, and the user pending with no password |
| 119 | assert object_signup.peek_token(token, object_signup.VERIFY_EMAIL, base_dir=tmp_path) is not None |
| 120 | assert not object_credentials.has_password(user["user_id"], base_dir=tmp_path) |
| 121 | |
| 122 | confirmed = object_signup.confirm_signup(token, PASSWORD, base_dir=tmp_path) |
| 123 | assert confirmed["status"] == "active" |
| 124 | assert object_credentials.verify_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 125 | assert object_signup.confirm_signup(token, "a second password", base_dir=tmp_path) is None |
| 126 | assert object_credentials.verify_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 127 | |
| 128 | |
| 129 | def test_a_stranger_who_signs_up_with_my_address_gets_no_way_in(tmp_path): |
| 130 | """The pre-hijack: signing up first with someone's address must not choose their password.""" |
| 131 | stranger = object_signup.register("victim@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 132 | # nothing the stranger typed can be used to sign in as that address |
| 133 | assert not object_credentials.has_password(stranger["user_id"], base_dir=tmp_path) |
| 134 | with pytest.raises(ValueError): |
| 135 | object_identity.create_session({"user_id": stranger["user_id"]}, base_dir=tmp_path, require_known_user=True) |
| 136 | |
| 137 | # the real owner signs up too, gets the link, and chooses the password |
| 138 | owner = object_signup.register("Victim@Example.org", base_dir=tmp_path, now=NOW) |
| 139 | assert owner["outcome"] == "pending" and owner["user"]["user_id"] == stranger["user_id"] |
| 140 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, stranger["user_id"], "victim@example.org", base_dir=tmp_path) |
| 141 | confirmed = object_signup.confirm_signup(token, "the owner's own password", " Victim Owner ", base_dir=tmp_path) |
| 142 | assert object_credentials.verify_password(stranger["user_id"], "the owner's own password", base_dir=tmp_path) |
| 143 | # the name is the mailbox holder's too, not whatever the first signup typed |
| 144 | assert confirmed["display_name"] == "Victim Owner" |
| 145 | |
| 146 | |
| 147 | def test_a_pending_user_made_before_the_fix_keeps_no_stranger_password(tmp_path): |
| 148 | """Old data: a pending user whose password a stranger chose at signup.""" |
| 149 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 150 | object_credentials.set_password(user["user_id"], "the stranger's password", base_dir=tmp_path) |
| 151 | # pending users cannot sign in, password or no password |
| 152 | with pytest.raises(ValueError): |
| 153 | object_identity.create_session({"user_id": user["user_id"]}, base_dir=tmp_path, require_known_user=True) |
| 154 | |
| 155 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, user["user_id"], user["email"], base_dir=tmp_path) |
| 156 | object_signup.confirm_signup(token, PASSWORD, base_dir=tmp_path) |
| 157 | assert object_credentials.verify_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 158 | assert not object_credentials.verify_password(user["user_id"], "the stranger's password", base_dir=tmp_path) |
| 159 | |
| 160 | |
| 161 | def test_a_confirm_link_never_changes_an_account_already_in_use(tmp_path): |
| 162 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 163 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, user["user_id"], user["email"], base_dir=tmp_path) |
| 164 | object_credentials.set_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 165 | object_identity.set_user_status(user["user_id"], "active", base_dir=tmp_path) |
| 166 | |
| 167 | assert object_signup.confirm_signup(token, "an attacker's password", base_dir=tmp_path) is None |
| 168 | assert object_credentials.verify_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 169 | |
| 170 | |
| 171 | def test_verify_never_reactivates_a_disabled_user(tmp_path): |
| 172 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 173 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, user["user_id"], user["email"], base_dir=tmp_path) |
| 174 | object_identity.set_user_status(user["user_id"], "disabled", base_dir=tmp_path) |
| 175 | |
| 176 | assert object_signup.confirm_signup(token, PASSWORD, base_dir=tmp_path) is None |
| 177 | assert object_identity.get_user(user["user_id"], base_dir=tmp_path)["status"] == "disabled" |
| 178 | |
| 179 | |
| 180 | def test_a_link_sent_to_an_old_address_proves_nothing(tmp_path): |
| 181 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 182 | token = object_signup.issue_token(object_signup.VERIFY_EMAIL, user["user_id"], "someone-else@example.org", base_dir=tmp_path) |
| 183 | |
| 184 | assert object_signup.confirm_signup(token, PASSWORD, base_dir=tmp_path) is None |
| 185 | assert object_identity.get_user(user["user_id"], base_dir=tmp_path)["status"] == "pending" |
| 186 | assert not object_credentials.has_password(user["user_id"], base_dir=tmp_path) |
| 187 | |
| 188 | |
| 189 | def test_reset_sets_the_password_and_signs_out_everywhere(tmp_path): |
| 190 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 191 | object_credentials.set_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 192 | object_identity.set_user_status(user["user_id"], "active", base_dir=tmp_path) |
| 193 | session = object_identity.create_session({"user_id": user["user_id"]}, base_dir=tmp_path, require_known_user=True) |
| 194 | token = object_signup.issue_token(object_signup.PASSWORD_RESET, user["user_id"], user["email"], base_dir=tmp_path) |
| 195 | |
| 196 | with pytest.raises(object_signup.SignupError): |
| 197 | object_signup.reset_password(token, "short", base_dir=tmp_path) |
| 198 | # a password that won't do leaves the link usable |
| 199 | assert object_signup.peek_token(token, object_signup.PASSWORD_RESET, base_dir=tmp_path) is not None |
| 200 | |
| 201 | assert object_signup.reset_password(token, "a brand new password", base_dir=tmp_path)["user_id"] == user["user_id"] |
| 202 | assert object_credentials.verify_password(user["user_id"], "a brand new password", base_dir=tmp_path) |
| 203 | assert not object_credentials.verify_password(user["user_id"], PASSWORD, base_dir=tmp_path) |
| 204 | assert object_identity.resolve_session_token(session["token"], base_dir=tmp_path) is None |
| 205 | assert object_signup.reset_password(token, "yet another password", base_dir=tmp_path) is None |
| 206 | |
| 207 | |
| 208 | def test_reset_confirms_a_pending_address(tmp_path): |
| 209 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 210 | token = object_signup.issue_token(object_signup.PASSWORD_RESET, user["user_id"], user["email"], base_dir=tmp_path) |
| 211 | |
| 212 | assert object_signup.reset_password(token, "a brand new password", base_dir=tmp_path)["status"] == "active" |
| 213 | |
| 214 | |
| 215 | def test_disabled_users_get_no_reset_link(tmp_path): |
| 216 | user = object_signup.register("ann@example.org", base_dir=tmp_path, now=NOW)["user"] |
| 217 | assert object_signup.reset_candidate("ANN@example.org", base_dir=tmp_path)["user_id"] == user["user_id"] |
| 218 | object_identity.set_user_status(user["user_id"], "disabled", base_dir=tmp_path) |
| 219 | assert object_signup.reset_candidate("ann@example.org", base_dir=tmp_path) is None |
| 220 | assert object_signup.reset_candidate("nobody@example.org", base_dir=tmp_path) is None |
| 221 | |
| 222 | |
| 223 | # ---- whether it may run --------------------------------------------------- |
| 224 | |
| 225 | |
| 226 | @pytest.mark.parametrize("value,expected", [ |
| 227 | ("https://example.org", "https://example.org"), |
| 228 | ("https://example.org/", "https://example.org"), |
| 229 | ("https://example.org/app", "https://example.org/app"), |
| 230 | ("http://localhost:8001", "http://localhost:8001"), |
| 231 | ("http://example.org", None), |
| 232 | ("example.org", None), |
| 233 | ("https://example.org/?next=x", None), |
| 234 | ("", None), |
| 235 | ]) |
| 236 | def test_public_url_must_be_https_except_on_this_machine(value, expected): |
| 237 | assert object_signup.public_url({object_signup.PUBLIC_URL_ENV: value}) == expected |
| 238 | |
| 239 | |
| 240 | def test_the_starter_policy_blocks_open_signup(): |
| 241 | starter = object_permissions.policy_from_dict(object_permission_store.starter_policy_payload()) |
| 242 | assert object_signup.unscoped_registered_rules(starter) |
| 243 | |
| 244 | scoped = object_permissions.PermissionPolicy(rules=( |
| 245 | object_permissions.PermissionRule.allow("registered", ["read", "create"], collection="notes", row_filter={"owner_id": "$user_id"}), |
| 246 | object_permissions.PermissionRule.allow("public", ["read"]), |
| 247 | )) |
| 248 | assert object_signup.unscoped_registered_rules(scoped) == [] |
| 249 | |
| 250 | |
| 251 | def test_blockers_name_everything_that_is_missing(tmp_path): |
| 252 | blockers = object_signup.signup_blockers( |
| 253 | policy=object_permissions.PermissionPolicy(), |
| 254 | enforcement_enabled=False, |
| 255 | password_login_enabled=False, |
| 256 | base_dir=tmp_path, |
| 257 | env={}, |
| 258 | ) |
| 259 | text = "\n".join(blockers) |
| 260 | for needed in ("DBBASIC_ENABLE_SIGNUP", "DBBASIC_ENABLE_PASSWORD_LOGIN", "DBBASIC_ENABLE_PERMISSION_ENFORCEMENT", |
| 261 | "DBBASIC_SMTP_MODE", "app-email", "DBBASIC_PUBLIC_URL"): |
| 262 | assert needed in text |
| 263 | |
| 264 | reset = "\n".join(object_signup.reset_blockers(password_login_enabled=True, base_dir=tmp_path, env={})) |
| 265 | assert "DBBASIC_ENABLE_PASSWORD_RESET" in reset and "DBBASIC_ENABLE_PERMISSION_ENFORCEMENT" not in reset |
| 266 | |
| 267 | |
| 268 | # ---- the browser flow ------------------------------------------------------- |
| 269 | |
| 270 | |
| 271 | @pytest.fixture |
| 272 | def open_signup(tmp_path, monkeypatch): |
| 273 | """A server where signup and reset may run: email in log mode, app-email installed.""" |
| 274 | data_dir = tmp_path / "data" |
| 275 | object_root = tmp_path / "objects" |
| 276 | object_root.mkdir() |
| 277 | object_packages.install_package("app-email", root=PACKAGES_ROOT, base_dir=data_dir, |
| 278 | object_roots=[object_root], allow_replace=True) |
| 279 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 280 | monkeypatch.setenv(object_server.PASSWORD_LOGIN_ENV, "true") |
| 281 | monkeypatch.setenv(object_signup.SIGNUP_ENV, "true") |
| 282 | monkeypatch.setenv(object_signup.PASSWORD_RESET_ENV, "true") |
| 283 | monkeypatch.setenv(object_signup.PUBLIC_URL_ENV, "https://desk.example.org") |
| 284 | monkeypatch.setenv("DBBASIC_SMTP_MODE", "log") |
| 285 | monkeypatch.setenv(object_server.COOKIE_SECURE_ENV, "false") |
| 286 | monkeypatch.setattr(object_server, "_permission_enforcement_enabled", lambda: True) |
| 287 | monkeypatch.setattr(object_server, "PASSWORD_LOGIN_FAILURE_DELAY_SECONDS", 0) |
| 288 | assert object_server._signup_blockers() == [] |
| 289 | return data_dir |
| 290 | |
| 291 | |
| 292 | def _post(path, form, headers=()): |
| 293 | return raw_request( |
| 294 | path, |
| 295 | method="POST", |
| 296 | body=urllib.parse.urlencode(form).encode(), |
| 297 | headers=[("content-type", "application/x-www-form-urlencoded"), *headers], |
| 298 | ) |
| 299 | |
| 300 | |
| 301 | def _mail(data_dir): |
| 302 | return object_records.read_collection_records("email_outbox", base_dir=data_dir) |
| 303 | |
| 304 | |
| 305 | def _token_from(mail_row, path): |
| 306 | match = re.search(r"https://desk\.example\.org" + re.escape(path) + r"\?token=([A-Za-z0-9_%-]+)", mail_row["text_body"]) |
| 307 | assert match, mail_row["text_body"] |
| 308 | return urllib.parse.unquote(match.group(1)) |
| 309 | |
| 310 | |
| 311 | def _cookie(headers): |
| 312 | value = headers.get(b"set-cookie", b"").decode() |
| 313 | return value.split(";", 1)[0] |
| 314 | |
| 315 | |
| 316 | def test_signup_is_closed_until_everything_is_in_place(tmp_path, monkeypatch): |
| 317 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 318 | monkeypatch.setenv(object_signup.SIGNUP_ENV, "true") |
| 319 | |
| 320 | status, _, body = raw_request("/signup") |
| 321 | assert status == 403 |
| 322 | assert b"can't be created" in body |
| 323 | status, _, _ = _post("/signup", {"email": "ann@example.org"}) |
| 324 | assert status == 403 |
| 325 | assert object_identity.list_users(base_dir=tmp_path) == [] |
| 326 | |
| 327 | monkeypatch.setenv(object_server.PASSWORD_LOGIN_ENV, "true") |
| 328 | _, _, login = raw_request("/login") |
| 329 | assert b"Create an account" not in login and b"Forgot your password" not in login |
| 330 | |
| 331 | |
| 332 | def test_signup_verify_and_sign_in(open_signup, monkeypatch): |
| 333 | data_dir = open_signup |
| 334 | _, _, login = raw_request("/login") |
| 335 | assert b'href="/signup"' in login and b'href="/reset"' in login |
| 336 | assert b"choose your password there" in login |
| 337 | |
| 338 | # the form asks for no password |
| 339 | _, _, form = raw_request("/signup") |
| 340 | assert b'name="password"' not in form |
| 341 | |
| 342 | # a password sent anyway (an old form, a script) is ignored |
| 343 | status, headers, _ = _post("/signup", {"email": "ann@example.org", "display_name": "Not Ann", "password": "sent by a stranger"}) |
| 344 | assert status == 303 and headers[b"location"] == b"/signup?sent=1" |
| 345 | (pending,) = object_identity.find_users_by_email("ann@example.org", base_dir=data_dir) |
| 346 | assert not object_credentials.has_password(pending["user_id"], base_dir=data_dir) |
| 347 | assert pending["display_name"] == "" |
| 348 | status, _, body = raw_request("/signup", query_string="sent=1") |
| 349 | assert b"Check your email" in body |
| 350 | |
| 351 | (mail,) = _mail(data_dir) |
| 352 | assert mail["to"] == "ann@example.org" |
| 353 | assert "choose your password" in mail["text_body"] |
| 354 | token = _token_from(mail, "/verify") |
| 355 | (user,) = object_identity.find_users_by_email("ann@example.org", base_dir=data_dir) |
| 356 | assert user["status"] == "pending" |
| 357 | |
| 358 | # a password typed into the signup form, or at sign-in, gets a stranger nothing |
| 359 | for guess in (PASSWORD, "sent by a stranger"): |
| 360 | status, headers, _ = _post("/login", {"email": "ann@example.org", "password": guess}) |
| 361 | assert headers[b"location"] == b"/login?error=1" |
| 362 | assert b"set-cookie" not in headers |
| 363 | assert len(_mail(data_dir)) == 1 |
| 364 | |
| 365 | # opening the link only shows the form, and does not use the link up |
| 366 | status, _, body = raw_request("/verify", query_string=f"token={token}") |
| 367 | assert status == 200 and b"Confirm my email" in body and b'name="password"' in body |
| 368 | assert object_signup.peek_token(token, object_signup.VERIFY_EMAIL, base_dir=data_dir) is not None |
| 369 | |
| 370 | # a password that won't do keeps the link alive |
| 371 | status, _, body = _post("/verify", {"token": token, "password": "short", "display_name": "Ann <b>"}) |
| 372 | assert status == 400 and b"at least 8 characters" in body and b"Ann <b>" in body |
| 373 | assert object_signup.peek_token(token, object_signup.VERIFY_EMAIL, base_dir=data_dir) is not None |
| 374 | |
| 375 | monkeypatch.setenv(object_server.LANDING_PATH_ENV, "/sell/?confirmed=1") |
| 376 | status, headers, _ = _post("/verify", {"token": token, "password": PASSWORD, "display_name": "Ann"}) |
| 377 | assert status == 303 and headers[b"location"] == b"/sell/?confirmed=1" |
| 378 | assert _cookie(headers).startswith("dbbasic_session=") and _cookie(headers) != "dbbasic_session=" |
| 379 | (user,) = object_identity.find_users_by_email("ann@example.org", base_dir=data_dir) |
| 380 | assert user["status"] == "active" and user["display_name"] == "Ann" |
| 381 | |
| 382 | status, _, body = raw_request("/verify", query_string=f"token={token}") |
| 383 | assert status == 400 and b"expired or was already used" in body and b'href="/signup"' in body |
| 384 | status, _, body = _post("/verify", {"token": token, "password": "another password"}) |
| 385 | assert status == 400 and b"expired or was already used" in body |
| 386 | |
| 387 | status, headers, _ = _post("/login", {"email": "ann@example.org", "password": PASSWORD}) |
| 388 | assert headers[b"location"] == b"/sell/?confirmed=1" and _cookie(headers).startswith("dbbasic_session=") |
| 389 | |
| 390 | |
| 391 | def test_a_landing_path_that_is_not_local_is_reported_not_silently_ignored(open_signup, monkeypatch): |
| 392 | assert object_server._signup_blockers() == [] |
| 393 | monkeypatch.setenv(object_server.LANDING_PATH_ENV, "https://evil.example/") |
| 394 | (blocker,) = object_server._signup_blockers() |
| 395 | assert "DBBASIC_AUTH_LANDING_PATH" in blocker |
| 396 | monkeypatch.setenv(object_server.LANDING_PATH_ENV, "/sell/?confirmed=1") |
| 397 | assert object_server._signup_blockers() == [] |
| 398 | |
| 399 | |
| 400 | def test_the_landing_path_is_local_only(monkeypatch): |
| 401 | monkeypatch.delenv(object_server.LANDING_PATH_ENV, raising=False) |
| 402 | assert object_server._landing_path() == "/" |
| 403 | for bad in ("https://evil.example/", "//evil.example", "evil", "/a\\b"): |
| 404 | monkeypatch.setenv(object_server.LANDING_PATH_ENV, bad) |
| 405 | assert object_server._landing_path() == "/" |
| 406 | monkeypatch.setenv(object_server.LANDING_PATH_ENV, "/sell/") |
| 407 | assert object_server._landing_path() == "/sell/" |
| 408 | |
| 409 | |
| 410 | def test_auth_pages_send_an_origin_with_their_forms(open_signup): |
| 411 | """no-referrer made browsers send "Origin: null" with a form post, which the same-origin check refuses.""" |
| 412 | for path in ("/login", "/signup", "/reset"): |
| 413 | _, _, body = raw_request(path) |
| 414 | assert b'<meta name="referrer" content="strict-origin">' in body |
| 415 | assert b"no-referrer" not in body |
| 416 | |
| 417 | |
| 418 | def test_signup_for_a_taken_address_looks_the_same_but_tells_the_mailbox(open_signup): |
| 419 | data_dir = open_signup |
| 420 | user = object_signup.register("ann@example.org", base_dir=data_dir)["user"] |
| 421 | object_credentials.set_password(user["user_id"], PASSWORD, base_dir=data_dir) |
| 422 | object_identity.set_user_status(user["user_id"], "active", base_dir=data_dir) |
| 423 | |
| 424 | status, headers, _ = _post("/signup", {"email": "Ann@Example.org"}) |
| 425 | assert status == 303 and headers[b"location"] == b"/signup?sent=1" |
| 426 | (mail,) = _mail(data_dir) |
| 427 | assert mail["subject"] == "You already have an account" |
| 428 | assert "https://desk.example.org/reset" in mail["text_body"] |
| 429 | assert len(object_identity.list_users(base_dir=data_dir)) == 1 |
| 430 | |
| 431 | |
| 432 | def test_signup_form_errors_keep_what_was_typed(open_signup): |
| 433 | status, _, body = _post("/signup", {"email": "not an email"}) |
| 434 | assert status == 400 |
| 435 | assert b"valid email" in body |
| 436 | assert b'value="not an email"' in body |
| 437 | |
| 438 | |
| 439 | def test_signup_is_rate_limited_per_address_and_per_ip(open_signup, monkeypatch): |
| 440 | monkeypatch.setattr(object_server, "SIGNUP_ATTEMPTS_PER_HOUR_PER_IP", 2) |
| 441 | _post("/signup", {"email": "a1@example.org"}) |
| 442 | _post("/signup", {"email": "a2@example.org"}) |
| 443 | status, _, body = _post("/signup", {"email": "a3@example.org"}) |
| 444 | assert status == 429 and b"Too many" in body |
| 445 | assert len(object_identity.list_users(base_dir=open_signup)) == 2 |
| 446 | |
| 447 | |
| 448 | def test_a_cross_site_signup_form_is_refused(open_signup): |
| 449 | status, _, _ = _post("/signup", {"email": "ann@example.org"}, |
| 450 | headers=[("host", "desk.example.org"), ("origin", "https://evil.example")]) |
| 451 | assert status == 400 |
| 452 | assert object_identity.list_users(base_dir=open_signup) == [] |
| 453 | |
| 454 | |
| 455 | def test_password_reset_end_to_end(open_signup): |
| 456 | data_dir = open_signup |
| 457 | user = object_signup.register("ann@example.org", base_dir=data_dir)["user"] |
| 458 | object_credentials.set_password(user["user_id"], PASSWORD, base_dir=data_dir) |
| 459 | object_identity.set_user_status(user["user_id"], "active", base_dir=data_dir) |
| 460 | session = object_identity.create_session({"user_id": user["user_id"]}, base_dir=data_dir, require_known_user=True) |
| 461 | |
| 462 | # an unknown address gets the same answer and no email |
| 463 | status, headers, _ = _post("/reset", {"email": "nobody@example.org"}) |
| 464 | assert headers[b"location"] == b"/reset?sent=1" |
| 465 | assert _mail(data_dir) == [] |
| 466 | |
| 467 | status, headers, _ = _post("/reset", {"email": "ANN@example.org"}) |
| 468 | assert headers[b"location"] == b"/reset?sent=1" |
| 469 | (mail,) = _mail(data_dir) |
| 470 | token = _token_from(mail, "/reset/confirm") |
| 471 | |
| 472 | status, _, body = raw_request("/reset/confirm", query_string=f"token={token}") |
| 473 | assert status == 200 and b"Choose a new password" in body |
| 474 | |
| 475 | status, _, body = _post("/reset/confirm", {"token": token, "password": "short"}) |
| 476 | assert status == 400 and b"at least 8 characters" in body |
| 477 | |
| 478 | status, headers, _ = _post("/reset/confirm", {"token": token, "password": "a brand new password"}) |
| 479 | assert status == 303 and headers[b"location"] == b"/login?reset=1" |
| 480 | assert object_identity.resolve_session_token(session["token"], base_dir=data_dir) is None |
| 481 | |
| 482 | _, _, body = raw_request("/login", query_string="reset=1") |
| 483 | assert b"Your password was changed" in body |
| 484 | status, headers, _ = _post("/login", {"email": "ann@example.org", "password": "a brand new password"}) |
| 485 | assert headers[b"location"] == b"/" |
| 486 | |
| 487 | status, _, body = _post("/reset/confirm", {"token": token, "password": "another new password"}) |
| 488 | assert status == 400 and b"expired or was already used" in body |
| 489 | |
| 490 | |
| 491 | def test_a_reset_clears_the_login_lockout(open_signup, monkeypatch): |
| 492 | data_dir = open_signup |
| 493 | monkeypatch.setenv(object_server.LOGIN_LOCKOUT_ATTEMPTS_ENV, "2") |
| 494 | user = object_signup.register("ann@example.org", base_dir=data_dir)["user"] |
| 495 | object_credentials.set_password(user["user_id"], PASSWORD, base_dir=data_dir) |
| 496 | object_identity.set_user_status(user["user_id"], "active", base_dir=data_dir) |
| 497 | for _ in range(2): |
| 498 | _post("/login", {"email": "ann@example.org", "password": "wrong password"}) |
| 499 | _, headers, _ = _post("/login", {"email": "ann@example.org", "password": PASSWORD}) |
| 500 | assert headers[b"location"] == b"/login?error=locked" |
| 501 | |
| 502 | token = object_signup.issue_token(object_signup.PASSWORD_RESET, user["user_id"], user["email"], base_dir=data_dir) |
| 503 | _post("/reset/confirm", {"token": token, "password": "a brand new password"}) |
| 504 | _, headers, _ = _post("/login", {"email": "ann@example.org", "password": "a brand new password"}) |
| 505 | assert headers[b"location"] == b"/" |
| 506 | |
| 507 | |
| 508 | def test_admin_capabilities_show_why_signup_is_off(tmp_path, monkeypatch): |
| 509 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 510 | identity = object_server._admin_capabilities_payload()["identity"] |
| 511 | assert identity["signup_enabled"] is False |
| 512 | assert any("DBBASIC_ENABLE_SIGNUP" in b for b in identity["signup_blockers"]) |
| 513 | assert identity["public_url_env"] == "DBBASIC_PUBLIC_URL" |