Files
furumi-web/src/podcasts/net_tests.rs
T
sanapachandClaude Opus 5.5 c51a9af9e5 Keep network details and URLs out of podcast fetch errors
Users see the kind of failure only; resolved addresses and transport
details go to the logs, without URLs. Feed and audio requests are
limited to the web ports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 15:12:38 +02:00

439 lines
16 KiB
Rust

//! SSRF guard: address classes, URL checks, the resolver, DNS rebinding,
//! redirects and headers. Network tests use the local test server; loopback
//! is allowed only through the test-only `AddressPolicy::test_allow_loopback`.
use std::net::IpAddr;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use reqwest::Url;
use reqwest::header::HeaderMap;
use super::net::{
AddressClass, AddressPolicy, BlockedAddress, GuardError, GuardedResolver, SafeHttp, check_url,
classify,
};
use super::test_server::{Route, ScriptedLookup, TEST_HTTP, TestServer, test_http};
// ---------------------------------------------------------------------------
// Address classes
// ---------------------------------------------------------------------------
fn ip(text: &str) -> IpAddr {
text.parse().unwrap()
}
#[test]
fn public_addresses_are_allowed() {
for address in [
"8.8.8.8",
"1.1.1.1",
"93.184.215.14",
"172.32.0.1",
"100.128.0.1",
"192.0.3.1",
"2606:4700:4700::1111",
"2a00:1450:4001:80b::200e",
"::ffff:8.8.8.8",
"64:ff9b::808:808",
"2002:808:808::1",
] {
assert_eq!(classify(ip(address)), None, "{address}");
assert_eq!(AddressPolicy::PUBLIC_ONLY.check(ip(address)), Ok(()));
}
}
#[test]
fn non_public_addresses_are_classified() {
use AddressClass::*;
let cases = [
// IPv4
("0.0.0.0", Unspecified),
("0.1.2.3", Unspecified),
("10.1.2.3", Private),
("100.64.0.1", SharedCgnat),
("100.127.255.254", SharedCgnat),
("127.0.0.1", Loopback),
("127.255.0.9", Loopback),
("169.254.1.1", LinkLocal),
("169.254.169.254", CloudMetadata),
("169.254.170.2", CloudMetadata),
("100.100.100.200", CloudMetadata),
("172.16.0.1", Private),
("172.31.255.255", Private),
("192.0.0.8", Reserved),
("192.0.2.5", Documentation),
("192.88.99.1", Reserved),
("192.168.1.1", Private),
("198.18.0.1", Benchmarking),
("198.19.255.255", Benchmarking),
("198.51.100.1", Documentation),
("203.0.113.1", Documentation),
("224.0.0.1", Multicast),
("239.255.255.250", Multicast),
("240.0.0.1", Reserved),
("255.255.255.255", Broadcast),
// IPv6
("::", Unspecified),
("::1", Loopback),
("::ffff:127.0.0.1", Loopback),
("::ffff:10.0.0.1", Private),
("::ffff:169.254.169.254", CloudMetadata),
("::ffff:0.0.0.0", Unspecified),
("::127.0.0.1", Reserved),
("::8.8.8.8", Reserved),
("64:ff9b::a00:1", Private),
("64:ff9b::7f00:1", Loopback),
("64:ff9b:1::1", Reserved),
("2002:7f00:1::1", Loopback),
("2002:a9fe:a9fe::1", CloudMetadata),
("2002:c0a8:101::1", Private),
("100::1", Reserved),
("2001::1", Reserved),
("2001:2::1", Benchmarking),
("2001:db8::1", Documentation),
("3fff::1", Documentation),
("fc00::1", UniqueLocal),
("fd12:3456::1", UniqueLocal),
("fd00:ec2::254", CloudMetadata),
("fe80::1", LinkLocal),
("febf::1", LinkLocal),
("fec0::1", Private),
("ff02::1", Multicast),
("4000::1", Reserved),
("::2", Reserved),
];
for (address, class) in cases {
assert_eq!(classify(ip(address)), Some(class), "{address}");
assert_eq!(
AddressPolicy::PUBLIC_ONLY.check(ip(address)),
Err(class),
"{address}"
);
}
}
#[test]
fn only_the_test_policy_allows_loopback_and_nothing_else() {
let test = AddressPolicy::test_allow_loopback();
assert_eq!(test.check(ip("127.0.0.1")), Ok(()));
assert_eq!(test.check(ip("::1")), Ok(()));
assert_eq!(test.check(ip("::ffff:127.0.0.1")), Ok(()));
assert_eq!(test.check(ip("10.0.0.1")), Err(AddressClass::Private));
assert_eq!(
test.check(ip("169.254.169.254")),
Err(AddressClass::CloudMetadata)
);
assert_eq!(
AddressPolicy::PUBLIC_ONLY.check(ip("127.0.0.1")),
Err(AddressClass::Loopback)
);
}
fn url(text: &str) -> Url {
Url::parse(text).unwrap()
}
#[test]
fn urls_are_checked_before_connecting() {
let policy = AddressPolicy::PUBLIC_ONLY;
for (text, class) in [
("http://127.0.0.1/feed", AddressClass::Loopback),
// Every IPv4 spelling is normalized by the URL parser.
("http://2130706433/feed", AddressClass::Loopback),
("http://0x7f.1/feed", AddressClass::Loopback),
("http://127.1/feed", AddressClass::Loopback),
("http://0177.0.0.1/feed", AddressClass::Loopback),
("http://127.0.0.1./feed", AddressClass::Loopback),
("http://0/feed", AddressClass::Unspecified),
("http://[::1]:8080/feed", AddressClass::Loopback),
("http://[::ffff:7f00:1]/feed", AddressClass::Loopback),
("http://[::ffff:a9fe:a9fe]/", AddressClass::CloudMetadata),
(
"http://169.254.169.254/latest/meta-data/",
AddressClass::CloudMetadata,
),
("https://10.0.0.5/audio.mp3", AddressClass::Private),
("https://[fd00::1]/audio.mp3", AddressClass::UniqueLocal),
] {
match check_url(policy, &url(text)) {
Err(GuardError::Blocked(blocked)) => assert_eq!(blocked.class, class, "{text}"),
other => panic!("{text}: {other:?}"),
}
}
assert!(matches!(
check_url(policy, &url("ftp://example.com/feed")),
Err(GuardError::UnsupportedScheme(_))
));
assert!(matches!(
check_url(policy, &url("file:///etc/passwd")),
Err(GuardError::UnsupportedScheme(_))
));
assert!(matches!(
check_url(policy, &url("https://user:secret@example.com/feed")),
Err(GuardError::Credentials)
));
assert!(matches!(
check_url(policy, &url("https://user@example.com/feed")),
Err(GuardError::Credentials)
));
// Names are checked by the resolver at connect time.
assert!(check_url(policy, &url("https://feeds.example.com/rss")).is_ok());
assert!(check_url(policy, &url("http://localhost/rss")).is_ok());
}
#[test]
fn only_web_ports_are_allowed() {
// A public address can be the server's own; its databases and admin
// ports must not be reachable through feed or audio requests.
let policy = AddressPolicy::PUBLIC_ONLY;
for text in [
"https://feeds.example.com/rss",
"http://feeds.example.com/rss",
"https://feeds.example.com:443/rss",
"http://feeds.example.com:8080/rss",
"https://feeds.example.com:8443/rss",
"https://8.8.8.8/rss",
] {
assert!(check_url(policy, &url(text)).is_ok(), "{text}");
}
for (text, port) in [
("http://feeds.example.com:5432/", 5432),
("https://feeds.example.com:22/", 22),
("http://8.8.8.8:6379/feed", 6379),
("https://feeds.example.com:3000/", 3000),
] {
match check_url(policy, &url(text)) {
Err(error @ GuardError::Port(refused)) => {
assert_eq!(refused, port, "{text}");
assert!(error.is_refusal());
}
other => panic!("{text}: {other:?}"),
}
}
// The local test servers listen on random ports.
assert!(
check_url(
AddressPolicy::test_allow_loopback(),
&url("http://127.0.0.1:5432/")
)
.is_ok()
);
}
// ---------------------------------------------------------------------------
// Resolver
// ---------------------------------------------------------------------------
fn blocked_class(error: &(dyn std::error::Error + Send + Sync + 'static)) -> Option<AddressClass> {
error
.downcast_ref::<BlockedAddress>()
.map(|blocked| blocked.class)
}
#[tokio::test]
async fn resolver_rejects_the_whole_answer_when_any_address_is_not_public() {
let lookup = ScriptedLookup::with(&[
("public.test", &[&["8.8.8.8", "2606:4700:4700::1111"]]),
("mixed.test", &[&["8.8.8.8", "10.0.0.1"]]),
("mapped.test", &[&["::ffff:192.168.0.10"]]),
("localhost", &[&["127.0.0.1", "::1"]]),
("empty.test", &[&[]]),
]);
let resolver = GuardedResolver::new(AddressPolicy::PUBLIC_ONLY, lookup);
assert_eq!(
resolver.resolve_checked("public.test").await.unwrap(),
vec![ip("8.8.8.8"), ip("2606:4700:4700::1111")]
);
let error = resolver.resolve_checked("mixed.test").await.unwrap_err();
assert_eq!(blocked_class(error.as_ref()), Some(AddressClass::Private));
let error = resolver.resolve_checked("mapped.test").await.unwrap_err();
assert_eq!(blocked_class(error.as_ref()), Some(AddressClass::Private));
let error = resolver.resolve_checked("localhost").await.unwrap_err();
assert_eq!(blocked_class(error.as_ref()), Some(AddressClass::Loopback));
assert!(resolver.resolve_checked("empty.test").await.is_err());
assert!(resolver.resolve_checked("nowhere.test").await.is_err());
}
fn public_http(lookup: Arc<ScriptedLookup>) -> SafeHttp {
SafeHttp::with_lookup(TEST_HTTP, AddressPolicy::PUBLIC_ONLY, lookup).unwrap()
}
#[tokio::test]
async fn production_policy_never_reaches_a_local_server() {
let server = TestServer::start();
server.set("/feed", Route::new("ok", "text/plain"));
let lookup = ScriptedLookup::with(&[("loop.test", &[&["127.0.0.1"]])]);
let http = public_http(Arc::clone(&lookup));
match http.get(url(&server.url("/feed")), &HeaderMap::new()).await {
Err(GuardError::Blocked(blocked)) => assert_eq!(blocked.class, AddressClass::Loopback),
other => panic!("{other:?}"),
}
// The test server's random port is refused before any lookup.
let named = url(&server.url_on("loop.test", "/feed"));
match http.get(named.clone(), &HeaderMap::new()).await {
Err(GuardError::Port(port)) => assert_eq!(Some(port), named.port()),
other => panic!("{other:?}"),
}
assert_eq!(lookup.calls.load(Ordering::SeqCst), 0);
// On an allowed port the resolver refuses the loopback answer.
let resolver = GuardedResolver::new(AddressPolicy::PUBLIC_ONLY, lookup);
let error = resolver.resolve_checked("loop.test").await.unwrap_err();
assert_eq!(blocked_class(error.as_ref()), Some(AddressClass::Loopback));
assert_eq!(server.request_count(), 0);
}
#[tokio::test]
async fn every_connection_uses_the_addresses_it_checked() {
// DNS rebinding: the first answer is allowed, the next one points to a
// private address. Each connection resolves and checks once and
// connects to exactly those addresses, so the rebound answer is refused
// instead of being used after an earlier successful check.
let server = TestServer::start();
server.set("/feed", Route::new("first", "text/plain"));
let lookup = ScriptedLookup::with(&[("rebind.test", &[&["127.0.0.1"], &["10.0.0.7"]])]);
let http = test_http(Arc::clone(&lookup));
let target = url(&server.url_on("rebind.test", "/feed"));
let first = http.get(target.clone(), &HeaderMap::new()).await.unwrap();
assert_eq!(first.response.text().await.unwrap(), "first");
match http.get(target, &HeaderMap::new()).await {
Err(GuardError::Blocked(blocked)) => {
assert_eq!(blocked.class, AddressClass::Private);
assert_eq!(blocked.address, ip("10.0.0.7"));
}
other => panic!("{other:?}"),
}
assert_eq!(lookup.calls.load(Ordering::SeqCst), 2);
assert_eq!(server.requests("/feed").len(), 1);
}
#[tokio::test]
async fn every_redirect_hop_is_checked() {
let server = TestServer::start();
let lookup = ScriptedLookup::with(&[("intranet.test", &[&["192.168.1.20"]])]);
let http = test_http(lookup);
let cases = [
(
"/to-private",
"http://10.0.0.1/feed".to_string(),
AddressClass::Private,
),
(
"/to-metadata",
"http://169.254.169.254/latest/meta-data/".to_string(),
AddressClass::CloudMetadata,
),
(
"/to-mapped",
"http://[::ffff:10.1.1.1]/".to_string(),
AddressClass::Private,
),
(
"/to-name",
server.url_on("intranet.test", "/feed"),
AddressClass::Private,
),
];
for (path, location, class) in &cases {
server.set(path, Route::redirect(302, location));
match http.get(url(&server.url(path)), &HeaderMap::new()).await {
Err(GuardError::Blocked(blocked)) => assert_eq!(blocked.class, *class, "{path}"),
other => panic!("{path}: {other:?}"),
}
}
server.set("/to-file", Route::redirect(301, "file:///etc/passwd"));
assert!(matches!(
http.get(url(&server.url("/to-file")), &HeaderMap::new())
.await,
Err(GuardError::UnsupportedScheme(_))
));
server.set("/no-location", Route::new("", "text/plain").status(302));
assert!(matches!(
http.get(url(&server.url("/no-location")), &HeaderMap::new())
.await,
Err(GuardError::InvalidRedirect(_))
));
// Hop limit (3 in the test client): /hop0 -> /hop1 -> ... -> /hop4.
for hop in 0..4 {
server.set(
&format!("/hop{hop}"),
Route::redirect(307, &format!("/hop{}", hop + 1)),
);
}
server.set("/hop4", Route::new("end", "text/plain"));
assert!(matches!(
http.get(url(&server.url("/hop0")), &HeaderMap::new()).await,
Err(GuardError::TooManyRedirects(3))
));
let followed = http
.get(url(&server.url("/hop1")), &HeaderMap::new())
.await
.unwrap();
assert_eq!(followed.redirects.len(), 3);
assert_eq!(followed.final_url.path(), "/hop4");
}
#[tokio::test]
async fn transport_errors_never_carry_the_url() {
// Feed and enclosure URLs often carry access tokens; errors are logged.
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
drop(listener);
let http = test_http(ScriptedLookup::with(&[]));
let error = http
.get(
url(&format!(
"http://127.0.0.1:{port}/private/feed.xml?token=secret"
)),
&HeaderMap::new(),
)
.await
.unwrap_err();
assert!(matches!(error, GuardError::Http(_)), "{error:?}");
let text = error.to_string();
assert!(
!text.contains("secret") && !text.contains("feed.xml"),
"{text}"
);
assert!(!format!("{error:?}").contains("secret"));
// A redirect to an unusable location does not echo it either.
let server = TestServer::start();
server.set("/bad", Route::redirect(302, "http://[::1/?token=secret"));
let error = http
.get(url(&server.url("/bad")), &HeaderMap::new())
.await
.unwrap_err();
assert!(matches!(error, GuardError::InvalidRedirect(_)), "{error:?}");
assert!(!error.to_string().contains("secret"), "{error}");
}
#[tokio::test]
async fn no_cookies_or_credentials_are_sent() {
let server = TestServer::start();
server.set(
"/start",
Route::redirect(302, "/next").header("Set-Cookie", "session=leak; Path=/"),
);
server.set(
"/next",
Route::new("ok", "text/plain").header("Set-Cookie", "other=leak; Path=/"),
);
let http = test_http(ScriptedLookup::with(&[]));
for _ in 0..2 {
let followed = http
.get(url(&server.url("/start")), &HeaderMap::new())
.await
.unwrap();
assert_eq!(followed.response.text().await.unwrap(), "ok");
}
for request in server
.requests("/start")
.iter()
.chain(&server.requests("/next"))
{
for header in ["cookie", "authorization", "referer", "proxy-authorization"] {
assert!(!request.contains_key(header), "{header} sent: {request:?}");
}
}
}