feat(security): harden gateway boundaries and usage policies

Consolidate subscription usage policy enforcement, privacy-safe persistence, and gateway security hardening into one reviewable change.

Includes bounded HTTP and execution envelopes, header and protocol guards, DNS and relay validation, authentication and secret projection hardening, secure backup/install paths, and regression coverage.
This commit is contained in:
elky
2026-09-04 03:45:52 +08:00
parent ddcbeb3ae9
commit 579f2c7cc1
1019 changed files with 190437 additions and 26080 deletions
+107 -15
View File
@@ -40,7 +40,7 @@ pub(crate) enum UpstreamProxyScheme {
Socks5h,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
pub(crate) struct UpstreamProxyConfig {
raw: String,
scheme: UpstreamProxyScheme,
@@ -50,6 +50,20 @@ pub(crate) struct UpstreamProxyConfig {
password: Option<String>,
}
impl std::fmt::Debug for UpstreamProxyConfig {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("UpstreamProxyConfig")
.field("url", &self.redacted_url())
.field("scheme", &self.scheme)
.field("host", &self.host)
.field("port", &self.port)
.field("username", &self.username.as_ref().map(|_| "[REDACTED]"))
.field("password", &self.password.as_ref().map(|_| "[REDACTED]"))
.finish()
}
}
impl UpstreamProxyConfig {
pub(crate) fn parse(raw: &str) -> Result<Self, String> {
let trimmed = raw.trim();
@@ -75,6 +89,18 @@ impl UpstreamProxyConfig {
.filter(|value| !value.is_empty())
.ok_or_else(|| "upstream proxy URL must include a host".to_string())?
.to_string();
// A proxy URL identifies the proxy origin. Path/query/fragment
// components are not part of HTTP CONNECT or SOCKS negotiation and
// silently ignoring them can make the configured endpoint differ
// from what operators see in configuration and logs.
if !matches!(parsed.path(), "" | "/")
|| parsed.query().is_some()
|| parsed.fragment().is_some()
{
return Err(
"upstream proxy URL must be an origin without path, query, or fragment".to_string(),
);
}
let port = parsed.port().unwrap_or(match scheme {
UpstreamProxyScheme::Http => 80,
UpstreamProxyScheme::Socks5 | UpstreamProxyScheme::Socks5h => 1080,
@@ -181,6 +207,38 @@ pub(crate) async fn connect_target_via_proxy(
Ok(tcp)
}
pub(crate) async fn connect_validated_target_via_proxy(
proxy: &UpstreamProxyConfig,
target_addr: SocketAddr,
options: ProxyConnectOptions,
) -> io::Result<TcpStream> {
let mut tcp = connect_proxy_tcp(
proxy,
options.connect_timeout,
options.tcp_nodelay,
options.tcp_keepalive,
options.ip_family,
)
.await?;
match proxy.scheme() {
UpstreamProxyScheme::Http => {
http_connect(&mut tcp, &target_addr.to_string(), proxy).await?;
}
UpstreamProxyScheme::Socks5 | UpstreamProxyScheme::Socks5h => {
socks5_connect(
&mut tcp,
proxy,
&target_addr.ip().to_string(),
target_addr.port(),
)
.await?;
}
}
Ok(tcp)
}
pub(crate) async fn connect_proxy_tcp(
proxy: &UpstreamProxyConfig,
connect_timeout: Duration,
@@ -188,16 +246,19 @@ pub(crate) async fn connect_proxy_tcp(
tcp_keepalive: Option<Duration>,
ip_family: IpFamily,
) -> io::Result<TcpStream> {
let resolved = tokio::time::timeout(
connect_timeout,
tokio::net::lookup_host((proxy.host(), proxy.port())),
)
.await
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "proxy DNS timeout"))?
.map_err(|err| io::Error::other(format!("proxy DNS failed: {err}")))?;
let resolved =
aether_http::lookup_host_with_limits(proxy.host(), proxy.port(), connect_timeout)
.await
.map_err(|err| {
if err.kind() == io::ErrorKind::TimedOut {
io::Error::new(io::ErrorKind::TimedOut, "proxy DNS timeout")
} else {
io::Error::other(format!("proxy DNS failed: {err}"))
}
})?;
let mut last_error = None;
for addr in resolved.filter(|addr| ip_family.allows(*addr)) {
for addr in resolved.into_iter().filter(|addr| ip_family.allows(*addr)) {
match tokio::time::timeout(connect_timeout, TcpStream::connect(addr)).await {
Ok(Ok(stream)) => {
configure_tcp_stream(&stream, tcp_nodelay, tcp_keepalive)?;
@@ -395,12 +456,16 @@ pub(crate) async fn socks5_target_address(
request.push(host.len() as u8);
request.extend_from_slice(host);
} else {
let mut resolved = tokio::net::lookup_host((target_host, target_port))
.await
.map_err(|err| io::Error::other(format!("SOCKS5 target DNS failed: {err}")))?;
let addr = resolved
.next()
.ok_or_else(|| io::Error::other("SOCKS5 target DNS returned no addresses"))?;
let addr = aether_http::lookup_host_with_limits(
target_host,
target_port,
aether_http::DEFAULT_DNS_LOOKUP_TIMEOUT,
)
.await
.map_err(|err| io::Error::other(format!("SOCKS5 target DNS failed: {err}")))?
.into_iter()
.next()
.ok_or_else(|| io::Error::other("SOCKS5 target DNS returned no addresses"))?;
push_socks5_socket_address(&mut request, addr);
}
request.extend_from_slice(&target_port.to_be_bytes());
@@ -481,6 +546,13 @@ mod tests {
proxy.basic_auth_header().as_deref(),
Some("Basic dXNlcjpwYXNz")
);
let debug = format!("{proxy:?}");
assert!(!debug.contains("user:pass"));
assert!(!debug.contains("Some(\"user\")"));
assert!(!debug.contains("Some(\"pass\")"));
assert!(!debug.contains("dXNlcjpwYXNz"));
assert!(debug.contains("[REDACTED]"));
}
#[test]
@@ -490,4 +562,24 @@ mod tests {
assert!(error.contains("unsupported upstream proxy scheme"));
}
#[test]
fn rejects_proxy_urls_with_non_origin_components() {
for value in [
"http://proxy.example/path",
"http://proxy.example?token=secret",
"socks5://proxy.example#fragment",
] {
let error = UpstreamProxyConfig::parse(value)
.expect_err("proxy URL with non-origin components should be rejected");
assert!(
error.contains("without path, query, or fragment"),
"unexpected error for {value}: {error}"
);
}
for value in ["http://proxy.example", "http://proxy.example/"] {
UpstreamProxyConfig::parse(value).expect("root proxy origin should be accepted");
}
}
}