Answer blocked host names like unreachable ones
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
402ad0a03a
commit
62141c0840
6 files changed
+121
-20
No files matched your search
+1
-2
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
+25
-11
@@ -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::<IpAddr>().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::<IpAddr>().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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user