Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/frontend/content/docs/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Always the approved version on the default branch: what the next build there wou

The manifest changes only when an image does, so a site can build its own gallery from it. Light and dark, or phone and desktop, are separate screenshots with their own URLs; pairing them is up to your page.

Both URLs work from any site (`Access-Control-Allow-Origin: *`). They take no query parameters: anything else is a 404.
Both URLs work from any site (`Access-Control-Allow-Origin: *`), including one that turns on cross-origin isolation (`Cross-Origin-Resource-Policy: cross-origin`). They take no query parameters: anything else is a 404.

## Caching and cost

Expand Down
2 changes: 0 additions & 2 deletions server/src/controllers/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ async fn manifest(
let mut r = Json(body).into_response();
let h = r.headers_mut();
h.insert(header::CACHE_CONTROL, HeaderValue::from_static(CACHE));
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*"));
Ok(r)
}

Expand Down Expand Up @@ -152,7 +151,6 @@ async fn image(
h.insert(header::X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"));
h.insert(header::CONTENT_SECURITY_POLICY, HeaderValue::from_static("default-src 'none'; sandbox"));
h.insert(header::CACHE_CONTROL, HeaderValue::from_static(CACHE));
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*"));
if let Ok(v) = HeaderValue::from_str(&etag) {
h.insert(header::ETAG, v);
}
Expand Down
8 changes: 8 additions & 0 deletions server/src/security_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const CLOUDFLARE_REPORT: &str = "https://cloudflareinsights.com";

static POLICY: OnceLock<HeaderValue> = OnceLock::new();

const CROSS_ORIGIN_RESOURCE_POLICY: header::HeaderName =
header::HeaderName::from_static("cross-origin-resource-policy");

/// `'sha256-…'` for each inline `<script>` (one without `src`) in `html`.
#[must_use]
pub fn inline_script_hashes(html: &str) -> Vec<String> {
Expand Down Expand Up @@ -83,9 +86,14 @@ pub fn init(storage_endpoint: Option<&str>) {

/// The middleware: adds the headers a response doesn't already have, and drops `x-powered-by`.
pub async fn add(request: Request, next: Next) -> Response {
let gallery = request.uri().path().starts_with("/g/");
let mut response = next.run(request).await;
let h = response.headers_mut();
h.remove("x-powered-by");
if gallery {
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static("*"));
h.insert(CROSS_ORIGIN_RESOURCE_POLICY, HeaderValue::from_static("cross-origin"));
}
h.entry(header::X_CONTENT_TYPE_OPTIONS).or_insert(HeaderValue::from_static("nosniff"));
h.entry(header::REFERRER_POLICY).or_insert(HeaderValue::from_static("strict-origin-when-cross-origin"));
h.entry(header::X_FRAME_OPTIONS).or_insert(HeaderValue::from_static("DENY"));
Expand Down
3 changes: 3 additions & 0 deletions server/tests/requests/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ async fn only_approved_default_branch_screenshots_in_a_group_are_served() {
assert_eq!(m.status_code(), 200, "{}", m.text());
assert_eq!(m.header("cache-control"), "public, max-age=300, s-maxage=86400");
assert_eq!(m.header("access-control-allow-origin"), "*");
assert_eq!(m.header("cross-origin-resource-policy"), "cross-origin");
let manifest: serde_json::Value = m.json();
let urls: Vec<String> = manifest["groups"][0]["items"]
.as_array()
Expand Down Expand Up @@ -209,6 +210,8 @@ async fn an_image_is_served_as_its_bytes_never_as_a_declared_html_type() {
assert_eq!(home.status_code(), 200);
assert_eq!(home.header("content-type"), "image/png");
assert_eq!(home.header("x-content-type-options"), "nosniff");
assert_eq!(home.header("access-control-allow-origin"), "*");
assert_eq!(home.header("cross-origin-resource-policy"), "cross-origin");
assert!(home.header("content-security-policy").to_str().unwrap().contains("sandbox"));

// And a client can't declare one: an image snapshot is a PNG, JPEG or WebP.
Expand Down
19 changes: 19 additions & 0 deletions server/tests/requests/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ async fn every_response_has_the_security_headers() {
// Images from the bucket the browser is sent to: here the endpoint itself (no public endpoint is set).
let bucket = std::env::var("S3_ENDPOINT").unwrap_or_else(|_| "http://127.0.0.1:59000".into());
assert!(csp.contains(&format!("https: {bucket};")), "{host}{path}: {csp}");
assert!(r.maybe_header("cross-origin-resource-policy").is_none(), "{host}{path}");
assert!(r.maybe_header("access-control-allow-origin").is_none(), "{host}{path}");
}
})
.await;
}

/// The public gallery is for other sites: any origin may read its responses (canvas use, `fetch`) and embed them, even
/// on a site with cross-origin isolation (COEP `require-corp`), whatever the answer. Its images' own tests check a 200.
#[tokio::test]
#[serial]
async fn the_gallery_may_be_used_from_any_site() {
request::<App, _, _>(|request, _ctx| async move {
for path in ["/g/nobody/nothing.json", "/g/nobody/nothing/web/home.png"] {
let r = request.get(path).add_header("host", APP).await;
assert_eq!(r.status_code(), 404, "{path}");
assert_eq!(r.header("cross-origin-resource-policy"), "cross-origin", "{path}");
assert_eq!(r.header("access-control-allow-origin"), "*", "{path}");
assert_eq!(r.header("x-content-type-options"), "nosniff", "{path}");
}
})
.await;
Expand Down
Loading