Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rs/web-transport-noq/examples/echo-client-advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main() -> anyhow::Result<()> {
let client = web_transport_noq::Client::new(client, config);

// Connect to the given URL.
tracing::info!(url = %args.url, "connecting");
tracing::info!(host = ?args.url.host_str(), path = args.url.path(), "connecting");
let session = client.connect(args.url).await?;

tracing::info!("connected");
Expand Down
5 changes: 3 additions & 2 deletions rs/web-transport-noq/examples/echo-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ async fn main() -> anyhow::Result<()> {
client.with_system_roots()?
};

tracing::info!(url = %args.url, "connecting");

// Connect to the given URL.
let mut request = ConnectRequest::new(args.url);
if let Some(protocol) = &args.protocol {
request = request.with_protocol(protocol);
}

// The Debug output redacts credentials, such as the query and header values.
tracing::info!(?request, "connecting");
let session = client.connect(request).await?;

tracing::info!("connected");
Expand Down
3 changes: 2 additions & 1 deletion rs/web-transport-noq/examples/echo-server-advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ async fn run_conn(conn: noq::Incoming) -> anyhow::Result<()> {

// Perform the WebTransport handshake.
let request = web_transport_noq::Request::accept(conn).await?;
tracing::info!(url = %request.url, "received WebTransport request");
// The Debug output redacts credentials, such as the query and header values.
tracing::info!(request = ?*request, "received WebTransport request");

// Accept the session.
let session = request.ok().await.context("failed to accept session")?;
Expand Down
3 changes: 2 additions & 1 deletion rs/web-transport-noq/examples/echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ async fn run_conn(
request: web_transport_noq::Request,
protocol: Option<String>,
) -> anyhow::Result<()> {
tracing::info!(url = %request.url, "received WebTransport request");
// The Debug output redacts credentials, such as the query and header values.
tracing::info!(request = ?*request, "received WebTransport request");

// Negotiate protocol if both client and server support it.
let negotiated = protocol.filter(|p| request.protocols.contains(p));
Expand Down
184 changes: 181 additions & 3 deletions rs/web-transport-proto/src/connect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{str::FromStr, sync::Arc};
use std::{fmt, str::FromStr, sync::Arc};

use bytes::{Buf, BufMut, BytesMut};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use url::Url;
use url::{Position, Url};

use super::{qpack, Frame, VarInt, MAX_FRAME_SIZE};

Expand Down Expand Up @@ -99,8 +99,14 @@ impl From<sfv::Error> for ConnectError {
}

/// A CONNECT request to initiate a WebTransport session.
///
/// The [`Debug`](fmt::Debug) output is safe to log. It keeps the URL's scheme,
/// host, port, and path, the protocols, and the header names, but redacts the
/// URL's userinfo, query, and fragment. Header values are redacted too, except
/// `origin` and `user-agent` when not marked
/// [sensitive](http::HeaderValue::set_sensitive).
#[non_exhaustive]
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct ConnectRequest {
/// The URL to connect to.
pub url: Url,
Expand Down Expand Up @@ -264,6 +270,70 @@ impl From<Url> for ConnectRequest {
}
}

/// Header values shown by [`ConnectRequest`]'s `Debug` output.
///
/// This is an allowlist so that credentials in unknown or custom headers
/// (`authorization`, `cookie`, `x-api-key`, ...) are redacted by default.
const DEBUG_VISIBLE_HEADERS: [http::HeaderName; 2] =
[http::header::ORIGIN, http::header::USER_AGENT];

/// Replaces a redacted value in `Debug` output.
struct Redacted;

impl fmt::Debug for Redacted {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("[redacted]")
}
}

/// Formats a URL with its userinfo, query, and fragment redacted.
struct RedactedUrl<'a>(&'a Url);

impl fmt::Debug for RedactedUrl<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let url = self.0;
let mut redacted = String::from(&url[..Position::BeforeUsername]);
if !url.username().is_empty() || url.password().is_some() {
redacted.push_str("[redacted]@");
}
redacted.push_str(&url[Position::BeforeHost..Position::AfterPath]);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if url.query().is_some() {
redacted.push_str("?[redacted]");
}
if url.fragment().is_some() {
redacted.push_str("#[redacted]");
}
fmt::Debug::fmt(&redacted, f)
}
}

/// Formats headers with every value outside [`DEBUG_VISIBLE_HEADERS`] redacted.
struct RedactedHeaders<'a>(&'a http::HeaderMap);

impl fmt::Debug for RedactedHeaders<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut map = f.debug_map();
for (name, value) in self.0 {
if DEBUG_VISIBLE_HEADERS.contains(name) && !value.is_sensitive() {
map.entry(name, value);
} else {
map.entry(name, &Redacted);
}
}
map.finish()
}
}

impl fmt::Debug for ConnectRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConnectRequest")
.field("url", &RedactedUrl(&self.url))
.field("protocols", &self.protocols)
.field("headers", &RedactedHeaders(&self.headers))
.finish()
}
}

/// A CONNECT response to accept or reject a WebTransport session.
#[non_exhaustive]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -944,4 +1014,112 @@ mod tests {
let err = ConnectRequest::read(&mut cursor).await.unwrap_err();
assert!(matches!(err, ConnectError::UnexpectedEnd));
}

#[test]
fn request_debug_keeps_url_without_credentials() {
let req = ConnectRequest::new(Url::parse("https://example.com:4443/moq/room").unwrap())
.with_protocol("moq-lite-03");
assert_eq!(
format!("{req:?}"),
r#"ConnectRequest { url: "https://example.com:4443/moq/room", protocols: ["moq-lite-03"], headers: {} }"#
);
}

#[test]
fn request_debug_redacts_url_credentials() {
let url = "https://alice:hunter2@example.com/moq?jwt=SECRET&x=1#access_token=SECRET";
let req = ConnectRequest::new(Url::parse(url).unwrap());
let debug = format!("{req:?}");
assert!(
debug.contains(r#"url: "https://[redacted]@example.com/moq?[redacted]#[redacted]""#),
"{debug}"
);
for secret in ["alice", "hunter2", "SECRET", "x=1"] {
assert!(!debug.contains(secret), "{secret} leaked: {debug}");
}

let req = ConnectRequest::new(Url::parse("https://alice@example.com/").unwrap());
assert!(format!("{req:?}").contains(r#""https://[redacted]@example.com/""#));
}

#[test]
fn request_debug_redacts_header_values() {
use http::{header, HeaderName, HeaderValue};

let mut sensitive_agent = HeaderValue::from_static("agent-SECRET");
sensitive_agent.set_sensitive(true);

let req = ConnectRequest::new(Url::parse("https://example.com/").unwrap())
.with_header(
header::AUTHORIZATION,
HeaderValue::from_static("Bearer SECRET"),
)
.with_header(
header::PROXY_AUTHORIZATION,
HeaderValue::from_static("Basic SECRET"),
)
.with_header(header::COOKIE, HeaderValue::from_static("session=SECRET"))
.with_header(
header::SET_COOKIE,
HeaderValue::from_static("session=SECRET"),
)
.with_header(
HeaderName::from_static("x-api-key"),
HeaderValue::from_static("SECRET"),
)
.with_header(
header::ORIGIN,
HeaderValue::from_static("https://app.example"),
)
.with_header(header::USER_AGENT, sensitive_agent);
let debug = format!("{req:?}");

assert!(!debug.contains("SECRET"), "{debug}");
for name in [
"authorization",
"proxy-authorization",
"cookie",
"set-cookie",
"x-api-key",
] {
assert!(
debug.contains(&format!(r#""{name}": [redacted]"#)),
"{name}: {debug}"
);
}
assert!(
debug.contains(r#""origin": "https://app.example""#),
"{debug}"
);
assert!(debug.contains(r#""user-agent": [redacted]"#), "{debug}");
}

#[test]
fn decoded_request_debug_redacts_credentials() {
let req = ConnectRequest::new(Url::parse("https://example.com/moq?jwt=SECRET").unwrap())
.with_header(
http::header::AUTHORIZATION,
http::HeaderValue::from_static("Bearer SECRET"),
)
.with_header(
http::header::USER_AGENT,
http::HeaderValue::from_static("moq-test"),
);
let mut wire = Vec::new();
req.encode(&mut wire).unwrap();

let decoded = ConnectRequest::decode(&mut wire.as_slice()).unwrap();
assert_eq!(decoded.url.query(), Some("jwt=SECRET"));

// Decoded header order is unspecified, so check each entry separately.
let debug = format!("{decoded:?}");
assert!(!debug.contains("SECRET"), "{debug}");
for expected in [
r#"url: "https://example.com/moq?[redacted]""#,
r#""authorization": [redacted]"#,
r#""user-agent": "moq-test""#,
] {
assert!(debug.contains(expected), "{expected}: {debug}");
}
}
}
2 changes: 1 addition & 1 deletion rs/web-transport-quiche/examples/echo-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() -> anyhow::Result<()> {
let mut settings = web_transport_quiche::Settings::default();
settings.verify_peer = !args.tls_disable_verify;

tracing::info!("connecting to {}", args.url);
tracing::info!(host = ?args.url.host_str(), path = args.url.path(), "connecting");
let session = client
.with_settings(settings)
.connect(args.url)
Expand Down
5 changes: 2 additions & 3 deletions rs/web-transport-quiche/examples/echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ async fn main() -> anyhow::Result<()> {

// Accept new connections.
while let Some(conn) = server.accept().await {
tracing::info!("accepted connection, url={}", conn.url);
// The Debug output redacts credentials, such as the query and header values.
tracing::info!(request = ?conn.request, "accepted connection");

tokio::spawn(async move {
match run_conn(conn).await {
Expand All @@ -68,8 +69,6 @@ async fn main() -> anyhow::Result<()> {
}

async fn run_conn(request: web_transport_quiche::h3::Request) -> anyhow::Result<()> {
tracing::info!("received WebTransport request: {}", request.url);

// Accept the session.
let session = request.ok().await.context("failed to accept session")?;
tracing::info!("accepted session");
Expand Down
20 changes: 19 additions & 1 deletion rs/web-transport-quiche/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ impl ClientBuilder {
Some(url::Host::Domain(host)) => host.to_string(),
Some(url::Host::Ipv4(ip)) => ip.to_string(),
Some(url::Host::Ipv6(ip)) => ip.to_string(),
None => return Err(ClientError::InvalidUrl(request.url.to_string())),
// Keep only the scheme: a hostless URL's path can carry data, such as
// `data:text/plain,SECRET`.
None => {
let scheme = request.url.scheme();
return Err(ClientError::InvalidUrl(format!("{scheme} URL has no host")));
}
};

Ok((host, request.url.port().unwrap_or(443)))
Expand All @@ -177,3 +182,16 @@ impl Connecting {
Connection::connect(conn, self.request).await
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hostless_url_error_omits_url_content() {
let request =
ConnectRequest::new(url::Url::parse("data:text/plain,SECRET?q=SECRET").unwrap());
let err = ClientBuilder::target(&request).unwrap_err();
assert_eq!(err.to_string(), "invalid URL: data URL has no host");
}
}
2 changes: 1 addition & 1 deletion rs/web-transport-quiche/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Connection {
// Accept logic is stateful, so use an Arc<Mutex> to share it.
let accept = SessionAccept::new(conn.clone(), session_id);

tracing::debug!(url = %connect.request.url, "WebTransport connection established");
tracing::debug!(request = ?connect.request, "WebTransport connection established");
let request = connect.request.clone();
let response = connect.response.clone();

Expand Down
2 changes: 1 addition & 1 deletion rs/web-transport-quinn/examples/echo-client-advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async fn main() -> anyhow::Result<()> {
let client = web_transport_quinn::Client::new(client, config);

// Connect to the given URL.
tracing::info!(url = %args.url, "connecting");
tracing::info!(host = ?args.url.host_str(), path = args.url.path(), "connecting");
let session = client.connect(args.url).await?;

tracing::info!("connected");
Expand Down
5 changes: 3 additions & 2 deletions rs/web-transport-quinn/examples/echo-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ async fn main() -> anyhow::Result<()> {
client.with_system_roots()?
};

tracing::info!(url = %args.url, "connecting");

// Connect to the given URL.
let mut request = ConnectRequest::new(args.url);
if let Some(protocol) = &args.protocol {
request = request.with_protocol(protocol);
}

// The Debug output redacts credentials, such as the query and header values.
tracing::info!(?request, "connecting");
let session = client.connect(request).await?;

tracing::info!("connected");
Expand Down
13 changes: 2 additions & 11 deletions rs/web-transport-quinn/examples/echo-server-advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,8 @@ async fn run_conn(conn: quinn::Incoming) -> anyhow::Result<()> {

// Perform the WebTransport handshake.
let request = web_transport_quinn::Request::accept(conn).await?;
tracing::info!(url = %request.url, "received WebTransport request");

// Log all HTTP3 headers
tracing::info!("HTTP3 headers:");
for (name, value) in request.headers.iter() {
let value = value.to_str().context("invalid header value")?;
tracing::info!(" {}: {}", name, value);
}
if request.headers.is_empty() {
tracing::info!(" (empty)");
}
// The Debug output redacts credentials, such as the query and header values.
tracing::info!(request = ?*request, "received WebTransport request");

// Accept the session.
let session = request.ok().await.context("failed to accept session")?;
Expand Down
3 changes: 2 additions & 1 deletion rs/web-transport-quinn/examples/echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ async fn run_conn(
request: web_transport_quinn::Request,
protocol: Option<String>,
) -> anyhow::Result<()> {
tracing::info!(url = %request.url, "received WebTransport request");
// The Debug output redacts credentials, such as the query and header values.
tracing::info!(request = ?*request, "received WebTransport request");

// Negotiate protocol if both client and server support it.
let negotiated = protocol.filter(|p| request.protocols.contains(p));
Expand Down
Loading