e43f9fa50c
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.
193 lines
10 KiB
Python
193 lines
10 KiB
Python
import importlib.util
|
|
import json
|
|
import io
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
spec = importlib.util.spec_from_file_location("pipelines_password", Path(__file__).with_name("password.py"))
|
|
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-")
|
|
self.addCleanup(self.temporary.cleanup)
|
|
self.home = Path(self.temporary.name).resolve()
|
|
self.auth = self.home / ".config/pipelines/web-auth.json"
|
|
self.auth.parent.mkdir(parents=True)
|
|
self.original = {"password": "old password", "secret": "s" * 32, "other": "preserve"}
|
|
self.auth.write_text(json.dumps(self.original))
|
|
self.auth.chmod(0o600)
|
|
|
|
def test_default_path_works_for_partial_install_without_manifest(self):
|
|
self.assertEqual(password.auth_path(home=self.home, environ={}), self.auth)
|
|
|
|
def test_manifest_custom_auth_and_explicit_environment(self):
|
|
prefix = self.home / "installation"
|
|
prefix.mkdir()
|
|
recorded = self.home / "custom-auth.json"
|
|
(prefix / "pipelines-install.json").write_text(json.dumps({
|
|
"format": 1, "prefix": str(prefix), "web": {"auth_file": str(recorded)}}))
|
|
self.assertEqual(password.auth_path(home=self.home, prefix=prefix, environ={}), recorded)
|
|
self.assertEqual(password.auth_path(home=self.home, prefix=prefix,
|
|
environ={"PIPELINES_WEB_AUTH_FILE": str(self.auth)}), self.auth)
|
|
|
|
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.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):
|
|
password.change_password(self.auth, "short")
|
|
self.assertEqual(json.loads(self.auth.read_text()), self.original)
|
|
|
|
def test_symlinked_password_is_refused_and_target_untouched(self):
|
|
link = self.home / "link.json"
|
|
link.symlink_to(self.auth)
|
|
with self.assertRaises(ValueError):
|
|
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)
|
|
self.auth.chmod(0o600)
|
|
self.auth.write_text("{broken")
|
|
with self.assertRaises(ValueError):
|
|
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)
|
|
self.assertEqual(self.auth.read_bytes(), before)
|
|
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
|
|
|
|
def test_atomic_replace_occurs_on_destination_filesystem(self):
|
|
replace = os.replace
|
|
calls = []
|
|
def checked_replace(source, target):
|
|
calls.append((source, target))
|
|
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)
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
def test_file_changed_during_update_is_not_overwritten(self):
|
|
replace = os.fsync
|
|
def external_update(descriptor):
|
|
self.auth.write_text(json.dumps(dict(self.original, password="changed elsewhere")))
|
|
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)
|
|
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()
|
|
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=[
|
|
"tiny9", "first strong password here", "wrong confirmation", NEW, NEW]), \
|
|
patch.object(password.sys, "stdout", output), \
|
|
patch.object(password.sys, "stderr", errors):
|
|
password.main()
|
|
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())
|
|
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):
|
|
with patch.object(password.sys, "argv", ["password"]), \
|
|
patch.object(password, "auth_path", return_value=self.auth), \
|
|
patch.object(password.sys.stdin, "isatty", return_value=False), \
|
|
patch.object(password.getpass, "getpass") as prompt:
|
|
with self.assertRaisesRegex(ValueError, "interactive terminal"):
|
|
password.main()
|
|
prompt.assert_not_called()
|
|
self.assertEqual(json.loads(self.auth.read_text()), self.original)
|