diff --git a/docs/administration.md b/docs/administration.md index b75ecc1..6c266fa 100644 --- a/docs/administration.md +++ b/docs/administration.md @@ -140,6 +140,11 @@ other port, are refused. System proxy settings are not used for these requests. Users only see the kind of failure (address not allowed, could not connect, HTTP status, not a feed); the details, without the URL, are in the server log and, for scheduled refreshes, in the job log with the podcast id. +"Address not allowed" is only said for what the URL itself shows (scheme, +credentials, port, an IP address). A host name that resolves to a blocked +address gets the same answer as a name that does not resolve or a server that +does not answer ("could not connect"), so users cannot find out which names +exist in the server's internal DNS. Episode audio is streamed by the browser directly from the publisher. Some publishers cannot be played that way (plain HTTP while this server uses diff --git a/docs/development.md b/docs/development.md index e3f5d13..6a5a942 100644 --- a/docs/development.md +++ b/docs/development.md @@ -116,7 +116,10 @@ The runtime around the store lives next to it in `src/podcasts/`: exactly the checked addresses, so a second DNS answer cannot be swapped in. IP literals are checked before connecting, redirects are followed manually (each hop checked, at most 5), and system proxies, cookies and `Referer` - are disabled. + are disabled. `GuardError::is_refusal` is true only for what the URL + shows (scheme, credentials, port, a blocked IP literal); a name that + resolves to a blocked address is reported to users like a transport + failure, so the answers do not reveal internal DNS names. - `fetch.rs` — `FeedFetcher`: conditional GET (`If-None-Match`, `If-Modified-Since`), `Accept-Encoding: identity`, 32 MiB and 60 s limits, `Retry-After` on 429/503, the shared `music_dht` parser. Only a chain of diff --git a/src/podcasts/api.rs b/src/podcasts/api.rs index 8069c4a..a9c8451 100644 --- a/src/podcasts/api.rs +++ b/src/podcasts/api.rs @@ -992,7 +992,7 @@ mod tests { let refused: ApiError = anyhow::Error::from(SubscribeUrlError::Fetch(super::super::fetch::FetchError { kind: FetchErrorKind::Refused, - message: "intranet.corp resolves to a private address (10.1.2.3)".into(), + message: "10.1.2.3 is a private address".into(), retry_after: None, })) .into(); @@ -1006,7 +1006,6 @@ mod tests { !refused.message.contains("10.1.2.3"), "no resolved addresses" ); - assert!(!refused.message.contains("intranet")); let timeout: ApiError = anyhow::Error::from(SubscribeUrlError::Fetch(super::super::fetch::FetchError { kind: FetchErrorKind::Timeout, diff --git a/src/podcasts/fetch_tests.rs b/src/podcasts/fetch_tests.rs index e612a2a..b5be9eb 100644 --- a/src/podcasts/fetch_tests.rs +++ b/src/podcasts/fetch_tests.rs @@ -242,3 +242,48 @@ fn regular_checks_are_hourly_with_a_stable_spread() { .collect(); assert!(spread.len() > 10); } + +/// A port on which nothing listens. +fn closed_port() -> u16 { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.local_addr().unwrap().port() +} + +#[tokio::test] +async fn internal_names_cannot_be_told_from_unreachable_ones() { + // A name that resolves to a private address, a name that does not + // resolve and a server that refuses the connection look the same to + // the user: otherwise "add RSS" would tell which names exist in the + // server's internal DNS. Only what the URL itself says (an IP literal, + // the port, the scheme) is refused as such. + let port = closed_port(); + let lookup = ScriptedLookup::with(&[ + ("vault.internal.test", &[&["10.20.30.40"]]), + ("closed.test", &[&["127.0.0.1"]]), + ]); + let fetcher = fetcher(lookup); + let mut answers = Vec::new(); + for text in [ + format!("http://vault.internal.test:{port}/feed"), + format!("http://nowhere.internal.test:{port}/feed"), + format!("http://closed.test:{port}/feed"), + ] { + let error = fetcher + .fetch(&feed_url(&text), &Validators::default()) + .await + .unwrap_err(); + assert!(!error.public_message().contains("internal"), "{text}"); + answers.push((error.public_message(), error.kind)); + } + assert_eq!(answers[0].1, FetchErrorKind::Transport, "{answers:?}"); + assert!( + answers.iter().all(|answer| *answer == answers[0]), + "{answers:?}" + ); + let literal = fetcher + .fetch(&feed_url("http://10.20.30.40/feed"), &Validators::default()) + .await + .unwrap_err(); + assert_eq!(literal.kind, FetchErrorKind::Refused); + assert_ne!(literal.public_message(), answers[0].0); +} diff --git a/src/podcasts/net.rs b/src/podcasts/net.rs index cd27bc8..4bc7a3c 100644 --- a/src/podcasts/net.rs +++ b/src/podcasts/net.rs @@ -257,10 +257,18 @@ pub(crate) struct BlockedAddress { pub(crate) class: AddressClass, } +impl BlockedAddress { + /// Whether the URL named the address itself (an IP literal) rather + /// than a host name that resolved to it. + pub(crate) fn is_literal(&self) -> bool { + let literal = self.host.trim_start_matches('[').trim_end_matches(']'); + literal.parse::().is_ok() + } +} + impl fmt::Display for BlockedAddress { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let literal = self.host.trim_start_matches('[').trim_end_matches(']'); - if literal.parse::().is_ok() { + if self.is_literal() { write!(f, "{} is a {} address", self.host, self.class)?; } else { write!( @@ -381,16 +389,22 @@ impl GuardError { matches!(self, Self::Http(error) if error.is_timeout()) } - /// Whether the guard refused the URL before or instead of connecting. + /// Whether the guard refused the URL for what it says, before any name + /// resolution: scheme, credentials, port, a blocked IP literal. + /// + /// A host *name* that resolves to a blocked address is not a refusal in + /// this sense: users are told the same as for a name that does not + /// resolve or a server that does not answer, so the answers cannot be + /// used to learn which names exist in the server's internal DNS. The + /// details stay in the server log. pub(crate) fn is_refusal(&self) -> bool { - matches!( - self, - Self::UnsupportedScheme(_) - | Self::Credentials - | Self::MissingHost - | Self::Blocked(_) - | Self::Port(_) - ) + match self { + Self::UnsupportedScheme(_) | Self::Credentials | Self::MissingHost | Self::Port(_) => { + true + } + Self::Blocked(blocked) => blocked.is_literal(), + _ => false, + } } } diff --git a/src/podcasts/proxy_tests.rs b/src/podcasts/proxy_tests.rs index fa0ca6f..9da443e 100644 --- a/src/podcasts/proxy_tests.rs +++ b/src/podcasts/proxy_tests.rs @@ -6,7 +6,7 @@ use std::time::Duration; use futures_util::StreamExt; use futures_util::stream::BoxStream; -use super::net::{AddressClass, GuardError}; +use super::net::{AddressClass, GuardError, REFUSED_MESSAGE}; use super::proxy::{AudioProxy, ProxyError, sanitize_range, sniff_audio}; use std::sync::Arc; @@ -165,12 +165,47 @@ async fn proxy_handles_servers_without_ranges_and_redirects() { assert_eq!(server.requests("/track/ep.mp3")[0]["range"], "bytes=10-19"); assert_eq!(server.requests("/cdn/ep.mp3")[0]["range"], "bytes=10-19"); - match proxy.fetch(&server.url("/track/private.mp3"), None).await { - Err(ProxyError::Upstream(GuardError::Blocked(blocked))) => { - assert_eq!(blocked.class, AddressClass::Private) - } - other => panic!("{other:?}"), + let Err(error) = proxy.fetch(&server.url("/track/private.mp3"), None).await else { + panic!("redirect to a private address was followed"); + }; + assert!( + matches!( + &error, + ProxyError::Upstream(GuardError::Blocked(blocked)) + if blocked.class == AddressClass::Private + ), + "{error:?}" + ); + // An IP literal is refused for what the URL says. + assert_eq!(error.public_message(), REFUSED_MESSAGE); +} + +#[tokio::test] +async fn proxy_answers_alike_for_internal_and_unreachable_names() { + // Episode URLs can come from the user's own clients: the answer must + // not tell a name with a private address from one that does not exist + // or a server that refuses the connection. + let port = std::net::TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + let proxy = proxy(ScriptedLookup::with(&[ + ("db.corp.test", &[&["192.168.7.7"]]), + ("closed.test", &[&["127.0.0.1"]]), + ])); + let mut answers = Vec::new(); + for host in ["db.corp.test", "nowhere.corp.test", "closed.test"] { + let target = format!("http://{host}:{port}/episode.mp3"); + let error = proxy.fetch(&target, None).await.unwrap_err(); + assert!(matches!(error, ProxyError::Upstream(_)), "{error:?}"); + answers.push((error.http_status(), error.public_message())); } + assert_eq!(answers[0].1, "could not connect to the publisher"); + assert!( + answers.iter().all(|answer| *answer == answers[0]), + "{answers:?}" + ); } /// `noise` that starts like a file of the given format.