| ... | ... | @@ -6,7 +6,9 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 6 | 6 | import json |
| 7 | 7 | import os |
| 8 | 8 | from pathlib import Path |
| 9 | import re |
| 9 | 10 | import socket |
| 11 | import socketserver |
| 10 | 12 | import sqlite3 |
| 11 | 13 | import subprocess |
| 12 | 14 | import tempfile |
| ... | ... | @@ -47,12 +49,21 @@ def main(): |
| 47 | 49 | issue = args.page.read_bytes() if args.page else b'<html><head></head><body><a href="/~guest-github-24465214"><img src="avatar"><span>~guest-github-24465214</span></a></body></html>' |
| 48 | 50 | sample = b'<a href="/~guest-astheno-abc">guest</a><a href="/~guest-github-unknown">unknown</a><a href="/~guest-github-24465214/repo">repo</a>' |
| 49 | 51 | blob = bytes(range(256)) * 65536 |
| 52 | csrf = 'initial' |
| 50 | 53 | |
| 51 | 54 | class Fixture(BaseHTTPRequestHandler): |
| 52 | 55 | protocol_version = 'HTTP/1.1' |
| 53 | 56 | |
| 54 | 57 | def do_GET(self): |
| 58 | nonlocal csrf |
| 55 | 59 | observations.append((self.command, self.path, {key.lower(): value for key, value in self.headers.items()})) |
| 60 | if self.path == '/': |
| 61 | account = b'<a class="usa-nav-link" href="/~fixture-member">~fixture-member</a>' if self.headers.get('Cookie') == 'SessionID=preserved' else b'' |
| 62 | return self.respond(b'<html><body><header class="usa-header--basic"><nav>' + account + b'</nav></header></body></html>', 'text/html') |
| 63 | if self.path == '/-/settings' or self.path.endswith('/issues/new'): |
| 64 | csrf += 'x' |
| 65 | account = '<body id="page-user-settings"><kbd>fixture-member</kbd>' if self.path == '/-/settings' else '<body>' |
| 66 | return self.respond(('<html>' + account + '<form method="post"><input type="hidden" name="csrf_token" value="' + csrf + '"></form></body></html>').encode(), 'text/html') |
| 56 | 67 | if self.path == '/binary': |
| 57 | 68 | return self.respond(blob, 'application/octet-stream') |
| 58 | 69 | if self.path == '/json': |
| ... | ... | @@ -75,6 +86,8 @@ def main(): |
| 75 | 86 | def do_POST(self): |
| 76 | 87 | body = self.rfile.read(int(self.headers.get('Content-Length', 0))) |
| 77 | 88 | observations.append((self.command, self.path, {key.lower(): value for key, value in self.headers.items()}, body)) |
| 89 | if self.path.endswith('/issues/new') and urllib.parse.parse_qs(body.decode()).get('csrf_token') != [csrf]: |
| 90 | return self.respond(b'E: unexpected csrf token', 'text/plain', 403) |
| 78 | 91 | self.respond(body, 'application/octet-stream', 201) |
| 79 | 92 | |
| 80 | 93 | def respond(self, body, content_type, status=200, headers=()): |
| ... | ... | @@ -108,7 +121,21 @@ def main(): |
| 108 | 121 | subprocess.run(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-days', '1', '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost', '-addext', 'basicConstraints=CA:FALSE', '-keyout', str(gateway_key), '-out', str(certificate)], check=True, capture_output=True) |
| 109 | 122 | data = root / 'data' |
| 110 | 123 | data.mkdir() |
| 111 | | environment = {**os.environ, 'PORT': str(dashboard_port), 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DATA_DIR': str(data), 'SNOWGLOBE_PROXY_TOKEN_FILE': str(token), 'SNOWGLOBE_INTERNAL_URL': f'https://localhost:{internal_port}', 'SNOWGLOBE_CA_BUNDLE': str(certificate), 'SNOWGLOBE_REPO': str(repo), 'SNOWGLOBE_WEB_DIR': str(repo / 'dashboard/dist')} |
| 124 | class Host(socketserver.StreamRequestHandler): |
| 125 | def handle(self): |
| 126 | request = json.loads(self.rfile.readline()) |
| 127 | value = {} |
| 128 | if request['operation'] == 'shale.access': |
| 129 | allowed = request['account'] == 'fixture-member' |
| 130 | value = {'allowed': allowed, 'deny': not allowed} |
| 131 | response = json.dumps({'value': value}).encode() |
| 132 | self.wfile.write(len(response).to_bytes(4, 'big') + response) |
| 133 | |
| 134 | host = socketserver.UnixStreamServer(str(root / 'host.sock'), Host) |
| 135 | stack.callback(host.server_close) |
| 136 | stack.callback(host.shutdown) |
| 137 | threading.Thread(target=host.serve_forever, daemon=True).start() |
| 138 | environment = {**os.environ, 'PORT': str(dashboard_port), 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DATA_DIR': str(data), 'SNOWGLOBE_PROXY_TOKEN_FILE': str(token), 'SNOWGLOBE_HOST_SOCKET': str(root / 'host.sock'), 'SNOWGLOBE_INTERNAL_URL': f'https://localhost:{internal_port}', 'SNOWGLOBE_CA_BUNDLE': str(certificate), 'SNOWGLOBE_REPO': str(repo), 'SNOWGLOBE_WEB_DIR': str(repo / 'dashboard/dist')} |
| 112 | 139 | for key in ['SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_YT_STATE']: |
| 113 | 140 | environment.pop(key, None) |
| 114 | 141 | log = stack.enter_context((root / 'dashboard.log').open('w')) |
| ... | ... | @@ -226,10 +253,22 @@ def main(): |
| 226 | 253 | assert len(request('/large')[2]) == 9 * 1024 * 1024 |
| 227 | 254 | status, headers, body = request('/submit?keep=yes', body=b'field=unchanged') |
| 228 | 255 | assert status == 201 and body == b'field=unchanged' and observations[-1][1] == '/submit?keep=yes' |
| 256 | for path in ['/the-knife/issues/new', '/another-repo/issues/new']: |
| 257 | status, _, form = request(path) |
| 258 | assert status == 200 |
| 259 | csrf_field = re.search(rb'name="csrf_token" value="([^"]+)"', form)[1] |
| 260 | assert request('/the-knife/')[0] == 200 |
| 261 | submitted = b'csrf_token=' + csrf_field + b'&title=preserved' |
| 262 | status, _, body = request(path, body=submitted) |
| 263 | assert status == 201 and body == submitted, (path, status, body) |
| 264 | for invalid in [b'title=missing', b'csrf_token=invalid']: |
| 265 | assert request(path, body=invalid)[0] == 403 |
| 266 | assert not any(item[1] == '/-/settings' for item in observations) |
| 267 | assert request('/the-knife/issues/new', headers={'Cookie': ''})[0] == 403 |
| 229 | 268 | assert request('/submit', body=b'blocked', headers={'Origin': 'https://other.example'})[0] == 403 |
| 230 | 269 | assert request('/auth/shale/page', direct=True)[0] == 403 |
| 231 | 270 | assert request('/auth/shale/page', direct=True, headers={'Snowglobe-Proxy-Token': proof, 'Snowglobe-Shale-Upstream': f'127.0.0.1:{fixture.server_port}', 'Snowglobe-Shale-Uri': '/', 'Host': 'snowglobe.snowglobe.test'})[0] == 403 |
| 232 | | print(json.dumps({'html_links': 'passed', 'provider_redirects': 'passed', 'cookies_forms_and_binary': 'passed', 'proxy_boundary': 'passed', 'head_injection': 'passed', 'origin': origin}), flush=True) |
| 271 | print(json.dumps({'html_links': 'passed', 'provider_redirects': 'passed', 'cookies_forms_and_binary': 'passed', 'csrf_preservation_and_rejection': 'passed', 'proxy_boundary': 'passed', 'head_injection': 'passed', 'origin': origin}), flush=True) |
| 233 | 272 | if args.browser_ready_file: |
| 234 | 273 | args.browser_ready_file.write_text(json.dumps({'origin': origin})) |
| 235 | 274 | stop = args.browser_ready_file.with_suffix('.stop') |