icons: harden the HTTPS fetch against SSRF (M2)

The single HTTPS fetch now sets no_proxy, rejects URL userinfo on every hop, and allow-lists IPv6 to 2000::/3 minus the special ranges; IPv4 rejects all of 0.0.0.0/8.
This commit is contained in:
raucao committed 2026-11-30 10:31:00 +04:00
1 parent c435fccd8f
commit 8df0357d13
2 files changed
+74 -13

No files matched your search

+13 -4
View File
@@ -848,12 +848,21 @@ UI lives in `data/resources/ui/*.blp` (Blueprint) and is compiled to `.ui` by
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
`tokio::net::lookup_host` and refuse any non-public address (loopback, private,
link-local, CGNAT `100.64/10`, `192.0.0/24`, `198.18/15`, reserved, multicast,
IPv6 ULA `fc00::/7`, `::1`, mapped-v4) — fail closed on a mixed answer; pin the
`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
`2000::/3` minus `2001::/23`, `2001:db8::/32`, `2002::/16` and `3fff::/20`
(which also refuses IPv4-compatible `::/96`, IPv4-translated, NAT64
`64:ff9b::/96`, ULA `fc00::/7`, link-local and discard ranges; IPv4-mapped
`::ffff:0:0/96` is judged as its embedded v4) — fail closed on a mixed answer; pin the
validated addresses; follow ≤3 redirects manually, re-validating and re-pinning
each `Location` (a scheme downgrade or a private address dies at the hop);
`timeout(5s)`, `connect_timeout(3s)`, no cookies/auth/proxy; stream the body
`timeout(5s)`, `connect_timeout(3s)`, no cookies/auth/proxy — **`.no_proxy()`
is mandatory**: without it an `HTTPS_PROXY`/`ALL_PROXY` would override the
pinned addresses and could reach internal hosts, at the cost that a host behind
a mandatory corporate proxy cannot serve icons or NIP-05 (fail-soft: initials /
unverified address). URLs with embedded userinfo are refused on every hop (a
`Location:` can carry it). Stream the body
with a **1 MiB cap**. The format gate is **magic bytes**, not `Content-Type`:
png/jpeg/gif/webp only, SVG and every other vector rejected **before** decode (a
sub-KiB SVG can allocate GBs, so the byte cap alone is not enough); a ≤4096²
+61 -9
View File
@@ -88,7 +88,7 @@ pub async fn fetch(url: &str) -> Result<Vec<u8>> {
/// Fetch bytes over `https`, hardened: public addresses only (resolved and then
/// pinned into the per-fetch client), ≤3 redirects re-validated and re-pinned at
/// every hop, a bounded body, no cookies/auth/proxy.
/// every hop, a bounded body, no cookies/auth/**proxy**.
///
/// The **only** HTTP fetch path in the app: the icon fetcher and the NIP-05
/// verifier (`store::nip05`) both go through here, so there is one place to
@@ -99,6 +99,12 @@ pub async fn fetch_https(url: &str, expect: Content) -> Result<Vec<u8>> {
if url.scheme() != "https" {
return Err(format!("refusing to fetch a {} URL", url.scheme()));
}
// Reject userinfo on **every** hop: a `Location:` can carry it, and
// reqwest would send it as Basic auth. Checked here, before any DNS or
// network work.
if !url.username().is_empty() || url.password().is_some() {
return Err("refusing a URL with embedded credentials".to_string());
}
let host = url.host_str().ok_or("the URL has no host")?.to_string();
let port = url.port_or_known_default().ok_or("the URL has no port")?;
let addrs = resolve_public(&host, port).await?;
@@ -106,8 +112,13 @@ pub async fn fetch_https(url: &str, expect: Content) -> Result<Vec<u8>> {
// `resolve_to_addrs` is baked in at build time, so the *client* is what
// carries the pin — a shared, process-wide client could not. Building one
// per hop is fine: icon fetches are rare and not latency-sensitive.
//
// `no_proxy` is mandatory: without it an `HTTPS_PROXY`/`ALL_PROXY`
// environment variable would override the pinned addresses and route the
// request through a proxy that can reach internal hosts.
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.no_proxy()
.timeout(TIMEOUT)
.connect_timeout(CONNECT_TIMEOUT)
.resolve_to_addrs(&host, &addrs)
@@ -209,6 +220,10 @@ fn is_public_v4(ip: Ipv4Addr) -> bool {
return false;
}
let o = ip.octets();
// 0.0.0.0/8 "this network" — `is_unspecified` only catches 0.0.0.0 itself.
if o[0] == 0 {
return false;
}
// 100.64.0.0/10 carrier-grade NAT.
if o[0] == 100 && (o[1] & 0xc0) == 64 {
return false;
@@ -237,22 +252,37 @@ fn is_public_v6(ip: Ipv6Addr) -> bool {
return false;
}
let segments = ip.segments();
// fc00::/7 unique local.
if (segments[0] & 0xfe00) == 0xfc00 {
let o = ip.octets();
// IPv4-mapped ::ffff:0:0/96 — judge the embedded v4. Evaluated **before** the
// global-unicast allow-list below, which would otherwise reject a
// mapped-public address (a test depends on `::ffff:8.8.8.8` staying public).
if o[..10].iter().all(|byte| *byte == 0) && o[10] == 0xff && o[11] == 0xff {
return is_public_v4(Ipv4Addr::new(o[12], o[13], o[14], o[15]));
}
// Allow only global unicast 2000::/3, then exclude the special ranges inside
// it. Everything else falls outside 2000::/3 and is refused — including
// ::/96 (IPv4-compatible), ::ffff:0:0:0/96 (IPv4-translated), NAT64
// 64:ff9b::/96 and 64:ff9b:1::/48, ULA fc00::/7, link-local fe80::/10,
// discard 100::/64 and site-local fec0::/10.
if (segments[0] & 0xe000) != 0x2000 {
return false;
}
// fe80::/10 link-local.
if (segments[0] & 0xffc0) == 0xfe80 {
// 2001::/23 (IETF protocol assignments / Teredo).
if segments[0] == 0x2001 && (segments[1] & 0xfe00) == 0 {
return false;
}
// 2001:db8::/32 documentation.
if segments[0] == 0x2001 && segments[1] == 0x0db8 {
return false;
}
// IPv4-mapped ::ffff:0:0/96 — judge the embedded v4.
let o = ip.octets();
if o[..10].iter().all(|byte| *byte == 0) && o[10] == 0xff && o[11] == 0xff {
return is_public_v4(Ipv4Addr::new(o[12], o[13], o[14], o[15]));
// 2002::/16 6to4.
if segments[0] == 0x2002 {
return false;
}
// 3ff0::/12 (covers 3fff::/20 and the deprecated 6bone 3ffe::/16) — an
// over-broad but fail-closed refusal of the top of 2000::/3.
if (segments[0] & 0xfff0) == 0x3ff0 {
return false;
}
true
}
@@ -712,10 +742,22 @@ mod tests {
"255.255.255.255",
"192.0.2.1",
"198.18.0.1",
"0.1.2.3",
"::1",
"fc00::1",
"fe80::1",
"::ffff:127.0.0.1",
// IPv4-compatible and IPv4-translated, NAT64, 6to4, Teredo/IETF,
// discard and site-local must all be refused.
"::127.0.0.1",
"::ffff:0:0:0:1",
"64:ff9b::169.254.169.254",
"64:ff9b:1::10.0.0.1",
"2002::1",
"2001::1",
"100::1",
"fec0::1",
"3fff::1",
] {
assert!(!is_public(parse_ip(text)), "{text} must not be public");
}
@@ -728,6 +770,16 @@ mod tests {
}
}
/// A URL with embedded credentials is refused before any DNS or network work
/// (the check runs before `resolve_public`, so this needs no network).
#[tokio::test]
async fn a_url_with_userinfo_is_refused() {
let error = fetch_https("https://user:pass@example.com/i.png", Content::Image)
.await
.expect_err("userinfo must be refused");
assert!(error.contains("credentials"), "{error}");
}
#[test]
fn only_raster_magic_bytes_pass() {
assert!(sniff_raster(&[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]).is_ok());