mod.rs
| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use api_types::{ |
| 4 | ADMIN_SETTINGS, ADMIN_USERS, AUTH_LOGIN, AUTH_LOGOUT, AUTH_ME, AUTH_SETUP, FILES, SEARCH, |
| 5 | SHARE, SHARE_UNLOCK_SUFFIX, SHARES, |
| 6 | }; |
| 7 | use axum::Router; |
| 8 | use axum::http::HeaderValue; |
| 9 | use axum::routing::{delete, get, post, put}; |
| 10 | use tower_http::set_header::SetResponseHeaderLayer; |
| 11 | |
| 12 | use crate::error::AppState; |
| 13 | |
| 14 | /// Content-Security-Policy tuned to the built Trunk frontend. |
| 15 | /// |
| 16 | /// * `script-src 'unsafe-inline'` — Trunk emits one inline bootstrap module |
| 17 | /// in index.html; every real JS file also carries an SRI integrity hash. |
| 18 | /// * `'wasm-unsafe-eval'` — compiling the same-origin WASM module. |
| 19 | /// * `style-src 'unsafe-inline'` — the context menu sets an inline `style=`. |
| 20 | /// * Everything else locked to the same origin; frames/plugins banned. |
| 21 | const CSP: &str = "default-src 'self'; script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; media-src 'self' blob:; connect-src 'self'; font-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none';"; |
| 22 | |
| 23 | /// Content-Security-Policy for served *user files* that the browser would |
| 24 | /// treat as a scripting document (HTML, SVG, XML). Lets such a file render as |
| 25 | /// a real page — the "share an HTML page" flow — without letting it act as the |
| 26 | /// app. |
| 27 | /// |
| 28 | /// The security hinges on one omission: `allow-scripts` **without** |
| 29 | /// `allow-same-origin`. That forces the document into a unique opaque origin, |
| 30 | /// so its JavaScript cannot read the session cookie's origin, cannot touch |
| 31 | /// `localStorage`, and cannot call `/api` as the viewer (the server sends no |
| 32 | /// CORS headers, so every cross-origin read fails). Never add |
| 33 | /// `allow-same-origin` here. |
| 34 | /// |
| 35 | /// Also deliberately absent: |
| 36 | /// * `allow-top-navigation` — a shared page cannot silently redirect the |
| 37 | /// viewer elsewhere. `-by-user-activation` still lets links work on click. |
| 38 | /// * `allow-popups-to-escape-sandbox` — a popup would drop the sandbox. |
| 39 | /// |
| 40 | /// `connect-src *` is deliberate: a shared page may call third-party APIs. |
| 41 | /// The trade-off is that it can also beacon (report that the link was opened, |
| 42 | /// and anything the page itself contains). It cannot exfiltrate anything of |
| 43 | /// the viewer's — the opaque origin means it has no session and no CORS read |
| 44 | /// access to this server. |
| 45 | pub(crate) const FILE_CSP: &str = "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; media-src 'self' blob:; font-src 'self' data:; connect-src *; object-src 'none'; frame-ancestors 'none'; sandbox allow-scripts allow-forms allow-modals allow-downloads allow-popups allow-top-navigation-by-user-activation;"; |
| 46 | |
| 47 | /// Content-Security-Policy for inline (preview) files that are *not* |
| 48 | /// scripting documents (PDF, media, …): the preview modal embeds them in a |
| 49 | /// same-origin `<iframe>`. Scriptable files never get this — see [`FILE_CSP`]. |
| 50 | pub(crate) const INLINE_CSP: &str = "frame-ancestors 'self';"; |
| 51 | |
| 52 | /// True for MIME types the browser parses as a scripting document. These are |
| 53 | /// the responses that need [`FILE_CSP`]; everything else keeps the app policy. |
| 54 | /// |
| 55 | /// This reads the *declared* type — the same value that becomes the |
| 56 | /// `Content-Type` header — on purpose. If the sandbox decision and the render |
| 57 | /// decision ever read different inputs, a file can be rendered as a scripting |
| 58 | /// document without being sandboxed. |
| 59 | pub(crate) fn is_scriptable_mime(mime: &str) -> bool { |
| 60 | let base = mime.split(';').next().unwrap_or("").trim(); |
| 61 | matches!( |
| 62 | base, |
| 63 | "text/html" | "application/xhtml+xml" | "image/svg+xml" | "text/xml" | "application/xml" |
| 64 | ) || base.ends_with("+xml") |
| 65 | } |
| 66 | |
| 67 | mod admin; |
| 68 | mod auth; |
| 69 | mod common; |
| 70 | mod files; |
| 71 | mod search; |
| 72 | mod shares; |
| 73 | mod spa; |
| 74 | |
| 75 | pub fn router(state: Arc<AppState>) -> Router { |
| 76 | // Route patterns: the server side of the shared endpoint strings in |
| 77 | // `api_types` (axum copies them into the route table on insert). |
| 78 | let files_root = format!("{FILES}/{{root_id}}"); |
| 79 | let files_item = format!("{FILES}/{{root_id}}/{{*path}}"); |
| 80 | let shares_id = format!("{SHARES}/{{id}}"); |
| 81 | let share_token = format!("{SHARE}/{{token}}"); |
| 82 | let share_unlock = format!("{SHARE}/{{token}}{SHARE_UNLOCK_SUFFIX}"); |
| 83 | let admin_user_id = format!("{ADMIN_USERS}/{{id}}"); |
| 84 | |
| 85 | Router::new() |
| 86 | .route(AUTH_LOGIN, post(auth::login)) |
| 87 | .route(AUTH_LOGOUT, post(auth::logout)) |
| 88 | .route(AUTH_ME, get(auth::me).put(auth::update_profile)) |
| 89 | .route(AUTH_SETUP, post(auth::setup)) |
| 90 | .route(SEARCH, get(search::search)) |
| 91 | .route(&files_root, get(files::list_root).put(files::file_put_root)) |
| 92 | .route(&files_item, get(files::file_get)) |
| 93 | .route(&files_item, put(files::file_put)) |
| 94 | .route(&files_root, post(files::dispatch_root)) |
| 95 | .route(&files_item, post(files::dispatch)) |
| 96 | .route(&files_item, delete(files::delete)) |
| 97 | .route(SHARES, get(shares::list)) |
| 98 | .route(SHARES, post(shares::create)) |
| 99 | .route(&shares_id, delete(shares::delete)) |
| 100 | .route(&share_token, get(shares::resolve)) |
| 101 | .route(&share_unlock, post(shares::unlock)) |
| 102 | .route(ADMIN_USERS, get(admin::list_users)) |
| 103 | .route(ADMIN_USERS, post(admin::create_user)) |
| 104 | .route(&admin_user_id, put(admin::update_user)) |
| 105 | .route(&admin_user_id, delete(admin::delete_user)) |
| 106 | .route(ADMIN_SETTINGS, get(admin::get_settings)) |
| 107 | .route(ADMIN_SETTINGS, put(admin::update_settings)) |
| 108 | .fallback(spa::fallback) |
| 109 | // The editor save body is checked against `MAX_TEXT_BYTES` in the |
| 110 | // handler; axum's default limit is the same 2 MiB, which would win |
| 111 | // with a plain 413 instead of the localized error. |
| 112 | .layer(axum::extract::DefaultBodyLimit::max( |
| 113 | files::MAX_TEXT_BYTES as usize + 64 * 1024, |
| 114 | )) |
| 115 | .with_state(state) |
| 116 | // Hard security headers on every response (API and static alike). |
| 117 | // CSP/XFO are `if_not_present` so file responses can substitute |
| 118 | // [`FILE_CSP`] or the same-origin-framable [`INLINE_CSP`]; everything |
| 119 | // else gets the app policy. |
| 120 | .layer(SetResponseHeaderLayer::if_not_present( |
| 121 | "content-security-policy".parse().unwrap(), |
| 122 | HeaderValue::from_static(CSP), |
| 123 | )) |
| 124 | .layer(SetResponseHeaderLayer::overriding( |
| 125 | "x-content-type-options".parse().unwrap(), |
| 126 | HeaderValue::from_static("nosniff"), |
| 127 | )) |
| 128 | .layer(SetResponseHeaderLayer::if_not_present( |
| 129 | "x-frame-options".parse().unwrap(), |
| 130 | HeaderValue::from_static("DENY"), |
| 131 | )) |
| 132 | .layer(SetResponseHeaderLayer::overriding( |
| 133 | "referrer-policy".parse().unwrap(), |
| 134 | HeaderValue::from_static("no-referrer"), |
| 135 | )) |
| 136 | } |
| 137 |