GITv

Sandbox raw files served from the site's own origin

Browse files

Raw files (an SVG opened directly, say) now get their own policy with `sandbox`, so they run as a unique origin with no scripts even if the policy were ever loosened. Inline styles are allowed so SVGs draw as meant; nothing may load from anywhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Author
Um9i vulgarmessiah@gmail.com
Date
22 Nov 2026, 22:02
Commit
2badbef8a008
Parent
c73daf9

2 files changed +15−2

@@ -102,6 +102,17 @@ const SECURITY: [(header::HeaderName, &str); 4] = [
(header::CACHE_CONTROL, "no-store"),
];
/// Headers for a raw file, which is repository content served from this site's own origin.
/// The sandbox makes a file opened directly (an SVG, say) a unique origin with no scripts, so
/// even a policy mistake can't let it act as the signed-in visitor. Inline styles are allowed
/// so SVGs draw as their authors meant; nothing may load from anywhere.
const RAW_SECURITY: [(header::HeaderName, &str); 4] = [
(header::CONTENT_SECURITY_POLICY, "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:"),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
(header::REFERRER_POLICY, "no-referrer"),
(header::CACHE_CONTROL, "no-store"),
];
impl View {
/// The response for `visitor`, who may need a download slot for a raw file.
async fn response(self, visitor: Visitor) -> Response {
@@ -155,13 +166,13 @@ async fn raw_response(visitor: Visitor, command: std::process::Command, size: us
Some("jpg" | "jpeg") => "image/jpeg",
Some("gif") => "image/gif",
Some("webp") => "image/webp",
// Safe to serve as an image: the page's policy blocks any script inside it.
// Safe to serve as an image: the sandbox (see RAW_SECURITY) blocks any script inside it.
Some("svg") => "image/svg+xml",
_ if binary => "application/octet-stream",
_ => "text/plain; charset=utf-8",
};
let body = output.body(prefix, None, permit, std::time::Duration::from_secs(300));
let mut response = (SECURITY, [(header::CONTENT_TYPE, kind)], body).into_response();
let mut response = (RAW_SECURITY, [(header::CONTENT_TYPE, kind)], body).into_response();
response.headers_mut().insert(header::CONTENT_LENGTH, size.into());
if kind == "application/octet-stream"
&& let Ok(value) = format!("attachment; filename*=UTF-8''{}", encode(&name)).parse()
@@ -291,6 +291,8 @@ fn every_page_sends_the_security_headers() {
assert!(csp.contains("default-src 'none'") && !csp.contains("script-src"), "{path}: {csp}");
assert_eq!(page.header("x-content-type-options"), "nosniff", "{path}");
assert_eq!(page.header("referrer-policy"), "no-referrer", "{path}");
let sandboxed = csp.split(';').any(|d| d.trim() == "sandbox");
assert_eq!(sandboxed, path.contains("/raw"), "raw files, and only they, are sandboxed: {path}: {csp}");
}
}