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`.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 20:35:51 +08:00
1 джерело de95beb345
коміт 58ccb9dbc9
1 file changed
+24 -2
+24 -2
Переглянути файл
@@ -781,7 +781,7 @@ document.getElementById("f").addEventListener("submit", async (e) => {
</script>
</body>
</html>"#;
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<Incoming>, state: Arc<ServerState>) -> Response<Full<Bytes>> {
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"), "<html><head></head><body></body></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();