Skip to main content

s4_server/
routing.rs

1//! `/health` と `/ready` の HTTP routing layer + CORS OPTIONS preflight
2//! interceptor + SigV4a verify gate。
3//!
4//! S3 server と同じポートで health probe に応答できると AWS ALB / NLB / k8s
5//! readiness probe との統合が単純になる。
6//!
7//! - `GET /health` → 常に `200 OK` (server プロセスが生きていれば返す)
8//! - `GET /ready` → `ready_check` future を await し、`Ok(())` なら 200、
9//!   それ以外 (backend 不通等) は 503。
10//! - `OPTIONS /<bucket>[/<key>]` (Origin + Access-Control-Request-Method 付き)
11//!   → v0.7 #44: `cors_manager` が attach されていれば、bucket の登録された
12//!   rule list に対して preflight match を実行し、200 + Allow-* header を
13//!   組み立てて返す (no match なら 403)。s3s framework は OPTIONS verb を
14//!   typed handler として持たないため、HTTP-level の interceptor で寄せる。
15//! - `Authorization: AWS4-ECDSA-P256-SHA256 ...` (SigV4a) を持つ request
16//!   → v0.7 #47: `sigv4a_gate` が attach されていれば、listener 側で署名を
17//!   verify し、success なら inner S3Service へ forward、failure なら 403
18//!   `SignatureDoesNotMatch` / `InvalidAccessKeyId` を直接返す。s3s 既存の
19//!   SigV4 verifier は `AWS4-ECDSA-P256-SHA256` を "unknown algorithm" として
20//!   reject するため、middleware を挟まないと SigV4a request は届かない。
21//! - その他のパス → inner S3Service へ委譲
22
23use std::convert::Infallible;
24use std::future::Future;
25use std::pin::Pin;
26use std::sync::Arc;
27
28use bytes::Bytes;
29use http_body_util::Full;
30use hyper::body::Incoming;
31use hyper::service::Service;
32use hyper::{Method, Request, Response, StatusCode};
33use metrics_exporter_prometheus::PrometheusHandle;
34
35use crate::cors::{CorsManager, CorsRule};
36use crate::service::SigV4aGate;
37
38/// readiness check 関数。bound is `Send + Sync` for cross-task use.
39pub type ReadyCheck =
40    Arc<dyn Fn() -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>> + Send + Sync>;
41
42/// inner service と health/ready/metrics + CORS preflight handler +
43/// SigV4a verify gate を合成する hyper Service。
44#[derive(Clone)]
45pub struct HealthRouter<S> {
46    pub inner: S,
47    pub ready_check: Option<ReadyCheck>,
48    pub metrics_handle: Option<PrometheusHandle>,
49    /// v0.7 #44: optional CORS bucket-config manager. When attached,
50    /// OPTIONS requests carrying `Origin` + `Access-Control-Request-Method`
51    /// are intercepted before reaching the s3s service and answered
52    /// directly with Access-Control-Allow-* headers (or 403 if no rule
53    /// matches). When `None`, OPTIONS falls through to the inner service
54    /// (s3s typically returns 405 since no S3 handler maps to OPTIONS).
55    pub cors_manager: Option<Arc<CorsManager>>,
56    /// v0.7 #47: optional SigV4a verify gate. When attached, requests
57    /// whose `Authorization` header begins with `AWS4-ECDSA-P256-SHA256`
58    /// (or that carry `X-Amz-Region-Set`) are verified at the HTTP
59    /// layer using the configured ECDSA-P-256 credential store; on
60    /// failure the listener returns 403 directly. When `None`, the
61    /// gate is a no-op so plain SigV4 deployments are unaffected.
62    pub sigv4a_gate: Option<Arc<SigV4aGate>>,
63    /// v0.7 #47: region name used when checking
64    /// `X-Amz-Region-Set` membership during SigV4a verification. The
65    /// listener is single-region in this milestone — operators that
66    /// front S4 with a Multi-Region Access Point set this to the
67    /// canonical "this listener's region" string. Defaults to
68    /// `"us-east-1"` (the AWS-default region when none is configured).
69    pub region: String,
70}
71
72impl<S> HealthRouter<S> {
73    pub fn new(inner: S, ready_check: Option<ReadyCheck>) -> Self {
74        Self {
75            inner,
76            ready_check,
77            metrics_handle: None,
78            cors_manager: None,
79            sigv4a_gate: None,
80            region: "us-east-1".to_string(),
81        }
82    }
83
84    #[must_use]
85    pub fn with_metrics(mut self, handle: PrometheusHandle) -> Self {
86        self.metrics_handle = Some(handle);
87        self
88    }
89
90    /// v0.7 #44: attach an `Arc<CorsManager>` so OPTIONS preflight
91    /// requests are handled at the HTTP layer instead of falling through
92    /// to s3s.
93    #[must_use]
94    pub fn with_cors_manager(mut self, mgr: Arc<CorsManager>) -> Self {
95        self.cors_manager = Some(mgr);
96        self
97    }
98
99    /// v0.7 #47: attach an `Arc<SigV4aGate>` so `AWS4-ECDSA-P256-SHA256`
100    /// requests are verified at the HTTP layer instead of being
101    /// rejected by s3s' SigV4 verifier as "unknown algorithm".
102    #[must_use]
103    pub fn with_sigv4a_gate(mut self, gate: Arc<SigV4aGate>) -> Self {
104        self.sigv4a_gate = Some(gate);
105        self
106    }
107
108    /// v0.7 #47: override the listener's "served region" string used
109    /// to check `X-Amz-Region-Set` membership during SigV4a
110    /// verification. Defaults to `"us-east-1"`.
111    #[must_use]
112    pub fn with_region(mut self, region: impl Into<String>) -> Self {
113        self.region = region.into();
114        self
115    }
116}
117
118/// v0.7 #44: HTTP-level OPTIONS preflight interceptor.
119///
120/// Returns:
121/// - `Some(response)` if `req` is an OPTIONS preflight (Origin +
122///   Access-Control-Request-Method headers present) targeting a bucket
123///   with CORS configured. The response is 200 with Allow-* headers
124///   when a rule matches, or 403 when no rule matches the
125///   (origin, method, headers) triple.
126/// - `None` if the request is not a preflight, or no CORS config is
127///   registered for the target bucket — caller forwards to the s3s
128///   service.
129///
130/// `cors` is `Option<&Arc<CorsManager>>` so callers can pass through
131/// the inner service's optional manager without unwrapping first.
132///
133/// Generic over the request body type `B` so unit tests can drive the
134/// matcher with `Request<()>` without constructing a real `Incoming`
135/// stream (only headers, method, and URI are inspected).
136#[must_use]
137pub fn try_handle_preflight<B>(
138    req: &Request<B>,
139    cors: Option<&Arc<CorsManager>>,
140) -> Option<Response<s3s::Body>> {
141    if req.method() != Method::OPTIONS {
142        return None;
143    }
144    let mgr = cors?;
145    // Path is `/<bucket>` or `/<bucket>/<key>` — first segment is bucket.
146    // Empty path or a query-only request has no bucket and is not a
147    // preflight we can answer.
148    let path = req.uri().path();
149    let bucket = path.trim_start_matches('/').split('/').next()?;
150    if bucket.is_empty() {
151        return None;
152    }
153    let origin = req.headers().get("origin")?.to_str().ok()?;
154    let method = req
155        .headers()
156        .get("access-control-request-method")?
157        .to_str()
158        .ok()?;
159    // Access-Control-Request-Headers is a comma-separated list, optional
160    // (browsers omit it when no custom headers are being sent).
161    let req_headers: Vec<String> = req
162        .headers()
163        .get("access-control-request-headers")
164        .and_then(|h| h.to_str().ok())
165        .map(|s| {
166            s.split(',')
167                .map(|t| t.trim().to_string())
168                .filter(|t| !t.is_empty())
169                .collect()
170        })
171        .unwrap_or_default();
172    // No config for this bucket → not our problem (let s3s handle / 404).
173    // We need to distinguish "no config" from "config but no rule matches"
174    // to correctly fall through vs. return 403.
175    let _ = mgr.get(bucket)?;
176    match mgr.match_preflight(bucket, origin, method, &req_headers) {
177        Some(rule) => Some(build_preflight_allow_response(&rule, origin)),
178        None => Some(build_preflight_deny_response()),
179    }
180}
181
182/// 200 response with the matched rule's Allow-* headers.
183fn build_preflight_allow_response(rule: &CorsRule, origin: &str) -> Response<s3s::Body> {
184    let mut builder = Response::builder().status(StatusCode::OK);
185    // Echo the matched origin: literal "*" if the rule used a wildcard,
186    // otherwise the requesting origin verbatim (S3 spec).
187    let allow_origin: String = if rule.allowed_origins.iter().any(|o| o == "*") {
188        "*".into()
189    } else {
190        origin.to_owned()
191    };
192    builder = builder.header("Access-Control-Allow-Origin", allow_origin);
193    builder = builder.header(
194        "Access-Control-Allow-Methods",
195        rule.allowed_methods.join(", "),
196    );
197    if !rule.allowed_headers.is_empty() {
198        builder = builder.header(
199            "Access-Control-Allow-Headers",
200            rule.allowed_headers.join(", "),
201        );
202    }
203    if !rule.expose_headers.is_empty() {
204        builder = builder.header(
205            "Access-Control-Expose-Headers",
206            rule.expose_headers.join(", "),
207        );
208    }
209    if let Some(secs) = rule.max_age_seconds {
210        builder = builder.header("Access-Control-Max-Age", secs.to_string());
211    }
212    // Empty body, but set content-length explicitly for clarity.
213    let bytes = Bytes::new();
214    builder = builder.header("content-length", "0");
215    builder
216        .body(s3s::Body::http_body(
217            Full::new(bytes).map_err(|never| match never {}),
218        ))
219        .expect("preflight response builder")
220}
221
222/// 403 response when an OPTIONS preflight reaches a bucket with CORS
223/// configured but no rule matches the (origin, method, headers) triple.
224fn build_preflight_deny_response() -> Response<s3s::Body> {
225    let body = Bytes::from_static(b"CORSResponse: This CORS request is not allowed.");
226    Response::builder()
227        .status(StatusCode::FORBIDDEN)
228        .header("content-type", "text/plain; charset=utf-8")
229        .header("content-length", body.len().to_string())
230        .body(s3s::Body::http_body(
231            Full::new(body).map_err(|never| match never {}),
232        ))
233        .expect("preflight deny response builder")
234}
235
236// ===========================================================================
237// v0.7 #47 — SigV4a verify gate middleware.
238// ===========================================================================
239
240/// v0.7 #47: Try to verify the request as SigV4a-signed.
241///
242/// Returns:
243/// - `None` if the request is not SigV4a-signed (no `AWS4-ECDSA-P256-SHA256`
244///   `Authorization` prefix and no `X-Amz-Region-Set` header) — the
245///   caller forwards the request to s3s for the default SigV4 path.
246/// - `Some(Ok(()))` if SigV4a verify succeeded — the caller forwards to
247///   the inner service so the S3 handler runs.
248/// - `Some(Err(response))` if SigV4a verify failed — the caller returns
249///   the 403 response directly without ever invoking the inner service.
250///
251/// `gate` is `Option<&Arc<SigV4aGate>>` so callers can pass through the
252/// router's optional gate without unwrapping first; when `None`, this
253/// function always returns `None` (no SigV4a verification configured).
254///
255/// `requested_region` is the listener's served region (used to validate
256/// the request's `X-Amz-Region-Set` header membership).
257///
258/// Generic over the request body type `B` so unit tests can drive the
259/// matcher with `Request<()>` without constructing a real `Incoming`
260/// stream — only headers, method, and URI participate in the canonical
261/// request bytes built here.
262///
263/// # Canonical request bytes
264///
265/// We build a SigV4-shaped canonical request from the HTTP-layer
266/// signal alone (method, URI path, sorted query string, headers in the
267/// order listed by `SignedHeaders=`, and `x-amz-content-sha256` as the
268/// payload hash — the standard "client-supplied body hash" convention
269/// every AWS SDK uses). Reading the body would force a `Request<Bytes>`
270/// rebuild and break the s3s framework's streaming-body assumptions, so
271/// the payload-hash header is the only correct source for SigV4a.
272///
273/// Clients that want to sign over the body must include the actual
274/// SHA-256 of the body in `x-amz-content-sha256`; clients that don't
275/// (most S3 SDKs default to `UNSIGNED-PAYLOAD` for streaming PUTs) sign
276/// over that literal string instead. Either way the bytes the gate
277/// compares against are exactly what the client computed.
278pub fn try_sigv4a_verify<B>(
279    req: &Request<B>,
280    gate: Option<&Arc<SigV4aGate>>,
281    requested_region: &str,
282) -> Option<Result<(), Response<s3s::Body>>> {
283    try_sigv4a_verify_at(req, gate, requested_region, chrono::Utc::now())
284}
285
286/// v0.8.4 #76: like [`try_sigv4a_verify`] but takes an explicit `now`
287/// for tests that need to pin the freshness clock without time-warping
288/// the system clock. Production callers always reach this via
289/// `try_sigv4a_verify` (which calls `chrono::Utc::now()`).
290pub fn try_sigv4a_verify_at<B>(
291    req: &Request<B>,
292    gate: Option<&Arc<SigV4aGate>>,
293    requested_region: &str,
294    now: chrono::DateTime<chrono::Utc>,
295) -> Option<Result<(), Response<s3s::Body>>> {
296    let gate = gate?;
297    if !crate::sigv4a::detect(req) {
298        // Not a SigV4a request — caller forwards to the SigV4 path.
299        return None;
300    }
301    // Pre-parse the Authorization header so we know which signed-headers
302    // list to canonicalise in. If the header is malformed, fail fast
303    // with 403 rather than building canonical bytes that can never
304    // verify.
305    //
306    // v0.8.4 #76: `parse_authorization_header` now returns `Result`
307    // (was `Option`) so the gate can surface scope-shape failures
308    // (`InvalidCredentialScope`, `WrongService`, etc.) as 400
309    // InvalidRequest. Any non-Ok parse falls through to the
310    // SignatureDoesNotMatch 403 the original code returned, since at
311    // this point we can't extract a `signed_headers` list to feed the
312    // canonical-request builder.
313    let auth_hdr = req
314        .headers()
315        .get(http::header::AUTHORIZATION)
316        .and_then(|v| v.to_str().ok());
317    let signed_headers: Vec<String> =
318        match auth_hdr.and_then(|hdr| crate::sigv4a::parse_authorization_header(hdr).ok()) {
319            Some(parsed) => parsed.signed_headers,
320            None => {
321                // No / unparseable Authorization header but `detect` flagged
322                // it as SigV4a-shaped (e.g. only the region-set header is
323                // present) — surface as SignatureDoesNotMatch directly.
324                return Some(Err(build_sigv4a_error_response(
325                    StatusCode::FORBIDDEN,
326                    "SignatureDoesNotMatch",
327                    "missing or malformed Authorization header for SigV4a request",
328                )));
329            }
330        };
331    let canonical = match build_canonical_request_bytes(req, &signed_headers) {
332        Ok(bytes) => bytes,
333        Err(err) => {
334            // v0.8.5 #84 H-4: duplicate signed header (only failure
335            // mode the canonical builder has today). Surface as
336            // `SignatureDoesNotMatch` 403 — the AWS SDKs treat that
337            // as the catch-all auth-failure code, and the diagnostic
338            // is in the response body / server log.
339            tracing::warn!(error = %err, "SigV4a canonical-request build rejected request");
340            return Some(Err(build_sigv4a_error_response(
341                StatusCode::FORBIDDEN,
342                "SignatureDoesNotMatch",
343                &err.to_string(),
344            )));
345        }
346    };
347    match gate.pre_route_at(req, requested_region, &canonical, now) {
348        Ok(()) => Some(Ok(())),
349        Err(err) => {
350            tracing::warn!(error = %err, "SigV4a verify rejected request");
351            Some(Err(build_sigv4a_error_response(
352                err.http_status(),
353                err.s3_error_code(),
354                &err.to_string(),
355            )))
356        }
357    }
358}
359
360/// v0.7 #47: build a SigV4-shaped canonical request from the HTTP
361/// surface alone (no body access). Returns the bytes that the
362/// SigV4a gate will check the ECDSA signature against.
363///
364/// Format (one element per line, joined with `\n`):
365/// 1. HTTP method (uppercase)
366/// 2. canonical URI (path; we leave it untouched since AWS SDKs
367///    pre-encode it the same way s3s receives it)
368/// 3. canonical query string (sorted by name, name=value pairs joined
369///    by `&`; empty when no query string)
370/// 4. canonical headers (one `name:trimmed-value\n` per signed header,
371///    in the **order** they appear in `SignedHeaders=`)
372/// 5. signed headers list (lowercase names joined by `;`)
373/// 6. payload hash (value of `x-amz-content-sha256`, or `UNSIGNED-PAYLOAD`
374///    if absent)
375///
376/// v0.8.5 #84 (audit H-4): every signed header is checked for being
377/// sent **exactly once** on the request. If a header in
378/// `SignedHeaders=` appears more than once we'd have to choose between
379/// the first value (`HeaderMap::get` semantics) and the comma-joined
380/// AWS-canonical form — and any S3 SDK / WAF / sidecar in front of us
381/// would make a different choice, opening "auth confusion" attacks
382/// (sign over the benign first `x-amz-date`, smuggle a second one for
383/// the inner parser). HTTP/1.1 spec already forbids duplicates of
384/// `host` / `x-amz-date` and the AWS SDKs never emit them, so any
385/// duplicate is a malicious or broken request — reject upfront with
386/// [`SigV4aError::DuplicateSignedHeader`].
387fn build_canonical_request_bytes<B>(
388    req: &Request<B>,
389    signed_headers: &[String],
390) -> Result<Vec<u8>, crate::sigv4a::SigV4aError> {
391    let mut buf = String::with_capacity(512);
392    buf.push_str(req.method().as_str());
393    buf.push('\n');
394    buf.push_str(req.uri().path());
395    buf.push('\n');
396    buf.push_str(&canonical_query_string(req.uri().query().unwrap_or("")));
397    buf.push('\n');
398    for name in signed_headers {
399        // v0.8.5 #84 H-4: count occurrences via `get_all` rather than
400        // `get`, which only ever returns the first value. Two
401        // `x-amz-date` headers with `get` would canonicalise to the
402        // first value while a downstream HTTP/1.1 parser might pick
403        // the second — auth confusion. Single-value reject is the
404        // safe choice; comma-join would be the AWS-canonical form
405        // for legitimately multi-valued signed headers, but the AWS
406        // SDKs never sign over comma-joined values for any header
407        // S3 cares about, so refusing duplicates outright matches
408        // every real-world client.
409        let occurrences = req.headers().get_all(name.as_str()).iter().count();
410        if occurrences > 1 {
411            return Err(crate::sigv4a::SigV4aError::DuplicateSignedHeader {
412                header: name.clone(),
413            });
414        }
415        let value = req
416            .headers()
417            .get(name.as_str())
418            .and_then(|v| v.to_str().ok())
419            .unwrap_or("");
420        buf.push_str(name);
421        buf.push(':');
422        // Trim whitespace and collapse repeated inner whitespace per
423        // SigV4 canonicalisation rules. This is the same trimming AWS
424        // SDKs do when they sign.
425        buf.push_str(&trim_collapse_ws(value));
426        buf.push('\n');
427    }
428    buf.push('\n');
429    buf.push_str(&signed_headers.join(";"));
430    buf.push('\n');
431    let payload_hash = req
432        .headers()
433        .get("x-amz-content-sha256")
434        .and_then(|v| v.to_str().ok())
435        .unwrap_or("UNSIGNED-PAYLOAD");
436    buf.push_str(payload_hash);
437    Ok(buf.into_bytes())
438}
439
440/// SigV4 canonical query string: split on `&`, parse each `k=v` (or
441/// `k`), sort lexicographically by name (then by value), re-join with
442/// `&`. Empty input → empty string. We do **not** re-encode the values
443/// — they already arrived URL-encoded over the wire, and AWS SDKs
444/// expect the server to compare the bytes verbatim.
445fn canonical_query_string(query: &str) -> String {
446    if query.is_empty() {
447        return String::new();
448    }
449    let mut pairs: Vec<(&str, &str)> = query
450        .split('&')
451        .filter(|s| !s.is_empty())
452        .map(|kv| match kv.split_once('=') {
453            Some((k, v)) => (k, v),
454            None => (kv, ""),
455        })
456        .collect();
457    pairs.sort_by(|a, b| a.0.cmp(b.0).then_with(|| a.1.cmp(b.1)));
458    let mut out = String::with_capacity(query.len());
459    for (i, (k, v)) in pairs.iter().enumerate() {
460        if i > 0 {
461            out.push('&');
462        }
463        out.push_str(k);
464        out.push('=');
465        out.push_str(v);
466    }
467    out
468}
469
470/// SigV4 header-value canonicalisation: trim leading + trailing
471/// whitespace and collapse runs of internal whitespace to a single
472/// space. This mirrors what AWS SDKs do client-side when computing the
473/// canonical request — without it, a header value with extra spaces
474/// would canonicalise differently on each side.
475fn trim_collapse_ws(s: &str) -> String {
476    let trimmed = s.trim();
477    let mut out = String::with_capacity(trimmed.len());
478    let mut prev_ws = false;
479    for c in trimmed.chars() {
480        if c.is_whitespace() {
481            if !prev_ws {
482                out.push(' ');
483            }
484            prev_ws = true;
485        } else {
486            out.push(c);
487            prev_ws = false;
488        }
489    }
490    out
491}
492
493/// v0.7 #47: build an AWS-shaped XML response for a SigV4a verify
494/// failure. The response body matches the wire format AWS S3 emits for
495/// the same conditions so SDKs surface the right exception class to the
496/// caller.
497///
498/// v0.8.4 #76: now takes `status` so the gate can return 400
499/// InvalidRequest for malformed-input failures (missing x-amz-date,
500/// wrong service scope, etc.) and 403 for actual auth failures.
501fn build_sigv4a_error_response(
502    status: StatusCode,
503    code: &str,
504    message: &str,
505) -> Response<s3s::Body> {
506    let body_str = format!(
507        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
508         <Error>\n  <Code>{code}</Code>\n  <Message>{message}</Message>\n</Error>"
509    );
510    let bytes = Bytes::from(body_str.into_bytes());
511    Response::builder()
512        .status(status)
513        .header("content-type", "application/xml")
514        .header("content-length", bytes.len().to_string())
515        .body(s3s::Body::http_body(
516            Full::new(bytes).map_err(|never| match never {}),
517        ))
518        .expect("sigv4a error response builder")
519}
520
521/// `/health` と `/ready` のレスポンス Body。
522/// inner S3Service の Body と互換する形にするために `s3s::Body` でラップ可能な
523/// `Full<Bytes>` を `s3s::Body::http_body` 経由で構築する。
524type RespBody = s3s::Body;
525
526fn make_text_response(status: StatusCode, body: &'static str) -> Response<RespBody> {
527    let bytes = Bytes::from_static(body.as_bytes());
528    Response::builder()
529        .status(status)
530        .header("content-type", "text/plain; charset=utf-8")
531        .header("content-length", bytes.len().to_string())
532        .body(s3s::Body::http_body(
533            Full::new(bytes).map_err(|never| match never {}),
534        ))
535        .expect("static response")
536}
537
538fn make_owned_text_response(
539    status: StatusCode,
540    content_type: &'static str,
541    body: String,
542) -> Response<RespBody> {
543    let bytes = Bytes::from(body.into_bytes());
544    Response::builder()
545        .status(status)
546        .header("content-type", content_type)
547        .header("content-length", bytes.len().to_string())
548        .body(s3s::Body::http_body(
549            Full::new(bytes).map_err(|never| match never {}),
550        ))
551        .expect("owned response")
552}
553
554impl<S> Service<Request<Incoming>> for HealthRouter<S>
555where
556    S: Service<Request<Incoming>, Response = Response<s3s::Body>, Error = s3s::HttpError>
557        + Clone
558        + Send
559        + 'static,
560    S::Future: Send + 'static,
561{
562    type Response = Response<RespBody>;
563    type Error = s3s::HttpError;
564    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
565
566    fn call(&self, req: Request<Incoming>) -> Self::Future {
567        // v0.7 #44: short-circuit CORS OPTIONS preflight at the HTTP layer
568        // before health/metrics dispatch. Preflight must run only for
569        // OPTIONS requests, and only when a CORS manager is attached and
570        // a config exists for the requested bucket; otherwise fall
571        // through to the existing routing logic.
572        if let Some(resp) = try_handle_preflight(&req, self.cors_manager.as_ref()) {
573            return Box::pin(async move { Ok(resp) });
574        }
575        // v0.7 #47: SigV4a verify gate. When the request is signed with
576        // `AWS4-ECDSA-P256-SHA256` and a credential store is configured,
577        // verify here at the HTTP layer (s3s' SigV4 verifier would
578        // otherwise reject the request as "unknown algorithm" before
579        // any handler ran). Plain SigV4 (HMAC) requests return `None`
580        // and fall through to the inner service untouched.
581        if let Some(result) = try_sigv4a_verify(&req, self.sigv4a_gate.as_ref(), &self.region) {
582            match result {
583                Ok(()) => {
584                    // verified — fall through to the path-routing logic
585                    // below (the health/metrics/inner-service dispatch).
586                }
587                Err(resp) => return Box::pin(async move { Ok(resp) }),
588            }
589        }
590        let path = req.uri().path();
591        match (req.method(), path) {
592            (&hyper::Method::GET, "/health") | (&hyper::Method::HEAD, "/health") => {
593                Box::pin(async { Ok(make_text_response(StatusCode::OK, "ok\n")) })
594            }
595            (&hyper::Method::GET, "/metrics") | (&hyper::Method::HEAD, "/metrics") => {
596                let handle = self.metrics_handle.clone();
597                Box::pin(async move {
598                    match handle {
599                        Some(h) => {
600                            let body = h.render();
601                            Ok(make_owned_text_response(
602                                StatusCode::OK,
603                                "text/plain; version=0.0.4; charset=utf-8",
604                                body,
605                            ))
606                        }
607                        None => Ok(make_text_response(
608                            StatusCode::SERVICE_UNAVAILABLE,
609                            "metrics not configured\n",
610                        )),
611                    }
612                })
613            }
614            (&hyper::Method::GET, "/ready") | (&hyper::Method::HEAD, "/ready") => {
615                let check = self.ready_check.clone();
616                Box::pin(async move {
617                    match check {
618                        Some(f) => match f().await {
619                            Ok(()) => Ok(make_text_response(StatusCode::OK, "ready\n")),
620                            Err(reason) => {
621                                tracing::warn!(%reason, "readiness check failed");
622                                Ok(make_text_response(
623                                    StatusCode::SERVICE_UNAVAILABLE,
624                                    "not ready\n",
625                                ))
626                            }
627                        },
628                        None => Ok(make_text_response(StatusCode::OK, "ready (no check)\n")),
629                    }
630                })
631            }
632            _ => {
633                let inner = self.inner.clone();
634                Box::pin(async move { inner.call(req).await })
635            }
636        }
637    }
638}
639
640/// `Infallible` を anything に変換するためのトリック (`Full::map_err` 用)
641trait FullExt<B> {
642    fn map_err<E, F: FnMut(Infallible) -> E>(
643        self,
644        f: F,
645    ) -> http_body_util::combinators::MapErr<Self, F>
646    where
647        Self: Sized;
648}
649impl<B> FullExt<B> for Full<B>
650where
651    B: bytes::Buf,
652{
653    fn map_err<E, F: FnMut(Infallible) -> E>(
654        self,
655        f: F,
656    ) -> http_body_util::combinators::MapErr<Self, F>
657    where
658        Self: Sized,
659    {
660        http_body_util::BodyExt::map_err(self, f)
661    }
662}
663
664#[cfg(test)]
665mod preflight_tests {
666    //! v0.7 #44: unit tests for the OPTIONS preflight interceptor.
667    //!
668    //! These exercise [`try_handle_preflight`] directly — no hyper
669    //! `Incoming` body is needed because the function is generic over
670    //! the body type. Behavioural matrix:
671    //!
672    //! 1. matching preflight → 200 + Allow-* headers
673    //! 2. no matching rule (config exists, but origin/method/headers fail)
674    //!    → 403
675    //! 3. missing `Origin` header → `None` (not a CORS preflight)
676    //! 4. non-OPTIONS verb → `None`
677    //! 5. no CORS config registered for the bucket → `None`
678    //! 6. no manager attached → `None`
679
680    use super::*;
681    use crate::cors::{CorsConfig, CorsManager, CorsRule};
682
683    fn rule(origins: &[&str], methods: &[&str], headers: &[&str]) -> CorsRule {
684        CorsRule {
685            allowed_origins: origins.iter().map(|s| (*s).to_owned()).collect(),
686            allowed_methods: methods.iter().map(|s| (*s).to_owned()).collect(),
687            allowed_headers: headers.iter().map(|s| (*s).to_owned()).collect(),
688            expose_headers: vec!["ETag".into()],
689            max_age_seconds: Some(600),
690            id: Some("test".into()),
691        }
692    }
693
694    /// Helper: build a `Request<()>` with the given method, path, and
695    /// headers — body is ignored by the matcher.
696    fn req(method: Method, path: &str, headers: &[(&str, &str)]) -> Request<()> {
697        let mut b = Request::builder().method(method).uri(path);
698        for (k, v) in headers {
699            b = b.header(*k, *v);
700        }
701        b.body(()).expect("request builder")
702    }
703
704    fn manager_with_rule() -> Arc<CorsManager> {
705        let mgr = CorsManager::new();
706        mgr.put(
707            "b",
708            CorsConfig {
709                rules: vec![rule(
710                    &["https://app.example.com"],
711                    &["GET", "PUT", "DELETE"],
712                    &["Content-Type", "X-Amz-Date"],
713                )],
714            },
715        );
716        Arc::new(mgr)
717    }
718
719    #[test]
720    fn preflight_match_returns_allow_response() {
721        let mgr = manager_with_rule();
722        let r = req(
723            Method::OPTIONS,
724            "/b/key.txt",
725            &[
726                ("origin", "https://app.example.com"),
727                ("access-control-request-method", "PUT"),
728                ("access-control-request-headers", "content-type, x-amz-date"),
729            ],
730        );
731        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
732        assert_eq!(resp.status(), StatusCode::OK);
733        let h = resp.headers();
734        assert_eq!(
735            h.get("access-control-allow-origin")
736                .and_then(|v| v.to_str().ok()),
737            Some("https://app.example.com")
738        );
739        assert_eq!(
740            h.get("access-control-allow-methods")
741                .and_then(|v| v.to_str().ok()),
742            Some("GET, PUT, DELETE")
743        );
744        assert_eq!(
745            h.get("access-control-allow-headers")
746                .and_then(|v| v.to_str().ok()),
747            Some("Content-Type, X-Amz-Date")
748        );
749        assert_eq!(
750            h.get("access-control-max-age")
751                .and_then(|v| v.to_str().ok()),
752            Some("600")
753        );
754        assert_eq!(
755            h.get("access-control-expose-headers")
756                .and_then(|v| v.to_str().ok()),
757            Some("ETag")
758        );
759    }
760
761    #[test]
762    fn preflight_no_match_returns_403() {
763        let mgr = manager_with_rule();
764        // Origin not in allow-list → no rule matches but bucket has CORS
765        // config, so we must answer 403 directly (not fall through to
766        // s3s, which would otherwise leak the bucket existence via 405).
767        let r = req(
768            Method::OPTIONS,
769            "/b/key.txt",
770            &[
771                ("origin", "https://evil.example.com"),
772                ("access-control-request-method", "PUT"),
773            ],
774        );
775        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
776        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
777        // 403 deny response must NOT carry Allow-Origin (RFC 7234 + S3 wire compat).
778        assert!(resp.headers().get("access-control-allow-origin").is_none());
779    }
780
781    #[test]
782    fn preflight_no_origin_falls_through() {
783        // OPTIONS without Origin is a generic OPTIONS (e.g. `OPTIONS *`)
784        // — not a CORS preflight, must not be intercepted.
785        let mgr = manager_with_rule();
786        let r = req(
787            Method::OPTIONS,
788            "/b/key.txt",
789            &[("access-control-request-method", "PUT")],
790        );
791        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
792    }
793
794    #[test]
795    fn non_options_falls_through() {
796        let mgr = manager_with_rule();
797        // Even with Origin + ACRM headers, GET is not a preflight.
798        let r = req(
799            Method::GET,
800            "/b/key.txt",
801            &[
802                ("origin", "https://app.example.com"),
803                ("access-control-request-method", "PUT"),
804            ],
805        );
806        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
807    }
808
809    #[test]
810    fn no_cors_config_for_bucket_falls_through() {
811        // Manager attached but no rule registered for "ghost" → fall
812        // through to inner service so backend can respond naturally.
813        let mgr = manager_with_rule();
814        let r = req(
815            Method::OPTIONS,
816            "/ghost/key.txt",
817            &[
818                ("origin", "https://app.example.com"),
819                ("access-control-request-method", "PUT"),
820            ],
821        );
822        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
823    }
824
825    #[test]
826    fn no_manager_attached_falls_through() {
827        let r = req(
828            Method::OPTIONS,
829            "/b/key.txt",
830            &[
831                ("origin", "https://app.example.com"),
832                ("access-control-request-method", "PUT"),
833            ],
834        );
835        assert!(try_handle_preflight(&r, None).is_none());
836    }
837
838    #[test]
839    fn preflight_wildcard_origin_echoes_star() {
840        // Rule with `*` origin → response echoes literal "*" (S3 spec).
841        let mgr = CorsManager::new();
842        mgr.put(
843            "b",
844            CorsConfig {
845                rules: vec![rule(&["*"], &["GET", "PUT"], &["*"])],
846            },
847        );
848        let mgr = Arc::new(mgr);
849        let r = req(
850            Method::OPTIONS,
851            "/b/key",
852            &[
853                ("origin", "https://anywhere.example"),
854                ("access-control-request-method", "PUT"),
855                ("access-control-request-headers", "x-custom-header"),
856            ],
857        );
858        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
859        assert_eq!(resp.status(), StatusCode::OK);
860        assert_eq!(
861            resp.headers()
862                .get("access-control-allow-origin")
863                .and_then(|v| v.to_str().ok()),
864            Some("*"),
865            "wildcard rule must echo literal '*' instead of requesting origin"
866        );
867    }
868
869    #[test]
870    fn preflight_empty_path_falls_through() {
871        let mgr = manager_with_rule();
872        let r = req(
873            Method::OPTIONS,
874            "/",
875            &[
876                ("origin", "https://app.example.com"),
877                ("access-control-request-method", "PUT"),
878            ],
879        );
880        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
881    }
882}
883
884#[cfg(test)]
885mod sigv4a_gate_tests {
886    //! v0.7 #47: unit tests for the SigV4a verify gate middleware.
887    //!
888    //! These exercise [`try_sigv4a_verify`] directly — no hyper
889    //! `Incoming` body is needed because the function is generic over
890    //! the body type. The canonical-request bytes computed by the
891    //! middleware are the same bytes the test signs over (we use the
892    //! `build_canonical_request_bytes` helper for both sides), so the
893    //! happy-path verify is end-to-end byte-exact.
894    //!
895    //! Behavioural matrix:
896    //!
897    //! 1. no `AWS4-ECDSA-P256-SHA256` prefix and no region-set header
898    //!    → `None` (caller forwards to s3s SigV4 path)
899    //! 2. SigV4a Authorization + valid signature → `Some(Ok(()))`
900    //! 3. SigV4a Authorization + tampered signature → `Some(Err(403))`
901    //!    with `SignatureDoesNotMatch` body
902    //! 4. SigV4a Authorization + region-set mismatch → `Some(Err(403))`
903    //! 5. gate is `None` (no credential store) → `None` even when the
904    //!    request looks SigV4a-shaped (caller forwards, and s3s will
905    //!    surface its own "unknown algorithm" error — operator sees the
906    //!    misconfiguration rather than a silent pass)
907    //! 6. unknown access-key-id → `Some(Err(403))` with
908    //!    `InvalidAccessKeyId` body
909    //! 7. SigV4a-shaped (region-set header only, no SigV4a auth header)
910    //!    → `Some(Err(403))` (we cannot verify without a parseable
911    //!    Authorization, fail closed)
912
913    use super::*;
914
915    use std::collections::HashMap;
916
917    use http_body_util::BodyExt;
918    use p256::ecdsa::SigningKey;
919    use p256::ecdsa::signature::Signer;
920    use rand::rngs::OsRng;
921
922    use crate::service::SigV4aGate;
923    use crate::sigv4a::{REGION_SET_HEADER, SigV4aCredentialStore};
924
925    fn lower_hex(bytes: &[u8]) -> String {
926        let mut s = String::with_capacity(bytes.len() * 2);
927        for b in bytes {
928            s.push_str(&format!("{b:02x}"));
929        }
930        s
931    }
932
933    /// Build a `Request<()>` with the given method, path, and headers.
934    fn req(method: Method, path: &str, headers: &[(&str, &str)]) -> Request<()> {
935        let mut b = Request::builder().method(method).uri(path);
936        for (k, v) in headers {
937            b = b.header(*k, *v);
938        }
939        b.body(()).expect("request builder")
940    }
941
942    /// Build the SigV4a Authorization header for the given access-key,
943    /// signed-headers list, and signature (lowercase hex DER).
944    fn build_auth_header(access_key: &str, signed_headers: &[&str], sig_hex: &str) -> String {
945        format!(
946            "AWS4-ECDSA-P256-SHA256 \
947             Credential={access_key}/20260513/s3/aws4_request, \
948             SignedHeaders={}, \
949             Signature={sig_hex}",
950            signed_headers.join(";")
951        )
952    }
953
954    /// Build a fully-signed SigV4a `Request<()>` ready for the gate to
955    /// verify. Returns the request and the verifying key it should be
956    /// loaded against.
957    fn make_signed_request(
958        access_key: &str,
959        method: Method,
960        path: &str,
961        region_set: &str,
962    ) -> (Request<()>, p256::ecdsa::VerifyingKey) {
963        let signing = SigningKey::random(&mut OsRng);
964        let verifying = p256::ecdsa::VerifyingKey::from(&signing);
965        let signed_headers_list = [
966            "host",
967            "x-amz-content-sha256",
968            "x-amz-date",
969            REGION_SET_HEADER,
970        ];
971        // Build the request first WITHOUT the Authorization header so we
972        // can compute canonical bytes and sign them; then re-build the
973        // request with the Authorization header attached.
974        let pre = Request::builder()
975            .method(method.clone())
976            .uri(path)
977            .header("host", "s3.example.com")
978            .header(
979                "x-amz-content-sha256",
980                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
981            )
982            .header("x-amz-date", "20260513T120000Z")
983            .header(REGION_SET_HEADER, region_set)
984            .body(())
985            .expect("pre-request");
986        let signed_headers: Vec<String> = signed_headers_list
987            .iter()
988            .map(|s| (*s).to_string())
989            .collect();
990        let canonical =
991            build_canonical_request_bytes(&pre, &signed_headers).expect("test fixture canonical");
992        let sig: p256::ecdsa::Signature = signing.sign(&canonical);
993        let sig_hex = lower_hex(sig.to_der().as_bytes());
994        let auth = build_auth_header(access_key, &signed_headers_list, &sig_hex);
995
996        // Rebuild with the Authorization header — every other header
997        // value is identical so the canonical bytes the gate computes
998        // match what we signed.
999        let r = Request::builder()
1000            .method(method)
1001            .uri(path)
1002            .header("host", "s3.example.com")
1003            .header(
1004                "x-amz-content-sha256",
1005                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1006            )
1007            .header("x-amz-date", "20260513T120000Z")
1008            .header(REGION_SET_HEADER, region_set)
1009            .header("authorization", auth)
1010            .body(())
1011            .expect("signed request");
1012        (r, verifying)
1013    }
1014
1015    fn make_gate_with(access_key: &str, vk: p256::ecdsa::VerifyingKey) -> Arc<SigV4aGate> {
1016        let mut m = HashMap::new();
1017        m.insert(access_key.to_string(), vk);
1018        let store = Arc::new(SigV4aCredentialStore::from_map(m));
1019        Arc::new(SigV4aGate::new(store))
1020    }
1021
1022    /// Drain a `s3s::Body` into bytes for body-content assertions.
1023    async fn body_to_bytes(resp: Response<s3s::Body>) -> Vec<u8> {
1024        resp.into_body()
1025            .collect()
1026            .await
1027            .expect("body collect")
1028            .to_bytes()
1029            .to_vec()
1030    }
1031
1032    /// v0.8.4 #76: pinned `now` matching the `x-amz-date: 20260513T120000Z`
1033    /// the test fixtures stamp. Without this the freshness check would
1034    /// reject every gate test (the timestamp would be days/weeks old by
1035    /// the time CI runs). Production callers use `try_sigv4a_verify`
1036    /// (which calls `Utc::now()`).
1037    fn fixture_now() -> chrono::DateTime<chrono::Utc> {
1038        chrono::DateTime::parse_from_rfc3339("2026-05-13T12:00:00Z")
1039            .unwrap()
1040            .with_timezone(&chrono::Utc)
1041    }
1042
1043    #[test]
1044    fn no_sigv4a_prefix_returns_none() {
1045        // Plain SigV4 (HMAC-SHA256) request — gate must defer to s3s.
1046        let (_, vk) = (
1047            (),
1048            p256::ecdsa::VerifyingKey::from(&SigningKey::random(&mut OsRng)),
1049        );
1050        let gate = make_gate_with("AKIAOK", vk);
1051        let r = req(
1052            Method::GET,
1053            "/bucket/key",
1054            &[(
1055                "authorization",
1056                "AWS4-HMAC-SHA256 Credential=AKIA/20260513/us-east-1/s3/aws4_request, \
1057                 SignedHeaders=host, Signature=deadbeef",
1058            )],
1059        );
1060        assert!(
1061            try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now()).is_none(),
1062            "plain SigV4 request must fall through to the inner service"
1063        );
1064    }
1065
1066    #[test]
1067    fn sigv4a_valid_signature_returns_ok() {
1068        let (r, vk) =
1069            make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1,us-west-2");
1070        let gate = make_gate_with("AKIAOK", vk);
1071        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1072            .expect("must intercept SigV4a request");
1073        assert!(
1074            result.is_ok(),
1075            "valid SigV4a signature must verify: {result:?}"
1076        );
1077    }
1078
1079    #[tokio::test]
1080    async fn sigv4a_tampered_signature_returns_403() {
1081        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1082        let gate = make_gate_with("AKIAOK", vk);
1083
1084        // Tamper one byte of the signature hex inside the Authorization
1085        // header — the DER decode may still succeed, but ECDSA verify
1086        // will fail (or the DER decode itself will fail; both surface
1087        // as `SignatureDoesNotMatch`).
1088        let auth = r
1089            .headers()
1090            .get("authorization")
1091            .and_then(|v| v.to_str().ok())
1092            .expect("auth header")
1093            .to_string();
1094        // Flip the last hex char to corrupt the signature.
1095        let mut chars: Vec<char> = auth.chars().collect();
1096        let last = chars.len() - 1;
1097        chars[last] = if chars[last] == '0' { '1' } else { '0' };
1098        let tampered_auth: String = chars.into_iter().collect();
1099        let tampered = req(
1100            Method::GET,
1101            "/bucket/key",
1102            &[
1103                ("host", "s3.example.com"),
1104                (
1105                    "x-amz-content-sha256",
1106                    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1107                ),
1108                ("x-amz-date", "20260513T120000Z"),
1109                (REGION_SET_HEADER, "us-east-1"),
1110                ("authorization", &tampered_auth),
1111            ],
1112        );
1113        let result = try_sigv4a_verify_at(&tampered, Some(&gate), "us-east-1", fixture_now())
1114            .expect("must intercept SigV4a request");
1115        let resp = result.expect_err("tampered signature must surface a 403 response");
1116        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1117        let body = body_to_bytes(resp).await;
1118        let body_str = String::from_utf8(body).expect("xml utf-8");
1119        assert!(
1120            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1121            "403 body must surface SignatureDoesNotMatch: {body_str}"
1122        );
1123    }
1124
1125    #[tokio::test]
1126    async fn sigv4a_region_set_mismatch_returns_403() {
1127        // Sign for `us-east-1` only, then verify with the listener
1128        // region claiming `eu-west-1` — must fail with
1129        // SignatureDoesNotMatch (the region-set check sits inside the
1130        // gate's verify path, and any failure there folds to
1131        // SignatureDoesNotMatch).
1132        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1133        let gate = make_gate_with("AKIAOK", vk);
1134        let result = try_sigv4a_verify_at(&r, Some(&gate), "eu-west-1", fixture_now())
1135            .expect("must intercept SigV4a request");
1136        let resp = result.expect_err("region mismatch must produce 403");
1137        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1138        let body = body_to_bytes(resp).await;
1139        let body_str = String::from_utf8(body).expect("xml utf-8");
1140        assert!(
1141            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1142            "region-set mismatch must surface SignatureDoesNotMatch: {body_str}"
1143        );
1144    }
1145
1146    #[test]
1147    fn no_gate_attached_returns_none() {
1148        // Even a SigV4a-shaped request returns None when no gate is
1149        // installed — the listener will hand it to s3s, which surfaces
1150        // its own "unknown algorithm" error so the misconfiguration is
1151        // visible to the operator.
1152        let (r, _vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1153        assert!(
1154            try_sigv4a_verify_at(&r, None, "us-east-1", fixture_now()).is_none(),
1155            "missing gate must defer to inner service"
1156        );
1157    }
1158
1159    #[tokio::test]
1160    async fn unknown_access_key_returns_403_invalid_access_key_id() {
1161        // Sign with one key but load the credential store with a
1162        // different access-key-id → InvalidAccessKeyId.
1163        let (r, _vk_unused) =
1164            make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1165        let other_signing = SigningKey::random(&mut OsRng);
1166        let other_vk = p256::ecdsa::VerifyingKey::from(&other_signing);
1167        let gate = make_gate_with("AKIASOMEONEELSE", other_vk);
1168        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1169            .expect("must intercept SigV4a request");
1170        let resp = result.expect_err("unknown key must produce 403");
1171        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1172        let body = body_to_bytes(resp).await;
1173        let body_str = String::from_utf8(body).expect("xml utf-8");
1174        assert!(
1175            body_str.contains("<Code>InvalidAccessKeyId</Code>"),
1176            "unknown access-key must surface InvalidAccessKeyId: {body_str}"
1177        );
1178    }
1179
1180    #[tokio::test]
1181    async fn region_set_header_only_without_sigv4a_auth_returns_403() {
1182        // Some legacy clients stamp the `X-Amz-Region-Set` header
1183        // before swapping the algorithm string. `detect` flags this as
1184        // SigV4a-shaped but we cannot verify without a parseable
1185        // Authorization → fail closed (SignatureDoesNotMatch).
1186        let signing = SigningKey::random(&mut OsRng);
1187        let vk = p256::ecdsa::VerifyingKey::from(&signing);
1188        let gate = make_gate_with("AKIAOK", vk);
1189        let r = req(
1190            Method::GET,
1191            "/bucket/key",
1192            &[
1193                // SigV4 algorithm + region-set header → detected, but
1194                // the Authorization is plain SigV4 so `parse_authorization_header`
1195                // returns None.
1196                (
1197                    "authorization",
1198                    "AWS4-HMAC-SHA256 Credential=AKIA/20260513/us-east-1/s3/aws4_request, \
1199                     SignedHeaders=host, Signature=deadbeef",
1200                ),
1201                (REGION_SET_HEADER, "us-east-1"),
1202            ],
1203        );
1204        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1205            .expect("must intercept SigV4a-shaped request");
1206        let resp = result.expect_err("region-set without sigv4a auth must produce 403");
1207        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1208        let body = body_to_bytes(resp).await;
1209        let body_str = String::from_utf8(body).expect("xml utf-8");
1210        assert!(
1211            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1212            "missing/malformed Authorization for SigV4a-shaped request must fail closed: {body_str}"
1213        );
1214    }
1215
1216    /// v0.8.4 #76 (audit H-6): captured-request replay outside the
1217    /// 15-min window → 403 RequestTimeTooSkewed (not
1218    /// SignatureDoesNotMatch). This is the headline gate-level
1219    /// behaviour change; pre-#76 the same captured request would have
1220    /// reached the inner service, allowing destructive replay (DELETE
1221    /// included).
1222    #[tokio::test]
1223    async fn sigv4a_replay_outside_window_returns_403_request_time_too_skewed() {
1224        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1225        let gate = make_gate_with("AKIAOK", vk);
1226        // Request stamped 20260513T120000Z; "now" is 30 min later → drift
1227        // 1800s, beyond the 900s default tolerance.
1228        let now = chrono::DateTime::parse_from_rfc3339("2026-05-13T12:30:00Z")
1229            .unwrap()
1230            .with_timezone(&chrono::Utc);
1231        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", now)
1232            .expect("must intercept SigV4a request");
1233        let resp = result.expect_err("replay outside window must reject");
1234        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1235        let body = body_to_bytes(resp).await;
1236        let body_str = String::from_utf8(body).expect("xml utf-8");
1237        assert!(
1238            body_str.contains("<Code>RequestTimeTooSkewed</Code>"),
1239            "replay outside window must surface RequestTimeTooSkewed: {body_str}"
1240        );
1241    }
1242
1243    /// Cover the canonical-request builder directly: empty query
1244    /// string, sorted multi-pair query, and header value collapsed
1245    /// whitespace all hit the right code paths.
1246    #[test]
1247    fn canonical_request_bytes_format() {
1248        let r = req(
1249            Method::PUT,
1250            "/bucket/key?z=1&a=2",
1251            &[
1252                ("host", "s3.example.com"),
1253                ("x-amz-content-sha256", "UNSIGNED-PAYLOAD"),
1254                ("x-amz-date", "  20260513T120000Z  "),
1255            ],
1256        );
1257        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1258            .iter()
1259            .map(|s| (*s).into())
1260            .collect();
1261        let bytes =
1262            build_canonical_request_bytes(&r, &signed).expect("canonical request bytes must build");
1263        let s = std::str::from_utf8(&bytes).expect("utf-8");
1264        let expected = "PUT\n\
1265                        /bucket/key\n\
1266                        a=2&z=1\n\
1267                        host:s3.example.com\n\
1268                        x-amz-content-sha256:UNSIGNED-PAYLOAD\n\
1269                        x-amz-date:20260513T120000Z\n\
1270                        \n\
1271                        host;x-amz-content-sha256;x-amz-date\n\
1272                        UNSIGNED-PAYLOAD";
1273        assert_eq!(s, expected, "canonical request bytes mismatch:\n{s}");
1274    }
1275
1276    /// v0.8.5 #84 H-4: duplicate `x-amz-date` headers must be rejected
1277    /// at canonical-request build time (not silently coalesced to the
1278    /// first value). HTTP/1.1 spec already forbids duplicates of
1279    /// `host` / `x-amz-date`; AWS SDKs never emit them; so any
1280    /// duplicate must be malicious or broken — single-value reject is
1281    /// the safe choice (see [`build_canonical_request_bytes`] doc).
1282    #[test]
1283    fn sigv4a_duplicate_x_amz_date_rejected() {
1284        // Two x-amz-date headers — first one matches the signature the
1285        // gate expects, second one is what a downstream parser might
1286        // pick up. This is the textbook auth-confusion vector.
1287        let r = Request::builder()
1288            .method(Method::GET)
1289            .uri("/b/k")
1290            .header("host", "s3.example.com")
1291            .header("x-amz-content-sha256", "UNSIGNED-PAYLOAD")
1292            .header("x-amz-date", "20260513T120000Z")
1293            .header("x-amz-date", "20260513T130000Z")
1294            .body(())
1295            .expect("dup-header request");
1296        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1297            .iter()
1298            .map(|s| (*s).into())
1299            .collect();
1300        let err = build_canonical_request_bytes(&r, &signed)
1301            .expect_err("duplicate x-amz-date must reject");
1302        match err {
1303            crate::sigv4a::SigV4aError::DuplicateSignedHeader { header } => {
1304                assert_eq!(header, "x-amz-date");
1305            }
1306            other => panic!("expected DuplicateSignedHeader, got {other:?}"),
1307        }
1308    }
1309
1310    /// v0.8.5 #84 H-4: counterpart to the duplicate-reject test —
1311    /// single-occurrence headers on the same path stay accepted.
1312    /// Guards against a regression where the duplicate-detect logic
1313    /// is over-eager and trips on a normally-formed request.
1314    #[test]
1315    fn sigv4a_canonicalization_single_header_passes() {
1316        let r = req(
1317            Method::GET,
1318            "/b/k",
1319            &[
1320                ("host", "s3.example.com"),
1321                ("x-amz-content-sha256", "UNSIGNED-PAYLOAD"),
1322                ("x-amz-date", "20260513T120000Z"),
1323            ],
1324        );
1325        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1326            .iter()
1327            .map(|s| (*s).into())
1328            .collect();
1329        let bytes =
1330            build_canonical_request_bytes(&r, &signed).expect("single-occurrence must accept");
1331        // Body content not asserted in detail (covered by
1332        // canonical_request_bytes_format); just confirm the bytes
1333        // parse as utf-8 and contain the date verbatim.
1334        let s = std::str::from_utf8(&bytes).expect("utf-8");
1335        assert!(
1336            s.contains("x-amz-date:20260513T120000Z"),
1337            "canonical bytes must echo the single x-amz-date verbatim:\n{s}"
1338        );
1339    }
1340
1341    /// v0.8.5 #84 H-4: end-to-end through the
1342    /// [`try_sigv4a_verify_at`] gate — duplicate `x-amz-date` on a
1343    /// SigV4a-shaped request must surface 403 SignatureDoesNotMatch
1344    /// (not silently authenticate against the first value).
1345    #[tokio::test]
1346    async fn sigv4a_pre_route_rejects_duplicate_signed_header() {
1347        let signing = SigningKey::random(&mut OsRng);
1348        let vk = p256::ecdsa::VerifyingKey::from(&signing);
1349        let gate = make_gate_with("AKIAOK", vk);
1350        // Authorization header lists x-amz-date in SignedHeaders —
1351        // signature value itself can be garbage; the duplicate-detect
1352        // path runs strictly before any ECDSA math.
1353        let auth = build_auth_header(
1354            "AKIAOK",
1355            &[
1356                "host",
1357                "x-amz-content-sha256",
1358                "x-amz-date",
1359                REGION_SET_HEADER,
1360            ],
1361            "deadbeef",
1362        );
1363        let r = Request::builder()
1364            .method(Method::GET)
1365            .uri("/bucket/key")
1366            .header("host", "s3.example.com")
1367            .header(
1368                "x-amz-content-sha256",
1369                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1370            )
1371            .header("x-amz-date", "20260513T120000Z")
1372            .header("x-amz-date", "20260513T130000Z")
1373            .header(REGION_SET_HEADER, "us-east-1")
1374            .header("authorization", auth)
1375            .body(())
1376            .expect("dup-header sigv4a request");
1377        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1378            .expect("must intercept SigV4a request");
1379        let resp = result.expect_err("duplicate signed header must reject at the gate");
1380        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1381        let body = body_to_bytes(resp).await;
1382        let body_str = String::from_utf8(body).expect("xml utf-8");
1383        assert!(
1384            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1385            "duplicate signed header must surface SignatureDoesNotMatch: {body_str}"
1386        );
1387        assert!(
1388            body_str.contains("duplicate signed header"),
1389            "diagnostic must mention duplicate header: {body_str}"
1390        );
1391    }
1392}