From 177bee2d01caa9e04999d5633b55103cbceccc49 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 16 Oct 2026 23:16:13 -0700 Subject: [PATCH] Route isolated service previews through their own authentication gateway Keep existing production routes until their service opts in, preserve preview metadata, and exclude generated bridge builds from releases. Assisted-by: gpt-6.1-sol Assisted-by: gpt-6-astra --- config/Service.pkl | 2 + tools/dashboard-stage-test.py | 46 ++++++ tools/dashboard-stage.py | 11 +- tools/deploy-test.py | 6 + tools/deploy.py | 5 +- tools/release.py | 8 +- tools/router-jellyfin-test.py | 265 ++++++++++++++++++++++++++++++++++ tools/router-zig-test.py | 100 +++++++++++++ tools/router.py | 140 +++++++++++++++++- tools/snowglobe.py | 3 + 10 files changed, 575 insertions(+), 11 deletions(-) create mode 100644 tools/router-jellyfin-test.py create mode 100644 tools/router-zig-test.py diff --git a/config/Service.pkl b/config/Service.pkl index 7f84e850fff723d66ea6a545e3b12e6de46dc0f6..d0473741351172c0d1fb9d096b6cd67834cfb62a 100644 --- a/config/Service.pkl +++ b/config/Service.pkl @@ -19,6 +19,8 @@ class Http { forwardRealIp: Boolean = false /// Require HTTPS when browsers send cookies issued by this route. secureCookies: Boolean = false + /// Require the native Snow sign-in gateway before Jellyfin handles requests. + jellyfinAuth: Boolean = false /// Generated secret whose value becomes the private proxy header name. identityProofSecret: String? /// Additional hosts routed to this container without SSO headers. diff --git a/tools/dashboard-stage-test.py b/tools/dashboard-stage-test.py index 99f1cfb78d04c5e922f95832ffb6f6cf53f2b8f8..f04fa0ff03b5bac8c36ce97edd452f0750ae215f 100644 --- a/tools/dashboard-stage-test.py +++ b/tools/dashboard-stage-test.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 from importlib import import_module +from contextlib import redirect_stdout +import io import json import os from pathlib import Path @@ -84,6 +86,50 @@ class PreviewBoundary(unittest.TestCase): self.assertFalse(any(call.args[:2] == ("zfs", "destroy") for call in commands.call_args_list)) +class PreviewProof(unittest.TestCase): + def test_generated_proof_is_private_mounted_and_retained_on_restage(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + state = root / "state" + site = {"root": str(root / "srv"), "domain": "fixture.test", "pool": "fixture", "nodeName": "fixture", "tlsInternal": True} + jobs = [] + + def command(*args, **kwargs): + output = "" + if args[0] == "pkl": + output = json.dumps(site) + elif args[:4] == ("nomad", "acl", "token", "create"): + output = json.dumps({"AccessorID": "fixture-accessor", "SecretID": "fixture-nomad-token"}) + elif args[:2] == ("podman", "run"): + output = json.dumps({"username": "preview", "password": "disposable-fixture-password"}) + elif args[:3] == ("nomad", "job", "run"): + jobs.append(json.loads(kwargs["input"])) + return subprocess.CompletedProcess(args, 0, stdout=output) + + def run(args, **kwargs): + return subprocess.CompletedProcess(args, 0, stdout="fixture-image" if args[0] == "nix" else "") + + original_exists = Path.exists + def exists(path): + return str(path).startswith("/run/snowglobe-preview/") and path.name == "host.sock" or original_exists(path) + + with patch.object(stage.snowglobe, "STATE", state), patch.object(stage.snowglobe, "REPO", root / "releases/1234567890abcdef"), patch.object(stage.snowglobe, "token"), patch.object(stage.snowglobe, "identity", return_value=os.getuid()), patch.object(stage.snowglobe, "check_staging_pool"), patch.object(stage.snowglobe, "check_http"), patch.object(stage.snowglobe, "command", side_effect=command), patch.object(stage.subprocess, "run", side_effect=run), patch.object(stage.os, "chown") as chown, patch.object(stage.snowglobe.transition, "host_environment", return_value="PATH=/bin"), patch.object(stage.snowglobe.transition, "stop_preview"), patch.object(Path, "exists", exists), redirect_stdout(io.StringIO()): + stage.stage() + task = jobs[-1]["TaskGroups"][0]["Tasks"][0] + directory = root / "srv/staging" / jobs[-1]["ID"] + proof = directory / "proxy.token" + value = proof.read_text() + self.assertRegex(value.strip(), r"^[0-9a-f]{64}$") + self.assertEqual(proof.stat().st_mode & 0o777, 0o400) + chown.assert_any_call(proof, os.getuid(), os.getuid()) + self.assertEqual(task["Env"]["SNOWGLOBE_PROXY_TOKEN_FILE"], "/run/secrets/proxy.token") + self.assertIn(str(proof) + ":/run/secrets/proxy.token:ro", task["Config"]["volumes"]) + self.assertNotIn(value.strip(), json.dumps(jobs[-1])) + stage.stage() + self.assertEqual(proof.read_text(), value) + self.assertEqual(jobs[-1]["ID"], jobs[-2]["ID"]) + + class NativePreview(unittest.TestCase): def test_empty_proof_requires_loopback_and_fresh_account_stays_private(self): binary = Path(os.environ.get("DASHBOARD_BINARY", "dashboard/target/debug/home-dashboard")).resolve() diff --git a/tools/dashboard-stage.py b/tools/dashboard-stage.py index 1c8e1e116faa9205d368bdf1d0052e3433feec00..d9c052a5fd52fc6330f859e761f7877f043cb49f 100644 --- a/tools/dashboard-stage.py +++ b/tools/dashboard-stage.py @@ -109,7 +109,7 @@ def stage(): env = { "SNOWGLOBE_DOMAIN": site["domain"], "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + hostname, "SNOWGLOBE_FILE_ORIGIN": "https://files-" + hostname, "SNOWGLOBE_AUTH_RP_ID": hostname, - "SNOWGLOBE_AUTH_REQUIRED": "1", "SNOWGLOBE_PROXY_TOKEN_FILE": "", "SNOWGLOBE_DATA_DIR": "/data", + "SNOWGLOBE_AUTH_REQUIRED": "1", "SNOWGLOBE_PROXY_TOKEN_FILE": "/run/secrets/proxy.token", "SNOWGLOBE_DATA_DIR": "/data", "SNOWGLOBE_PREVIEW": "1", "SNOWGLOBE_METRICS": "follow", "SNOWGLOBE_LISTEN_ADDRESS": "127.0.0.1", "PORT": "${NOMAD_PORT_http}", "SNOWGLOBE_NOMAD_TOKEN_FILE": "/run/secrets/nomad.token", "NOMAD_ADDR": "http://127.0.0.1:4646", @@ -132,6 +132,11 @@ def stage(): fixture = root / "files/preview.txt" fixture.write_text("Disposable Snow Globe dashboard preview.\n") fixture.chmod(0o644) + proof = root / "proxy.token" + if not proof.exists(): + snowglobe.write_private(proof, secrets.token_hex(32) + "\n") + os.chown(proof, saved["uid"], saved["uid"]) + proof.chmod(0o400) directory.mkdir(parents=True, exist_ok=True) saved.update(release=snowglobe.REPO.name, ready=False) snowglobe.write_private(directory / f"{identity}.json", json.dumps(saved)) @@ -148,6 +153,7 @@ def stage(): "--user=" + str(saved["uid"]), "--cap-drop=ALL", "--security-opt=no-new-privileges", "--entrypoint=/bin/home-dashboard", "--volume=" + str(root / "data") + ":/data:rw", + "--volume=" + str(proof) + ":/run/secrets/proxy.token:ro", *["--env=" + key + "=" + value for key, value in env.items()], image, "--preview-account", capture=True) snowglobe.write_private(root / "access.json", imported.stdout) runtime = "/run/snowglobe-preview/" + identity @@ -172,7 +178,8 @@ def stage(): "Tasks": [{"Name": "app", "Driver": "podman", "User": str(saved["uid"]) + ":" + str(saved["uid"]), "Env": env, "Config": {"image": image, "network_mode": "host", "cap_drop": ["ALL"], "security_opt": ["no-new-privileges"], "volumes": [str(root / "data") + ":/data:rw", str(root / "files") + ":/srv:rw,rslave", runtime + ":/run/snowglobe-host:ro", - str(root / "nomad.token") + ":/run/secrets/nomad.token:ro"]}, + str(root / "nomad.token") + ":/run/secrets/nomad.token:ro", + str(proof) + ":/run/secrets/proxy.token:ro"]}, "Resources": {"CPU": 1000, "MemoryMB": 2048}}]}]} snowglobe.command("nomad", "job", "run", "-json", "-", input=json.dumps(job)) snowglobe.check_http(hostname, "/sign-in", site["tlsInternal"]) diff --git a/tools/deploy-test.py b/tools/deploy-test.py index bb7c1f434c2f1143d73437a77fd5e65fffc1a51a..28995dededbc4d8981121374ade0bf883ec95806 100644 --- a/tools/deploy-test.py +++ b/tools/deploy-test.py @@ -40,6 +40,11 @@ class MainExport(unittest.TestCase): subprocess.run(["jj", "new"], cwd=repo, check=True, capture_output=True) (repo / "service/fixture").write_text("unfinished") (repo / "service/new").write_text("new working file") + generated = repo / "service/jellyfin/bridge/tests/bin/Release/net10.0/fixture.dll" + generated.parent.mkdir(parents=True) + digest = release.tree_digest(repo) + generated.write_bytes(b"compiled fixture") + self.assertEqual(release.tree_digest(repo), digest) original = subprocess.run def transport(argv, **kwargs): @@ -66,6 +71,7 @@ class MainExport(unittest.TestCase): self.assertEqual(json.loads((main / ".snowglobe-release.json").read_text())["main"], {"commit": commit, "description": "Test main\n"}) self.assertEqual((stage / "service/fixture").read_text(), "unfinished") self.assertEqual((stage / "service/new").read_text(), "new working file") + self.assertFalse((stage / generated.relative_to(repo)).exists()) self.assertNotIn("main", json.loads((stage / ".snowglobe-release.json").read_text())) for exported in (main, stage): self.assertEqual((exported / "service/app/service.pkl").read_text(), "service definition") diff --git a/tools/deploy.py b/tools/deploy.py index e2503f1631644297fa2f745039b3151818263c0f..b36cd8cac55f94a4c74ad81a971e4f4d1c0656f5 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -12,7 +12,7 @@ import sys import tempfile import time -from release import SOURCES, tree_digest +from release import BUILD_DIRS, SOURCES, tree_digest REPO = Path(__file__).resolve().parent.parent @@ -92,8 +92,7 @@ def upload(main=False): subprocess.run( ["rsync", "-a", "--exclude=.DS_Store", "--exclude=__pycache__", "--exclude=*.pyc", "--exclude=.identities.lock", "--exclude=identities.pending", "--exclude=._*", - "--exclude=dashboard/node_modules", "--exclude=dashboard/dist", - "--exclude=dashboard/.cache", "--exclude=dashboard/data", "--exclude=dashboard/target", + *(f"--exclude={directory}" for directory in BUILD_DIRS), *(str(REPO / path) for path in SOURCES), str(snapshot) + "/"], check=True, ) diff --git a/tools/release.py b/tools/release.py index 2a4745b9055f6623b592244cff7f03969b14db66..6958f06c1ec3268755808b126f6b5669d24693fc 100644 --- a/tools/release.py +++ b/tools/release.py @@ -19,6 +19,8 @@ RELEASES = ROOT / "releases" STATE = Path("/var/lib/snowglobe") HISTORY = STATE / "deployments.json" SOURCES = ("config", "service", "tools", "nixos", "dashboard", "guest", "flake.nix", "flake.lock", "readme.md", "AGENTS.md") +BUILD_DIRS = ("dashboard/node_modules", "dashboard/dist", "dashboard/.cache", "dashboard/data", "dashboard/target", "guest/build", + "service/jellyfin/bridge/bin", "service/jellyfin/bridge/obj", "service/jellyfin/bridge/tests/bin", "service/jellyfin/bridge/tests/obj") RELEASE_ID = re.compile(r"[0-9a-f]{16}\Z") STAGE_ID = re.compile(r"[a-z][a-z0-9-]*\Z") @@ -31,11 +33,7 @@ def files(root): paths = source.rglob("*") if source.is_dir() else [source] for path in sorted(paths): relative = path.relative_to(root) - if relative.parts[:2] in ( - ("dashboard", "node_modules"), ("dashboard", "dist"), - ("dashboard", ".cache"), ("dashboard", "data"), ("dashboard", "target"), - ("guest", "build"), - ): + if any(relative.is_relative_to(directory) for directory in BUILD_DIRS): continue if any(part in {".DS_Store", "__pycache__", ".identities.lock", "identities.pending"} or part.startswith("._") or part.endswith(".pyc") for part in relative.parts): continue diff --git a/tools/router-jellyfin-test.py b/tools/router-jellyfin-test.py new file mode 100644 index 0000000000000000000000000000000000000000..6c42be794b70c633b06bebbefabab48c4e7a200b --- /dev/null +++ b/tools/router-jellyfin-test.py @@ -0,0 +1,265 @@ +#!/usr/bin/env python3 +import argparse +from http.client import HTTPConnection +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import os +import socket +import subprocess +import tempfile +import threading +import time +from pathlib import Path +import unittest +import urllib.parse +from unittest.mock import patch + +import router + + +class Backend(BaseHTTPRequestHandler): + requests = [] + bodies = [] + + def do_GET(self): + type(self).requests.append((self.command, self.path, dict(self.headers))) + type(self).bodies.append(self.rfile.read(int(self.headers.get('Content-Length', '0')))) + if self.headers.get('Upgrade', '').lower() == 'websocket': + self.send_response(101) + self.send_header('Connection', 'Upgrade') + self.send_header('Upgrade', 'websocket') + self.end_headers() + return + self.send_response(206 if self.headers.get('Range') else 200) + self.send_header('Content-Type', 'application/octet-stream') + self.send_header('ETag', '"fixture"') + self.send_header('Content-Length', '5') + if self.headers.get('Range'): + self.send_header('Content-Range', 'bytes 0-4/10') + self.end_headers() + if self.command != 'HEAD': + self.wfile.write(b'hello') + + do_HEAD = do_GET + do_POST = do_GET + + def log_message(self, *_): + pass + + +class Gateway(BaseHTTPRequestHandler): + requests = [] + + def do_GET(self): + size = int(self.headers.get('Content-Length', '0')) + body = self.rfile.read(size) + type(self).requests.append((self.command, self.path, dict(self.headers), body)) + trusted = self.headers.get('Snowglobe-Proxy-Token') == 'a' * 64 + original = self.headers.get('Snowglobe-Jellyfin-Uri', '') + if not trusted: + self.send_error(403) + elif self.path == '/auth/jellyfin/public': + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + self.wfile.write(json.dumps({'method': self.headers.get('Snowglobe-Jellyfin-Method'), 'body': body.decode()}).encode()) + elif self.headers.get('Authorization') == 'allow' or original.startswith('/Items/' + '1' * 32 + '/Images/Primary'): + self.send_response(204) + path, _, query = original.partition('?') + kept = [part for part in query.split('&') if part and urllib.parse.unquote_plus(part.split('=', 1)[0]).lower() not in {'apikey', 'api_key'}] + self.send_header('X-Snowglobe-Jellyfin-Uri', path + ('?' + '&'.join(kept) if kept else '')) + if self.headers.get('Authorization'): + self.send_header('X-Snowglobe-Jellyfin-Authorization', 'MediaBrowser Token="normalized"') + self.send_header('Set-Cookie', '__Host-snow-jellyfin=normalized; HttpOnly; Secure; Path=/') + self.end_headers() + else: + self.send_error(401) + + do_POST = do_GET + do_OPTIONS = do_GET + + def log_message(self, *_): + pass + + +class GatewayAssociation(unittest.TestCase): + def test_only_tagged_preview_uses_its_own_gateway(self): + with tempfile.TemporaryDirectory(prefix='jellyfin-gateway-') as temporary: + root = Path(temporary) + stages = root / 'stages' + stages.mkdir() + jellyfin = 'jellyfin-preview-1234abcd' + dashboard = 'dashboard-preview-1234abcd' + (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) + mount = root / 'staging' / dashboard + mount.mkdir(parents=True) + proxy_proof = mount / 'proxy.token' + proxy_proof.write_text('b' * 64) + proxy_proof.chmod(0o400) + (stages / (dashboard + '.json')).write_text(json.dumps({'sourceId': 'dashboard', 'mount': str(mount)})) + proof = root / 'proof' + proof.write_text('a' * 64) + services = { + 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), + 'copyparty': (1112, ['caddy-host=file.paperclover.net']), + jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), + dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), + } + + def nomad(path, _): + if path == '/v1/services': + return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in services]}] + if path.startswith('/v1/service/'): + name = path.rsplit('/', 1)[1] + port, tags = services[name] + return [{'ServiceName': name, 'JobID': name, 'AllocID': name, 'Address': '127.0.0.1', 'Port': port, 'Tags': tags}] + return {'http': {'Status': 'success'}} + + environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '4444', + 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'SNOWGLOBE_STATE': temporary} + with patch.dict(os.environ, environment, clear=True), patch.object(router, 'nomad', nomad), patch.object(router, 'ROUTE_DIR', str(root / 'routes')): + rendered = router.render('fixture') + legacy = rendered.split('jelly.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertNotIn('/auth/jellyfin/', legacy) + self.assertIn('reverse_proxy 127.0.0.1:1111', legacy) + self.assertIn('reverse_proxy 127.0.0.1:3333', preview) + self.assertNotIn('reverse_proxy 127.0.0.1:4444', preview) + self.assertNotIn('a' * 64, preview) + self.assertIn('b' * 64, preview) + dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) + self.assertNotIn('tracing', preview) + files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertNotIn('@zig_mirror', files) + services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) + enabled = router.render('fixture') + files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('reverse_proxy 127.0.0.1:5555', files) + (stages / (jellyfin + '.json')).unlink() + rendered = router.render('fixture') + preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('respond 503', preview) + self.assertNotIn('reverse_proxy', preview) + + +class JellyfinRouter(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.root = tempfile.TemporaryDirectory(prefix='snowglobe-jellyfin-router-') + cls.backend = ThreadingHTTPServer(('127.0.0.1', 0), Backend) + cls.gateway = ThreadingHTTPServer(('127.0.0.1', 0), Gateway) + for server in (cls.backend, cls.gateway): + threading.Thread(target=server.serve_forever, daemon=True).start() + with socket.socket() as listener: + listener.bind(('127.0.0.1', 0)) + cls.port = listener.getsockname()[1] + conf = Path(cls.root.name) / 'Caddyfile' + route = '\n'.join(router.jellyfin_routes('jellyfin-preview-1234abcd', {f'127.0.0.1:{cls.backend.server_port}'}, f'127.0.0.1:{cls.gateway.server_port}', 'a' * 64)) + conf.write_text('{\n admin off\n auto_https off\n}\nhttp://127.0.0.1:' + str(cls.port) + ' {\n' + route + '\n}\n') + validated = subprocess.run([str(CADDY), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) + if validated.returncode: + raise RuntimeError(validated.stderr) + + cls.log = (Path(cls.root.name) / 'caddy.log').open('w+') + cls.caddy = subprocess.Popen([str(CADDY), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=cls.log, stderr=cls.log) + deadline = time.monotonic() + 10 + while True: + try: + cls.request('GET', '/ready') + break + except OSError: + if time.monotonic() > deadline or cls.caddy.poll() is not None: + cls.log.seek(0) + raise RuntimeError(cls.log.read()) + time.sleep(.05) + + @classmethod + def tearDownClass(cls): + cls.caddy.terminate() + cls.caddy.wait(timeout=10) + cls.backend.shutdown() + cls.gateway.shutdown() + cls.backend.server_close() + cls.gateway.server_close() + cls.log.close() + cls.root.cleanup() + + @classmethod + def request(cls, method, path, body=None, headers=None): + connection = HTTPConnection('127.0.0.1', cls.port, timeout=3) + connection.request(method, path, body=body, headers=headers or {}) + response = connection.getresponse() + result = response.status, dict(response.getheaders()), response.read() + connection.close() + return result + + def setUp(self): + Backend.requests.clear() + Gateway.requests.clear() + + def test_default_deny_includes_theme_assets_and_fallback(self): + for path in ['/web/', '/web/extra/cloverfin.css', '/favicon.ico', '/Items/known', '/Videos/x/stream', '/socket']: + with self.subTest(path=path): + self.assertEqual(self.request('GET', path, headers={'Snowglobe-Proxy-Token': 'forged', 'Snowglobe-Jellyfin-Service': 'jellyfin'})[0], 401) + self.assertEqual(Backend.requests, []) + self.assertTrue(all(r[2]['Snowglobe-Jellyfin-Service'] == 'jellyfin-preview-1234abcd' for r in Gateway.requests)) + count = len(Gateway.requests) + for path in ['/Snowglobe/Bridge', '/sNoWgLoBe/Bridge', '/metrics', '/Users/AuthenticateByName', '/Users/' + '1' * 32 + '/Authenticate', '/Users/ForgotPassword/Pin', '/QuickConnect/Authorize']: + self.assertEqual(self.request('POST', path, body='{}', headers={'Authorization': 'allow'})[0], 404) + self.assertEqual(len(Gateway.requests), count) + self.assertEqual(Backend.requests, []) + + def test_public_protocol_preserves_method_body_and_uri(self): + body = '{"DeviceId":"fixture"}' + status, _, payload = self.request('POST', '/QuickConnect/Initiate?fixture=1', body=body) + self.assertEqual(status, 200) + self.assertEqual(json.loads(payload), {'method': 'POST', 'body': body}) + self.assertEqual(Gateway.requests[-1][2]['Snowglobe-Jellyfin-Uri'], '/QuickConnect/Initiate?fixture=1') + self.assertEqual(Backend.requests, []) + self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) + + def test_authenticated_ranges_head_websocket_and_normalized_credential(self): + headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} + status, response_headers, body = self.request('GET', '/Videos/fixture/stream?ApiKey=old&api_key=old&aPiKeY=old&static=true', headers={**headers, 'Range': 'bytes=0-4'}) + self.assertEqual((status, body), (206, b'hello')) + self.assertEqual(response_headers['Content-Range'], 'bytes 0-4/10') + self.assertEqual({k.lower(): v for k, v in response_headers.items()}['etag'], '"fixture"') + self.assertIn('HttpOnly', response_headers['Set-Cookie']) + method, path, upstream = Backend.requests[-1] + self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) + self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') + self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) + status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) + self.assertEqual((status, body), (200, b'')) + self.assertEqual(Backend.requests[-1][0], 'HEAD') + self.assertEqual(self.request('POST', '/Items/fixture/PlaybackInfo', body='synthetic playback options', headers=headers)[0], 200) + self.assertEqual(Backend.requests[-1][0], 'POST') + self.assertEqual(Backend.bodies[-1], b'synthetic playback options') + self.assertEqual(self.request('GET', '/socket', headers={**headers, 'Connection': 'Upgrade', 'Upgrade': 'websocket'})[0], 101) + self.assertEqual(Backend.requests[-1][2]['Upgrade'], 'websocket') + + def test_narrow_artwork_still_passes_gateway_check(self): + self.assertEqual(self.request('GET', '/Items/' + '1' * 32 + '/Images/Primary?maxWidth=300')[0], 200) + self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/check') + self.assertEqual(len(Backend.requests), 1) + + def test_z_upstream_failure_does_not_log_native_credentials(self): + self.gateway.shutdown() + self.gateway.server_close() + secret = 'SYNTHETIC-PAIRING-SECRET' + self.assertEqual(self.request('GET', '/QuickConnect/Connect?secret=' + secret, + headers={'X-Emby-Token': 'SYNTHETIC-NATIVE-TOKEN'})[0], 503) + self.log.flush() + self.log.seek(0) + log = self.log.read() + self.assertNotIn(secret, log) + self.assertNotIn('SYNTHETIC-NATIVE-TOKEN', log) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--caddy', type=Path, required=True) + args, remaining = parser.parse_known_args() + CADDY = args.caddy + unittest.main(argv=[__file__, *remaining]) diff --git a/tools/router-zig-test.py b/tools/router-zig-test.py new file mode 100644 index 0000000000000000000000000000000000000000..4f0cdbaac15d8dc6830613d3b82f6dc8c11fd12a --- /dev/null +++ b/tools/router-zig-test.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +import argparse +from http.client import HTTPConnection +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import os +from pathlib import Path +import socket +import subprocess +import tempfile +import threading +import time +from unittest.mock import patch + +import router + + +class Mirror(BaseHTTPRequestHandler): + requests = [] + + def do_GET(self): + type(self).requests.append((self.command, self.path, dict(self.headers))) + self.send_response(206 if self.headers.get('Range') else 200) + self.send_header('Content-Length', '5') + self.end_headers() + if self.command != 'HEAD': + self.wfile.write(b'hello') + + do_HEAD = do_GET + + def log_message(self, *_): + pass + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--caddy', type=Path, required=True) + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix='snowglobe-zig-route-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Mirror) as backend: + root = Path(temporary) + stages = root / 'stages' + stages.mkdir() + paired = 'copyparty-preview-1234abcd' + mirror = 'zig-mirror-preview-1234abcd' + (stages / (paired + '.json')).write_text(json.dumps({'sourceId': 'copyparty', 'mirrorStage': mirror})) + (stages / (mirror + '.json')).write_text(json.dumps({'sourceId': 'zig-mirror'})) + routes = {(80, 'mirror.localhost'): {'services': {'zig-mirror'}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}, + (80, 'preview.localhost'): {'services': {mirror}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}} + with socket.socket() as listener: + listener.bind(('127.0.0.1', 0)) + port = listener.getsockname()[1] + lines = ['{', ' admin off', ' auto_https off', '}'] + with patch.dict(os.environ, {'SNOWGLOBE_STATE': temporary}): + for host, service in [('prod.localhost', 'copyparty'), ('paired.localhost', paired), ('unpaired.localhost', 'copyparty-preview-ffffffff')]: + lines += [f'http://{host}:{port} {{', *router.zig_mirror_routes(service, routes), ' handle {', ' respond 204', ' }', '}'] + conf = root / 'Caddyfile' + conf.write_text('\n'.join(lines)) + validated = subprocess.run([str(args.caddy), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) + assert validated.returncode == 0, validated.stderr + threading.Thread(target=backend.serve_forever, daemon=True).start() + with (root / 'caddy.log').open('w') as log: + caddy = subprocess.Popen([str(args.caddy), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=log, stderr=log) + try: + def request(host, path, method='GET', **headers): + connection = HTTPConnection('127.0.0.1', port, timeout=3) + connection.request(method, path, headers={'Host': host + ':' + str(port), **headers}) + response = connection.getresponse() + result = response.status, response.read() + connection.close() + return result + deadline = time.monotonic() + 10 + while True: + try: + request('prod.localhost', '/') + break + except OSError: + assert time.monotonic() < deadline and caddy.poll() is None + time.sleep(.05) + archive = '/mirrors/zig/zig-x86_64-linux-0.16.0-dev.123+abc123.tar.xz' + for host in ['prod.localhost', 'paired.localhost']: + assert request(host, archive + '?download=1', Range='bytes=0-4', Cookie='secret=do-not-forward', Authorization='private') == (206, b'hello') + method, path, headers = Mirror.requests[-1] + assert path == archive + '?download=1' + assert 'Cookie' not in headers and 'Authorization' not in headers + assert request(host, archive, method='HEAD') == (200, b'') + for path in ['/mirrors/zig/', '/mirrors/zig/releases/', '/mirrors/zig/dev/', '/mirrors/zig/releases/zig-0.0.0.tar.xz']: + assert request(host, path) == (204, b'') + assert request(host, archive, method='POST') == (204, b'') + count = len(Mirror.requests) + assert request('unpaired.localhost', archive) == (503, b'') + assert len(Mirror.requests) == count + finally: + caddy.terminate() + caddy.wait(timeout=10) + backend.shutdown() + print('Zig archive routes: production, explicitly paired preview, absent pairing, methods, ranges, query, and nested browse passed') + + +if __name__ == '__main__': + main() diff --git a/tools/router.py b/tools/router.py index 26be8c9d00e55ee2666dc48402fcf21b91083870..d99d06f2279c8c1eda5afde82d7a15e2a709b319 100644 --- a/tools/router.py +++ b/tools/router.py @@ -99,6 +99,127 @@ def shale_git_routes(upstreams): return [*lines, " }", " }"] +def zig_mirror_routes(service, routes): + if service == "copyparty": + mirror = "zig-mirror" + else: + state = os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe") + stages = os.path.join(state, "stages") + try: + with open(os.path.join(stages, service + ".json")) as source: + stage = json.load(source) + mirror = stage.get("mirrorStage", "") + if stage.get("sourceId") != "copyparty" or not isinstance(mirror, str) or not re.fullmatch(r"zig-mirror-preview-[0-9a-f]{8}", mirror): + mirror = None + if mirror: + with open(os.path.join(stages, mirror + ".json")) as source: + if json.load(source).get("sourceId") != "zig-mirror": + mirror = None + except FileNotFoundError: + mirror = None + upstreams = set().union(*(route["upstreams"] for route in routes.values() if mirror in route["services"])) + pattern = r"^/mirrors/zig/zig(?:|-bootstrap|-[a-zA-Z0-9_]+-[a-zA-Z0-9_]+)-([0-9]+\.[0-9]+\.[0-9]+(?:-dev\.[0-9]+\+[0-9a-f]+)?)\.(?:tar\.xz|zip)(?:\.minisig)?$" + lines = [" @zig_mirror {", " method GET HEAD", f" path_regexp zig_mirror {pattern}", " }", + " handle @zig_mirror {"] + if upstreams: + if any(not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream) for upstream in upstreams): + raise ValueError("Zig mirror requires private loopback upstreams") + lines += [" request_header -Authorization", " request_header -Cookie", + *proxy(" ".join(sorted(upstreams)), " ")] + else: + lines.append(" respond 503") + return [*lines, " }"] + + +def dashboard_preview_proof(service): + if not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", service): + return None + stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") + try: + with open(os.path.join(stages, service + ".json")) as source: + stage = json.load(source) + mount = stage.get("mount", "") + if stage.get("sourceId") != "dashboard" or not os.path.isabs(mount) or os.path.basename(mount) != service or os.path.basename(os.path.dirname(mount)) != "staging": + return None + path = os.path.join(mount, "proxy.token") + if os.path.islink(path) or os.stat(path).st_mode & 0o077: + return None + with open(path) as source: + proof = source.read().strip() + except FileNotFoundError: + return None + if not re.fullmatch(r"[0-9a-f]{64}", proof): + raise ValueError("invalid dashboard preview proxy proof") + return proof + + +def jellyfin_gateway(service, routes, proof): + if service == "jellyfin": + port = int(os.environ["SNOWGLOBE_DASHBOARD_PORT"]) + if not 1 <= port <= 65535: + raise ValueError("invalid dashboard port for Jellyfin") + return f"127.0.0.1:{port}", proof + stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") + try: + with open(os.path.join(stages, service + ".json")) as source: + stage = json.load(source) + gateway = stage.get("gatewayStage", "") + if stage.get("sourceId") != "jellyfin" or not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway): + return None, None + with open(os.path.join(stages, gateway + ".json")) as source: + if json.load(source).get("sourceId") != "dashboard": + return None, None + except FileNotFoundError: + return None, None + upstreams = set().union(*(route["upstreams"] for route in routes.values() if gateway in route["services"])) + if len(upstreams) != 1: + return None, None + upstream = next(iter(upstreams)) + if not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream): + raise ValueError("Jellyfin preview gateway must bind loopback") + proof = dashboard_preview_proof(gateway) + return (upstream, proof) if proof else (None, None) + + +def jellyfin_routes(service, upstreams, gateway, proof): + if len(upstreams) != 1 or not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", next(iter(upstreams))): + raise ValueError("Jellyfin requires one private loopback upstream") + upstream = next(iter(upstreams)) + trusted = [f" header_up Snowglobe-Proxy-Token {proof}" if proof else " header_up -Snowglobe-Proxy-Token", + f" header_up Snowglobe-Jellyfin-Service {service}", + f" header_up Snowglobe-Jellyfin-Upstream {upstream}", + " header_up Snowglobe-Jellyfin-Uri {http.vars.jellyfin_uri}", + " header_up Snowglobe-Jellyfin-Method {http.vars.jellyfin_method}", + " header_up X-Snowglobe-Client-IP {remote_host}"] + lines = [" route {", " vars jellyfin_uri {uri}", " vars jellyfin_method {method}", + " request_header -Snowglobe-Jellyfin-*", " request_header -Snowglobe-Proxy-Token", + " request_header -X-Snowglobe-Jellyfin-*", " request_header -User-Name", " request_header -User-Groups", + " @jellyfin_private path_regexp jellyfin_private (?i)^/(?:snowglobe(?:/|$)|metrics(?:/|$)|users/(?:authenticatebyname|[0-9a-f-]+/authenticate|forgotpassword(?:/pin)?)(?:/|$)|quickconnect/authorize(?:/|$))", + " respond @jellyfin_private 404"] + paths = { + "GET": "/System/Info/Public /System/Ping /Users/Public /Branding/Configuration /QuickConnect/Enabled /QuickConnect/Connect /auth/jellyfin/start /auth/jellyfin/callback", + "POST": "/System/Ping /QuickConnect/Initiate /Users/AuthenticateWithQuickConnect /auth/jellyfin/session /auth/jellyfin/logout", + } + for method, allowed in paths.items(): + name = "@jellyfin_public_" + method.lower() + lines += [f" {name} {{", f" method {method}", f" path {allowed}", " }", + f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", + *[" " + line for line in trusted], " }", " }"] + lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", + f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", + *[" " + line for line in trusted], " }", " }", " handle {", + f" reverse_proxy {gateway} {{", " method GET", " rewrite /auth/jellyfin/check?", + *[" " + line for line in trusted], " @jellyfin_allowed status 204", " handle_response @jellyfin_allowed {", + " header +Set-Cookie {rp.header.Set-Cookie}", + " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", + " request_header -X-Emby-Authorization", " request_header -Cookie", + " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", + " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}", f" reverse_proxy {upstream} {{", + " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", + " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] + return lines + + def render(token): with open(os.environ["SNOWGLOBE_PROXY_TOKEN_FILE"]) as file: dashboard_proof = file.read().strip() @@ -150,7 +271,11 @@ def render(token): host = tag.split("=", 1)[1] if not HOST.fullmatch(host): raise ValueError(f"invalid Caddy host: {host!r}") - route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False}) + route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False, "jellyfinAuth": False}) + if "caddy-jellyfin-auth=true" in tags: + if not (instance["ServiceName"] == "jellyfin" or re.fullmatch(r"jellyfin-preview-[0-9a-f]{8}", instance["ServiceName"])): + raise ValueError("Jellyfin gateway tag belongs to another service") + route["jellyfinAuth"] = True if access and route["authRole"] not in (None, access[0]): raise ValueError("conflicting route access roles") if user_headers and route["userHeader"] not in (None, user_headers[0]): @@ -214,8 +339,18 @@ def render(token): if not 1 <= port <= 65535: raise ValueError("invalid dashboard port for Shale linking") lines += shale_mcp_routes(port) + if route["jellyfinAuth"]: + gateway, proof = jellyfin_gateway(service, routes, dashboard_proof) + if gateway: + lines += jellyfin_routes(service, route["upstreams"], gateway, proof) + else: + lines.append(" respond 503") + lines.append("}") + continue lines += [" tracing {", f" span {service}", " span_attributes {", f" snowglobe.service {service}", " snowglobe.kind edge", " }", " }"] + if (service == "copyparty" and "zig-mirror" in internal_services) or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service): + lines += zig_mirror_routes(service, routes) manifest = os.path.join(ROUTE_DIR, service + ".json") assets = {} headers = {} @@ -304,6 +439,9 @@ def render(token): f" request_header -{scrub[0]}", " }", " }", *proxy(upstreams, " "), " }", "}"] else: + preview_proof = dashboard_preview_proof(service) + if preview_proof: + lines.append(f" request_header Snowglobe-Proxy-Token {preview_proof}") page_upstream = upstreams shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) page_headers = [] diff --git a/tools/snowglobe.py b/tools/snowglobe.py index 7f6397de3a70a367a3b73afe0811731383d638a8..6b4a5e21dc17550b9623714507993dbacf51bbf7 100644 --- a/tools/snowglobe.py +++ b/tools/snowglobe.py @@ -348,6 +348,8 @@ def render(data, definitions): tags.append("caddy-user-header=" + endpoint["userHeader"]) if endpoint["forwardRealIp"]: tags.append("caddy-real-ip=true") + if endpoint.get("jellyfinAuth"): + tags.append("caddy-jellyfin-auth=true") if endpoint.get("secureCookies"): tags.append("caddy-secure-cookies=true") if endpoint["tlsInternal"]: @@ -1225,6 +1227,7 @@ def main(): check_http(hostname, http[0]["checkPath"], http[0]["tlsInternal"]) configure(data) if REPO.parent == Path("/opt/snowglobe/releases").resolve(): + metadata = {**json.loads((stage_dir / f"{stage}.json").read_text()), **metadata} metadata["ready"] = True metadata["overrides"] = [assignment.partition("=")[0] for assignment in args.env] write_private(stage_dir / f"{stage}.json", json.dumps(metadata)) -- 2.54.1