From 58ccb9dbc9c407dbb3ecd1c663889f69f45feedb Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 6 Dec 2026 20:35:51 +0800 Subject: [PATCH] fix(server): stop other sites from framing the web workbench (#8380) `openidea-server --web` served the workbench and its sign-in page without framing or content-type headers, so any site could load the IDE in a hidden frame and trick a click into it. Both pages now send `Content-Security-Policy: frame-ancestors 'none'; object-src 'none'; base-uri 'self'`, `X-Frame-Options: DENY`, `X-Content-Type-Options: nosniff` and `Referrer-Policy: no-referrer`. --- crates/ide-host/src/web_server/mod.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/ide-host/src/web_server/mod.rs b/crates/ide-host/src/web_server/mod.rs index 23bfbfdecf..e2c04292f4 100644 --- a/crates/ide-host/src/web_server/mod.rs +++ b/crates/ide-host/src/web_server/mod.rs @@ -781,7 +781,7 @@ document.getElementById("f").addEventListener("submit", async (e) => { "#; - Response::builder() + page_headers(Response::builder()) .status(StatusCode::UNAUTHORIZED) .header(header::CONTENT_TYPE, "text/html; charset=utf-8") .header(header::CACHE_CONTROL, "no-store") @@ -789,6 +789,17 @@ document.getElementById("f").addEventListener("submit", async (e) => { .unwrap() } +/// Headers of the pages the browser shows (the workbench and the sign-in page): no other site +/// may frame them, which would let it trick a click into the IDE, and nothing is guessed from +/// content or leaked through `Referer`. +fn page_headers(builder: hyper::http::response::Builder) -> hyper::http::response::Builder { + builder + .header(header::CONTENT_SECURITY_POLICY, "frame-ancestors 'none'; object-src 'none'; base-uri 'self'") + .header(header::X_FRAME_OPTIONS, "DENY") + .header(header::X_CONTENT_TYPE_OPTIONS, "nosniff") + .header(header::REFERRER_POLICY, "no-referrer") +} + fn handle_ws_upgrade(req: Request, state: Arc) -> Response> { let Some(key) = req.headers().get(header::SEC_WEBSOCKET_KEY).and_then(|v| v.to_str().ok()).map(str::to_owned) else { @@ -1228,7 +1239,7 @@ async fn serve_index_html(web_dir: &Path, head_only: bool, gzip: bool) -> Respon }; let len = payload.len(); let body = if head_only { Bytes::new() } else { payload }; - let mut builder = Response::builder() + let mut builder = page_headers(Response::builder()) .status(StatusCode::OK) .header(header::CONTENT_TYPE, "text/html; charset=utf-8") .header(header::CONTENT_LENGTH, len.to_string()) @@ -1244,6 +1255,17 @@ async fn serve_index_html(web_dir: &Path, head_only: bool, gzip: bool) -> Respon mod tests { use super::*; + #[tokio::test] + async fn pages_cannot_be_framed_by_other_sites() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("index.html"), "").unwrap(); + let response = serve_index_html(dir.path(), false, false).await; + let headers = response.headers(); + assert_eq!(headers[header::X_FRAME_OPTIONS], "DENY"); + assert!(headers[header::CONTENT_SECURITY_POLICY].to_str().unwrap().contains("frame-ancestors 'none'")); + assert_eq!(headers[header::X_CONTENT_TYPE_OPTIONS], "nosniff"); + } + #[test] fn resolves_direct_and_reverse_proxy_prefixed_assets_safely() { let dir = tempfile::tempdir().unwrap();