| author | |
| committer | |
| log | b2655181e9b34cb6018cb3105a88bf703cfdfd15 |
| tree | bbbc7396b4bff94396c80fcbfd1867e33f7226ad |
| parent | 736bb2fee51197e82d7d5ded84c99a5b5b12a2b0 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
7 files changed, 71 insertions(+), 366 deletions(-)
config/Service.pkl-2| ... | @@ -19,8 +19,6 @@ class Http { | ... | @@ -19,8 +19,6 @@ class Http { |
| 19 | forwardRealIp: Boolean = false | 19 | forwardRealIp: Boolean = false |
| 20 | /// Require HTTPS when browsers send cookies issued by this route. | 20 | /// Require HTTPS when browsers send cookies issued by this route. |
| 21 | secureCookies: Boolean = false | 21 | secureCookies: Boolean = false |
| 22 | /// Require the native Snow sign-in gateway before Jellyfin handles requests. | ||
| 23 | jellyfinAuth: Boolean = false | ||
| 24 | /// Generated secret whose value becomes the private proxy header name. | 22 | /// Generated secret whose value becomes the private proxy header name. |
| 25 | identityProofSecret: String? | 23 | identityProofSecret: String? |
| 26 | /// Additional hosts routed to this container without SSO headers. | 24 | /// Additional hosts routed to this container without SSO headers. |
tools/deploy-test.py+1-1| ... | @@ -40,7 +40,7 @@ class MainExport(unittest.TestCase): | ... | @@ -40,7 +40,7 @@ class MainExport(unittest.TestCase): |
| 40 | subprocess.run(["jj", "new"], cwd=repo, check=True, capture_output=True) | 40 | subprocess.run(["jj", "new"], cwd=repo, check=True, capture_output=True) |
| 41 | (repo / "service/fixture").write_text("unfinished") | 41 | (repo / "service/fixture").write_text("unfinished") |
| 42 | (repo / "service/new").write_text("new working file") | 42 | (repo / "service/new").write_text("new working file") |
| 43 | generated = repo / "service/jellyfin/bridge/tests/bin/Release/net10.0/fixture.dll" | 43 | generated = repo / "guest/build/fixture" |
| 44 | generated.parent.mkdir(parents=True) | 44 | generated.parent.mkdir(parents=True) |
| 45 | digest = release.tree_digest(repo) | 45 | digest = release.tree_digest(repo) |
| 46 | generated.write_bytes(b"compiled fixture") | 46 | generated.write_bytes(b"compiled fixture") |
tools/release.py+1-2| ... | @@ -19,8 +19,7 @@ RELEASES = ROOT / "releases" | ... | @@ -19,8 +19,7 @@ RELEASES = ROOT / "releases" |
| 19 | STATE = Path("/var/lib/snowglobe") | 19 | STATE = Path("/var/lib/snowglobe") |
| 20 | HISTORY = STATE / "deployments.json" | 20 | HISTORY = STATE / "deployments.json" |
| 21 | SOURCES = ("config", "service", "tools", "nixos", "dashboard", "guest", "flake.nix", "flake.lock", "readme.md", "AGENTS.md") | 21 | SOURCES = ("config", "service", "tools", "nixos", "dashboard", "guest", "flake.nix", "flake.lock", "readme.md", "AGENTS.md") |
| 22 | BUILD_DIRS = ("dashboard/node_modules", "dashboard/dist", "dashboard/.cache", "dashboard/data", "dashboard/target", "guest/build", | 22 | BUILD_DIRS = ("dashboard/node_modules", "dashboard/dist", "dashboard/.cache", "dashboard/data", "dashboard/target", "guest/build") |
| 23 | "service/jellyfin/bridge/bin", "service/jellyfin/bridge/obj", "service/jellyfin/bridge/tests/bin", "service/jellyfin/bridge/tests/obj") | ||
| 24 | RELEASE_ID = re.compile(r"[0-9a-f]{16}\Z") | 23 | RELEASE_ID = re.compile(r"[0-9a-f]{16}\Z") |
| 25 | STAGE_ID = re.compile(r"[a-z][a-z0-9-]*\Z") | 24 | STAGE_ID = re.compile(r"[a-z][a-z0-9-]*\Z") |
| 26 | 25 |
tools/router-jellyfin-test.py deleted-300| ... | @@ -1,300 +0,0 @@ | ||
| 1 | #!/usr/bin/env python3 | ||
| 2 | import argparse | ||
| 3 | from http.client import HTTPConnection | ||
| 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | ||
| 5 | import json | ||
| 6 | import os | ||
| 7 | import socket | ||
| 8 | import subprocess | ||
| 9 | import tempfile | ||
| 10 | import threading | ||
| 11 | import time | ||
| 12 | from pathlib import Path | ||
| 13 | import unittest | ||
| 14 | import urllib.parse | ||
| 15 | from unittest.mock import patch | ||
| 16 | |||
| 17 | import router | ||
| 18 | |||
| 19 | |||
| 20 | class Backend(BaseHTTPRequestHandler): | ||
| 21 | requests = [] | ||
| 22 | bodies = [] | ||
| 23 | |||
| 24 | def do_GET(self): | ||
| 25 | type(self).requests.append((self.command, self.path, dict(self.headers))) | ||
| 26 | type(self).bodies.append(self.rfile.read(int(self.headers.get('Content-Length', '0')))) | ||
| 27 | if self.headers.get('Upgrade', '').lower() == 'websocket': | ||
| 28 | self.send_response(101) | ||
| 29 | self.send_header('Connection', 'Upgrade') | ||
| 30 | self.send_header('Upgrade', 'websocket') | ||
| 31 | self.end_headers() | ||
| 32 | return | ||
| 33 | self.send_response(206 if self.headers.get('Range') else 200) | ||
| 34 | self.send_header('Content-Type', 'application/octet-stream') | ||
| 35 | self.send_header('ETag', '"fixture"') | ||
| 36 | self.send_header('Content-Length', '5') | ||
| 37 | if self.headers.get('Range'): | ||
| 38 | self.send_header('Content-Range', 'bytes 0-4/10') | ||
| 39 | self.end_headers() | ||
| 40 | if self.command != 'HEAD': | ||
| 41 | self.wfile.write(b'hello') | ||
| 42 | |||
| 43 | do_HEAD = do_GET | ||
| 44 | do_POST = do_GET | ||
| 45 | |||
| 46 | def log_message(self, *_): | ||
| 47 | pass | ||
| 48 | |||
| 49 | |||
| 50 | class Gateway(BaseHTTPRequestHandler): | ||
| 51 | requests = [] | ||
| 52 | |||
| 53 | def do_GET(self): | ||
| 54 | size = int(self.headers.get('Content-Length', '0')) | ||
| 55 | body = self.rfile.read(size) | ||
| 56 | type(self).requests.append((self.command, self.path, dict(self.headers), body)) | ||
| 57 | trusted = self.headers.get('Snowglobe-Proxy-Token') == 'a' * 64 | ||
| 58 | original = self.headers.get('Snowglobe-Jellyfin-Uri', '') | ||
| 59 | if not trusted: | ||
| 60 | self.send_error(403) | ||
| 61 | elif self.path == '/auth/jellyfin/public': | ||
| 62 | self.send_response(200) | ||
| 63 | self.send_header('Content-Type', 'application/json') | ||
| 64 | self.end_headers() | ||
| 65 | self.wfile.write(json.dumps({'method': self.headers.get('Snowglobe-Jellyfin-Method'), 'body': body.decode()}).encode()) | ||
| 66 | elif self.headers.get('Authorization') == 'allow' or original.startswith('/Items/' + '1' * 32 + '/Images/Primary'): | ||
| 67 | self.send_response(204) | ||
| 68 | path, _, query = original.partition('?') | ||
| 69 | kept = [part for part in query.split('&') if part and urllib.parse.unquote_plus(part.split('=', 1)[0]).lower() not in {'apikey', 'api_key'}] | ||
| 70 | self.send_header('X-Snowglobe-Jellyfin-Uri', path + ('?' + '&'.join(kept) if kept else '')) | ||
| 71 | if self.headers.get('Authorization'): | ||
| 72 | self.send_header('X-Snowglobe-Jellyfin-Authorization', 'MediaBrowser Token="normalized"') | ||
| 73 | self.send_header('Set-Cookie', '__Host-snow-jellyfin=normalized; HttpOnly; Secure; Path=/') | ||
| 74 | self.end_headers() | ||
| 75 | else: | ||
| 76 | self.send_error(401) | ||
| 77 | |||
| 78 | do_POST = do_GET | ||
| 79 | do_OPTIONS = do_GET | ||
| 80 | |||
| 81 | def log_message(self, *_): | ||
| 82 | pass | ||
| 83 | |||
| 84 | |||
| 85 | class GatewayAssociation(unittest.TestCase): | ||
| 86 | def test_only_tagged_preview_uses_its_own_gateway(self): | ||
| 87 | with tempfile.TemporaryDirectory(prefix='jellyfin-gateway-') as temporary: | ||
| 88 | root = Path(temporary) | ||
| 89 | stages = root / 'stages' | ||
| 90 | stages.mkdir() | ||
| 91 | jellyfin = 'jellyfin-preview-1234abcd' | ||
| 92 | shale = 'shale-preview-5678abcd' | ||
| 93 | dashboard = 'dashboard-preview-1234abcd' | ||
| 94 | (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) | ||
| 95 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale', 'gatewayStage': dashboard})) | ||
| 96 | mount = root / 'staging' / dashboard | ||
| 97 | mount.mkdir(parents=True) | ||
| 98 | proxy_proof = mount / 'proxy.token' | ||
| 99 | proxy_proof.write_text('b' * 64) | ||
| 100 | proxy_proof.chmod(0o400) | ||
| 101 | (stages / (dashboard + '.json')).write_text(json.dumps({'sourceId': 'dashboard', 'mount': str(mount)})) | ||
| 102 | proof = root / 'proof' | ||
| 103 | proof.write_text('a' * 64) | ||
| 104 | services = { | ||
| 105 | 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), | ||
| 106 | 'copyparty': (1112, ['caddy-host=file.paperclover.net']), | ||
| 107 | jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), | ||
| 108 | shale: (6666, ['caddy-host=' + shale + '.paperclover.net']), | ||
| 109 | dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), | ||
| 110 | } | ||
| 111 | |||
| 112 | def nomad(path, _): | ||
| 113 | if path == '/v1/services': | ||
| 114 | return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in services]}] | ||
| 115 | if path.startswith('/v1/service/'): | ||
| 116 | name = path.rsplit('/', 1)[1] | ||
| 117 | port, tags = services[name] | ||
| 118 | return [{'ServiceName': name, 'JobID': name, 'AllocID': name, 'Address': '127.0.0.1', 'Port': port, 'Tags': tags}] | ||
| 119 | return {'http': {'Status': 'success'}} | ||
| 120 | |||
| 121 | environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '4444', | ||
| 122 | 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'SNOWGLOBE_STATE': temporary} | ||
| 123 | with patch.dict(os.environ, environment, clear=True), patch.object(router, 'nomad', nomad), patch.object(router, 'ROUTE_DIR', str(root / 'routes')): | ||
| 124 | rendered = router.render('fixture') | ||
| 125 | legacy = rendered.split('jelly.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 126 | preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 127 | self.assertNotIn('/auth/jellyfin/', legacy) | ||
| 128 | self.assertIn('reverse_proxy 127.0.0.1:1111', legacy) | ||
| 129 | self.assertIn('reverse_proxy 127.0.0.1:3333', preview) | ||
| 130 | self.assertNotIn('reverse_proxy 127.0.0.1:4444', preview) | ||
| 131 | self.assertNotIn('a' * 64, preview) | ||
| 132 | self.assertIn('b' * 64, preview) | ||
| 133 | dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 134 | self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) | ||
| 135 | self.assertNotIn('tracing', preview) | ||
| 136 | shale_route = rendered.split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 137 | self.assertIn('reverse_proxy 127.0.0.1:3333', shale_route) | ||
| 138 | self.assertIn('Snowglobe-Shale-Upstream "127.0.0.1:6666"', shale_route) | ||
| 139 | self.assertIn('b' * 64, shale_route) | ||
| 140 | self.assertNotIn('a' * 64, shale_route) | ||
| 141 | self.assertNotIn('reverse_proxy 127.0.0.1:4444', shale_route) | ||
| 142 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) | ||
| 143 | denied = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 144 | self.assertIn('respond 503', denied) | ||
| 145 | self.assertNotIn('/auth/shale/page', denied) | ||
| 146 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale'})) | ||
| 147 | unbound = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 148 | self.assertIn('reverse_proxy 127.0.0.1:4444', unbound) | ||
| 149 | self.assertIn('a' * 64, unbound) | ||
| 150 | files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 151 | self.assertIn('handle @mirror_private {\n respond 404', files) | ||
| 152 | self.assertIn('handle @zig_mirror {\n respond 503', files) | ||
| 153 | services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) | ||
| 154 | enabled = router.render('fixture') | ||
| 155 | files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 156 | self.assertIn('reverse_proxy 127.0.0.1:5555', files) | ||
| 157 | (stages / (jellyfin + '.json')).unlink() | ||
| 158 | rendered = router.render('fixture') | ||
| 159 | preview = rendered.split(jellyfin + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 160 | self.assertIn('respond 503', preview) | ||
| 161 | self.assertNotIn('reverse_proxy', preview) | ||
| 162 | |||
| 163 | |||
| 164 | class JellyfinRouter(unittest.TestCase): | ||
| 165 | @classmethod | ||
| 166 | def setUpClass(cls): | ||
| 167 | cls.root = tempfile.TemporaryDirectory(prefix='snowglobe-jellyfin-router-') | ||
| 168 | cls.backend = ThreadingHTTPServer(('127.0.0.1', 0), Backend) | ||
| 169 | cls.gateway = ThreadingHTTPServer(('127.0.0.1', 0), Gateway) | ||
| 170 | for server in (cls.backend, cls.gateway): | ||
| 171 | threading.Thread(target=server.serve_forever, daemon=True).start() | ||
| 172 | with socket.socket() as listener: | ||
| 173 | listener.bind(('127.0.0.1', 0)) | ||
| 174 | cls.port = listener.getsockname()[1] | ||
| 175 | conf = Path(cls.root.name) / 'Caddyfile' | ||
| 176 | 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)) | ||
| 177 | conf.write_text('{\n admin off\n auto_https off\n}\nhttp://127.0.0.1:' + str(cls.port) + ' {\n' + route + '\n}\n') | ||
| 178 | validated = subprocess.run([str(CADDY), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) | ||
| 179 | if validated.returncode: | ||
| 180 | raise RuntimeError(validated.stderr) | ||
| 181 | |||
| 182 | cls.log = (Path(cls.root.name) / 'caddy.log').open('w+') | ||
| 183 | cls.caddy = subprocess.Popen([str(CADDY), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=cls.log, stderr=cls.log) | ||
| 184 | deadline = time.monotonic() + 10 | ||
| 185 | while True: | ||
| 186 | try: | ||
| 187 | cls.request('GET', '/ready') | ||
| 188 | break | ||
| 189 | except OSError: | ||
| 190 | if time.monotonic() > deadline or cls.caddy.poll() is not None: | ||
| 191 | cls.log.seek(0) | ||
| 192 | raise RuntimeError(cls.log.read()) | ||
| 193 | time.sleep(.05) | ||
| 194 | |||
| 195 | @classmethod | ||
| 196 | def tearDownClass(cls): | ||
| 197 | cls.caddy.terminate() | ||
| 198 | cls.caddy.wait(timeout=10) | ||
| 199 | cls.backend.shutdown() | ||
| 200 | cls.gateway.shutdown() | ||
| 201 | cls.backend.server_close() | ||
| 202 | cls.gateway.server_close() | ||
| 203 | cls.log.close() | ||
| 204 | cls.root.cleanup() | ||
| 205 | |||
| 206 | @classmethod | ||
| 207 | def request(cls, method, path, body=None, headers=None): | ||
| 208 | connection = HTTPConnection('127.0.0.1', cls.port, timeout=3) | ||
| 209 | connection.request(method, path, body=body, headers=headers or {}) | ||
| 210 | response = connection.getresponse() | ||
| 211 | result = response.status, dict(response.getheaders()), response.read() | ||
| 212 | connection.close() | ||
| 213 | return result | ||
| 214 | |||
| 215 | def setUp(self): | ||
| 216 | Backend.requests.clear() | ||
| 217 | Gateway.requests.clear() | ||
| 218 | |||
| 219 | def test_default_deny_includes_theme_assets_and_fallback(self): | ||
| 220 | for path in ['/web/', '/web/extra/cloverfin.css', '/favicon.ico', '/Items/known', '/Videos/x/stream', '/socket']: | ||
| 221 | with self.subTest(path=path): | ||
| 222 | self.assertEqual(self.request('GET', path, headers={'Snowglobe-Proxy-Token': 'forged', 'Snowglobe-Jellyfin-Service': 'jellyfin'})[0], 401) | ||
| 223 | self.assertEqual(Backend.requests, []) | ||
| 224 | self.assertTrue(all(r[2]['Snowglobe-Jellyfin-Service'] == 'jellyfin-preview-1234abcd' for r in Gateway.requests)) | ||
| 225 | count = len(Gateway.requests) | ||
| 226 | for path in ['/Snowglobe/Bridge', '/sNoWgLoBe/Bridge', '/metrics', '/Users/AuthenticateByName', '/Users/' + '1' * 32 + '/Authenticate', '/Users/ForgotPassword/Pin', '/QuickConnect/Authorize']: | ||
| 227 | self.assertEqual(self.request('POST', path, body='{}', headers={'Authorization': 'allow'})[0], 404) | ||
| 228 | self.assertEqual(len(Gateway.requests), count) | ||
| 229 | self.assertEqual(Backend.requests, []) | ||
| 230 | |||
| 231 | def test_public_protocol_preserves_method_body_and_uri(self): | ||
| 232 | body = '{"DeviceId":"fixture"}' | ||
| 233 | status, _, payload = self.request('POST', '/QuickConnect/Initiate?fixture=1', body=body) | ||
| 234 | self.assertEqual(status, 200) | ||
| 235 | self.assertEqual(json.loads(payload), {'method': 'POST', 'body': body}) | ||
| 236 | self.assertEqual(Gateway.requests[-1][2]['Snowglobe-Jellyfin-Uri'], '/QuickConnect/Initiate?fixture=1') | ||
| 237 | self.assertEqual(Backend.requests, []) | ||
| 238 | self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) | ||
| 239 | |||
| 240 | def test_legacy_public_route_case_keeps_exact_method_and_path_boundary(self): | ||
| 241 | for path in ['/users/public', '/branding/configuration', '/qUiCkCoNnEcT/eNaBlEd']: | ||
| 242 | self.assertEqual(self.request('GET', path)[0], 200) | ||
| 243 | self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/public') | ||
| 244 | self.assertEqual(self.request('POST', '/quickconnect/initiate', body='{}')[0], 200) | ||
| 245 | for path in ['/users/public/extra', '/quickconnect/initiate/extra', '/users/private', '/items/known']: | ||
| 246 | self.assertEqual(self.request('GET', path)[0], 401) | ||
| 247 | self.assertEqual(self.request('POST', '/users/public', body='{}')[0], 401) | ||
| 248 | self.assertEqual(self.request('POST', '/qUiCkCoNnEcT/aUtHoRiZe', body='{}')[0], 404) | ||
| 249 | self.assertEqual(Backend.requests, []) | ||
| 250 | |||
| 251 | def test_authenticated_ranges_head_websocket_and_normalized_credential(self): | ||
| 252 | headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} | ||
| 253 | 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'}) | ||
| 254 | self.assertEqual((status, body), (206, b'hello')) | ||
| 255 | self.assertEqual(response_headers['Content-Range'], 'bytes 0-4/10') | ||
| 256 | self.assertEqual({k.lower(): v for k, v in response_headers.items()}['etag'], '"fixture"') | ||
| 257 | self.assertIn('HttpOnly', response_headers['Set-Cookie']) | ||
| 258 | method, path, upstream = Backend.requests[-1] | ||
| 259 | self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) | ||
| 260 | self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') | ||
| 261 | self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) | ||
| 262 | for path, expected in [ | ||
| 263 | ('/Videos/fixture/stream?ApiKey=old', '/Videos/fixture/stream'), | ||
| 264 | ('/Videos/fixture/stream?api_key=old&static=true&signature=abc%3D%3D&name=a%26b', '/Videos/fixture/stream?static=true&signature=abc%3D%3D&name=a%26b'), | ||
| 265 | ]: | ||
| 266 | self.assertEqual(self.request('GET', path, headers=headers)[0], 200) | ||
| 267 | self.assertEqual(Backend.requests[-1][1], expected) | ||
| 268 | status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) | ||
| 269 | self.assertEqual((status, body), (200, b'')) | ||
| 270 | self.assertEqual(Backend.requests[-1][0], 'HEAD') | ||
| 271 | self.assertEqual(self.request('POST', '/Items/fixture/PlaybackInfo', body='synthetic playback options', headers=headers)[0], 200) | ||
| 272 | self.assertEqual(Backend.requests[-1][0], 'POST') | ||
| 273 | self.assertEqual(Backend.bodies[-1], b'synthetic playback options') | ||
| 274 | self.assertEqual(self.request('GET', '/socket', headers={**headers, 'Connection': 'Upgrade', 'Upgrade': 'websocket'})[0], 101) | ||
| 275 | self.assertEqual(Backend.requests[-1][2]['Upgrade'], 'websocket') | ||
| 276 | |||
| 277 | def test_narrow_artwork_still_passes_gateway_check(self): | ||
| 278 | self.assertEqual(self.request('GET', '/Items/' + '1' * 32 + '/Images/Primary?maxWidth=300')[0], 200) | ||
| 279 | self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/check') | ||
| 280 | self.assertEqual(len(Backend.requests), 1) | ||
| 281 | |||
| 282 | def test_z_upstream_failure_does_not_log_native_credentials(self): | ||
| 283 | self.gateway.shutdown() | ||
| 284 | self.gateway.server_close() | ||
| 285 | secret = 'SYNTHETIC-PAIRING-SECRET' | ||
| 286 | self.assertEqual(self.request('GET', '/QuickConnect/Connect?secret=' + secret, | ||
| 287 | headers={'X-Emby-Token': 'SYNTHETIC-NATIVE-TOKEN'})[0], 503) | ||
| 288 | self.log.flush() | ||
| 289 | self.log.seek(0) | ||
| 290 | log = self.log.read() | ||
| 291 | self.assertNotIn(secret, log) | ||
| 292 | self.assertNotIn('SYNTHETIC-NATIVE-TOKEN', log) | ||
| 293 | |||
| 294 | |||
| 295 | if __name__ == '__main__': | ||
| 296 | parser = argparse.ArgumentParser() | ||
| 297 | parser.add_argument('--caddy', type=Path, required=True) | ||
| 298 | args, remaining = parser.parse_known_args() | ||
| 299 | CADDY = args.caddy | ||
| 300 | unittest.main(argv=[__file__, *remaining]) | ||
tools/router-shale-gateway-test.py created+65| ... | @@ -0,0 +1,65 @@ | ||
| 1 | import json | ||
| 2 | import os | ||
| 3 | from pathlib import Path | ||
| 4 | import tempfile | ||
| 5 | import unittest | ||
| 6 | from unittest.mock import patch | ||
| 7 | |||
| 8 | import router | ||
| 9 | |||
| 10 | |||
| 11 | class GatewayAssociation(unittest.TestCase): | ||
| 12 | def test_preview_uses_its_own_gateway(self): | ||
| 13 | with tempfile.TemporaryDirectory(prefix='shale-gateway-') as temporary: | ||
| 14 | root = Path(temporary) | ||
| 15 | stages = root / 'stages' | ||
| 16 | stages.mkdir() | ||
| 17 | shale = 'shale-preview-5678abcd' | ||
| 18 | dashboard = 'dashboard-preview-1234abcd' | ||
| 19 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale', 'gatewayStage': dashboard})) | ||
| 20 | mount = root / 'staging' / dashboard | ||
| 21 | mount.mkdir(parents=True) | ||
| 22 | proxy_proof = mount / 'proxy.token' | ||
| 23 | proxy_proof.write_text('b' * 64) | ||
| 24 | proxy_proof.chmod(0o400) | ||
| 25 | (stages / (dashboard + '.json')).write_text(json.dumps({'sourceId': 'dashboard', 'mount': str(mount)})) | ||
| 26 | proof = root / 'proof' | ||
| 27 | proof.write_text('a' * 64) | ||
| 28 | services = { | ||
| 29 | shale: (6666, ['caddy-host=' + shale + '.paperclover.net']), | ||
| 30 | dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), | ||
| 31 | } | ||
| 32 | |||
| 33 | def nomad(path, _): | ||
| 34 | if path == '/v1/services': | ||
| 35 | return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in services]}] | ||
| 36 | if path.startswith('/v1/service/'): | ||
| 37 | name = path.rsplit('/', 1)[1] | ||
| 38 | port, tags = services[name] | ||
| 39 | return [{'ServiceName': name, 'JobID': name, 'AllocID': name, 'Address': '127.0.0.1', 'Port': port, 'Tags': tags}] | ||
| 40 | return {'http': {'Status': 'success'}} | ||
| 41 | |||
| 42 | environment = {'SNOWGLOBE_DOMAIN': 'paperclover.net', 'SNOWGLOBE_DASHBOARD_PORT': '4444', | ||
| 43 | 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'SNOWGLOBE_STATE': temporary} | ||
| 44 | with patch.dict(os.environ, environment, clear=True), patch.object(router, 'nomad', nomad), patch.object(router, 'ROUTE_DIR', str(root / 'routes')): | ||
| 45 | rendered = router.render('fixture') | ||
| 46 | dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 47 | self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) | ||
| 48 | shale_route = rendered.split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 49 | self.assertIn('reverse_proxy 127.0.0.1:3333', shale_route) | ||
| 50 | self.assertIn('Snowglobe-Shale-Upstream "127.0.0.1:6666"', shale_route) | ||
| 51 | self.assertIn('b' * 64, shale_route) | ||
| 52 | self.assertNotIn('a' * 64, shale_route) | ||
| 53 | self.assertNotIn('reverse_proxy 127.0.0.1:4444', shale_route) | ||
| 54 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'wrong', 'gatewayStage': dashboard})) | ||
| 55 | denied = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 56 | self.assertIn('respond 503', denied) | ||
| 57 | self.assertNotIn('/auth/shale/page', denied) | ||
| 58 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale'})) | ||
| 59 | unbound = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 60 | self.assertIn('reverse_proxy 127.0.0.1:4444', unbound) | ||
| 61 | self.assertIn('a' * 64, unbound) | ||
| 62 | |||
| 63 | |||
| 64 | if __name__ == '__main__': | ||
| 65 | unittest.main() | ||
tools/router.py+4-59| ... | @@ -163,9 +163,9 @@ def dashboard_preview_proof(service): | ... | @@ -163,9 +163,9 @@ def dashboard_preview_proof(service): |
| 163 | return proof | 163 | return proof |
| 164 | 164 | ||
| 165 | 165 | ||
| 166 | def service_gateway(service, routes, proof): | 166 | def shale_gateway(service, routes, proof): |
| 167 | source = service.split("-preview-", 1)[0] | 167 | source = service.split("-preview-", 1)[0] |
| 168 | if source not in {"jellyfin", "shale"}: | 168 | if source != "shale": |
| 169 | return None, None | 169 | return None, None |
| 170 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") | 170 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") |
| 171 | gateway = None | 171 | gateway = None |
| ... | @@ -178,8 +178,6 @@ def service_gateway(service, routes, proof): | ... | @@ -178,8 +178,6 @@ def service_gateway(service, routes, proof): |
| 178 | if stage.get("sourceId") != source: | 178 | if stage.get("sourceId") != source: |
| 179 | return None, None | 179 | return None, None |
| 180 | gateway = stage.get("gatewayStage") | 180 | gateway = stage.get("gatewayStage") |
| 181 | if gateway is None and source != "shale": | ||
| 182 | return None, None | ||
| 183 | if gateway is not None and (not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway)): | 181 | if gateway is not None and (not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway)): |
| 184 | return None, None | 182 | return None, None |
| 185 | if gateway is None: | 183 | if gateway is None: |
| ... | @@ -203,47 +201,6 @@ def service_gateway(service, routes, proof): | ... | @@ -203,47 +201,6 @@ def service_gateway(service, routes, proof): |
| 203 | return (upstream, proof) if proof else (None, None) | 201 | return (upstream, proof) if proof else (None, None) |
| 204 | 202 | ||
| 205 | 203 | ||
| 206 | def jellyfin_routes(service, upstreams, gateway, proof): | ||
| 207 | if len(upstreams) != 1 or not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", next(iter(upstreams))): | ||
| 208 | raise ValueError("Jellyfin requires one private loopback upstream") | ||
| 209 | upstream = next(iter(upstreams)) | ||
| 210 | trusted = [f" header_up Snowglobe-Proxy-Token {proof}" if proof else " header_up -Snowglobe-Proxy-Token", | ||
| 211 | f" header_up Snowglobe-Jellyfin-Service {service}", | ||
| 212 | f" header_up Snowglobe-Jellyfin-Upstream {upstream}", | ||
| 213 | " header_up Snowglobe-Jellyfin-Uri {http.vars.jellyfin_uri}", | ||
| 214 | " header_up Snowglobe-Jellyfin-Method {http.vars.jellyfin_method}", | ||
| 215 | " header_up X-Snowglobe-Client-IP {remote_host}"] | ||
| 216 | lines = [" route {", " vars jellyfin_uri {uri}", " vars jellyfin_method {method}", | ||
| 217 | " request_header -Snowglobe-Jellyfin-*", " request_header -Snowglobe-Proxy-Token", | ||
| 218 | " request_header -X-Snowglobe-Jellyfin-*", " request_header -User-Name", " request_header -User-Groups", | ||
| 219 | " @jellyfin_private path_regexp jellyfin_private (?i)^/(?:snowglobe(?:/|$)|metrics(?:/|$)|users/(?:authenticatebyname|[0-9a-f-]+/authenticate|forgotpassword(?:/pin)?)(?:/|$)|quickconnect/authorize(?:/|$))", | ||
| 220 | " respond @jellyfin_private 404"] | ||
| 221 | paths = { | ||
| 222 | "GET": "/System/Info/Public /System/Ping /Users/Public /Branding/Configuration /QuickConnect/Enabled /QuickConnect/Connect /auth/jellyfin/start /auth/jellyfin/callback", | ||
| 223 | "POST": "/System/Ping /QuickConnect/Initiate /Users/AuthenticateWithQuickConnect /auth/jellyfin/session /auth/jellyfin/logout", | ||
| 224 | } | ||
| 225 | for method, allowed in paths.items(): | ||
| 226 | name = "@jellyfin_public_" + method.lower() | ||
| 227 | pattern = "(?i)^(?:" + "|".join(re.escape(path) for path in allowed.split()) + ")$" | ||
| 228 | lines += [f" {name} {{", f" method {method}", f" path_regexp {name[1:]} {pattern}", " }", | ||
| 229 | f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", | ||
| 230 | *[" " + line for line in trusted], " }", " }"] | ||
| 231 | lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", | ||
| 232 | f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", | ||
| 233 | *[" " + line for line in trusted], " }", " }", " handle {", | ||
| 234 | f" reverse_proxy {gateway} {{", " method GET", " rewrite /auth/jellyfin/check?", | ||
| 235 | *[" " + line for line in trusted], " @jellyfin_allowed status 204", " handle_response @jellyfin_allowed {", | ||
| 236 | " header +Set-Cookie {rp.header.Set-Cookie}", | ||
| 237 | " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", | ||
| 238 | " request_header -X-Emby-Authorization", " request_header -Cookie", | ||
| 239 | " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", | ||
| 240 | # Caddy needs a literal query delimiter to replace a query from a response header. | ||
| 241 | " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}?", f" reverse_proxy {upstream} {{", | ||
| 242 | " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", | ||
| 243 | " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] | ||
| 244 | return lines | ||
| 245 | |||
| 246 | |||
| 247 | def render(token): | 204 | def render(token): |
| 248 | with open(os.environ["SNOWGLOBE_PROXY_TOKEN_FILE"]) as file: | 205 | with open(os.environ["SNOWGLOBE_PROXY_TOKEN_FILE"]) as file: |
| 249 | dashboard_proof = file.read().strip() | 206 | dashboard_proof = file.read().strip() |
| ... | @@ -297,11 +254,7 @@ def render(token): | ... | @@ -297,11 +254,7 @@ def render(token): |
| 297 | user_header = user_headers[0] if user_headers and role else None | 254 | user_header = user_headers[0] if user_headers and role else None |
| 298 | if not HOST.fullmatch(host): | 255 | if not HOST.fullmatch(host): |
| 299 | raise ValueError(f"invalid Caddy host: {host!r}") | 256 | raise ValueError(f"invalid Caddy host: {host!r}") |
| 300 | route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False, "jellyfinAuth": False}) | 257 | route = routes.setdefault((80, host), {"upstreams": set(), "services": set(), "authRole": None, "userHeader": None, "internal": False, "realIp": False, "secureCookies": False}) |
| 301 | if "caddy-jellyfin-auth=true" in tags: | ||
| 302 | if not (instance["ServiceName"] == "jellyfin" or re.fullmatch(r"jellyfin-preview-[0-9a-f]{8}", instance["ServiceName"])): | ||
| 303 | raise ValueError("Jellyfin gateway tag belongs to another service") | ||
| 304 | route["jellyfinAuth"] = True | ||
| 305 | if role and route["authRole"] not in (None, role): | 258 | if role and route["authRole"] not in (None, role): |
| 306 | raise ValueError("conflicting route access roles") | 259 | raise ValueError("conflicting route access roles") |
| 307 | if user_header and route["userHeader"] not in (None, user_header): | 260 | if user_header and route["userHeader"] not in (None, user_header): |
| ... | @@ -366,14 +319,6 @@ def render(token): | ... | @@ -366,14 +319,6 @@ def render(token): |
| 366 | if not 1 <= port <= 65535: | 319 | if not 1 <= port <= 65535: |
| 367 | raise ValueError("invalid dashboard port for Shale linking") | 320 | raise ValueError("invalid dashboard port for Shale linking") |
| 368 | lines += shale_mcp_routes(port) | 321 | lines += shale_mcp_routes(port) |
| 369 | if route["jellyfinAuth"]: | ||
| 370 | gateway, proof = service_gateway(service, routes, dashboard_proof) | ||
| 371 | if gateway: | ||
| 372 | lines += jellyfin_routes(service, route["upstreams"], gateway, proof) | ||
| 373 | else: | ||
| 374 | lines.append(" respond 503") | ||
| 375 | lines.append("}") | ||
| 376 | continue | ||
| 377 | lines += [" tracing {", f" span {service}", " span_attributes {", | 322 | lines += [" tracing {", f" span {service}", " span_attributes {", |
| 378 | f" snowglobe.service {service}", " snowglobe.kind edge", " }", " }"] | 323 | f" snowglobe.service {service}", " snowglobe.kind edge", " }", " }"] |
| 379 | if service == "copyparty" or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service): | 324 | if service == "copyparty" or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service): |
| ... | @@ -475,7 +420,7 @@ def render(token): | ... | @@ -475,7 +420,7 @@ def render(token): |
| 475 | shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) | 420 | shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) |
| 476 | page_headers = [] | 421 | page_headers = [] |
| 477 | if shale_page: | 422 | if shale_page: |
| 478 | page_upstream, proof = service_gateway(service, routes, dashboard_proof) | 423 | page_upstream, proof = shale_gateway(service, routes, dashboard_proof) |
| 479 | if not page_upstream: | 424 | if not page_upstream: |
| 480 | lines += [" respond 503", " }", "}"] | 425 | lines += [" respond 503", " }", "}"] |
| 481 | continue | 426 | continue |
tools/snowglobe.py-2| ... | @@ -349,8 +349,6 @@ def render(data, definitions): | ... | @@ -349,8 +349,6 @@ def render(data, definitions): |
| 349 | tags.append("caddy-user-header=" + endpoint["userHeader"]) | 349 | tags.append("caddy-user-header=" + endpoint["userHeader"]) |
| 350 | if endpoint["forwardRealIp"]: | 350 | if endpoint["forwardRealIp"]: |
| 351 | tags.append("caddy-real-ip=true") | 351 | tags.append("caddy-real-ip=true") |
| 352 | if endpoint.get("jellyfinAuth"): | ||
| 353 | tags.append("caddy-jellyfin-auth=true") | ||
| 354 | if endpoint.get("secureCookies"): | 352 | if endpoint.get("secureCookies"): |
| 355 | tags.append("caddy-secure-cookies=true") | 353 | tags.append("caddy-secure-cookies=true") |
| 356 | if endpoint["tlsInternal"]: | 354 | if endpoint["tlsInternal"]: |