icons: bound inline data URLs before decoding (L7)
This commit is contained in:
2 files changed
+27
-1
No files matched your search
@@ -928,7 +928,9 @@ UI lives in `data/resources/ui/*.blp` (Blueprint) and is compiled to `.ui` by
|
||||
reintroduce "no icon file ⇒ fetch": `changes` fires on every answered request,
|
||||
so that is an unbounded background loop and a multiplied presence leak.
|
||||
- **Hardening (all of it mandatory).** `https` only (plus inline
|
||||
`data:image/*;base64`, which never leaves the process); resolve with
|
||||
`data:image/*;base64`, which never leaves the process, bounded by a length
|
||||
check **before** base64 decoding so a hostile payload cannot allocate past the
|
||||
cap — L7); resolve with
|
||||
`tokio::net::lookup_host` and refuse any non-public address — IPv4 rejects
|
||||
loopback, private, link-local, CGNAT `100.64/10`, `0.0.0.0/8`, `192.0.0/24`,
|
||||
`198.18/15`, reserved and multicast; IPv6 is **allow-listed** to global unicast
|
||||
|
||||
@@ -313,6 +313,12 @@ fn decode_data_url(rest: &str) -> Result<Vec<u8>> {
|
||||
if !meta.contains(";base64") {
|
||||
return Err("only base64 data URLs are supported".to_string());
|
||||
}
|
||||
// Reject an over-sized payload **before** decoding: base64 expands 3 bytes
|
||||
// to 4, so this bound is exactly the encoded length of `MAX_BYTES` plus
|
||||
// padding. Decoding first would allocate the whole payload only to reject it.
|
||||
if payload.len() > ((MAX_BYTES / 3) + 1) * 4 {
|
||||
return Err("the body is larger than 1 MiB".to_string());
|
||||
}
|
||||
use base64::Engine;
|
||||
let bytes = base64::engine::general_purpose::STANDARD
|
||||
.decode(payload)
|
||||
@@ -802,6 +808,24 @@ mod tests {
|
||||
assert!(decode_data_url("image/png;base64").is_err(), "no comma");
|
||||
}
|
||||
|
||||
/// An over-sized inline data URL is refused **before** base64 decoding (L7):
|
||||
/// the size error fires, not the base64 error, even for invalid base64.
|
||||
#[test]
|
||||
fn an_oversized_data_url_is_refused_before_decoding() {
|
||||
use base64::Engine;
|
||||
|
||||
let too_long = ((MAX_BYTES / 3) + 1) * 4 + 1;
|
||||
let error = decode_data_url(&format!("image/png;base64,{}", "!".repeat(too_long)))
|
||||
.expect_err("must refuse");
|
||||
assert!(error.contains("larger than 1 MiB"), "got: {error}");
|
||||
|
||||
// A small valid PNG data URL still decodes.
|
||||
let png =
|
||||
base64::engine::general_purpose::STANDARD.encode([0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
let ok = decode_data_url(&format!("image/png;base64,{png}")).expect("decode");
|
||||
assert_eq!(&ok, &[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_url_is_not_retried_until_it_changes() {
|
||||
let store = IconStore::at(temp_dir());
|
||||
|
||||
Reference in new issue
Block a user