Run sessions-web behind Authelia, configured only by env vars
Drop the app's own login (password hash, signed cookies, CSRF tokens, rate limiting): Authelia via Caddy forward_auth is the login. The app refuses any request without the Remote-User header (optionally limited to SESSIONS_ALLOWED_USERS) and any cross-site POST (Sec-Fetch-Site / Origin). The SSH key and the dev box host key now come from environment variables printed by `sessions-ctl keygen`, so the container needs no secrets store or mounts. Add compose.yaml so prod's panel can build and deploy it; remove the Quadlet unit and the files it needed. README covers the panel deploy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KKYfRST9cTJYae3f7Jf3ac
This commit is contained in:
parent
ca66db3aeb
commit
b55ea07c96
9 changed files with 254 additions and 332 deletions
|
|
@ -1,6 +1,10 @@
|
|||
"""Unit tests for sessions-web. Run: python3 -m unittest discover -s web/tests"""
|
||||
|
||||
import base64
|
||||
import importlib.util
|
||||
import os
|
||||
import stat
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
|
|
@ -8,64 +12,60 @@ _spec = importlib.util.spec_from_file_location("app", Path(__file__).parents[1]
|
|||
app = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(app)
|
||||
|
||||
SECRET = b"x" * 32
|
||||
FAKE_KEY = b"-----BEGIN OPENSSH PRIVATE KEY-----\nAAAAfake\n-----END OPENSSH PRIVATE KEY-----\n"
|
||||
ENV = {
|
||||
"SESSIONS_DEV_HOST": "2.28.100.223",
|
||||
"SESSIONS_DEV_HOST_KEY": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEdLIHPjrkeUPfw1CDcIHC2Yydl39X46GBjmn6vqd+6E",
|
||||
"SESSIONS_SSH_KEY_B64": base64.b64encode(FAKE_KEY).decode(),
|
||||
}
|
||||
|
||||
|
||||
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))
|
||||
class SshFiles(unittest.TestCase):
|
||||
def test_writes_private_key_and_pinned_host(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
key, known = app.materialise_ssh_files(ENV, d)
|
||||
self.assertEqual(Path(key).read_bytes(), FAKE_KEY)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(key).st_mode), 0o600)
|
||||
self.assertEqual(Path(known).read_text(), f"[2.28.100.223]:14922 {ENV['SESSIONS_DEV_HOST_KEY']}\n")
|
||||
|
||||
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))
|
||||
def test_rejects_bad_values(self):
|
||||
bad = [
|
||||
{"SESSIONS_SSH_KEY_B64": "not base64!"},
|
||||
{"SESSIONS_SSH_KEY_B64": base64.b64encode(b"hello").decode()},
|
||||
{"SESSIONS_DEV_HOST_KEY": "ssh-ed25519"},
|
||||
{"SESSIONS_DEV_HOST_KEY": "ssh-ed25519 AAAA\n[evil]:22 ssh-ed25519 AAAA"},
|
||||
]
|
||||
for override in bad:
|
||||
with tempfile.TemporaryDirectory() as d, self.assertRaises(app.ConfigError, msg=override):
|
||||
app.materialise_ssh_files({**ENV, **override}, d)
|
||||
|
||||
|
||||
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")
|
||||
class RequestChecks(unittest.TestCase):
|
||||
def test_same_origin(self):
|
||||
host = "sessions.reudy.net"
|
||||
self.assertTrue(app.same_origin({"Sec-Fetch-Site": "same-origin"}, host))
|
||||
self.assertFalse(app.same_origin({"Sec-Fetch-Site": "cross-site", "Origin": f"https://{host}"}, host))
|
||||
self.assertFalse(app.same_origin({"Sec-Fetch-Site": "same-site"}, host))
|
||||
self.assertTrue(app.same_origin({"Origin": f"https://{host}"}, host))
|
||||
self.assertFalse(app.same_origin({"Origin": "https://evil.example"}, host))
|
||||
self.assertFalse(app.same_origin({}, host))
|
||||
|
||||
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))
|
||||
def test_allowed_user(self):
|
||||
self.assertFalse(app.allowed_user("", set()))
|
||||
self.assertTrue(app.allowed_user("reudy", set()))
|
||||
self.assertTrue(app.allowed_user("reudy", {"reudy"}))
|
||||
self.assertFalse(app.allowed_user("mallory", {"reudy"}))
|
||||
|
||||
|
||||
class SshArgv(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dev = app.DevBox({"SESSIONS_DEV_HOST": "dev.example"})
|
||||
self.dev = app.DevBox({"SESSIONS_DEV_HOST": "dev.example"}, "/k", "/kh")
|
||||
|
||||
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.assertIn("UserKnownHostsFile=/kh", argv)
|
||||
self.assertEqual(argv[-2:], ["--", "create fix-login papure feature/x"])
|
||||
|
||||
def test_rejects_odd_arguments(self):
|
||||
|
|
@ -81,8 +81,9 @@ class Rendering(unittest.TestCase):
|
|||
"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()
|
||||
out = app.dashboard(listing, {"repos": ["papure"]}, "<u>", ("err", "<b>")).decode()
|
||||
self.assertNotIn("<script>", out)
|
||||
self.assertNotIn("<u>", out)
|
||||
self.assertNotIn('" onclick="', out)
|
||||
self.assertIn("<b>", out)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue