A push's pack is capped at 2 GiB (receive.maxInputSize), so a push or an inflating gzip body cannot fill the disk
Browse files at this commite47fd94473ddb4a3f9788421df66f83b4778a831Parent: bdd842c- harden
- a push's pack is capped at 2 GiB (receive.maxInputSize), so a push or an inflating gzip body cannot fill the disk
- label descriptions are capped at 500 characters and a repo at 500 labels
- fix
- /assets with a stale or missing ?v= revalidates instead of being cached as immutable for a year
6 changed files +84 -38
- crates/forge-core/src/issues.rs +17 -9
- crates/forge-core/src/store/labels.rs +17 -2
- crates/forge-git/src/service.rs +8 -0
- crates/forge-web/src/importer/mod.rs +6 -1
- crates/forge-web/src/pages/assets.rs +34 -24
- crates/forge-web/src/pages/labels_page.rs +2 -2
crates/forge-core/src/issues.rsmodified+17 -9
| @@ -10,6 +10,8 @@ pub const MAX_TITLE: usize = 256; | |||
| 10 | pub const MAX_BODY: usize = 65536; | 10 | pub const MAX_BODY: usize = 65536; |
| 11 | /// Longest accepted label name, in characters. | 11 | /// Longest accepted label name, in characters. |
| 12 | pub const MAX_LABEL_NAME: usize = 50; | 12 | pub const MAX_LABEL_NAME: usize = 50; |
| 13 | +/// Longest label description, in characters. | ||
| 14 | +pub const MAX_LABEL_DESCRIPTION: usize = 500; | ||
| 13 | /// Most assignees one issue can have. | 15 | /// Most assignees one issue can have. |
| 14 | pub const MAX_ASSIGNEES: usize = 10; | 16 | pub const MAX_ASSIGNEES: usize = 10; |
| 15 | 17 | ||
| @@ -251,7 +253,7 @@ pub fn validate_body(b: &str) -> Result<()> { | |||
| 251 | 253 | ||
| 252 | /// Returns `(trimmed name, normalized color)`. The name is 1..=50 characters with no control | 254 | /// Returns `(trimmed name, normalized color)`. The name is 1..=50 characters with no control |
| 253 | /// characters. The color is `#rgb` or `#rrggbb` in any case and comes back as lowercase `#rrggbb`. | 255 | /// characters. The color is `#rgb` or `#rrggbb` in any case and comes back as lowercase `#rrggbb`. |
| 254 | -pub fn validate_label(name: &str, color: &str) -> Result<(String, String)> { | 256 | +pub fn validate_label(name: &str, color: &str, description: &str) -> Result<(String, String)> { |
| 255 | let name = name.trim(); | 257 | let name = name.trim(); |
| 256 | let len = name.chars().count(); | 258 | let len = name.chars().count(); |
| 257 | if len == 0 || len > MAX_LABEL_NAME { | 259 | if len == 0 || len > MAX_LABEL_NAME { |
| @@ -266,6 +268,11 @@ pub fn validate_label(name: &str, color: &str) -> Result<(String, String)> { | |||
| 266 | } | 268 | } |
| 267 | let color = normalize_color(color) | 269 | let color = normalize_color(color) |
| 268 | .ok_or_else(|| Error::Invalid(format!("label color {color:?} is not #rgb or #rrggbb")))?; | 270 | .ok_or_else(|| Error::Invalid(format!("label color {color:?} is not #rgb or #rrggbb")))?; |
| 271 | + if description.chars().count() > MAX_LABEL_DESCRIPTION { | ||
| 272 | + return Err(Error::Invalid(format!( | ||
| 273 | + "label description must be at most {MAX_LABEL_DESCRIPTION} characters" | ||
| 274 | + ))); | ||
| 275 | + } | ||
| 269 | Ok((name.to_string(), color)) | 276 | Ok((name.to_string(), color)) |
| 270 | } | 277 | } |
| 271 | 278 | ||
| @@ -321,18 +328,19 @@ mod tests { | |||
| 321 | #[test] | 328 | #[test] |
| 322 | fn label_name_and_color_are_normalized() { | 329 | fn label_name_and_color_are_normalized() { |
| 323 | assert_eq!( | 330 | assert_eq!( |
| 324 | - validate_label(" bug ", "#FF0000").unwrap(), | 331 | + validate_label(" bug ", "#FF0000", "").unwrap(), |
| 325 | ("bug".to_string(), "#ff0000".to_string()) | 332 | ("bug".to_string(), "#ff0000".to_string()) |
| 326 | ); | 333 | ); |
| 327 | assert_eq!( | 334 | assert_eq!( |
| 328 | - validate_label("x", "#AbC").unwrap(), | 335 | + validate_label("x", "#AbC", "").unwrap(), |
| 329 | ("x".to_string(), "#aabbcc".to_string()) | 336 | ("x".to_string(), "#aabbcc".to_string()) |
| 330 | ); | 337 | ); |
| 331 | - assert!(validate_label("", "#fff").is_err()); | 338 | + assert!(validate_label("", "#fff", "").is_err()); |
| 332 | - assert!(validate_label(" ", "#fff").is_err()); | 339 | + assert!(validate_label(" ", "#fff", "").is_err()); |
| 333 | - assert!(validate_label(&"a".repeat(MAX_LABEL_NAME), "#fff").is_ok()); | 340 | + assert!(validate_label(&"a".repeat(MAX_LABEL_NAME), "#fff", "").is_ok()); |
| 334 | - assert!(validate_label(&"a".repeat(MAX_LABEL_NAME + 1), "#fff").is_err()); | 341 | + assert!(validate_label(&"a".repeat(MAX_LABEL_NAME + 1), "#fff", "").is_err()); |
| 335 | - assert!(validate_label("a\u{7}", "#fff").is_err()); | 342 | + assert!(validate_label("a\u{7}", "#fff", "").is_err()); |
| 343 | + assert!(validate_label("x", "#fff", &"d".repeat(MAX_LABEL_DESCRIPTION + 1)).is_err()); | ||
| 336 | } | 344 | } |
| 337 | 345 | ||
| 338 | #[test] | 346 | #[test] |
| @@ -340,7 +348,7 @@ mod tests { | |||
| 340 | for bad in [ | 348 | for bad in [ |
| 341 | "", "fff", "#ff", "#ffff", "#fffffff", "#gggggg", "red", "# fff", "#ff00 0", | 349 | "", "fff", "#ff", "#ffff", "#fffffff", "#gggggg", "red", "# fff", "#ff00 0", |
| 342 | ] { | 350 | ] { |
| 343 | - assert!(validate_label("x", bad).is_err(), "{bad:?} accepted"); | 351 | + assert!(validate_label("x", bad, "").is_err(), "{bad:?} accepted"); |
| 344 | } | 352 | } |
| 345 | } | 353 | } |
| 346 | 354 | ||
crates/forge-core/src/store/labels.rsmodified+17 -2
| @@ -20,6 +20,8 @@ use crate::time::now_stamp; | |||
| 20 | 20 | ||
| 21 | /// Most assignees one issue can have. | 21 | /// Most assignees one issue can have. |
| 22 | pub const MAX_ASSIGNEES: usize = 10; | 22 | pub const MAX_ASSIGNEES: usize = 10; |
| 23 | +/// Most labels one repo can have. | ||
| 24 | +pub const MAX_LABELS: i64 = 500; | ||
| 23 | 25 | ||
| 24 | const LABEL_TAKEN: &str = "a label with that name already exists"; | 26 | const LABEL_TAKEN: &str = "a label with that name already exists"; |
| 25 | const MILESTONE_TAKEN: &str = "a milestone with that title already exists"; | 27 | const MILESTONE_TAKEN: &str = "a milestone with that title already exists"; |
| @@ -92,7 +94,7 @@ impl Store { | |||
| 92 | color: &str, | 94 | color: &str, |
| 93 | description: &str, | 95 | description: &str, |
| 94 | ) -> Result<Label> { | 96 | ) -> Result<Label> { |
| 95 | - let (name, color) = validate_label(name, color)?; | 97 | + let (name, color) = validate_label(name, color, description)?; |
| 96 | let folded = normalize_name(&name); | 98 | let folded = normalize_name(&name); |
| 97 | let label = Label { | 99 | let label = Label { |
| 98 | id: new_id(), | 100 | id: new_id(), |
| @@ -102,6 +104,19 @@ impl Store { | |||
| 102 | description: description.to_string(), | 104 | description: description.to_string(), |
| 103 | }; | 105 | }; |
| 104 | let conn = self.inner.conn.lock().await; | 106 | let conn = self.inner.conn.lock().await; |
| 107 | + let count = fetch_opt( | ||
| 108 | + &conn, | ||
| 109 | + "SELECT COUNT(*) FROM labels WHERE repo_id = ?1", | ||
| 110 | + (repo_id.to_string(),), | ||
| 111 | + |row| col_int(row, 0), | ||
| 112 | + ) | ||
| 113 | + .await? | ||
| 114 | + .unwrap_or(0); | ||
| 115 | + if count >= MAX_LABELS { | ||
| 116 | + return Err(Error::Invalid(format!( | ||
| 117 | + "a repository can have at most {MAX_LABELS} labels" | ||
| 118 | + ))); | ||
| 119 | + } | ||
| 105 | conn.execute( | 120 | conn.execute( |
| 106 | "INSERT INTO labels (id, repo_id, name, name_folded, color, description) \ | 121 | "INSERT INTO labels (id, repo_id, name, name_folded, color, description) \ |
| 107 | VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | 122 | VALUES (?1, ?2, ?3, ?4, ?5, ?6)", |
| @@ -142,7 +157,7 @@ impl Store { | |||
| 142 | color: &str, | 157 | color: &str, |
| 143 | description: &str, | 158 | description: &str, |
| 144 | ) -> Result<()> { | 159 | ) -> Result<()> { |
| 145 | - let (name, color) = validate_label(name, color)?; | 160 | + let (name, color) = validate_label(name, color, description)?; |
| 146 | let folded = normalize_name(&name); | 161 | let folded = normalize_name(&name); |
| 147 | let conn = self.inner.conn.lock().await; | 162 | let conn = self.inner.conn.lock().await; |
| 148 | let n = conn | 163 | let n = conn |
crates/forge-git/src/service.rsmodified+8 -0
| @@ -37,6 +37,9 @@ impl GitService { | |||
| 37 | } | 37 | } |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | +/// The largest pack one push may send (2 GiB, like the LFS object limit). Larger files belong in LFS. | ||
| 41 | +const MAX_PUSH_CONFIG: &str = "receive.maxInputSize=2g"; | ||
| 42 | + | ||
| 40 | /// Builds `git <upload-pack|receive-pack> [--stateless-rpc] [--advertise-refs] -- <repo>`. | 43 | /// Builds `git <upload-pack|receive-pack> [--stateless-rpc] [--advertise-refs] -- <repo>`. |
| 41 | /// `git_protocol` is passed as `GIT_PROTOCOL`. For SSH, pass `stateless_rpc=false` and | 44 | /// `git_protocol` is passed as `GIT_PROTOCOL`. For SSH, pass `stateless_rpc=false` and |
| 42 | /// `advertise_refs=false` to get no flags. | 45 | /// `advertise_refs=false` to get no flags. |
| @@ -49,6 +52,11 @@ pub fn service_command( | |||
| 49 | git_protocol: Option<&str>, | 52 | git_protocol: Option<&str>, |
| 50 | ) -> GitCommand { | 53 | ) -> GitCommand { |
| 51 | let mut cmd = GitCommand::new(env, svc.subcommand()); | 54 | let mut cmd = GitCommand::new(env, svc.subcommand()); |
| 55 | + if svc == GitService::ReceivePack { | ||
| 56 | + // One push's pack may be at most this big; git refuses the rest and stops reading, so a | ||
| 57 | + // client (or a small gzip body that inflates) cannot stream into the disk without end. | ||
| 58 | + cmd = cmd.config(MAX_PUSH_CONFIG); | ||
| 59 | + } | ||
| 52 | if stateless_rpc { | 60 | if stateless_rpc { |
| 53 | cmd = cmd.arg("--stateless-rpc"); | 61 | cmd = cmd.arg("--stateless-rpc"); |
| 54 | } | 62 | } |
crates/forge-web/src/importer/mod.rsmodified+6 -1
| @@ -621,9 +621,14 @@ impl<A: RemoteApi> Run<'_, A> { | |||
| 621 | if self.mapped("label", &key).await?.is_some() { | 621 | if self.mapped("label", &key).await?.is_some() { |
| 622 | continue; | 622 | continue; |
| 623 | } | 623 | } |
| 624 | + let description: String = label | ||
| 625 | + .description | ||
| 626 | + .chars() | ||
| 627 | + .take(forge_core::issues::MAX_LABEL_DESCRIPTION) | ||
| 628 | + .collect(); | ||
| 624 | match self | 629 | match self |
| 625 | .store() | 630 | .store() |
| 626 | - .create_label(&self.repo.id, &label.name, &label.color, &label.description) | 631 | + .create_label(&self.repo.id, &label.name, &label.color, &description) |
| 627 | .await | 632 | .await |
| 628 | { | 633 | { |
| 629 | Ok(created) => { | 634 | Ok(created) => { |
crates/forge-web/src/pages/assets.rsmodified+34 -24
| @@ -13,6 +13,21 @@ use topcoat::router::{HeaderMap, StatusCode, header, route}; | |||
| 13 | use crate::accent; | 13 | use crate::accent; |
| 14 | use crate::custom_colour; | 14 | use crate::custom_colour; |
| 15 | use crate::render::highlight::highlight_css; | 15 | use crate::render::highlight::highlight_css; |
| 16 | +use crate::views::{NAV_SCRIPT_HREF, STYLESHEET_HREF, highlight_href}; | ||
| 17 | + | ||
| 18 | +/// Immutable only when the request names the current version (`href` is the URL the pages link). | ||
| 19 | +/// Any other `?v=` (a page from an older or newer build, or none) revalidates, so a mismatched | ||
| 20 | +/// version is never cached for a year under a URL that later means this build's file. | ||
| 21 | +fn cache_for(cx: &Cx, href: &str) -> topcoat::router::HeaderValue { | ||
| 22 | + let request = uri(cx); | ||
| 23 | + let current = href.split_once('?').map(|(_, query)| query); | ||
| 24 | + let value = if request.query() == current { | ||
| 25 | + "public, max-age=31536000, immutable" | ||
| 26 | + } else { | ||
| 27 | + "no-cache" | ||
| 28 | + }; | ||
| 29 | + topcoat::router::HeaderValue::from_static(value) | ||
| 30 | +} | ||
| 16 | 31 | ||
| 17 | const CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css")); | 32 | const CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/main.css")); |
| 18 | 33 | ||
| @@ -24,9 +39,9 @@ pub fn highlight_css_text() -> &'static str { | |||
| 24 | HIGHLIGHT_CSS.get_or_init(highlight_css).as_str() | 39 | HIGHLIGHT_CSS.get_or_init(highlight_css).as_str() |
| 25 | } | 40 | } |
| 26 | 41 | ||
| 27 | -/// Immutable: the URL carries a content hash, so a changed file gets a new URL. | 42 | +/// Immutable when the URL carries the current content hash, so a changed file gets a new URL. |
| 28 | #[route(GET "/assets/main.css")] | 43 | #[route(GET "/assets/main.css")] |
| 29 | -async fn main_css() -> Result<(StatusCode, HeaderMap, &'static str)> { | 44 | +async fn main_css(cx: &Cx) -> Result<(StatusCode, HeaderMap, &'static str)> { |
| 30 | let mut headers = HeaderMap::new(); | 45 | let mut headers = HeaderMap::new(); |
| 31 | headers.insert( | 46 | headers.insert( |
| 32 | header::CONTENT_TYPE, | 47 | header::CONTENT_TYPE, |
| @@ -34,12 +49,7 @@ async fn main_css() -> Result<(StatusCode, HeaderMap, &'static str)> { | |||
| 34 | .parse() | 49 | .parse() |
| 35 | .expect("static header value"), | 50 | .expect("static header value"), |
| 36 | ); | 51 | ); |
| 37 | - headers.insert( | 52 | + headers.insert(header::CACHE_CONTROL, cache_for(cx, STYLESHEET_HREF)); |
| 38 | - header::CACHE_CONTROL, | ||
| 39 | - "public, max-age=31536000, immutable" | ||
| 40 | - .parse() | ||
| 41 | - .expect("static header value"), | ||
| 42 | - ); | ||
| 43 | Ok((StatusCode::OK, headers, CSS)) | 53 | Ok((StatusCode::OK, headers, CSS)) |
| 44 | } | 54 | } |
| 45 | 55 | ||
| @@ -49,7 +59,7 @@ const NAV_JS: &str = include_str!("../../assets/nav.js"); | |||
| 49 | 59 | ||
| 50 | /// Immutable like `main.css`: the URL carries a content hash. | 60 | /// Immutable like `main.css`: the URL carries a content hash. |
| 51 | #[route(GET "/assets/nav.js")] | 61 | #[route(GET "/assets/nav.js")] |
| 52 | -async fn nav_js() -> Result<(StatusCode, HeaderMap, &'static str)> { | 62 | +async fn nav_js(cx: &Cx) -> Result<(StatusCode, HeaderMap, &'static str)> { |
| 53 | let mut headers = HeaderMap::new(); | 63 | let mut headers = HeaderMap::new(); |
| 54 | headers.insert( | 64 | headers.insert( |
| 55 | header::CONTENT_TYPE, | 65 | header::CONTENT_TYPE, |
| @@ -57,18 +67,13 @@ async fn nav_js() -> Result<(StatusCode, HeaderMap, &'static str)> { | |||
| 57 | .parse() | 67 | .parse() |
| 58 | .expect("static header value"), | 68 | .expect("static header value"), |
| 59 | ); | 69 | ); |
| 60 | - headers.insert( | 70 | + headers.insert(header::CACHE_CONTROL, cache_for(cx, NAV_SCRIPT_HREF)); |
| 61 | - header::CACHE_CONTROL, | ||
| 62 | - "public, max-age=31536000, immutable" | ||
| 63 | - .parse() | ||
| 64 | - .expect("static header value"), | ||
| 65 | - ); | ||
| 66 | Ok((StatusCode::OK, headers, NAV_JS)) | 71 | Ok((StatusCode::OK, headers, NAV_JS)) |
| 67 | } | 72 | } |
| 68 | 73 | ||
| 69 | /// Immutable like `main.css`: the URL carries a content hash. | 74 | /// Immutable like `main.css`: the URL carries a content hash. |
| 70 | #[route(GET "/assets/highlight.css")] | 75 | #[route(GET "/assets/highlight.css")] |
| 71 | -async fn highlight_css_route() -> Result<(StatusCode, HeaderMap, &'static str)> { | 76 | +async fn highlight_css_route(cx: &Cx) -> Result<(StatusCode, HeaderMap, &'static str)> { |
| 72 | let mut headers = HeaderMap::new(); | 77 | let mut headers = HeaderMap::new(); |
| 73 | headers.insert( | 78 | headers.insert( |
| 74 | header::CONTENT_TYPE, | 79 | header::CONTENT_TYPE, |
| @@ -76,12 +81,7 @@ async fn highlight_css_route() -> Result<(StatusCode, HeaderMap, &'static str)> | |||
| 76 | .parse() | 81 | .parse() |
| 77 | .expect("static header value"), | 82 | .expect("static header value"), |
| 78 | ); | 83 | ); |
| 79 | - headers.insert( | 84 | + headers.insert(header::CACHE_CONTROL, cache_for(cx, highlight_href())); |
| 80 | - header::CACHE_CONTROL, | ||
| 81 | - "public, max-age=31536000, immutable" | ||
| 82 | - .parse() | ||
| 83 | - .expect("static header value"), | ||
| 84 | - ); | ||
| 85 | Ok((StatusCode::OK, headers, highlight_css_text())) | 85 | Ok((StatusCode::OK, headers, highlight_css_text())) |
| 86 | } | 86 | } |
| 87 | 87 | ||
| @@ -323,7 +323,7 @@ mod tests { | |||
| 323 | let router: Router = Router::builder().discover().cookies().build(); | 323 | let router: Router = Router::builder().discover().cookies().build(); |
| 324 | let req = Request::builder() | 324 | let req = Request::builder() |
| 325 | .method("GET") | 325 | .method("GET") |
| 326 | - .uri("/assets/main.css") | 326 | + .uri(crate::views::STYLESHEET_HREF) |
| 327 | .body(Body::empty()) | 327 | .body(Body::empty()) |
| 328 | .unwrap(); | 328 | .unwrap(); |
| 329 | let res = router.handle(req).await; | 329 | let res = router.handle(req).await; |
| @@ -349,7 +349,7 @@ mod tests { | |||
| 349 | let router: Router = Router::builder().discover().cookies().build(); | 349 | let router: Router = Router::builder().discover().cookies().build(); |
| 350 | // Each bundle is immutable under its own type. Its body holds the marker, when one is given. | 350 | // Each bundle is immutable under its own type. Its body holds the marker, when one is given. |
| 351 | for (uri, kind, marker) in [ | 351 | for (uri, kind, marker) in [ |
| 352 | - ("/assets/main.css", "text/css; charset=utf-8", ""), | 352 | + (crate::views::STYLESHEET_HREF, "text/css; charset=utf-8", ""), |
| 353 | ( | 353 | ( |
| 354 | crate::views::NAV_SCRIPT_HREF, | 354 | crate::views::NAV_SCRIPT_HREF, |
| 355 | "text/javascript; charset=utf-8", | 355 | "text/javascript; charset=utf-8", |
| @@ -380,6 +380,16 @@ mod tests { | |||
| 380 | assert!(!body.is_empty(), "{uri}"); | 380 | assert!(!body.is_empty(), "{uri}"); |
| 381 | assert!(String::from_utf8_lossy(&body).contains(marker), "{uri}"); | 381 | assert!(String::from_utf8_lossy(&body).contains(marker), "{uri}"); |
| 382 | } | 382 | } |
| 383 | + // A version other than the current one revalidates instead of being cached for a year. | ||
| 384 | + let req = Request::builder() | ||
| 385 | + .method("GET") | ||
| 386 | + .uri("/assets/main.css?v=000000000000") | ||
| 387 | + .body(Body::empty()) | ||
| 388 | + .unwrap(); | ||
| 389 | + assert_eq!( | ||
| 390 | + router.handle(req).await.headers()["cache-control"], | ||
| 391 | + "no-cache" | ||
| 392 | + ); | ||
| 383 | } | 393 | } |
| 384 | 394 | ||
| 385 | #[tokio::test] | 395 | #[tokio::test] |
crates/forge-web/src/pages/labels_page.rsmodified+2 -2
| @@ -125,7 +125,7 @@ async fn labels_body( | |||
| 125 | </label> | 125 | </label> |
| 126 | <label> | 126 | <label> |
| 127 | <span>(g::t(lang, K::Description))</span> | 127 | <span>(g::t(lang, K::Description))</span> |
| 128 | - <input type="text" name="description" value=(row.description.as_str()) autocomplete="off"> | 128 | + <input type="text" name="description" value=(row.description.as_str()) autocomplete="off" maxlength="500"> |
| 129 | </label> | 129 | </label> |
| 130 | <div> | 130 | <div> |
| 131 | <button type="submit">(g::t(lang, K::SaveLabel))</button> | 131 | <button type="submit">(g::t(lang, K::SaveLabel))</button> |
| @@ -157,7 +157,7 @@ async fn labels_body( | |||
| 157 | </label> | 157 | </label> |
| 158 | <label> | 158 | <label> |
| 159 | <span>(g::t(lang, K::Description))</span> | 159 | <span>(g::t(lang, K::Description))</span> |
| 160 | - <input type="text" name="description" autocomplete="off"> | 160 | + <input type="text" name="description" autocomplete="off" maxlength="500"> |
| 161 | </label> | 161 | </label> |
| 162 | <div> | 162 | <div> |
| 163 | <button type="submit" class="primary">(g::t(lang, K::CreateLabel))</button> | 163 | <button type="submit" class="primary">(g::t(lang, K::CreateLabel))</button> |