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>> {...@@ -487,7 +487,7 @@ fn fields(query: &str) -> Result<HashMap<String, String>> {
487 }487 }
488 Ok(fields)488 Ok(fields)
489}489}
490pub(crate) fn username(page: &str) -> Result<String> {490fn username(page: &str) -> Result<String> {
491 let document = Html::parse_document(page);491 let document = Html::parse_document(page);
492 if document492 if document
493 .select(&Selector::parse("body#page-user-settings").unwrap())493 .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 {...@@ -360,6 +360,16 @@ fn knife_uri(path: &str) -> bool {
360 false360 false
361}361}
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
363fn restricted(authenticate: bool) -> Response {373fn restricted(authenticate: bool) -> Response {
364 (if authenticate { StatusCode::UNAUTHORIZED } else { StatusCode::FORBIDDEN }, [374 (if authenticate { StatusCode::UNAUTHORIZED } else { StatusCode::FORBIDDEN }, [
365 ("content-type", "text/html; charset=utf-8"),375 ("content-type", "text/html; charset=utf-8"),
...@@ -443,11 +453,12 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {...@@ -443,11 +453,12 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
443 if knife {453 if knife {
444 let mut account = String::new();454 let mut account = String::new();
445 if let Some(cookie) = parts.headers.get("cookie") {455 if let Some(cookie) = parts.headers.get("cookie") {
446 let settings = match &app.internal {456 // Opening settings rotates the session's CSRF token, invalidating pending forms.
447 Some((base, _)) => format!("{base}services/{service}/-/settings"),457 let index = match &app.internal {
448 None => format!("http://{upstream}/-/settings"),458 Some((base, _)) => format!("{base}services/{service}/"),
459 None => format!("http://{upstream}/"),
449 };460 };
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));
451 if app.internal.is_none() { identity = identity.header("host", host); }462 if app.internal.is_none() { identity = identity.header("host", host); }
452 for name in ["x-forwarded-for", "x-real-ip", "user-agent"] {463 for name in ["x-forwarded-for", "x-real-ip", "user-agent"] {
453 if let Some(value) = parts.headers.get(name) { identity = identity.header(name, value); }464 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> {...@@ -456,7 +467,7 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> {
456 && response.status() == StatusCode::OK467 && response.status() == StatusCode::OK
457 && let Ok(body) = response.text().await468 && let Ok(body) = response.text().await
458 {469 {
459 account = shale::username(&body).unwrap_or_default();470 account = session_account(&body).unwrap_or_default();
460 }471 }
461 }472 }
462 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 {473 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 {...@@ -619,6 +630,19 @@ mod tests {
619 use rusqlite::params as sql;630 use rusqlite::params as sql;
620 use scraper::{Html, Selector};631 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
622 #[test]646 #[test]
623 fn native_content_is_removed_from_both_diff_styles_and_history_rows() {647 fn native_content_is_removed_from_both_diff_styles_and_history_rows() {
624 let policy = json!({"commits":["deadbeef"],"safeDiff":"PUBLIC SOURCE\nThe Knife's Native UI is restricted\n"});648 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...@@ -6,7 +6,9 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
6import json6import json
7import os7import os
8from pathlib import Path8from pathlib import Path
9import re
9import socket10import socket
11import socketserver
10import sqlite312import sqlite3
11import subprocess13import subprocess
12import tempfile14import tempfile
...@@ -47,12 +49,21 @@ def main():...@@ -47,12 +49,21 @@ def main():
47 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>'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 sample = b'<a href="/~guest-astheno-abc">guest</a><a href="/~guest-github-unknown">unknown</a><a href="/~guest-github-24465214/repo">repo</a>'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 blob = bytes(range(256)) * 6553651 blob = bytes(range(256)) * 65536
52 csrf = 'initial'
5053
51 class Fixture(BaseHTTPRequestHandler):54 class Fixture(BaseHTTPRequestHandler):
52 protocol_version = 'HTTP/1.1'55 protocol_version = 'HTTP/1.1'
5356
54 def do_GET(self):57 def do_GET(self):
58 nonlocal csrf
55 observations.append((self.command, self.path, {key.lower(): value for key, value in self.headers.items()}))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 if self.path == '/binary':67 if self.path == '/binary':
57 return self.respond(blob, 'application/octet-stream')68 return self.respond(blob, 'application/octet-stream')
58 if self.path == '/json':69 if self.path == '/json':
...@@ -75,6 +86,8 @@ def main():...@@ -75,6 +86,8 @@ def main():
75 def do_POST(self):86 def do_POST(self):
76 body = self.rfile.read(int(self.headers.get('Content-Length', 0)))87 body = self.rfile.read(int(self.headers.get('Content-Length', 0)))
77 observations.append((self.command, self.path, {key.lower(): value for key, value in self.headers.items()}, body))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 self.respond(body, 'application/octet-stream', 201)91 self.respond(body, 'application/octet-stream', 201)
7992
80 def respond(self, body, content_type, status=200, headers=()):93 def respond(self, body, content_type, status=200, headers=()):
...@@ -108,7 +121,21 @@ def main():...@@ -108,7 +121,21 @@ def main():
108 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)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 data = root / 'data'122 data = root / 'data'
110 data.mkdir()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 for key in ['SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_YT_STATE']:139 for key in ['SNOWGLOBE_INDEX_POOL', 'SNOWGLOBE_AUTH_REQUIRED', 'SNOWGLOBE_YT_STATE']:
113 environment.pop(key, None)140 environment.pop(key, None)
114 log = stack.enter_context((root / 'dashboard.log').open('w'))141 log = stack.enter_context((root / 'dashboard.log').open('w'))
...@@ -226,10 +253,22 @@ def main():...@@ -226,10 +253,22 @@ def main():
226 assert len(request('/large')[2]) == 9 * 1024 * 1024253 assert len(request('/large')[2]) == 9 * 1024 * 1024
227 status, headers, body = request('/submit?keep=yes', body=b'field=unchanged')254 status, headers, body = request('/submit?keep=yes', body=b'field=unchanged')
228 assert status == 201 and body == b'field=unchanged' and observations[-1][1] == '/submit?keep=yes'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 assert request('/submit', body=b'blocked', headers={'Origin': 'https://other.example'})[0] == 403268 assert request('/submit', body=b'blocked', headers={'Origin': 'https://other.example'})[0] == 403
230 assert request('/auth/shale/page', direct=True)[0] == 403269 assert request('/auth/shale/page', direct=True)[0] == 403
231 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] == 403270 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 if args.browser_ready_file:272 if args.browser_ready_file:
234 args.browser_ready_file.write_text(json.dumps({'origin': origin}))273 args.browser_ready_file.write_text(json.dumps({'origin': origin}))
235 stop = args.browser_ready_file.with_suffix('.stop')274 stop = args.browser_ready_file.with_suffix('.stop')