Hash the web password and revoke sessions server-side

Store a scrypt hash (migrating a plaintext password without changing it),
sign sessions with a generation that ./password and --revoke-all rotate,
make logout a same-origin POST that revokes the token, add sign out everywhere,
and close proxied websockets whose session ends. Enforce a strength rule for
new passwords, back off logins per client and server-wide, check Origin on
login, tighten next, add security headers and a __Host- cookie over https.
This commit is contained in:
2026-11-10 01:21:28 +00:00
parent f11ff8650d
commit e43f9fa50c
17 changed files with 1353 additions and 125 deletions
+80 -12
View File
@@ -12,6 +12,9 @@ password = importlib.util.module_from_spec(spec)
spec.loader.exec_module(password)
NEW = "tulip orbit canyon ember"
class PasswordTests(unittest.TestCase):
def setUp(self):
self.temporary = tempfile.TemporaryDirectory(prefix="pipelines-password-test-")
@@ -36,12 +39,61 @@ class PasswordTests(unittest.TestCase):
self.assertEqual(password.auth_path(home=self.home, prefix=prefix,
environ={"PIPELINES_WEB_AUTH_FILE": str(self.auth)}), self.auth)
def test_change_preserves_cookie_secret_metadata_and_private_permissions(self):
password.change_password(self.auth, "new password")
def test_change_stores_a_hash_rotates_sessions_and_keeps_metadata(self):
password.change_password(self.auth, NEW)
result = json.loads(self.auth.read_text())
self.assertEqual(result, dict(self.original, password="new password"))
self.assertNotIn("password", result)
self.assertNotIn(NEW, self.auth.read_text())
self.assertTrue(result["password_hash"].startswith("scrypt$32768$8$1$"))
self.assertTrue(password.verify_password(result["password_hash"], NEW))
self.assertFalse(password.verify_password(result["password_hash"], "old password"))
self.assertIs(result["password_weak"], False)
self.assertNotEqual(result["secret"], self.original["secret"])
self.assertGreaterEqual(len(result["secret"]), 32)
self.assertEqual(result["generation"], 1)
self.assertEqual(result["other"], "preserve")
self.assertEqual(self.auth.stat().st_mode & 0o777, 0o600)
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
password.change_password(self.auth, "k7#Rv9!qLm2@Xz4s")
self.assertEqual(json.loads(self.auth.read_text())["generation"], 2)
def test_revoke_all_rotates_secret_and_generation_but_keeps_password(self):
password.change_password(self.auth, NEW)
before = json.loads(self.auth.read_text())
password.revoke_all(self.auth)
after = json.loads(self.auth.read_text())
self.assertEqual(after["password_hash"], before["password_hash"])
self.assertEqual(after["generation"], before["generation"] + 1)
self.assertNotEqual(after["secret"], before["secret"])
def test_revoke_all_cli_needs_no_terminal_and_prints_no_secret(self):
output = io.StringIO()
with patch.object(password.sys, "argv", ["password", "--revoke-all"]), \
patch.object(password, "auth_path", return_value=self.auth), \
patch.object(password.sys.stdin, "isatty", return_value=False), \
patch.object(password.sys, "stdout", output):
password.main()
result = json.loads(self.auth.read_text())
self.assertEqual(result["generation"], 1)
self.assertEqual(result["password"], "old password") # unchanged, still the proxy's to migrate
self.assertIn("signed out", output.getvalue())
for value in ("old password", result["secret"], self.original["secret"]):
self.assertNotIn(value, output.getvalue())
def test_strength_rule(self):
for strong in (NEW, "correct horse battery staple", "k7#Rv9!qLm2@Xz4s", "cat dog sun red"):
self.assertIsNone(password.password_problem(strong), strong)
for weak in ("short", "old password", "J8m!x2Lq0#vR9tZ", "password password password",
"a" * 20, "Password12345678!", "qwertyuiop1234567890", "password welcome admin letmein",
"dog dog dog dog dog"):
self.assertIsNotNone(password.password_problem(weak), weak)
def test_hash_matches_rfc7914_scrypt_and_rejects_garbage(self):
stored = password.hash_password("pleaseletmein", n=16384)
self.assertTrue(password.verify_password(stored, "pleaseletmein"))
self.assertFalse(password.verify_password(stored, "pleaseletmeim"))
self.assertFalse(password.verify_password("scrypt$x", "pleaseletmein"))
self.assertFalse(password.verify_password("plain$1$1$1$a$b", "pleaseletmein"))
def test_short_password_does_not_modify_auth(self):
with self.assertRaises(ValueError):
@@ -52,25 +104,25 @@ class PasswordTests(unittest.TestCase):
link = self.home / "link.json"
link.symlink_to(self.auth)
with self.assertRaises(ValueError):
password.change_password(link, "new password")
password.change_password(link, NEW)
self.assertEqual(json.loads(self.auth.read_text()), self.original)
self.assertTrue(link.is_symlink())
def test_public_or_malformed_auth_is_not_replaced(self):
self.auth.chmod(0o644)
with self.assertRaisesRegex(ValueError, "private"):
password.change_password(self.auth, "new password")
password.change_password(self.auth, NEW)
self.auth.chmod(0o600)
self.auth.write_text("{broken")
with self.assertRaises(ValueError):
password.change_password(self.auth, "new password")
password.change_password(self.auth, NEW)
self.assertEqual(self.auth.read_text(), "{broken")
def test_replacement_failure_preserves_original_and_cleans_staging(self):
before = self.auth.read_bytes()
with patch.object(password.os, "replace", side_effect=OSError("fixture failure")):
with self.assertRaises(OSError):
password.change_password(self.auth, "new password")
password.change_password(self.auth, NEW)
self.assertEqual(self.auth.read_bytes(), before)
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
@@ -82,7 +134,7 @@ class PasswordTests(unittest.TestCase):
self.assertEqual(Path(source).parent, self.auth.parent)
return replace(source, target)
with patch.object(password.os, "replace", side_effect=checked_replace):
password.change_password(self.auth, "new password")
password.change_password(self.auth, NEW)
self.assertEqual(len(calls), 1)
def test_file_changed_during_update_is_not_overwritten(self):
@@ -92,10 +144,24 @@ class PasswordTests(unittest.TestCase):
return replace(descriptor)
with patch.object(password.os, "fsync", side_effect=external_update):
with self.assertRaisesRegex(ValueError, "changed during"):
password.change_password(self.auth, "new password")
password.change_password(self.auth, NEW)
self.assertEqual(json.loads(self.auth.read_text())["password"], "changed elsewhere")
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
def test_cli_warns_once_about_a_weak_current_password(self):
self.auth.write_text(json.dumps(dict(self.original, password_weak=True)))
errors = io.StringIO()
with patch.object(password.sys, "argv", ["password"]), \
patch.object(password, "auth_path", return_value=self.auth), \
patch.object(password.sys.stdin, "isatty", return_value=True), \
patch.object(password.getpass, "getpass", side_effect=[NEW, NEW]), \
patch.object(password.sys, "stdout", io.StringIO()), \
patch.object(password.sys, "stderr", errors):
password.main()
self.assertEqual(errors.getvalue().count("weak"), 1)
self.assertIs(json.loads(self.auth.read_text())["password_weak"], False)
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
def test_cli_retries_mismatch_and_does_not_print_password(self):
output = io.StringIO()
errors = io.StringIO()
@@ -103,14 +169,16 @@ class PasswordTests(unittest.TestCase):
patch.object(password, "auth_path", return_value=self.auth), \
patch.object(password.sys.stdin, "isatty", return_value=True), \
patch.object(password.getpass, "getpass", side_effect=[
"first password", "wrong confirmation", "new password", "new password"]), \
"tiny9", "first strong password here", "wrong confirmation", NEW, NEW]), \
patch.object(password.sys, "stdout", output), \
patch.object(password.sys, "stderr", errors):
password.main()
self.assertEqual(json.loads(self.auth.read_text())["password"], "new password")
self.assertTrue(password.verify_password(json.loads(self.auth.read_text())["password_hash"], NEW))
self.assertIn("do not match", errors.getvalue())
self.assertIn("Too short", errors.getvalue())
self.assertIn("No service restart", output.getvalue())
for value in ("first password", "wrong confirmation", "new password", "old password"):
self.assertIn("signed out", output.getvalue())
for value in ("tiny9", "first strong password here", "wrong confirmation", NEW, "old password"):
self.assertNotIn(value, output.getvalue() + errors.getvalue())
def test_cli_refuses_non_tty_before_prompting(self):