Files
buzz/crates/buzz-auth/tests/mesh_jwks_proxy.rs
Brad Seiler 2d0f78c6e1 feat(auth): support explicit mesh JWKS transport (#8382)
## Summary

Allow an issuer to opt into mesh HTTP for its JSON Web Key Set (JWKS).
Public HTTPS remains the default. This supports deployments whose
service mesh authenticates and protects the key endpoint.

The sealed source contract selects transport and contributes to policy
identity. Mesh mode requires a DNS hostname and checks every resolved
address against an explicit allow-list before pinning one address. Both
modes disable proxies, reject redirects, cap responses, and enforce a
whole-fetch deadline. The relay cannot verify mesh security: operators
must enforce mutual TLS, endpoint authorization, and egress
restrictions.

### Related issue

No duplicate mesh-JWKS issue or PR found. Existing NIP-FI relay
enforcement: #7224. No deployment change is included.

### Testing

- At `eccb8cb19420abda44cf6e5e62b1361e2843bd1d`: full auth package and
auth/relay all-target clippy passed. Full relay package passed: 1,457
library tests, 37 binary tests, and 9 boot tests; 446 tests were
ignored.
- At prior head `113939838cd81d5e8d0b0f8783c42ffaae44a4d3`: the full
relay library suite failed the demo echo test (1,455 passed, 1 failed,
440 ignored). Independent checks reproduced that timeout at base
`d61fe2071d2b3d8e56dd931947f16bfe0d6173fe`; the receive-cancellation
defect is outside this change. A later pass does not establish that
defect is fixed.
- Three falsifying mutations at `eccb8cb194` were caught: mixed DNS
answers, proxy bypass, and whole-fetch deadline. The full locked auth
package passed after restoring the exact source.
- At `eccb8cb194`, `cargo build --locked --release -p buzz-relay -p
buzz-cli -p buzz-admin` passed with command-local
`CARGO_PROFILE_RELEASE_STRIP=none`. This avoids a macOS loader error
without a source, lockfile, or project-profile change.
- Earlier hooks failed the ACP signal test and Tauri login-shell PATH
test. The ACP failure also reproduced on unmodified main `93ffc37c`.
Main merged without conflict. The full Tauri package then passed with
`RUST_TEST_THREADS=1`: 3,422 library tests and 3 integration tests, 19
ignored. All pre-push hooks passed with that same command-local setting;
no hook or test was skipped.
- At `eccb8cb194`, isolated `just test` passed all 107 runner steps. The
monolithic `just ci` run timed out after 1,120 seconds; all recipes
passed across that run and separate `just desktop-tauri-test`, `just
web-build`, and `just mobile-test` invocations. No monolithic pass is
claimed.
- Exact-head release live-local checks passed: loopback HTTP mesh JWKS
warm; valid assertion 101; expired/wrong issuer/wrong audience 403;
valid signed disconnect 200; wrong command audience 403. Redirect-only
JWKS returned assertion 503 without a request to its target. Both
omitted/default and explicit public transport rejected HTTP at startup.
- A separate NIP-FI-off release CLI smoke test created an open channel,
sent an accepted message, read it back, and fetched its thread. The
assertion probe closes after upgrade; it does not prove a complete
NIP-42 session. Local loopback tests do not prove deployment mesh
authentication, endpoint authorization, egress controls, or real `240/4`
routing.

Generated with Codex

## Human Testing
valid assertion / configured community audience: HTTP 101 (expected 101)
expired assertion: HTTP 403 (expected 403)
wrong issuer: HTTP 403 (expected 403)
wrong community audience: HTTP 403 (expected 403)
valid command JWT / configured command audience: HTTP 200,
body=b'{"disconnected": true}' (expected 200)
valid command signature / wrong audience: HTTP 403 (expected 403)

---------

Signed-off-by: seiler <seiler@squareup.com>
Signed-off-by: Local Builder <1c476cb1127ea270cb900bdc3891060140bdd1173f91498030ee9b4db01c2f20@buzz.block.builderlab.xyz>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Local Builder <1c476cb1127ea270cb900bdc3891060140bdd1173f91498030ee9b4db01c2f20@buzz.block.builderlab.xyz>
2026-10-16 14:26:08 -04:00

257 lines
8.7 KiB
Rust

use std::io::{self, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
use std::process::Output;
use std::thread::{self, JoinHandle};
use std::time::Duration;
use buzz_auth::nip_fi::{HttpJwksFetcher, JwksFetcher, JwksSourceContract};
const CHILD_MARKER: &str = "BUZZ_AUTH_JWKS_PROXY_TEST_CHILD";
const CHILD_TARGET: &str = "BUZZ_AUTH_JWKS_PROXY_TEST_TARGET";
const JWKS_BODY: &str = r#"{"keys":[{"kty":"EC","crv":"P-256","x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU","y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0","use":"sig","alg":"ES256","kid":"proxy-test"}]}"#;
const SHUTDOWN_PATH: &str = "/__mesh_jwks_proxy_test_shutdown__";
#[test]
fn mesh_fetch_for_contract_ignores_proxy_environment() {
if std::env::var_os(CHILD_MARKER).is_some() {
run_child_test();
} else {
run_parent_test();
}
}
fn run_parent_test() {
let mut servers = LocalServers::start().expect("start local JWKS and proxy listeners");
let proxy_url = format!("http://{}", servers.proxy_addr);
let target_url = format!("http://localhost:{}", servers.target_addr.port());
let executable = std::env::current_exe().expect("current integration-test binary");
let mut command = tokio::process::Command::new(executable);
command
.args([
"--exact",
"mesh_fetch_for_contract_ignores_proxy_environment",
"--nocapture",
])
.env(CHILD_MARKER, "1")
.env(CHILD_TARGET, target_url)
.env("HTTP_PROXY", &proxy_url)
.env("http_proxy", &proxy_url)
.env_remove("HTTPS_PROXY")
.env_remove("https_proxy")
.env_remove("ALL_PROXY")
.env_remove("all_proxy")
.env_remove("NO_PROXY")
.env_remove("no_proxy")
.env_remove("REQUEST_METHOD")
.kill_on_drop(true);
// The child receives a controlled proxy environment at process creation,
// before it creates its current-thread Tokio runtime. Its test process
// cannot alter or race the environment of the other buzz-auth tests.
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("build parent runtime");
let output = runtime
.block_on(async { tokio::time::timeout(Duration::from_secs(20), command.output()).await });
let shutdown = servers.shutdown();
shutdown.expect("stop local proxy and JWKS servers");
let output = output.expect("proxy regression child must finish within 20 seconds");
let output = output.expect("run proxy regression child");
assert_child_succeeded(output);
}
fn run_child_test() {
let target_url = std::env::var(CHILD_TARGET).expect("parent provides local JWKS URL");
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("build child current-thread runtime");
runtime.block_on(async move {
let default_client = reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.build()
.expect("build default-proxy positive control client");
let control = tokio::time::timeout(
Duration::from_secs(3),
default_client
.get(format!("{target_url}/proxy-control"))
.send(),
)
.await
.expect("default client should reach the local proxy")
.expect("local proxy should return its positive-control response");
assert_eq!(control.status(), reqwest::StatusCode::BAD_GATEWAY);
assert_eq!(
control
.headers()
.get("x-test-proxy")
.and_then(|value| value.to_str().ok()),
Some("intercepted"),
"positive control proves proxy environment variables are active"
);
let contract = JwksSourceContract::new_mesh(format!("{target_url}/jwks.json"), 300, 3600)
.expect("valid sealed mesh JWKS contract");
let body = tokio::time::timeout(
Duration::from_secs(12),
HttpJwksFetcher::new().fetch_for_contract(&contract),
)
.await
.expect("contract fetch is bounded")
.expect("mesh fetch must bypass the configured proxy and reach loopback");
assert_eq!(body, JWKS_BODY);
});
}
fn assert_child_succeeded(output: Output) {
assert!(
output.status.success(),
"proxy regression child failed\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
}
struct LocalServers {
target_addr: SocketAddr,
proxy_addr: SocketAddr,
target_thread: Option<JoinHandle<io::Result<()>>>,
proxy_thread: Option<JoinHandle<io::Result<()>>>,
}
impl LocalServers {
fn start() -> io::Result<Self> {
let target_addr = ("localhost", 0)
.to_socket_addrs()?
.next()
.filter(|address| address.ip().is_loopback())
.ok_or_else(|| io::Error::other("localhost has no loopback address"))?;
let target_listener = TcpListener::bind(target_addr)?;
let proxy_listener = TcpListener::bind(("127.0.0.1", 0))?;
let target_addr = target_listener.local_addr()?;
let proxy_addr = proxy_listener.local_addr()?;
let target_thread = thread::spawn(move || serve_target(target_listener));
let proxy_thread = thread::spawn(move || serve_proxy(proxy_listener));
Ok(Self {
target_addr,
proxy_addr,
target_thread: Some(target_thread),
proxy_thread: Some(proxy_thread),
})
}
fn shutdown(&mut self) -> Result<(), String> {
if self
.target_thread
.as_ref()
.is_some_and(|thread| !thread.is_finished())
{
send_shutdown(self.target_addr);
}
if self
.proxy_thread
.as_ref()
.is_some_and(|thread| !thread.is_finished())
{
send_shutdown(self.proxy_addr);
}
join_server(&mut self.target_thread)?;
join_server(&mut self.proxy_thread)
}
}
impl Drop for LocalServers {
fn drop(&mut self) {
let _ = self.shutdown();
}
}
fn join_server(handle: &mut Option<JoinHandle<io::Result<()>>>) -> Result<(), String> {
let Some(handle) = handle.take() else {
return Ok(());
};
handle
.join()
.map_err(|_| "local server thread panicked".to_owned())?
.map_err(|error| error.to_string())
}
fn send_shutdown(address: SocketAddr) {
if let Ok(mut stream) = TcpStream::connect_timeout(&address, Duration::from_secs(1)) {
let _ = stream.write_all(
format!("GET {SHUTDOWN_PATH} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.as_bytes(),
);
}
}
fn serve_proxy(listener: TcpListener) -> io::Result<()> {
let (mut stream, _) = listener.accept()?;
set_stream_timeouts(&stream)?;
let request = read_request_headers(&mut stream)?;
if request
.lines()
.next()
.unwrap_or_default()
.contains(SHUTDOWN_PATH)
{
return Ok(());
}
stream.write_all(
b"HTTP/1.1 502 Bad Gateway\r\nX-Test-Proxy: intercepted\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
)
}
fn serve_target(listener: TcpListener) -> io::Result<()> {
let (mut stream, _) = listener.accept()?;
set_stream_timeouts(&stream)?;
let request = read_request_headers(&mut stream)?;
let first_line = request.lines().next().unwrap_or_default();
if first_line.contains(SHUTDOWN_PATH) {
return Ok(());
}
if first_line.starts_with("GET /jwks.json ") {
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
JWKS_BODY.len(),
JWKS_BODY
);
return stream.write_all(response.as_bytes());
}
stream.write_all(
b"HTTP/1.1 418 Unexpected Target Request\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
)
}
fn set_stream_timeouts(stream: &TcpStream) -> io::Result<()> {
stream.set_read_timeout(Some(Duration::from_secs(5)))?;
stream.set_write_timeout(Some(Duration::from_secs(5)))
}
fn read_request_headers(stream: &mut TcpStream) -> io::Result<String> {
const MAX_HEADER_BYTES: usize = 16 * 1024;
let mut request = Vec::new();
let mut chunk = [0; 1024];
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
let read = stream.read(&mut chunk)?;
if read == 0 {
break;
}
request.extend_from_slice(&chunk[..read]);
if request.len() > MAX_HEADER_BYTES {
return Err(io::Error::other("request headers exceed test limit"));
}
}
Ok(String::from_utf8_lossy(&request).into_owned())
}