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    // v0.8.17 G-1: presigned-URL detection runs BEFORE the
297    // `gate.is_none()` short-circuit. The v0.8.16 F-5 fix only
298    // emitted the 501 when a SigV4a verifier was already wired
299    // (`--sigv4a-credentials <DIR>`); operators who hadn't
300    // configured one had `?X-Amz-Algorithm=AWS4-ECDSA-P256-SHA256`
301    // requests silently fall through to the SigV4 path, which
302    // doesn't understand SigV4a query auth either — request
303    // effectively accepted as unsigned. We now surface the 501
304    // unconditionally for SigV4a-shaped query auth, so the
305    // "deterministic failure" the F-5 comment promised holds for
306    // every deployment.
307    if crate::sigv4a::detect_presigned(req) {
308        return Some(Err(build_sigv4a_error_response(
309            StatusCode::NOT_IMPLEMENTED,
310            "NotImplemented",
311            "SigV4a presigned URLs (query auth) are not yet supported on this gateway; \
312             use Authorization-header SigV4a instead",
313        )));
314    }
315    let gate = gate?;
316    if !crate::sigv4a::detect(req) {
317        // Not a SigV4a request — caller forwards to the SigV4 path.
318        return None;
319    }
320    // Pre-parse the Authorization header so we know which signed-headers
321    // list to canonicalise in. If the header is malformed, fail fast
322    // with 403 rather than building canonical bytes that can never
323    // verify.
324    //
325    // v0.8.4 #76: `parse_authorization_header` now returns `Result`
326    // (was `Option`) so the gate can surface scope-shape failures
327    // (`InvalidCredentialScope`, `WrongService`, etc.) as 400
328    // InvalidRequest. Any non-Ok parse falls through to the
329    // SignatureDoesNotMatch 403 the original code returned, since at
330    // this point we can't extract a `signed_headers` list to feed the
331    // canonical-request builder.
332    let auth_hdr = req
333        .headers()
334        .get(http::header::AUTHORIZATION)
335        .and_then(|v| v.to_str().ok());
336    let signed_headers: Vec<String> =
337        match auth_hdr.and_then(|hdr| crate::sigv4a::parse_authorization_header(hdr).ok()) {
338            Some(parsed) => parsed.signed_headers,
339            None => {
340                // No / unparseable Authorization header but `detect` flagged
341                // it as SigV4a-shaped (e.g. only the region-set header is
342                // present) — surface as SignatureDoesNotMatch directly.
343                return Some(Err(build_sigv4a_error_response(
344                    StatusCode::FORBIDDEN,
345                    "SignatureDoesNotMatch",
346                    "missing or malformed Authorization header for SigV4a request",
347                )));
348            }
349        };
350    let canonical = match build_canonical_request_bytes(req, &signed_headers) {
351        Ok(bytes) => bytes,
352        Err(err) => {
353            // v0.8.5 #84 H-4: duplicate signed header (only failure
354            // mode the canonical builder has today). Surface as
355            // `SignatureDoesNotMatch` 403 — the AWS SDKs treat that
356            // as the catch-all auth-failure code, and the diagnostic
357            // is in the response body / server log.
358            tracing::warn!(error = %err, "SigV4a canonical-request build rejected request");
359            return Some(Err(build_sigv4a_error_response(
360                StatusCode::FORBIDDEN,
361                "SignatureDoesNotMatch",
362                &err.to_string(),
363            )));
364        }
365    };
366    match gate.pre_route_at(req, requested_region, &canonical, now) {
367        Ok(()) => Some(Ok(())),
368        Err(err) => {
369            tracing::warn!(error = %err, "SigV4a verify rejected request");
370            Some(Err(build_sigv4a_error_response(
371                err.http_status(),
372                err.s3_error_code(),
373                &err.to_string(),
374            )))
375        }
376    }
377}
378
379/// v0.7 #47: build a SigV4-shaped canonical request from the HTTP
380/// surface alone (no body access). Returns the bytes that the
381/// SigV4a gate will check the ECDSA signature against.
382///
383/// Format (one element per line, joined with `\n`):
384/// 1. HTTP method (uppercase)
385/// 2. canonical URI (path; we leave it untouched since AWS SDKs
386///    pre-encode it the same way s3s receives it)
387/// 3. canonical query string (sorted by name, name=value pairs joined
388///    by `&`; empty when no query string)
389/// 4. canonical headers (one `name:trimmed-value\n` per signed header,
390///    in the **order** they appear in `SignedHeaders=`)
391/// 5. signed headers list (lowercase names joined by `;`)
392/// 6. payload hash (value of `x-amz-content-sha256`, or `UNSIGNED-PAYLOAD`
393///    if absent)
394///
395/// v0.8.5 #84 (audit H-4): every signed header is checked for being
396/// sent **exactly once** on the request. If a header in
397/// `SignedHeaders=` appears more than once we'd have to choose between
398/// the first value (`HeaderMap::get` semantics) and the comma-joined
399/// AWS-canonical form — and any S3 SDK / WAF / sidecar in front of us
400/// would make a different choice, opening "auth confusion" attacks
401/// (sign over the benign first `x-amz-date`, smuggle a second one for
402/// the inner parser). HTTP/1.1 spec already forbids duplicates of
403/// `host` / `x-amz-date` and the AWS SDKs never emit them, so any
404/// duplicate is a malicious or broken request — reject upfront with
405/// [`SigV4aError::DuplicateSignedHeader`].
406fn build_canonical_request_bytes<B>(
407    req: &Request<B>,
408    signed_headers: &[String],
409) -> Result<Vec<u8>, crate::sigv4a::SigV4aError> {
410    let mut buf = String::with_capacity(512);
411    buf.push_str(req.method().as_str());
412    buf.push('\n');
413    // v0.8.15 H-d: canonical URI per RFC 3986 unreserved set. Real
414    // AWS SDKs decode + re-encode (uppercase hex, only unreserved
415    // chars left literal) before hashing, so receiving the same
416    // request through a normalising TLS terminator that lowercases
417    // `%2f` to `%2F` (or vice versa) would otherwise produce a
418    // different canonical form than what the SDK signed. `/`
419    // path-segment separators stay literal — S3 doesn't escape them
420    // in the canonical path.
421    buf.push_str(&canonical_uri_path(req.uri().path()));
422    buf.push('\n');
423    buf.push_str(&canonical_query_string(req.uri().query().unwrap_or("")));
424    buf.push('\n');
425    for name in signed_headers {
426        // v0.8.5 #84 H-4: count occurrences via `get_all` rather than
427        // `get`, which only ever returns the first value. Two
428        // `x-amz-date` headers with `get` would canonicalise to the
429        // first value while a downstream HTTP/1.1 parser might pick
430        // the second — auth confusion. Single-value reject is the
431        // safe choice; comma-join would be the AWS-canonical form
432        // for legitimately multi-valued signed headers, but the AWS
433        // SDKs never sign over comma-joined values for any header
434        // S3 cares about, so refusing duplicates outright matches
435        // every real-world client.
436        let occurrences = req.headers().get_all(name.as_str()).iter().count();
437        if occurrences > 1 {
438            return Err(crate::sigv4a::SigV4aError::DuplicateSignedHeader {
439                header: name.clone(),
440            });
441        }
442        // v0.8.16 F-4: presence is required. A signed header that's
443        // missing from the request used to canonicalise as `name:\n`
444        // (empty value) — a client could sign over a placeholder
445        // value, then drop the actual header on the wire. The gate
446        // would happily verify because both sides agreed on the
447        // empty-string canonical form. AWS S3 returns
448        // SignatureDoesNotMatch; we surface a typed variant so the
449        // gate can map to 403 with a clear message.
450        let value = match req
451            .headers()
452            .get(name.as_str())
453            .and_then(|v| v.to_str().ok())
454        {
455            Some(v) => v,
456            None => {
457                return Err(crate::sigv4a::SigV4aError::SignedHeaderMissing {
458                    header: name.clone(),
459                });
460            }
461        };
462        buf.push_str(name);
463        buf.push(':');
464        // Trim whitespace and collapse repeated inner whitespace per
465        // SigV4 canonicalisation rules. This is the same trimming AWS
466        // SDKs do when they sign.
467        buf.push_str(&trim_collapse_ws(value));
468        buf.push('\n');
469    }
470    buf.push('\n');
471    buf.push_str(&signed_headers.join(";"));
472    buf.push('\n');
473    let payload_hash = req
474        .headers()
475        .get("x-amz-content-sha256")
476        .and_then(|v| v.to_str().ok())
477        .unwrap_or("UNSIGNED-PAYLOAD");
478    buf.push_str(payload_hash);
479    Ok(buf.into_bytes())
480}
481
482/// SigV4 canonical query string: split on `&`, parse each `k=v` (or
483/// `k`), sort lexicographically by name (then by value), re-join with
484/// `&`. Empty input → empty string. We do **not** re-encode the values
485/// — they already arrived URL-encoded over the wire, and AWS SDKs
486/// expect the server to compare the bytes verbatim.
487fn canonical_query_string(query: &str) -> String {
488    if query.is_empty() {
489        return String::new();
490    }
491    // v0.8.15 H-d: AWS SigV4 / SigV4a spec — decode each key/value to
492    // raw bytes, then re-encode with the AWS canonical form (RFC
493    // 3986 unreserved set, uppercase hex), then sort by the encoded
494    // key (and value as tiebreaker). The pre-H-d code took the raw
495    // wire bytes and sorted those, which produced a different
496    // canonical string than the SDK's output for any of these
497    // mismatches:
498    //
499    // 1. Lowercase `%2f` in the wire vs. SDK-canonical uppercase
500    //    `%2F` (some TLS terminators normalise).
501    // 2. Mixed encoding choices (one side encodes `=` as `%3D`, the
502    //    other leaves it bare).
503    // 3. Sort order on raw bytes vs. encoded bytes differs when one
504    //    side encodes a char the other left literal.
505    //
506    // Real AWS SDKs always emit fully-encoded canonical form, so the
507    // pre-H-d "verbatim sort" only matched signatures the gate itself
508    // produced, not signatures real clients ship.
509    // v0.8.16 F-6: byte-level decode + re-encode. The pre-F-6
510    // helpers ran `decode_utf8_lossy()` which silently replaced
511    // any non-UTF8 percent-encoded byte (e.g. `%FF`) with the
512    // U+FFFD replacement character (`%EF%BF%BD` after re-encode),
513    // mismatching every signer that operates on raw bytes (most
514    // AWS SDKs do). Now we work with `Vec<u8>` end-to-end so the
515    // canonical form is bit-for-bit identical to what AWS SDKs
516    // emit, including for non-UTF8 path / query content.
517    let mut pairs: Vec<(String, String)> = query
518        .split('&')
519        .filter(|s| !s.is_empty())
520        .map(|kv| match kv.split_once('=') {
521            Some((k, v)) => (percent_decode_bytes(k), percent_decode_bytes(v)),
522            None => (percent_decode_bytes(kv), Vec::new()),
523        })
524        .map(|(k, v)| {
525            (
526                aws_canonical_encode_bytes(&k),
527                aws_canonical_encode_bytes(&v),
528            )
529        })
530        .collect();
531    pairs.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
532    let mut out = String::with_capacity(query.len());
533    for (i, (k, v)) in pairs.iter().enumerate() {
534        if i > 0 {
535            out.push('&');
536        }
537        out.push_str(k);
538        out.push('=');
539        out.push_str(v);
540    }
541    out
542}
543
544/// v0.8.15 H-d: AWS canonical URI path encoding. Pulls each segment
545/// out of the slash-separated path, decodes any percent-encoded
546/// bytes, then re-encodes with the canonical form. Slashes are
547/// preserved literal (S3 doesn't escape segment separators in the
548/// canonical path).
549fn canonical_uri_path(path: &str) -> String {
550    if path.is_empty() {
551        return "/".to_owned();
552    }
553    // v0.8.16 F-6: byte-level. See `canonical_query_string` for
554    // the rationale — `decode_utf8_lossy` mangled non-UTF8 paths
555    // into U+FFFD before re-encoding, mismatching the signer.
556    let mut out = String::with_capacity(path.len());
557    let mut first = true;
558    for segment in path.split('/') {
559        if !first {
560            out.push('/');
561        }
562        first = false;
563        let decoded = percent_decode_bytes(segment);
564        out.push_str(&aws_canonical_encode_bytes(&decoded));
565    }
566    out
567}
568
569/// v0.8.16 F-6: decode a percent-encoded string to its raw bytes
570/// (`Vec<u8>`). Preserves non-UTF8 sequences verbatim so the
571/// downstream re-encode produces the same bytes a byte-level signer
572/// (e.g. `aws-crt-cpp`, `aws-sigv4` Rust crate) would compute.
573fn percent_decode_bytes(s: &str) -> Vec<u8> {
574    percent_encoding::percent_decode_str(s).collect()
575}
576
577/// v0.8.16 F-6: encode a raw byte sequence per AWS SigV4 canonical
578/// form. AWS canonical set = RFC 3986 unreserved (`A-Z a-z 0-9 - _
579/// . ~`); every other byte becomes `%XX` with uppercase hex.
580/// Operates on `&[u8]` so it never panics on non-UTF8 input.
581fn aws_canonical_encode_bytes(bytes: &[u8]) -> String {
582    let mut out = String::with_capacity(bytes.len());
583    for &b in bytes {
584        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
585            out.push(b as char);
586        } else {
587            use std::fmt::Write as _;
588            let _ = write!(out, "%{b:02X}");
589        }
590    }
591    out
592}
593
594#[allow(dead_code)]
595/// v0.8.15 H-d (kept for any UTF-8-only call site): encode a UTF-8
596/// string per AWS SigV4 canonical form. Prefer
597/// [`aws_canonical_encode_bytes`] which doesn't lossy-decode.
598fn aws_canonical_encode(s: &str) -> String {
599    /// AWS canonical set per SigV4 spec — equivalent to RFC 3986
600    /// unreserved. Everything else gets `%XX`.
601    const AWS_CANONICAL_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
602        .remove(b'-')
603        .remove(b'_')
604        .remove(b'.')
605        .remove(b'~');
606    percent_encoding::utf8_percent_encode(s, AWS_CANONICAL_SET).to_string()
607}
608
609/// SigV4 header-value canonicalisation: trim leading + trailing
610/// whitespace and collapse runs of internal whitespace to a single
611/// space. This mirrors what AWS SDKs do client-side when computing the
612/// canonical request — without it, a header value with extra spaces
613/// would canonicalise differently on each side.
614fn trim_collapse_ws(s: &str) -> String {
615    let trimmed = s.trim();
616    let mut out = String::with_capacity(trimmed.len());
617    let mut prev_ws = false;
618    for c in trimmed.chars() {
619        if c.is_whitespace() {
620            if !prev_ws {
621                out.push(' ');
622            }
623            prev_ws = true;
624        } else {
625            out.push(c);
626            prev_ws = false;
627        }
628    }
629    out
630}
631
632/// v0.7 #47: build an AWS-shaped XML response for a SigV4a verify
633/// failure. The response body matches the wire format AWS S3 emits for
634/// the same conditions so SDKs surface the right exception class to the
635/// caller.
636///
637/// v0.8.4 #76: now takes `status` so the gate can return 400
638/// InvalidRequest for malformed-input failures (missing x-amz-date,
639/// wrong service scope, etc.) and 403 for actual auth failures.
640fn build_sigv4a_error_response(
641    status: StatusCode,
642    code: &str,
643    message: &str,
644) -> Response<s3s::Body> {
645    let body_str = format!(
646        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
647         <Error>\n  <Code>{code}</Code>\n  <Message>{message}</Message>\n</Error>"
648    );
649    let bytes = Bytes::from(body_str.into_bytes());
650    Response::builder()
651        .status(status)
652        .header("content-type", "application/xml")
653        .header("content-length", bytes.len().to_string())
654        .body(s3s::Body::http_body(
655            Full::new(bytes).map_err(|never| match never {}),
656        ))
657        .expect("sigv4a error response builder")
658}
659
660/// `/health` と `/ready` のレスポンス Body。
661/// inner S3Service の Body と互換する形にするために `s3s::Body` でラップ可能な
662/// `Full<Bytes>` を `s3s::Body::http_body` 経由で構築する。
663type RespBody = s3s::Body;
664
665fn make_text_response(status: StatusCode, body: &'static str) -> Response<RespBody> {
666    let bytes = Bytes::from_static(body.as_bytes());
667    Response::builder()
668        .status(status)
669        .header("content-type", "text/plain; charset=utf-8")
670        .header("content-length", bytes.len().to_string())
671        .body(s3s::Body::http_body(
672            Full::new(bytes).map_err(|never| match never {}),
673        ))
674        .expect("static response")
675}
676
677fn make_owned_text_response(
678    status: StatusCode,
679    content_type: &'static str,
680    body: String,
681) -> Response<RespBody> {
682    let bytes = Bytes::from(body.into_bytes());
683    Response::builder()
684        .status(status)
685        .header("content-type", content_type)
686        .header("content-length", bytes.len().to_string())
687        .body(s3s::Body::http_body(
688            Full::new(bytes).map_err(|never| match never {}),
689        ))
690        .expect("owned response")
691}
692
693impl<S> Service<Request<Incoming>> for HealthRouter<S>
694where
695    S: Service<Request<Incoming>, Response = Response<s3s::Body>, Error = s3s::HttpError>
696        + Clone
697        + Send
698        + 'static,
699    S::Future: Send + 'static,
700{
701    type Response = Response<RespBody>;
702    type Error = s3s::HttpError;
703    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
704
705    fn call(&self, req: Request<Incoming>) -> Self::Future {
706        // v0.7 #44: short-circuit CORS OPTIONS preflight at the HTTP layer
707        // before health/metrics dispatch. Preflight must run only for
708        // OPTIONS requests, and only when a CORS manager is attached and
709        // a config exists for the requested bucket; otherwise fall
710        // through to the existing routing logic.
711        if let Some(resp) = try_handle_preflight(&req, self.cors_manager.as_ref()) {
712            return Box::pin(async move { Ok(resp) });
713        }
714        // v0.7 #47: SigV4a verify gate. When the request is signed with
715        // `AWS4-ECDSA-P256-SHA256` and a credential store is configured,
716        // verify here at the HTTP layer (s3s' SigV4 verifier would
717        // otherwise reject the request as "unknown algorithm" before
718        // any handler ran). Plain SigV4 (HMAC) requests return `None`
719        // and fall through to the inner service untouched.
720        if let Some(result) = try_sigv4a_verify(&req, self.sigv4a_gate.as_ref(), &self.region) {
721            match result {
722                Ok(()) => {
723                    // verified — fall through to the path-routing logic
724                    // below (the health/metrics/inner-service dispatch).
725                }
726                Err(resp) => return Box::pin(async move { Ok(resp) }),
727            }
728        }
729        let path = req.uri().path();
730        match (req.method(), path) {
731            (&hyper::Method::GET, "/health") | (&hyper::Method::HEAD, "/health") => {
732                Box::pin(async { Ok(make_text_response(StatusCode::OK, "ok\n")) })
733            }
734            (&hyper::Method::GET, "/metrics") | (&hyper::Method::HEAD, "/metrics") => {
735                let handle = self.metrics_handle.clone();
736                Box::pin(async move {
737                    match handle {
738                        Some(h) => {
739                            let body = h.render();
740                            Ok(make_owned_text_response(
741                                StatusCode::OK,
742                                "text/plain; version=0.0.4; charset=utf-8",
743                                body,
744                            ))
745                        }
746                        None => Ok(make_text_response(
747                            StatusCode::SERVICE_UNAVAILABLE,
748                            "metrics not configured\n",
749                        )),
750                    }
751                })
752            }
753            (&hyper::Method::GET, "/ready") | (&hyper::Method::HEAD, "/ready") => {
754                let check = self.ready_check.clone();
755                Box::pin(async move {
756                    match check {
757                        Some(f) => match f().await {
758                            Ok(()) => Ok(make_text_response(StatusCode::OK, "ready\n")),
759                            Err(reason) => {
760                                tracing::warn!(%reason, "readiness check failed");
761                                Ok(make_text_response(
762                                    StatusCode::SERVICE_UNAVAILABLE,
763                                    "not ready\n",
764                                ))
765                            }
766                        },
767                        None => Ok(make_text_response(StatusCode::OK, "ready (no check)\n")),
768                    }
769                })
770            }
771            _ => {
772                let inner = self.inner.clone();
773                Box::pin(async move { inner.call(req).await })
774            }
775        }
776    }
777}
778
779/// `Infallible` を anything に変換するためのトリック (`Full::map_err` 用)
780trait FullExt<B> {
781    fn map_err<E, F: FnMut(Infallible) -> E>(
782        self,
783        f: F,
784    ) -> http_body_util::combinators::MapErr<Self, F>
785    where
786        Self: Sized;
787}
788impl<B> FullExt<B> for Full<B>
789where
790    B: bytes::Buf,
791{
792    fn map_err<E, F: FnMut(Infallible) -> E>(
793        self,
794        f: F,
795    ) -> http_body_util::combinators::MapErr<Self, F>
796    where
797        Self: Sized,
798    {
799        http_body_util::BodyExt::map_err(self, f)
800    }
801}
802
803#[cfg(test)]
804mod preflight_tests {
805    //! v0.7 #44: unit tests for the OPTIONS preflight interceptor.
806    //!
807    //! These exercise [`try_handle_preflight`] directly — no hyper
808    //! `Incoming` body is needed because the function is generic over
809    //! the body type. Behavioural matrix:
810    //!
811    //! 1. matching preflight → 200 + Allow-* headers
812    //! 2. no matching rule (config exists, but origin/method/headers fail)
813    //!    → 403
814    //! 3. missing `Origin` header → `None` (not a CORS preflight)
815    //! 4. non-OPTIONS verb → `None`
816    //! 5. no CORS config registered for the bucket → `None`
817    //! 6. no manager attached → `None`
818
819    use super::*;
820    use crate::cors::{CorsConfig, CorsManager, CorsRule};
821
822    fn rule(origins: &[&str], methods: &[&str], headers: &[&str]) -> CorsRule {
823        CorsRule {
824            allowed_origins: origins.iter().map(|s| (*s).to_owned()).collect(),
825            allowed_methods: methods.iter().map(|s| (*s).to_owned()).collect(),
826            allowed_headers: headers.iter().map(|s| (*s).to_owned()).collect(),
827            expose_headers: vec!["ETag".into()],
828            max_age_seconds: Some(600),
829            id: Some("test".into()),
830        }
831    }
832
833    /// Helper: build a `Request<()>` with the given method, path, and
834    /// headers — body is ignored by the matcher.
835    fn req(method: Method, path: &str, headers: &[(&str, &str)]) -> Request<()> {
836        let mut b = Request::builder().method(method).uri(path);
837        for (k, v) in headers {
838            b = b.header(*k, *v);
839        }
840        b.body(()).expect("request builder")
841    }
842
843    fn manager_with_rule() -> Arc<CorsManager> {
844        let mgr = CorsManager::new();
845        mgr.put(
846            "b",
847            CorsConfig {
848                rules: vec![rule(
849                    &["https://app.example.com"],
850                    &["GET", "PUT", "DELETE"],
851                    &["Content-Type", "X-Amz-Date"],
852                )],
853            },
854        );
855        Arc::new(mgr)
856    }
857
858    #[test]
859    fn preflight_match_returns_allow_response() {
860        let mgr = manager_with_rule();
861        let r = req(
862            Method::OPTIONS,
863            "/b/key.txt",
864            &[
865                ("origin", "https://app.example.com"),
866                ("access-control-request-method", "PUT"),
867                ("access-control-request-headers", "content-type, x-amz-date"),
868            ],
869        );
870        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
871        assert_eq!(resp.status(), StatusCode::OK);
872        let h = resp.headers();
873        assert_eq!(
874            h.get("access-control-allow-origin")
875                .and_then(|v| v.to_str().ok()),
876            Some("https://app.example.com")
877        );
878        assert_eq!(
879            h.get("access-control-allow-methods")
880                .and_then(|v| v.to_str().ok()),
881            Some("GET, PUT, DELETE")
882        );
883        assert_eq!(
884            h.get("access-control-allow-headers")
885                .and_then(|v| v.to_str().ok()),
886            Some("Content-Type, X-Amz-Date")
887        );
888        assert_eq!(
889            h.get("access-control-max-age")
890                .and_then(|v| v.to_str().ok()),
891            Some("600")
892        );
893        assert_eq!(
894            h.get("access-control-expose-headers")
895                .and_then(|v| v.to_str().ok()),
896            Some("ETag")
897        );
898    }
899
900    #[test]
901    fn preflight_no_match_returns_403() {
902        let mgr = manager_with_rule();
903        // Origin not in allow-list → no rule matches but bucket has CORS
904        // config, so we must answer 403 directly (not fall through to
905        // s3s, which would otherwise leak the bucket existence via 405).
906        let r = req(
907            Method::OPTIONS,
908            "/b/key.txt",
909            &[
910                ("origin", "https://evil.example.com"),
911                ("access-control-request-method", "PUT"),
912            ],
913        );
914        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
915        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
916        // 403 deny response must NOT carry Allow-Origin (RFC 7234 + S3 wire compat).
917        assert!(resp.headers().get("access-control-allow-origin").is_none());
918    }
919
920    #[test]
921    fn preflight_no_origin_falls_through() {
922        // OPTIONS without Origin is a generic OPTIONS (e.g. `OPTIONS *`)
923        // — not a CORS preflight, must not be intercepted.
924        let mgr = manager_with_rule();
925        let r = req(
926            Method::OPTIONS,
927            "/b/key.txt",
928            &[("access-control-request-method", "PUT")],
929        );
930        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
931    }
932
933    #[test]
934    fn non_options_falls_through() {
935        let mgr = manager_with_rule();
936        // Even with Origin + ACRM headers, GET is not a preflight.
937        let r = req(
938            Method::GET,
939            "/b/key.txt",
940            &[
941                ("origin", "https://app.example.com"),
942                ("access-control-request-method", "PUT"),
943            ],
944        );
945        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
946    }
947
948    #[test]
949    fn no_cors_config_for_bucket_falls_through() {
950        // Manager attached but no rule registered for "ghost" → fall
951        // through to inner service so backend can respond naturally.
952        let mgr = manager_with_rule();
953        let r = req(
954            Method::OPTIONS,
955            "/ghost/key.txt",
956            &[
957                ("origin", "https://app.example.com"),
958                ("access-control-request-method", "PUT"),
959            ],
960        );
961        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
962    }
963
964    #[test]
965    fn no_manager_attached_falls_through() {
966        let r = req(
967            Method::OPTIONS,
968            "/b/key.txt",
969            &[
970                ("origin", "https://app.example.com"),
971                ("access-control-request-method", "PUT"),
972            ],
973        );
974        assert!(try_handle_preflight(&r, None).is_none());
975    }
976
977    #[test]
978    fn preflight_wildcard_origin_echoes_star() {
979        // Rule with `*` origin → response echoes literal "*" (S3 spec).
980        let mgr = CorsManager::new();
981        mgr.put(
982            "b",
983            CorsConfig {
984                rules: vec![rule(&["*"], &["GET", "PUT"], &["*"])],
985            },
986        );
987        let mgr = Arc::new(mgr);
988        let r = req(
989            Method::OPTIONS,
990            "/b/key",
991            &[
992                ("origin", "https://anywhere.example"),
993                ("access-control-request-method", "PUT"),
994                ("access-control-request-headers", "x-custom-header"),
995            ],
996        );
997        let resp = try_handle_preflight(&r, Some(&mgr)).expect("must intercept");
998        assert_eq!(resp.status(), StatusCode::OK);
999        assert_eq!(
1000            resp.headers()
1001                .get("access-control-allow-origin")
1002                .and_then(|v| v.to_str().ok()),
1003            Some("*"),
1004            "wildcard rule must echo literal '*' instead of requesting origin"
1005        );
1006    }
1007
1008    #[test]
1009    fn preflight_empty_path_falls_through() {
1010        let mgr = manager_with_rule();
1011        let r = req(
1012            Method::OPTIONS,
1013            "/",
1014            &[
1015                ("origin", "https://app.example.com"),
1016                ("access-control-request-method", "PUT"),
1017            ],
1018        );
1019        assert!(try_handle_preflight(&r, Some(&mgr)).is_none());
1020    }
1021}
1022
1023/// v0.8.18 P3: AWS SigV4 canonical-request test vectors. These
1024/// exercise [`canonical_uri_path`] and [`canonical_query_string`]
1025/// (the v0.8.16 #150 byte-level helpers).
1026///
1027/// Provenance — mixed sources:
1028///
1029/// - **AWS-published vectors** (`get-vanilla`,
1030///   `get-vanilla-query-order-key-case`,
1031///   `get-vanilla-query-order-value`, `get-utf8`): vector names
1032///   match the public
1033///   <https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html>
1034///   reference suite; expected outputs taken from the AWS
1035///   canonical-request recipe.
1036/// - **S3 spec-derived edge vectors** (`non_utf8_path_byte_roundtrip`,
1037///   `query_reserved_chars_uppercase_hex`,
1038///   `path_mixed_case_percent_encoding_normalised`,
1039///   `query_bare_key_canonicalises_with_empty_value`,
1040///   `unreserved_set_kept_literal`,
1041///   `s3_listobjects_v2_canonical_query`, `s3_path_with_spaces`):
1042///   derived from RFC 3986 + the SigV4 spec to pin the v0.8.16 #150
1043///   byte-level fix against the cases that motivated it.
1044///
1045/// Both classes follow the same compare-byte-for-byte contract.
1046#[cfg(test)]
1047mod aws_sigv4_canonical_vectors {
1048    use super::{canonical_query_string, canonical_uri_path};
1049
1050    /// AWS `get-vanilla` — path `/`, no query. Canonical URI is `/`,
1051    /// canonical query is empty.
1052    #[test]
1053    fn get_vanilla() {
1054        assert_eq!(canonical_uri_path("/"), "/");
1055        assert_eq!(canonical_query_string(""), "");
1056    }
1057
1058    /// AWS `get-vanilla-query-order-key-case` — query keys must
1059    /// sort by encoded form. `Param2=value2&Param1=value1` →
1060    /// `Param1=value1&Param2=value2`. Case-sensitive comparison
1061    /// per the spec.
1062    #[test]
1063    fn get_vanilla_query_order_key_case() {
1064        let canon = canonical_query_string("Param2=value2&Param1=value1");
1065        assert_eq!(canon, "Param1=value1&Param2=value2");
1066    }
1067
1068    /// AWS `get-vanilla-query-order-value` — same key with
1069    /// different values must sort by encoded value as tiebreaker.
1070    #[test]
1071    fn get_vanilla_query_order_value() {
1072        let canon = canonical_query_string("Param1=value2&Param1=Value1");
1073        // `V` (0x56) sorts before `v` (0x76) in byte order; the
1074        // canonical form preserves the encoded value sort.
1075        assert_eq!(canon, "Param1=Value1&Param1=value2");
1076    }
1077
1078    /// AWS `get-utf8` — UTF-8 characters in the path must be
1079    /// percent-encoded with uppercase hex.
1080    #[test]
1081    fn get_utf8_path() {
1082        // Japanese for "Hello": "こんにちは" (5 chars, 15 bytes
1083        // UTF-8). Encoded per AWS canonical:
1084        // %E3%81%93 %E3%82%93 %E3%81%AB %E3%81%A1 %E3%81%AF
1085        let canon = canonical_uri_path("/こんにちは");
1086        assert_eq!(canon, "/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF");
1087    }
1088
1089    /// v0.8.16 #150 motivation — non-UTF-8 percent-encoded bytes
1090    /// must round-trip byte-identically. Pre-#150 `decode_utf8_lossy`
1091    /// turned `%FF` into U+FFFD (`%EF%BF%BD`).
1092    #[test]
1093    fn non_utf8_path_byte_roundtrip() {
1094        // Raw `%FF` in a path. AWS-canonical re-encoding must
1095        // produce `%FF` again, not `%EF%BF%BD`.
1096        let canon = canonical_uri_path("/foo/%FF");
1097        assert_eq!(canon, "/foo/%FF");
1098    }
1099
1100    /// AWS canonical query: keys / values that contain reserved
1101    /// chars get re-encoded with uppercase hex.
1102    #[test]
1103    fn query_reserved_chars_uppercase_hex() {
1104        // `key with space=value/with%2Fslash` after canonical
1105        // encoding:
1106        let canon = canonical_query_string("key%20with%20space=value%2Fwith%2Fslash");
1107        assert_eq!(canon, "key%20with%20space=value%2Fwith%2Fslash");
1108    }
1109
1110    /// AWS canonical path: existing percent-encoded uppercase
1111    /// stays uppercase; lowercase `%2f` from a normalising
1112    /// terminator gets re-canonicalised to `%2F`.
1113    #[test]
1114    fn path_mixed_case_percent_encoding_normalised() {
1115        assert_eq!(canonical_uri_path("/a/%2Fb"), "/a/%2Fb");
1116        assert_eq!(canonical_uri_path("/a/%2fb"), "/a/%2Fb");
1117    }
1118
1119    /// AWS canonical: query key without value (e.g. `?delete`).
1120    /// Canonical form is `delete=`.
1121    #[test]
1122    fn query_bare_key_canonicalises_with_empty_value() {
1123        assert_eq!(canonical_query_string("delete"), "delete=");
1124    }
1125
1126    /// Reserved set per RFC 3986 unreserved: A-Z a-z 0-9 - _ . ~
1127    /// Everything else must be %-encoded.
1128    #[test]
1129    fn unreserved_set_kept_literal() {
1130        assert_eq!(
1131            canonical_uri_path("/-_.~ABCDEabcde012-_.~"),
1132            "/-_.~ABCDEabcde012-_.~"
1133        );
1134    }
1135
1136    /// Common AWS S3 query: `?list-type=2&prefix=foo%2F` — verify
1137    /// the canonical form matches what aws-sdk-rust signs.
1138    #[test]
1139    fn s3_listobjects_v2_canonical_query() {
1140        let canon = canonical_query_string("list-type=2&prefix=foo%2F");
1141        assert_eq!(canon, "list-type=2&prefix=foo%2F");
1142    }
1143
1144    /// S3 canonical URI: `/bucket/path with space/file` →
1145    /// `/bucket/path%20with%20space/file`.
1146    #[test]
1147    fn s3_path_with_spaces() {
1148        let canon = canonical_uri_path("/bucket/path with space/file");
1149        assert_eq!(canon, "/bucket/path%20with%20space/file");
1150    }
1151}
1152
1153#[cfg(test)]
1154mod sigv4a_gate_tests {
1155    //! v0.7 #47: unit tests for the SigV4a verify gate middleware.
1156    //!
1157    //! These exercise [`try_sigv4a_verify`] directly — no hyper
1158    //! `Incoming` body is needed because the function is generic over
1159    //! the body type. The canonical-request bytes computed by the
1160    //! middleware are the same bytes the test signs over (we use the
1161    //! `build_canonical_request_bytes` helper for both sides), so the
1162    //! happy-path verify is end-to-end byte-exact.
1163    //!
1164    //! Behavioural matrix:
1165    //!
1166    //! 1. no `AWS4-ECDSA-P256-SHA256` prefix and no region-set header
1167    //!    → `None` (caller forwards to s3s SigV4 path)
1168    //! 2. SigV4a Authorization + valid signature → `Some(Ok(()))`
1169    //! 3. SigV4a Authorization + tampered signature → `Some(Err(403))`
1170    //!    with `SignatureDoesNotMatch` body
1171    //! 4. SigV4a Authorization + region-set mismatch → `Some(Err(403))`
1172    //! 5. gate is `None` (no credential store) → `None` even when the
1173    //!    request looks SigV4a-shaped (caller forwards, and s3s will
1174    //!    surface its own "unknown algorithm" error — operator sees the
1175    //!    misconfiguration rather than a silent pass)
1176    //! 6. unknown access-key-id → `Some(Err(403))` with
1177    //!    `InvalidAccessKeyId` body
1178    //! 7. SigV4a-shaped (region-set header only, no SigV4a auth header)
1179    //!    → `Some(Err(403))` (we cannot verify without a parseable
1180    //!    Authorization, fail closed)
1181
1182    use super::*;
1183
1184    use std::collections::HashMap;
1185
1186    use http_body_util::BodyExt;
1187    use p256::ecdsa::SigningKey;
1188    use p256::ecdsa::signature::Signer;
1189    use rand::rngs::OsRng;
1190
1191    use crate::service::SigV4aGate;
1192    use crate::sigv4a::{REGION_SET_HEADER, SigV4aCredentialStore};
1193
1194    fn lower_hex(bytes: &[u8]) -> String {
1195        let mut s = String::with_capacity(bytes.len() * 2);
1196        for b in bytes {
1197            s.push_str(&format!("{b:02x}"));
1198        }
1199        s
1200    }
1201
1202    /// Build a `Request<()>` with the given method, path, and headers.
1203    fn req(method: Method, path: &str, headers: &[(&str, &str)]) -> Request<()> {
1204        let mut b = Request::builder().method(method).uri(path);
1205        for (k, v) in headers {
1206            b = b.header(*k, *v);
1207        }
1208        b.body(()).expect("request builder")
1209    }
1210
1211    /// Build the SigV4a Authorization header for the given access-key,
1212    /// signed-headers list, and signature (lowercase hex DER).
1213    fn build_auth_header(access_key: &str, signed_headers: &[&str], sig_hex: &str) -> String {
1214        format!(
1215            "AWS4-ECDSA-P256-SHA256 \
1216             Credential={access_key}/20260513/s3/aws4_request, \
1217             SignedHeaders={}, \
1218             Signature={sig_hex}",
1219            signed_headers.join(";")
1220        )
1221    }
1222
1223    /// Build a fully-signed SigV4a `Request<()>` ready for the gate to
1224    /// verify. Returns the request and the verifying key it should be
1225    /// loaded against.
1226    fn make_signed_request(
1227        access_key: &str,
1228        method: Method,
1229        path: &str,
1230        region_set: &str,
1231    ) -> (Request<()>, p256::ecdsa::VerifyingKey) {
1232        let signing = SigningKey::random(&mut OsRng);
1233        let verifying = p256::ecdsa::VerifyingKey::from(&signing);
1234        let signed_headers_list = [
1235            "host",
1236            "x-amz-content-sha256",
1237            "x-amz-date",
1238            REGION_SET_HEADER,
1239        ];
1240        // Build the request first WITHOUT the Authorization header so we
1241        // can compute canonical bytes and sign them; then re-build the
1242        // request with the Authorization header attached.
1243        let pre = Request::builder()
1244            .method(method.clone())
1245            .uri(path)
1246            .header("host", "s3.example.com")
1247            .header(
1248                "x-amz-content-sha256",
1249                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1250            )
1251            .header("x-amz-date", "20260513T120000Z")
1252            .header(REGION_SET_HEADER, region_set)
1253            .body(())
1254            .expect("pre-request");
1255        let signed_headers: Vec<String> = signed_headers_list
1256            .iter()
1257            .map(|s| (*s).to_string())
1258            .collect();
1259        let canonical =
1260            build_canonical_request_bytes(&pre, &signed_headers).expect("test fixture canonical");
1261        // v0.8.12 #126 (MED-A): sign the AWS-spec string-to-sign so
1262        // the routing-layer SigV4a fixture matches the new
1263        // `verify_request` body (which hashes the canonical request
1264        // and signs the algo / date / scope / hash concatenation).
1265        let canonical_hash = {
1266            use sha2::{Digest, Sha256};
1267            let mut h = Sha256::new();
1268            h.update(&canonical);
1269            let out = h.finalize();
1270            let mut s = String::with_capacity(out.len() * 2);
1271            for b in out {
1272                use std::fmt::Write as _;
1273                let _ = write!(s, "{b:02x}");
1274            }
1275            s
1276        };
1277        let sts = format!(
1278            "AWS4-ECDSA-P256-SHA256\n20260513T120000Z\n20260513/s3/aws4_request\n{canonical_hash}"
1279        );
1280        let sig: p256::ecdsa::Signature = signing.sign(sts.as_bytes());
1281        let sig_hex = lower_hex(sig.to_der().as_bytes());
1282        let auth = build_auth_header(access_key, &signed_headers_list, &sig_hex);
1283
1284        // Rebuild with the Authorization header — every other header
1285        // value is identical so the canonical bytes the gate computes
1286        // match what we signed.
1287        let r = Request::builder()
1288            .method(method)
1289            .uri(path)
1290            .header("host", "s3.example.com")
1291            .header(
1292                "x-amz-content-sha256",
1293                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1294            )
1295            .header("x-amz-date", "20260513T120000Z")
1296            .header(REGION_SET_HEADER, region_set)
1297            .header("authorization", auth)
1298            .body(())
1299            .expect("signed request");
1300        (r, verifying)
1301    }
1302
1303    fn make_gate_with(access_key: &str, vk: p256::ecdsa::VerifyingKey) -> Arc<SigV4aGate> {
1304        let mut m = HashMap::new();
1305        m.insert(access_key.to_string(), vk);
1306        let store = Arc::new(SigV4aCredentialStore::from_map(m));
1307        Arc::new(SigV4aGate::new(store))
1308    }
1309
1310    /// Drain a `s3s::Body` into bytes for body-content assertions.
1311    async fn body_to_bytes(resp: Response<s3s::Body>) -> Vec<u8> {
1312        resp.into_body()
1313            .collect()
1314            .await
1315            .expect("body collect")
1316            .to_bytes()
1317            .to_vec()
1318    }
1319
1320    /// v0.8.4 #76: pinned `now` matching the `x-amz-date: 20260513T120000Z`
1321    /// the test fixtures stamp. Without this the freshness check would
1322    /// reject every gate test (the timestamp would be days/weeks old by
1323    /// the time CI runs). Production callers use `try_sigv4a_verify`
1324    /// (which calls `Utc::now()`).
1325    fn fixture_now() -> chrono::DateTime<chrono::Utc> {
1326        chrono::DateTime::parse_from_rfc3339("2026-05-13T12:00:00Z")
1327            .unwrap()
1328            .with_timezone(&chrono::Utc)
1329    }
1330
1331    #[test]
1332    fn no_sigv4a_prefix_returns_none() {
1333        // Plain SigV4 (HMAC-SHA256) request — gate must defer to s3s.
1334        let (_, vk) = (
1335            (),
1336            p256::ecdsa::VerifyingKey::from(&SigningKey::random(&mut OsRng)),
1337        );
1338        let gate = make_gate_with("AKIAOK", vk);
1339        let r = req(
1340            Method::GET,
1341            "/bucket/key",
1342            &[(
1343                "authorization",
1344                "AWS4-HMAC-SHA256 Credential=AKIA/20260513/us-east-1/s3/aws4_request, \
1345                 SignedHeaders=host, Signature=deadbeef",
1346            )],
1347        );
1348        assert!(
1349            try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now()).is_none(),
1350            "plain SigV4 request must fall through to the inner service"
1351        );
1352    }
1353
1354    #[test]
1355    fn sigv4a_valid_signature_returns_ok() {
1356        let (r, vk) =
1357            make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1,us-west-2");
1358        let gate = make_gate_with("AKIAOK", vk);
1359        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1360            .expect("must intercept SigV4a request");
1361        assert!(
1362            result.is_ok(),
1363            "valid SigV4a signature must verify: {result:?}"
1364        );
1365    }
1366
1367    #[tokio::test]
1368    async fn sigv4a_tampered_signature_returns_403() {
1369        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1370        let gate = make_gate_with("AKIAOK", vk);
1371
1372        // Tamper one byte of the signature hex inside the Authorization
1373        // header — the DER decode may still succeed, but ECDSA verify
1374        // will fail (or the DER decode itself will fail; both surface
1375        // as `SignatureDoesNotMatch`).
1376        let auth = r
1377            .headers()
1378            .get("authorization")
1379            .and_then(|v| v.to_str().ok())
1380            .expect("auth header")
1381            .to_string();
1382        // Flip the last hex char to corrupt the signature.
1383        let mut chars: Vec<char> = auth.chars().collect();
1384        let last = chars.len() - 1;
1385        chars[last] = if chars[last] == '0' { '1' } else { '0' };
1386        let tampered_auth: String = chars.into_iter().collect();
1387        let tampered = req(
1388            Method::GET,
1389            "/bucket/key",
1390            &[
1391                ("host", "s3.example.com"),
1392                (
1393                    "x-amz-content-sha256",
1394                    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1395                ),
1396                ("x-amz-date", "20260513T120000Z"),
1397                (REGION_SET_HEADER, "us-east-1"),
1398                ("authorization", &tampered_auth),
1399            ],
1400        );
1401        let result = try_sigv4a_verify_at(&tampered, Some(&gate), "us-east-1", fixture_now())
1402            .expect("must intercept SigV4a request");
1403        let resp = result.expect_err("tampered signature must surface a 403 response");
1404        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1405        let body = body_to_bytes(resp).await;
1406        let body_str = String::from_utf8(body).expect("xml utf-8");
1407        assert!(
1408            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1409            "403 body must surface SignatureDoesNotMatch: {body_str}"
1410        );
1411    }
1412
1413    #[tokio::test]
1414    async fn sigv4a_region_set_mismatch_returns_403() {
1415        // Sign for `us-east-1` only, then verify with the listener
1416        // region claiming `eu-west-1` — must fail with
1417        // SignatureDoesNotMatch (the region-set check sits inside the
1418        // gate's verify path, and any failure there folds to
1419        // SignatureDoesNotMatch).
1420        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1421        let gate = make_gate_with("AKIAOK", vk);
1422        let result = try_sigv4a_verify_at(&r, Some(&gate), "eu-west-1", fixture_now())
1423            .expect("must intercept SigV4a request");
1424        let resp = result.expect_err("region mismatch must produce 403");
1425        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1426        let body = body_to_bytes(resp).await;
1427        let body_str = String::from_utf8(body).expect("xml utf-8");
1428        assert!(
1429            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1430            "region-set mismatch must surface SignatureDoesNotMatch: {body_str}"
1431        );
1432    }
1433
1434    #[test]
1435    fn no_gate_attached_returns_none() {
1436        // Even a SigV4a-shaped request returns None when no gate is
1437        // installed — the listener will hand it to s3s, which surfaces
1438        // its own "unknown algorithm" error so the misconfiguration is
1439        // visible to the operator.
1440        let (r, _vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1441        assert!(
1442            try_sigv4a_verify_at(&r, None, "us-east-1", fixture_now()).is_none(),
1443            "missing gate must defer to inner service"
1444        );
1445    }
1446
1447    #[tokio::test]
1448    async fn unknown_access_key_returns_403_invalid_access_key_id() {
1449        // Sign with one key but load the credential store with a
1450        // different access-key-id → InvalidAccessKeyId.
1451        let (r, _vk_unused) =
1452            make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1453        let other_signing = SigningKey::random(&mut OsRng);
1454        let other_vk = p256::ecdsa::VerifyingKey::from(&other_signing);
1455        let gate = make_gate_with("AKIASOMEONEELSE", other_vk);
1456        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1457            .expect("must intercept SigV4a request");
1458        let resp = result.expect_err("unknown key must produce 403");
1459        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1460        let body = body_to_bytes(resp).await;
1461        let body_str = String::from_utf8(body).expect("xml utf-8");
1462        assert!(
1463            body_str.contains("<Code>InvalidAccessKeyId</Code>"),
1464            "unknown access-key must surface InvalidAccessKeyId: {body_str}"
1465        );
1466    }
1467
1468    #[tokio::test]
1469    async fn region_set_header_only_without_sigv4a_auth_returns_403() {
1470        // Some legacy clients stamp the `X-Amz-Region-Set` header
1471        // before swapping the algorithm string. `detect` flags this as
1472        // SigV4a-shaped but we cannot verify without a parseable
1473        // Authorization → fail closed (SignatureDoesNotMatch).
1474        let signing = SigningKey::random(&mut OsRng);
1475        let vk = p256::ecdsa::VerifyingKey::from(&signing);
1476        let gate = make_gate_with("AKIAOK", vk);
1477        let r = req(
1478            Method::GET,
1479            "/bucket/key",
1480            &[
1481                // SigV4 algorithm + region-set header → detected, but
1482                // the Authorization is plain SigV4 so `parse_authorization_header`
1483                // returns None.
1484                (
1485                    "authorization",
1486                    "AWS4-HMAC-SHA256 Credential=AKIA/20260513/us-east-1/s3/aws4_request, \
1487                     SignedHeaders=host, Signature=deadbeef",
1488                ),
1489                (REGION_SET_HEADER, "us-east-1"),
1490            ],
1491        );
1492        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1493            .expect("must intercept SigV4a-shaped request");
1494        let resp = result.expect_err("region-set without sigv4a auth must produce 403");
1495        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1496        let body = body_to_bytes(resp).await;
1497        let body_str = String::from_utf8(body).expect("xml utf-8");
1498        assert!(
1499            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1500            "missing/malformed Authorization for SigV4a-shaped request must fail closed: {body_str}"
1501        );
1502    }
1503
1504    /// v0.8.4 #76 (audit H-6): captured-request replay outside the
1505    /// 15-min window → 403 RequestTimeTooSkewed (not
1506    /// SignatureDoesNotMatch). This is the headline gate-level
1507    /// behaviour change; pre-#76 the same captured request would have
1508    /// reached the inner service, allowing destructive replay (DELETE
1509    /// included).
1510    #[tokio::test]
1511    async fn sigv4a_replay_outside_window_returns_403_request_time_too_skewed() {
1512        let (r, vk) = make_signed_request("AKIAOK", Method::GET, "/bucket/key", "us-east-1");
1513        let gate = make_gate_with("AKIAOK", vk);
1514        // Request stamped 20260513T120000Z; "now" is 30 min later → drift
1515        // 1800s, beyond the 900s default tolerance.
1516        let now = chrono::DateTime::parse_from_rfc3339("2026-05-13T12:30:00Z")
1517            .unwrap()
1518            .with_timezone(&chrono::Utc);
1519        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", now)
1520            .expect("must intercept SigV4a request");
1521        let resp = result.expect_err("replay outside window must reject");
1522        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1523        let body = body_to_bytes(resp).await;
1524        let body_str = String::from_utf8(body).expect("xml utf-8");
1525        assert!(
1526            body_str.contains("<Code>RequestTimeTooSkewed</Code>"),
1527            "replay outside window must surface RequestTimeTooSkewed: {body_str}"
1528        );
1529    }
1530
1531    /// Cover the canonical-request builder directly: empty query
1532    /// string, sorted multi-pair query, and header value collapsed
1533    /// whitespace all hit the right code paths.
1534    #[test]
1535    fn canonical_request_bytes_format() {
1536        let r = req(
1537            Method::PUT,
1538            "/bucket/key?z=1&a=2",
1539            &[
1540                ("host", "s3.example.com"),
1541                ("x-amz-content-sha256", "UNSIGNED-PAYLOAD"),
1542                ("x-amz-date", "  20260513T120000Z  "),
1543            ],
1544        );
1545        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1546            .iter()
1547            .map(|s| (*s).into())
1548            .collect();
1549        let bytes =
1550            build_canonical_request_bytes(&r, &signed).expect("canonical request bytes must build");
1551        let s = std::str::from_utf8(&bytes).expect("utf-8");
1552        let expected = "PUT\n\
1553                        /bucket/key\n\
1554                        a=2&z=1\n\
1555                        host:s3.example.com\n\
1556                        x-amz-content-sha256:UNSIGNED-PAYLOAD\n\
1557                        x-amz-date:20260513T120000Z\n\
1558                        \n\
1559                        host;x-amz-content-sha256;x-amz-date\n\
1560                        UNSIGNED-PAYLOAD";
1561        assert_eq!(s, expected, "canonical request bytes mismatch:\n{s}");
1562    }
1563
1564    /// v0.8.5 #84 H-4: duplicate `x-amz-date` headers must be rejected
1565    /// at canonical-request build time (not silently coalesced to the
1566    /// first value). HTTP/1.1 spec already forbids duplicates of
1567    /// `host` / `x-amz-date`; AWS SDKs never emit them; so any
1568    /// duplicate must be malicious or broken — single-value reject is
1569    /// the safe choice (see [`build_canonical_request_bytes`] doc).
1570    #[test]
1571    fn sigv4a_duplicate_x_amz_date_rejected() {
1572        // Two x-amz-date headers — first one matches the signature the
1573        // gate expects, second one is what a downstream parser might
1574        // pick up. This is the textbook auth-confusion vector.
1575        let r = Request::builder()
1576            .method(Method::GET)
1577            .uri("/b/k")
1578            .header("host", "s3.example.com")
1579            .header("x-amz-content-sha256", "UNSIGNED-PAYLOAD")
1580            .header("x-amz-date", "20260513T120000Z")
1581            .header("x-amz-date", "20260513T130000Z")
1582            .body(())
1583            .expect("dup-header request");
1584        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1585            .iter()
1586            .map(|s| (*s).into())
1587            .collect();
1588        let err = build_canonical_request_bytes(&r, &signed)
1589            .expect_err("duplicate x-amz-date must reject");
1590        match err {
1591            crate::sigv4a::SigV4aError::DuplicateSignedHeader { header } => {
1592                assert_eq!(header, "x-amz-date");
1593            }
1594            other => panic!("expected DuplicateSignedHeader, got {other:?}"),
1595        }
1596    }
1597
1598    /// v0.8.5 #84 H-4: counterpart to the duplicate-reject test —
1599    /// single-occurrence headers on the same path stay accepted.
1600    /// Guards against a regression where the duplicate-detect logic
1601    /// is over-eager and trips on a normally-formed request.
1602    #[test]
1603    fn sigv4a_canonicalization_single_header_passes() {
1604        let r = req(
1605            Method::GET,
1606            "/b/k",
1607            &[
1608                ("host", "s3.example.com"),
1609                ("x-amz-content-sha256", "UNSIGNED-PAYLOAD"),
1610                ("x-amz-date", "20260513T120000Z"),
1611            ],
1612        );
1613        let signed: Vec<String> = ["host", "x-amz-content-sha256", "x-amz-date"]
1614            .iter()
1615            .map(|s| (*s).into())
1616            .collect();
1617        let bytes =
1618            build_canonical_request_bytes(&r, &signed).expect("single-occurrence must accept");
1619        // Body content not asserted in detail (covered by
1620        // canonical_request_bytes_format); just confirm the bytes
1621        // parse as utf-8 and contain the date verbatim.
1622        let s = std::str::from_utf8(&bytes).expect("utf-8");
1623        assert!(
1624            s.contains("x-amz-date:20260513T120000Z"),
1625            "canonical bytes must echo the single x-amz-date verbatim:\n{s}"
1626        );
1627    }
1628
1629    /// v0.8.5 #84 H-4: end-to-end through the
1630    /// [`try_sigv4a_verify_at`] gate — duplicate `x-amz-date` on a
1631    /// SigV4a-shaped request must surface 403 SignatureDoesNotMatch
1632    /// (not silently authenticate against the first value).
1633    #[tokio::test]
1634    async fn sigv4a_pre_route_rejects_duplicate_signed_header() {
1635        let signing = SigningKey::random(&mut OsRng);
1636        let vk = p256::ecdsa::VerifyingKey::from(&signing);
1637        let gate = make_gate_with("AKIAOK", vk);
1638        // Authorization header lists x-amz-date in SignedHeaders —
1639        // signature value itself can be garbage; the duplicate-detect
1640        // path runs strictly before any ECDSA math.
1641        let auth = build_auth_header(
1642            "AKIAOK",
1643            &[
1644                "host",
1645                "x-amz-content-sha256",
1646                "x-amz-date",
1647                REGION_SET_HEADER,
1648            ],
1649            "deadbeef",
1650        );
1651        let r = Request::builder()
1652            .method(Method::GET)
1653            .uri("/bucket/key")
1654            .header("host", "s3.example.com")
1655            .header(
1656                "x-amz-content-sha256",
1657                "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
1658            )
1659            .header("x-amz-date", "20260513T120000Z")
1660            .header("x-amz-date", "20260513T130000Z")
1661            .header(REGION_SET_HEADER, "us-east-1")
1662            .header("authorization", auth)
1663            .body(())
1664            .expect("dup-header sigv4a request");
1665        let result = try_sigv4a_verify_at(&r, Some(&gate), "us-east-1", fixture_now())
1666            .expect("must intercept SigV4a request");
1667        let resp = result.expect_err("duplicate signed header must reject at the gate");
1668        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1669        let body = body_to_bytes(resp).await;
1670        let body_str = String::from_utf8(body).expect("xml utf-8");
1671        assert!(
1672            body_str.contains("<Code>SignatureDoesNotMatch</Code>"),
1673            "duplicate signed header must surface SignatureDoesNotMatch: {body_str}"
1674        );
1675        assert!(
1676            body_str.contains("duplicate signed header"),
1677            "diagnostic must mention duplicate header: {body_str}"
1678        );
1679    }
1680}