diff --git a/crates/edge/src/lib.rs b/crates/edge/src/lib.rs index a7292bb..a1ab7f1 100644 --- a/crates/edge/src/lib.rs +++ b/crates/edge/src/lib.rs @@ -180,6 +180,15 @@ pub fn server(shared: Arc, opts: &Options) -> Result Ok(server) } +/// For running edge inside a test process on Windows (the differential suite with Windows +/// paths, T4). Pingora on Windows stops only on Ctrl-C, so `stop` is ignored and the server +/// lives until the test process ends. Ianitor does not ship edge on Windows (Caddy mode). +#[cfg(windows)] +pub fn run_until(server: Server, stop: tokio::sync::oneshot::Receiver<()>) { + drop(stop); + server.run(pingora::server::RunArgs::default()); +} + /// Run until `stop` resolves (or is dropped). For running edge inside a test process. #[cfg(unix)] pub fn run_until(server: Server, stop: tokio::sync::oneshot::Receiver<()>) { diff --git a/crates/edge/src/proxy.rs b/crates/edge/src/proxy.rs index f79e8cd..0ec74ad 100644 --- a/crates/edge/src/proxy.rs +++ b/crates/edge/src/proxy.rs @@ -391,15 +391,20 @@ impl ProxyHttp for EdgeProxy { ctx: &mut Ctx, ) -> Result<()> { let Some(host) = &ctx.host else { return Ok(()) }; - apply(&host.response_ops, |n, v| { - match v { - Some(v) => resp.insert_header(n.clone(), v.clone())?, - None => { - resp.remove_header(n); + // Response rules are for HTTP answers, not the switch to a websocket: Caddy leaves a + // `101` as the upstream sent it, and so does edge (found by the T4 preview fixture in + // the differential suite: `X-Robots-Tag` on a websocket upgrade). + if resp.status.as_u16() != 101 { + apply(&host.response_ops, |n, v| { + match v { + Some(v) => resp.insert_header(n.clone(), v.clone())?, + None => { + resp.remove_header(n); + } } - } - Ok(()) - })?; + Ok(()) + })?; + } // Defence in depth: never hand a secret back to a client, even if the upstream echoes it. if !host.secret_values.is_empty() { let leaking: Vec = resp diff --git a/tests/diff/Cargo.toml b/tests/diff/Cargo.toml index be876f6..7261e19 100644 --- a/tests/diff/Cargo.toml +++ b/tests/diff/Cargo.toml @@ -15,6 +15,7 @@ tokio.workspace = true serde_json.workspace = true rustls.workspace = true rcgen.workspace = true +toml.workspace = true [dev-dependencies] ianitor-control = { path = "../../crates/control" } diff --git a/tests/diff/src/lib.rs b/tests/diff/src/lib.rs index 6d7446c..1464b9e 100644 --- a/tests/diff/src/lib.rs +++ b/tests/diff/src/lib.rs @@ -7,9 +7,12 @@ //! to Caddy (the `caddy` binary running the generated JSON); status, selected headers and //! body are compared. Everything listens on 127.0.0.1. //! +//! It runs on Linux and on Windows. On Windows the paths in both configs are Windows paths +//! (`F:/…/certs/x.crt`, Caddy's `{file.F:/…/creds/api_key}`), which is what Windows Server's +//! Caddy mode serves from (T4); Pingora runs in-process there only as the reference. +//! //! Test code: panics are failures. -#![cfg(unix)] #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use std::collections::BTreeMap; @@ -50,6 +53,46 @@ fn host(name: &str, body: &str) -> (String, String) { ) } +/// The preview domain of the fixtures, and two branch previews in it (T4): one with a plain +/// branch name, one with a hostile one (mapped to a hashed label). +pub const PREVIEW_DOMAIN: &str = "pv.test"; +pub const PREVIEW_PLAIN: &str = "fix-42--shop.pv.test"; + +/// The host name of the hostile-branch preview. +pub fn preview_hostile() -> &'static str { + static H: std::sync::OnceLock = std::sync::OnceLock::new(); + H.get_or_init(|| { + ianitor_core::preview::hostname("github:acme/Web.App", "feature/Ünï ../x", PREVIEW_DOMAIN) + .expect("a preview host name") + }) +} + +/// A preview host file exactly as control writes it (`ianitor_control::previews::spec`). +fn preview_host(project: &str, branch: &str, upstream: &str) -> (String, String) { + let name = ianitor_core::preview::hostname(project, branch, PREVIEW_DOMAIN).unwrap(); + let mut t = toml::Table::new(); + t.insert("hostname".into(), name.clone().into()); + t.insert( + "upstreams".into(), + toml::Value::Array(vec![upstream.into()]), + ); + t.insert("tls".into(), "domi".into()); + let mut rule = toml::Table::new(); + rule.insert("action".into(), "set".into()); + rule.insert("name".into(), "X-Robots-Tag".into()); + rule.insert("value".into(), "noindex, nofollow".into()); + t.insert( + "response_headers".into(), + toml::Value::Array(vec![toml::Value::Table(rule)]), + ); + let mut p = toml::Table::new(); + p.insert("project".into(), project.into()); + p.insert("branch".into(), branch.into()); + p.insert("expires_at".into(), toml::Value::Integer(4_000_000_000)); + t.insert("preview".into(), toml::Value::Table(p)); + (format!("hosts/{name}.toml"), toml::to_string(&t).unwrap()) +} + impl Env { pub async fn start() -> Env { let app = Echo::start("app").await; @@ -117,7 +160,15 @@ impl Env { "device.test", &format!("upstreams = [\"{}\"]\ndevice_certs = true\n", app.url()), ), + preview_host("shop", "fix-42", &app.url()), + preview_host("github:acme/Web.App", "feature/Ünï ../x", &ws.url()), ]; + assert!( + hosts + .iter() + .any(|(f, _)| f == &format!("hosts/{PREVIEW_PLAIN}.toml")), + "the plain preview fixture is {PREVIEW_PLAIN}" + ); let sources: Vec = hosts .iter() .map(|(n, c)| SourceFile { @@ -171,13 +222,16 @@ impl Env { } pub fn options(&self, engine: &str) -> CompileOptions { + // On Windows these are drive paths (F:\\…), written with forward slashes as control + // writes them (`ianitor_core::compile::windows_path`). + let w = |p: String| compile::windows_path(&p); CompileOptions { http_listen: local(free_port()), https_listen: local(free_port()), - cert_dir: self.dir.str("certs"), - state_dir: self.dir.str(&format!("state-{engine}")), - access_log: self.dir.str(&format!("logs/{engine}-access.log")), - caddy_credentials_dir: self.dir.str("creds"), + cert_dir: w(self.dir.str("certs")), + state_dir: w(self.dir.str(&format!("state-{engine}"))), + access_log: w(self.dir.str(&format!("logs/{engine}-access.log"))), + caddy_credentials_dir: w(self.dir.str("creds")), caddy_admin: None, caddy_admin_socket: None, trusted_proxies: Vec::new(), @@ -445,6 +499,30 @@ pub fn corpus() -> Vec { Req::get(p, "https://evil.test/x"), ), http("HTTP redirects to HTTPS", Req::get(p, "/a/b?c=d")), + https( + "preview: plain branch", + PREVIEW_PLAIN, + Req::get(PREVIEW_PLAIN, "/a?b=c"), + ), + http( + "preview: HTTP redirects to HTTPS", + Req::get(PREVIEW_PLAIN, "/x"), + ), + https( + "preview: unknown branch of the preview domain", + PREVIEW_PLAIN, + Req::get("main--shop.pv.test", "/"), + ), + https( + "preview: hostile branch", + preview_hostile(), + Req::get(preview_hostile(), "/"), + ), + ws( + "preview: websocket on a hostile branch", + preview_hostile(), + vec![], + ), http("HTTP unknown host", Req::get("unknown.test", "/")), http( "HTTP unknown ACME token", diff --git a/tests/diff/tests/diff.rs b/tests/diff/tests/diff.rs index 9e4d5f3..342030c 100644 --- a/tests/diff/tests/diff.rs +++ b/tests/diff/tests/diff.rs @@ -7,17 +7,8 @@ #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] -#[cfg(not(unix))] -#[test] -fn skipped_on_windows() { - eprintln!( - "SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md", - file!() - ); -} - -#[cfg(unix)] -mod unix { +// Runs on Linux and on Windows (with Windows paths; see the crate's docs). +mod both { use ianitor_diff::{ Action, Env, break_caddy, break_edge, compile_pair, corpus, run_cases, run_corpus, settle, start_caddy, start_edge, diff --git a/tests/support/src/proc.rs b/tests/support/src/proc.rs index 1f85fb4..a0c3339 100644 --- a/tests/support/src/proc.rs +++ b/tests/support/src/proc.rs @@ -126,8 +126,13 @@ pub fn find_binary(name: &str) -> Option { return Some(PathBuf::from(p)); } let path = std::env::var_os("PATH")?; + let file = if cfg!(windows) { + format!("{name}.exe") + } else { + name.to_owned() + }; std::env::split_paths(&path) - .map(|d| d.join(name)) + .map(|d| d.join(&file)) .find(|p| p.is_file()) }