authoravatarclover caruso<git@paperclover.net>2026-10-21 18:51:36-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 19:21:44-07:00
log8bc89c9ebdfc6894c2903e5954c9d720fef9e108
tree36968acb246ecc44821a17f95438926e586f36e6
parent792a5281167703c9ee7525e7016cb3faf1fc06b5
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Preserve Shale CSRF tokens during native access checks

Read the authenticated account from the homepage navigation instead of settings, whose handler rotates the session CSRF token. Reject missing or ambiguous identities without changing access policy. Verified 57 Rust tests, the stateful Caddy proxy regression against old and fixed binaries, and staged issue creation, comments, title edits, settings saves, token rejection, and native restrictions. Assisted-by: gpt-6-astra

3 files changed, 71 insertions(+), 8 deletions(-)

dashboard/src/shale.rs+1-1
......@@ -487,7 +487,7 @@ fn fields(query: &str) -> Result<HashMap<String, String>> {
487487 }
488488 Ok(fields)
489489}
490pub(crate) fn username(page: &str) -> Result<String> {
490fn username(page: &str) -> Result<String> {
491491 let document = Html::parse_document(page);
492492 if document
493493 .select(&Selector::parse("body#page-user-settings").unwrap())
dashboard/src/shale_page.rs+29-5
......@@ -360,6 +360,16 @@ fn knife_uri(path: &str) -> bool {
360360 false
361361}
362362
363fn session_account(html: &str) -> Option<String> {
364 let document = scraper::Html::parse_document(html);
365 let selector = scraper::Selector::parse("body > header.usa-header--basic nav a.usa-nav-link[href^='/~']").unwrap();
366 let mut accounts = document.select(&selector);
367 let name = accounts.next()?.attr("href")?.strip_prefix("/~")?;
368 let name = percent_encoding::percent_decode_str(name).decode_utf8().ok()?;
369 (accounts.next().is_none() && !name.is_empty() && !name.contains(['/', '\\', '?', '#']))
370 .then(|| name.into_owned())
371}
372
363373fn restricted(authenticate: bool) -> Response {
364374 (if authenticate { StatusCode::UNAUTHORIZED } else { StatusCode::FORBIDDEN }, [
365375 ("content-type", "text/html; charset=utf-8"),
......@@ -443,11 +453,12 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
443453 if knife {
444454 let mut account = String::new();
445455 if let Some(cookie) = parts.headers.get("cookie") {
446 let settings = match &app.internal {
447 Some((base, _)) => format!("{base}services/{service}/-/settings"),
448 None => format!("http://{upstream}/-/settings"),
456 // Opening settings rotates the session's CSRF token, invalidating pending forms.
457 let index = match &app.internal {
458 Some((base, _)) => format!("{base}services/{service}/"),
459 None => format!("http://{upstream}/"),
449460 };
450 let mut identity = app.shale_http.get(settings).header("cookie", cookie).header("accept", "text/html").timeout(Duration::from_secs(3));
461 let mut identity = app.shale_http.get(index).header("cookie", cookie).header("accept", "text/html").timeout(Duration::from_secs(3));
451462 if app.internal.is_none() { identity = identity.header("host", host); }
452463 for name in ["x-forwarded-for", "x-real-ip", "user-agent"] {
453464 if let Some(value) = parts.headers.get(name) { identity = identity.header(name, value); }
......@@ -456,7 +467,7 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
456467 && response.status() == StatusCode::OK
457468 && let Ok(body) = response.text().await
458469 {
459 account = shale::username(&body).unwrap_or_default();
470 account = session_account(&body).unwrap_or_default();
460471 }
461472 }
462473 knife_policy = match host::call(json!({"operation":"shale.access","service":service,"uri":uri.to_string(),"account":account,"authorization":parts.headers.get("authorization").and_then(|value| value.to_str().ok()).unwrap_or_default()})).await {
......@@ -619,6 +630,19 @@ mod tests {
619630 use rusqlite::params as sql;
620631 use scraper::{Html, Selector};
621632
633 #[test]
634 fn session_account_uses_only_unambiguous_site_navigation() {
635 let header = |name: &str| format!("<header class='usa-header usa-header--basic'><nav><a class='usa-nav-link' href='/~{name}'>account</a></nav></header>");
636 assert_eq!(session_account(&header("clover")), Some("clover".into()));
637 assert_eq!(session_account(&header("guest%2Dname")), Some("guest-name".into()));
638 assert_eq!(session_account(&format!("{}<main><header class='usa-header--basic'><nav><a class='usa-nav-link' href='/~clover'>fake</a></nav></header></main>", header("guest"))), Some("guest".into()));
639 assert_eq!(session_account(&format!("{}{}", header("guest"), header("clover"))), None);
640 for name in ["", "%FF", "clover%2Frepo", "clover%5Crepo", "clover?fake", "clover#fake"] {
641 assert_eq!(session_account(&header(name)), None, "{name}");
642 }
643 assert_eq!(session_account("<a class='usa-nav-link' href='/~clover'>fake</a>"), None);
644 }
645
622646 #[test]
623647 fn native_content_is_removed_from_both_diff_styles_and_history_rows() {
624648 let policy = json!({"commits":["deadbeef"],"safeDiff":"PUBLIC SOURCE\nThe Knife's Native UI is restricted\n"});
tools/dashboard-shale-page-test.py+41-2
......@@ -6,7 +6,9 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
66import json
77import os
88from pathlib import Path
9import re
910import socket
11import socketserver
1012import sqlite3
1113import subprocess
1214import tempfile
......@@ -47,12 +49,21 @@ def main():
4749 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>'
4850 sample = b'<a href="/~guest-astheno-abc">guest</a><a href="/~guest-github-unknown">unknown</a><a href="/~guest-github-24465214/repo">repo</a>'
4951 blob = bytes(range(256)) * 65536
52 csrf = 'initial'
5053
5154 class Fixture(BaseHTTPRequestHandler):
5255 protocol_version = 'HTTP/1.1'
5356
5457 def do_GET(self):
58 nonlocal csrf
5559 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')
5667 if self.path == '/binary':
5768 return self.respond(blob, 'application/octet-stream')
5869 if self.path == '/json':
......@@ -75,6 +86,8 @@ def main():
7586 def do_POST(self):
7687 body = self.rfile.read(int(self.headers.get('Content-Length', 0)))
7788 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)
7891 self.respond(body, 'application/octet-stream', 201)
7992
8093 def respond(self, body, content_type, status=200, headers=()):
......@@ -108,7 +121,21 @@ def main():
108121 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)
109122 data = root / 'data'
110123 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')}
112139 for key in ['SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_YT_STATE']:
113140 environment.pop(key, None)
114141 log = stack.enter_context((root / 'dashboard.log').open('w'))
......@@ -226,10 +253,22 @@ def main():
226253 assert len(request('/large')[2]) == 9 * 1024 * 1024
227254 status, headers, body = request('/submit?keep=yes', body=b'field=unchanged')
228255 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
229268 assert request('/submit', body=b'blocked', headers={'Origin': 'https://other.example'})[0] == 403
230269 assert request('/auth/shale/page', direct=True)[0] == 403
231270 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)
233272 if args.browser_ready_file:
234273 args.browser_ready_file.write_text(json.dumps({'origin': origin}))
235274 stop = args.browser_ready_file.with_suffix('.stop')