272 lines
9.5 KiB
Rust
272 lines
9.5 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! An injected secret reaches the upstream and never appears in a response, a log line, the
|
|
//! access log or the compiled config, even with the most verbose log level.
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
#[cfg(unix)]
|
|
mod common;
|
|
|
|
// On Windows this target has nothing to run: it shows as one *ignored* test, never as a
|
|
// pass (ROADMAP §8.3; run it in WSL2, see CLAUDE.md).
|
|
#[cfg(not(unix))]
|
|
#[test]
|
|
#[ignore = "needs Linux (Pingora runner, Caddy, Pebble): run it in WSL2, see CLAUDE.md"]
|
|
fn needs_linux() {}
|
|
|
|
#[cfg(unix)]
|
|
mod unix {
|
|
|
|
use crate::common::{EdgeSpec, host_file, spawn, start};
|
|
use ianitor_testkit::client::{self, Req, Resp, WsOutcome};
|
|
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
|
|
|
|
fn unique_secret() -> String {
|
|
let nanos = std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_nanos();
|
|
format!("sekret-{}-{nanos:x}-tail", std::process::id())
|
|
}
|
|
|
|
fn assert_clean(what: &str, haystack: &[u8], secret: &str) {
|
|
let hay = String::from_utf8_lossy(haystack);
|
|
assert!(!hay.contains(secret), "secret leaked into {what}:\n{hay}");
|
|
}
|
|
|
|
fn assert_resp_clean(what: &str, r: &Resp, secret: &str) {
|
|
for (n, v) in &r.headers {
|
|
assert_clean(
|
|
&format!("{what}: response header {n}"),
|
|
v.as_bytes(),
|
|
secret,
|
|
);
|
|
}
|
|
assert_clean(&format!("{what}: response body"), &r.body, secret);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn injected_secret_never_appears_in_a_response_or_log() {
|
|
let secret = unique_secret();
|
|
let echo = Echo::start("app").await;
|
|
let ws = WsEcho::start().await;
|
|
let rule =
|
|
"[[request_headers]]\naction = \"set\"\nname = \"X-Api-Key\"\nsecret = \"api_key\"\n";
|
|
let edge = start(EdgeSpec {
|
|
site: "secrets = [\"api_key\"]\n",
|
|
hosts: vec![
|
|
host_file(
|
|
"app.test",
|
|
&format!(
|
|
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
|
echo.url()
|
|
),
|
|
),
|
|
host_file(
|
|
"down.test",
|
|
&format!(
|
|
"upstreams = [\"http://127.0.0.1:{}\"]\naccess_log = \"truncated\"\n{rule}",
|
|
closed_port()
|
|
),
|
|
),
|
|
host_file(
|
|
"ws.test",
|
|
&format!(
|
|
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
|
ws.url()
|
|
),
|
|
),
|
|
],
|
|
// systemd credentials usually end with a newline; it is not part of the value.
|
|
secrets: vec![("api_key".into(), format!("{secret}\n").into_bytes())],
|
|
log_level: "trace",
|
|
..Default::default()
|
|
});
|
|
let roots = edge.roots();
|
|
let go = |sni: &'static str, req: Req| {
|
|
let roots = roots.clone();
|
|
let addr = edge.https;
|
|
async move { client::https(addr, sni, roots, &req).await.unwrap() }
|
|
};
|
|
|
|
let mut responses = Vec::new();
|
|
responses.push((
|
|
"plain GET",
|
|
go("app.test", Req::get("app.test", "/hello?token=abc")).await,
|
|
));
|
|
responses.push((
|
|
"POST",
|
|
go(
|
|
"app.test",
|
|
Req::new("POST", "app.test", "/submit").body(b"payload"),
|
|
)
|
|
.await,
|
|
));
|
|
responses.push((
|
|
"client tries to set the header",
|
|
go(
|
|
"app.test",
|
|
Req::get("app.test", "/").header("x-api-key", "attacker"),
|
|
)
|
|
.await,
|
|
));
|
|
responses.push((
|
|
"upstream error status",
|
|
go("app.test", Req::get("app.test", "/status/500")).await,
|
|
));
|
|
responses.push((
|
|
"upstream reflects the secret in a header",
|
|
go("app.test", Req::get("app.test", "/reflect?h=x-api-key")).await,
|
|
));
|
|
responses.push((
|
|
"upstream down",
|
|
go("down.test", Req::get("down.test", "/")).await,
|
|
));
|
|
responses.push((
|
|
"unknown host",
|
|
go("app.test", Req::get("nope.test", "/")).await,
|
|
));
|
|
let plain = client::http(edge.http, &Req::get("app.test", "/x?y=1"))
|
|
.await
|
|
.unwrap();
|
|
responses.push(("plain HTTP redirect", plain));
|
|
|
|
for (what, r) in &responses {
|
|
assert_resp_clean(what, r, &secret);
|
|
}
|
|
assert_eq!(responses[0].1.status, 200);
|
|
assert_eq!(responses[4].1.status, 200);
|
|
assert!(
|
|
responses[4].1.header("x-reflected").is_none(),
|
|
"edge strips a response header carrying a secret"
|
|
);
|
|
assert_eq!(responses[5].1.status, 502);
|
|
|
|
match client::websocket(
|
|
edge.https,
|
|
"ws.test",
|
|
roots.clone(),
|
|
"ws.test",
|
|
"/socket",
|
|
&[],
|
|
"ping",
|
|
)
|
|
.await
|
|
.unwrap()
|
|
{
|
|
WsOutcome::Upgraded { status, echo, .. } => {
|
|
assert_eq!(status, 101);
|
|
assert_eq!(echo, "ping");
|
|
}
|
|
WsOutcome::Refused(r) => panic!("websocket refused: {r:?}"),
|
|
}
|
|
|
|
// The secret did reach the upstream, without the trailing newline, replacing the client's value.
|
|
let seen = echo.proxied();
|
|
assert!(seen.len() >= 5, "{seen:?}");
|
|
for r in &seen {
|
|
assert_eq!(
|
|
r.header("x-api-key"),
|
|
Some(secret.as_bytes()),
|
|
"upstream request {}",
|
|
r.path
|
|
);
|
|
}
|
|
|
|
// Give the access log a moment to flush, then check every output channel.
|
|
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
|
let access = edge.access_log();
|
|
assert!(
|
|
access.lines().count() >= 7,
|
|
"access log has the requests:\n{access}"
|
|
);
|
|
assert!(
|
|
access.contains("\"127.0.0.0\""),
|
|
"IPv4 truncated to /24:\n{access}"
|
|
);
|
|
assert!(
|
|
!access.contains("127.0.0.1"),
|
|
"full client IP must not be logged:\n{access}"
|
|
);
|
|
assert!(
|
|
!access.contains("token=abc"),
|
|
"query strings are not logged:\n{access}"
|
|
);
|
|
let stderr = edge.stderr();
|
|
assert!(
|
|
stderr.len() > 100,
|
|
"trace logging produced output:\n{stderr}"
|
|
);
|
|
assert_clean("stderr (trace level)", stderr.as_bytes(), &secret);
|
|
assert_clean("access log", access.as_bytes(), &secret);
|
|
assert_clean("compiled edge config", edge.config_text.as_bytes(), &secret);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn missing_secret_stops_startup_without_leaking_others() {
|
|
let secret = unique_secret();
|
|
let echo = Echo::start("app").await;
|
|
let mut edge = spawn(EdgeSpec {
|
|
site: "secrets = [\"present\", \"absent\"]\n",
|
|
hosts: vec![host_file(
|
|
"app.test",
|
|
&format!(
|
|
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"present\"\n\
|
|
[[request_headers]]\naction = \"set\"\nname = \"X-B\"\nsecret = \"absent\"\n",
|
|
echo.url()
|
|
),
|
|
)],
|
|
secrets: vec![("present".into(), secret.clone().into_bytes())],
|
|
log_level: "trace",
|
|
..Default::default()
|
|
});
|
|
let status = wait_exit(&mut edge);
|
|
assert!(!status.success(), "edge must refuse to start");
|
|
let err = edge.stderr();
|
|
assert!(err.contains("absent"), "names the missing secret:\n{err}");
|
|
assert_clean("stderr", err.as_bytes(), &secret);
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn malformed_secret_is_rejected_without_echoing_it() {
|
|
let secret = unique_secret();
|
|
let echo = Echo::start("app").await;
|
|
let mut edge = spawn(EdgeSpec {
|
|
site: "secrets = [\"api_key\"]\n",
|
|
hosts: vec![host_file(
|
|
"app.test",
|
|
&format!(
|
|
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"api_key\"\n",
|
|
echo.url()
|
|
),
|
|
)],
|
|
// A newline inside the value would split the header: refused.
|
|
secrets: vec![(
|
|
"api_key".into(),
|
|
format!("{secret}\ninjected: yes\n").into_bytes(),
|
|
)],
|
|
log_level: "trace",
|
|
..Default::default()
|
|
});
|
|
let status = wait_exit(&mut edge);
|
|
assert!(
|
|
!status.success(),
|
|
"edge must refuse a secret that is not a valid header value"
|
|
);
|
|
assert_clean("stderr", edge.stderr().as_bytes(), &secret);
|
|
}
|
|
|
|
fn wait_exit(edge: &mut crate::common::Edge) -> std::process::ExitStatus {
|
|
for _ in 0..200 {
|
|
if let Some(s) = edge.exited() {
|
|
return s;
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(50));
|
|
}
|
|
panic!("edge kept running:\n{}", edge.stderr());
|
|
}
|
|
}
|