Stdlib-only Python app: password login (scrypt), signed SameSite=Strict cookie, CSRF tokens, login rate limiting, strict CSP and no JavaScript. Every action is one ssh call to sessions-ctl with a pinned host key. Ships a Containerfile and a Podman Quadlet unit (rootless, read-only root, all capabilities dropped, published on 127.0.0.1 only, secrets via podman secret). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
3.5 KiB
Python
91 lines
3.5 KiB
Python
"""Unit tests for sessions-web. Run: python3 -m unittest discover -s web/tests"""
|
|
|
|
import importlib.util
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
_spec = importlib.util.spec_from_file_location("app", Path(__file__).parents[1] / "app.py")
|
|
app = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(app)
|
|
|
|
SECRET = b"x" * 32
|
|
|
|
|
|
class Passwords(unittest.TestCase):
|
|
def test_roundtrip(self):
|
|
h = app.hash_password("correct horse battery", n=2**10)
|
|
self.assertTrue(app.verify_password("correct horse battery", h))
|
|
self.assertFalse(app.verify_password("wrong", h))
|
|
|
|
def test_garbage_hash_is_false_not_crash(self):
|
|
for h in ("", "plain", "scrypt$x$y$z$a$b", "md5$1$2$3$4$5"):
|
|
self.assertFalse(app.verify_password("pw", h))
|
|
|
|
|
|
class Cookies(unittest.TestCase):
|
|
def test_sign_unsign(self):
|
|
tok = app.sign(SECRET, {"sid": "abc", "exp": 2_000_000_000})
|
|
self.assertEqual(app.unsign(SECRET, tok)["sid"], "abc")
|
|
|
|
def test_tampered_or_expired(self):
|
|
tok = app.sign(SECRET, {"sid": "abc", "exp": 2_000_000_000})
|
|
body, mac = tok.rsplit(".", 1)
|
|
forged = app.sign(b"y" * 32, {"sid": "abc", "exp": 2_000_000_000})
|
|
self.assertIsNone(app.unsign(SECRET, forged))
|
|
self.assertIsNone(app.unsign(SECRET, body + "." + "0" * 64))
|
|
self.assertIsNone(app.unsign(SECRET, "nodot"))
|
|
old = app.sign(SECRET, {"sid": "abc", "exp": 100})
|
|
self.assertIsNone(app.unsign(SECRET, old))
|
|
|
|
def test_csrf_bound_to_session(self):
|
|
self.assertNotEqual(app.csrf_token(SECRET, "a"), app.csrf_token(SECRET, "b"))
|
|
|
|
|
|
class Limiter(unittest.TestCase):
|
|
def test_per_ip_and_window(self):
|
|
rl = app.RateLimiter(per_ip=2, total=10, window=100)
|
|
rl.fail("1.1.1.1", now=0)
|
|
self.assertFalse(rl.blocked("1.1.1.1", now=1))
|
|
rl.fail("1.1.1.1", now=1)
|
|
self.assertTrue(rl.blocked("1.1.1.1", now=2))
|
|
self.assertFalse(rl.blocked("2.2.2.2", now=2))
|
|
self.assertFalse(rl.blocked("1.1.1.1", now=200))
|
|
|
|
def test_global_cap(self):
|
|
rl = app.RateLimiter(per_ip=5, total=3, window=100)
|
|
for i in range(3):
|
|
rl.fail(f"10.0.0.{i}", now=0)
|
|
self.assertTrue(rl.blocked("9.9.9.9", now=1))
|
|
|
|
|
|
class SshArgv(unittest.TestCase):
|
|
def setUp(self):
|
|
self.dev = app.DevBox({"SESSIONS_DEV_HOST": "dev.example"})
|
|
|
|
def test_shape(self):
|
|
argv = self.dev.argv("create", "fix-login", "papure", "feature/x")
|
|
self.assertEqual(argv[0], "ssh")
|
|
self.assertIn("StrictHostKeyChecking=yes", argv)
|
|
self.assertEqual(argv[-2:], ["--", "create fix-login papure feature/x"])
|
|
|
|
def test_rejects_odd_arguments(self):
|
|
for bad in ("", "-oProxyCommand=x", "a b", "$(id)", "a;b", "A"):
|
|
with self.assertRaises(ValueError, msg=bad):
|
|
self.dev.argv("status", bad)
|
|
self.assertFalse(self.dev.call("status", "a;b")["ok"])
|
|
|
|
|
|
class Rendering(unittest.TestCase):
|
|
def test_escapes_everything(self):
|
|
listing = {"ok": True, "max_running": 2, "sessions": [{
|
|
"name": "<script>", "repo": "r", "branch": "b", "created_at": "2026-09-30T11:00:00",
|
|
"state": "active", "enabled": True, "restarts": 0,
|
|
"remote_control_url": 'https://claude.ai/code/session_x" onclick="x', "needs_attention": None}]}
|
|
out = app.dashboard(listing, {"repos": ["papure"]}, "tok", ("err", "<b>")).decode()
|
|
self.assertNotIn("<script>", out)
|
|
self.assertNotIn('" onclick="', out)
|
|
self.assertIn("<b>", out)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|