| 1 |
1 |
|
//! HMAC-SHA256 authentication for internal API requests from MNW.
|
| 2 |
2 |
|
//!
|
| 3 |
|
- |
//! The v2 signed message binds method + path + nonce as well as timestamp +
|
| 4 |
|
- |
//! body — `HMAC-SHA256(timestamp \n METHOD \n PATH \n NONCE \n body)` — sent in
|
|
3 |
+ |
//! The signed message binds method + path + nonce as well as timestamp + body —
|
|
4 |
+ |
//! `HMAC-SHA256(timestamp \n METHOD \n PATH \n NONCE \n body)` — sent in
|
| 5 |
5 |
|
//! `X-Internal-{Timestamp,Signature,Nonce}`. Binding method+path stops a
|
| 6 |
6 |
|
//! captured signature being replayed to a different endpoint; the nonce, checked
|
| 7 |
7 |
|
//! against a single-use cache, stops it being re-sent at all within the 60s
|
| 8 |
8 |
|
//! freshness window.
|
| 9 |
9 |
|
//!
|
| 10 |
|
- |
//! **Lockstep rollout:** this verifier is in the dual-accept transition state —
|
| 11 |
|
- |
//! a request with no `X-Internal-Nonce` is verified against the legacy v1
|
| 12 |
|
- |
//! message (timestamp+body) for compatibility with a not-yet-upgraded MNW
|
| 13 |
|
- |
//! signer. Once the server signer is fully deployed, TIGHTEN: require a nonce
|
| 14 |
|
- |
//! and delete the v1 fallback (`verify_internal_signature` + the `None` branch
|
| 15 |
|
- |
//! of `verify_signed_request`). Until then there is a brief window where a v1
|
| 16 |
|
- |
//! signature is replayable — keep the dual-accept period short.
|
|
10 |
+ |
//! A nonce is **mandatory**: there is exactly one verification path. A request
|
|
11 |
+ |
//! with no `X-Internal-Nonce` is rejected outright (401), not downgraded — the
|
|
12 |
+ |
//! legacy v1 (timestamp+body) format and its dual-accept fallback were deleted
|
|
13 |
+ |
//! once the MNW signer moved fully to v2, closing the replay window an attacker
|
|
14 |
+ |
//! could otherwise select by simply omitting the nonce header.
|
| 17 |
15 |
|
|
| 18 |
16 |
|
use std::collections::HashMap;
|
| 19 |
17 |
|
use std::sync::{LazyLock, Mutex};
|
| 116 |
114 |
|
)
|
| 117 |
115 |
|
.map_err(|(status, msg)| (status, msg).into_response())?;
|
| 118 |
116 |
|
|
| 119 |
|
- |
// Single-use: reject a replayed nonce (only meaningful once the v2
|
| 120 |
|
- |
// signer is live; v1 requests carry no nonce).
|
|
117 |
+ |
// Single-use: reject a replayed nonce. `verify_signed_request` has
|
|
118 |
+ |
// already guaranteed the nonce is present, so this always runs.
|
| 121 |
119 |
|
if let Some(nonce) = nonce_header.as_deref()
|
| 122 |
120 |
|
&& !record_nonce(nonce, now)
|
| 123 |
121 |
|
{
|
| 128 |
126 |
|
}
|
| 129 |
127 |
|
}
|
| 130 |
128 |
|
|
| 131 |
|
- |
/// Compute the hex-encoded HMAC-SHA256 signature for an internal request.
|
| 132 |
|
- |
/// `secret` may be any length (HMAC-SHA256 accepts any key length).
|
| 133 |
|
- |
pub(crate) fn compute_internal_signature(secret: &str, timestamp_str: &str, body: &[u8]) -> String {
|
| 134 |
|
- |
// MAC over raw bytes (`timestamp\n` ++ body), not a lossy UTF-8 string. For
|
| 135 |
|
- |
// valid-UTF-8 bodies (all our JSON) this is byte-identical to the old
|
| 136 |
|
- |
// `format!`-based message, so it stays wire-compatible with MNW's signer;
|
| 137 |
|
- |
// it also closes the latent hole where two distinct non-UTF-8 bodies both
|
| 138 |
|
- |
// collapsed to the empty string and signed identically.
|
| 139 |
|
- |
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes())
|
| 140 |
|
- |
.expect("HMAC-SHA256 accepts any key length");
|
| 141 |
|
- |
mac.update(timestamp_str.as_bytes());
|
| 142 |
|
- |
mac.update(b"\n");
|
| 143 |
|
- |
mac.update(body);
|
| 144 |
|
- |
hex::encode(mac.finalize().into_bytes())
|
| 145 |
|
- |
}
|
| 146 |
|
- |
|
| 147 |
|
- |
/// Compute the v2 signature, which binds method + path + nonce in addition to
|
|
129 |
+ |
/// Compute the signature, which binds method + path + nonce in addition to
|
| 148 |
130 |
|
/// timestamp + body. The canonical message is newline-delimited with a fixed
|
| 149 |
131 |
|
/// field order, body last so an embedded newline in the body can never be
|
| 150 |
132 |
|
/// confused with a field separator:
|
| 151 |
133 |
|
/// `timestamp \n METHOD \n PATH \n NONCE \n <raw body bytes>`
|
| 152 |
134 |
|
/// METHOD is uppercase ASCII, PATH is the request path only (no query string).
|
|
135 |
+ |
/// Body is MAC'd as raw bytes, not a lossy UTF-8 string, closing the latent
|
|
136 |
+ |
/// hole where two distinct non-UTF-8 bodies both collapsed to "" and signed
|
|
137 |
+ |
/// identically.
|
| 153 |
138 |
|
pub(crate) fn compute_internal_signature_v2(
|
| 154 |
139 |
|
secret: &str,
|
| 155 |
140 |
|
timestamp_str: &str,
|
| 186 |
171 |
|
Ok(timestamp)
|
| 187 |
172 |
|
}
|
| 188 |
173 |
|
|
| 189 |
|
- |
/// Pure verification of the legacy (v1) message: timestamp + body only.
|
| 190 |
|
- |
/// Retained as the back-compat path during the lockstep rollout (a request with
|
| 191 |
|
- |
/// no `X-Internal-Nonce` is assumed to come from a pre-upgrade signer).
|
|
174 |
+ |
/// Verify a signed internal request, binding method + path + nonce. A nonce is
|
|
175 |
+ |
/// mandatory — a request without one is rejected (401), never downgraded. This
|
|
176 |
+ |
/// is the single verification path; the legacy v1 (timestamp+body) fallback was
|
|
177 |
+ |
/// deleted once the MNW signer moved fully to v2. Freshness is checked first.
|
| 192 |
178 |
|
///
|
| 193 |
179 |
|
/// Headers are passed as `Option<&str>` so callers can extract them with any
|
| 194 |
180 |
|
/// strategy (axum `HeaderMap`, manual `Bytes`, tests).
|
| 195 |
|
- |
pub(crate) fn verify_internal_signature(
|
| 196 |
|
- |
secret: &str,
|
| 197 |
|
- |
timestamp_header: Option<&str>,
|
| 198 |
|
- |
signature_header: Option<&str>,
|
| 199 |
|
- |
body: &[u8],
|
| 200 |
|
- |
now_unix: i64,
|
| 201 |
|
- |
) -> Result<(), (StatusCode, &'static str)> {
|
| 202 |
|
- |
let timestamp_str = timestamp_header
|
| 203 |
|
- |
.ok_or((StatusCode::UNAUTHORIZED, "Missing X-Internal-Timestamp"))?;
|
| 204 |
|
- |
let signature = signature_header
|
| 205 |
|
- |
.ok_or((StatusCode::UNAUTHORIZED, "Missing X-Internal-Signature"))?;
|
| 206 |
|
- |
|
| 207 |
|
- |
check_freshness(timestamp_str, now_unix)?;
|
| 208 |
|
- |
|
| 209 |
|
- |
let expected = compute_internal_signature(secret, timestamp_str, body);
|
| 210 |
|
- |
if !constant_time_eq(expected.as_bytes(), signature.as_bytes()) {
|
| 211 |
|
- |
return Err((StatusCode::UNAUTHORIZED, "Invalid signature"));
|
| 212 |
|
- |
}
|
| 213 |
|
- |
Ok(())
|
| 214 |
|
- |
}
|
| 215 |
|
- |
|
| 216 |
|
- |
/// Verify a signed internal request, binding method + path + nonce when the
|
| 217 |
|
- |
/// signer supplied a nonce (the v2 format), and falling back to the legacy v1
|
| 218 |
|
- |
/// message otherwise.
|
| 219 |
|
- |
///
|
| 220 |
|
- |
/// This is the dual-accept transition state: MT accepts both an upgraded signer
|
| 221 |
|
- |
/// (nonce present → v2 + replay protection) and a pre-upgrade signer (no nonce →
|
| 222 |
|
- |
/// v1). Once the server signer is fully rolled out, tighten this to require a
|
| 223 |
|
- |
/// nonce and drop the v1 branch. Freshness is always checked first.
|
| 224 |
181 |
|
///
|
| 225 |
182 |
|
/// Nonce replay is NOT checked here (that is stateful); the caller records the
|
| 226 |
183 |
|
/// nonce via [`record_nonce`] after this returns Ok.
|
| 235 |
192 |
|
body: &[u8],
|
| 236 |
193 |
|
now_unix: i64,
|
| 237 |
194 |
|
) -> Result<(), (StatusCode, &'static str)> {
|
| 238 |
|
- |
let Some(nonce) = nonce_header else {
|
| 239 |
|
- |
// No nonce → legacy signer. Verify the v1 message for back-compat.
|
| 240 |
|
- |
return verify_internal_signature(secret, timestamp_header, signature_header, body, now_unix);
|
| 241 |
|
- |
};
|
|
195 |
+ |
let nonce = nonce_header.ok_or((StatusCode::UNAUTHORIZED, "Missing X-Internal-Nonce"))?;
|
| 242 |
196 |
|
|
| 243 |
197 |
|
let timestamp_str = timestamp_header
|
| 244 |
198 |
|
.ok_or((StatusCode::UNAUTHORIZED, "Missing X-Internal-Timestamp"))?;
|
| 323 |
277 |
|
assert!(!constant_time_eq(b"hello", b"hell"));
|
| 324 |
278 |
|
}
|
| 325 |
279 |
|
|
| 326 |
|
- |
#[test]
|
| 327 |
|
- |
fn hmac_signature_roundtrip() {
|
| 328 |
|
- |
let secret = "test-secret";
|
| 329 |
|
- |
let timestamp = "1234567890";
|
| 330 |
|
- |
let body = r#"{"name":"test"}"#;
|
| 331 |
|
- |
let message = format!("{}\n{}", timestamp, body);
|
| 332 |
|
- |
|
| 333 |
|
- |
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap();
|
| 334 |
|
- |
mac.update(message.as_bytes());
|
| 335 |
|
- |
let sig = hex::encode(mac.finalize().into_bytes());
|
| 336 |
|
- |
|
| 337 |
|
- |
// Verify the same computation matches
|
| 338 |
|
- |
let mut mac2 = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap();
|
| 339 |
|
- |
mac2.update(message.as_bytes());
|
| 340 |
|
- |
let expected = hex::encode(mac2.finalize().into_bytes());
|
| 341 |
|
- |
|
| 342 |
|
- |
assert!(constant_time_eq(sig.as_bytes(), expected.as_bytes()));
|
| 343 |
|
- |
}
|
|
280 |
+ |
// ── compute_internal_signature_v2 pins HMAC message construction ──
|
| 344 |
281 |
|
|
| 345 |
|
- |
// ── compute_internal_signature pins HMAC message construction ──
|
|
282 |
+ |
fn v2(secret: &str, ts: &str, method: &str, path: &str, nonce: &str, body: &[u8]) -> String {
|
|
283 |
+ |
compute_internal_signature_v2(secret, ts, method, path, nonce, body)
|
|
284 |
+ |
}
|
| 346 |
285 |
|
|
| 347 |
286 |
|
#[test]
|
| 348 |
287 |
|
fn signature_is_64_hex_chars() {
|
| 349 |
|
- |
let sig = compute_internal_signature("secret", "100", b"body");
|
|
288 |
+ |
let sig = v2("secret", "100", "POST", "/x", "n", b"body");
|
| 350 |
289 |
|
assert_eq!(sig.len(), 64, "SHA-256 hex is 64 chars");
|
| 351 |
290 |
|
assert!(sig.chars().all(|c| c.is_ascii_hexdigit()));
|
| 352 |
291 |
|
}
|
| 354 |
293 |
|
#[test]
|
| 355 |
294 |
|
fn signature_changes_with_secret() {
|
| 356 |
295 |
|
// Pins that the secret feeds into the MAC key.
|
| 357 |
|
- |
let s1 = compute_internal_signature("alpha", "100", b"body");
|
| 358 |
|
- |
let s2 = compute_internal_signature("beta", "100", b"body");
|
| 359 |
|
- |
assert_ne!(s1, s2);
|
| 360 |
|
- |
}
|
| 361 |
|
- |
|
| 362 |
|
- |
#[test]
|
| 363 |
|
- |
fn signature_changes_with_timestamp() {
|
| 364 |
|
- |
// Pins the `format!("{}\n{}", timestamp_str, body)` ordering — a
|
| 365 |
|
- |
// mutation that drops the timestamp or swaps the order would make
|
| 366 |
|
- |
// these two signatures match.
|
| 367 |
|
- |
let s1 = compute_internal_signature("secret", "100", b"body");
|
| 368 |
|
- |
let s2 = compute_internal_signature("secret", "101", b"body");
|
| 369 |
|
- |
assert_ne!(s1, s2);
|
|
296 |
+ |
assert_ne!(
|
|
297 |
+ |
v2("alpha", "100", "POST", "/x", "n", b"body"),
|
|
298 |
+ |
v2("beta", "100", "POST", "/x", "n", b"body"),
|
|
299 |
+ |
);
|
| 370 |
300 |
|
}
|
| 371 |
301 |
|
|
| 372 |
302 |
|
#[test]
|
| 373 |
|
- |
fn signature_changes_with_body() {
|
| 374 |
|
- |
let s1 = compute_internal_signature("secret", "100", b"hello");
|
| 375 |
|
- |
let s2 = compute_internal_signature("secret", "100", b"hello!");
|
| 376 |
|
- |
assert_ne!(s1, s2);
|
|
303 |
+ |
fn signature_changes_with_each_bound_field() {
|
|
304 |
+ |
// Pins that timestamp, method, path, nonce, and body each feed the MAC —
|
|
305 |
+ |
// a mutation dropping any field would collide one of these pairs.
|
|
306 |
+ |
let base = v2("s", "100", "POST", "/x", "n", b"body");
|
|
307 |
+ |
assert_ne!(base, v2("s", "101", "POST", "/x", "n", b"body"), "timestamp bound");
|
|
308 |
+ |
assert_ne!(base, v2("s", "100", "GET", "/x", "n", b"body"), "method bound");
|
|
309 |
+ |
assert_ne!(base, v2("s", "100", "POST", "/y", "n", b"body"), "path bound");
|
|
310 |
+ |
assert_ne!(base, v2("s", "100", "POST", "/x", "m", b"body"), "nonce bound");
|
|
311 |
+ |
assert_ne!(base, v2("s", "100", "POST", "/x", "n", b"body!"), "body bound");
|
| 377 |
312 |
|
}
|
| 378 |
313 |
|
|
| 379 |
314 |
|
#[test]
|
| 380 |
|
- |
fn signature_separator_is_newline_not_concat() {
|
| 381 |
|
- |
// Pins `format!("{}\n{}", ...)` — without the `\n`, "1" + "00body"
|
| 382 |
|
- |
// would collide with "10" + "0body".
|
| 383 |
|
- |
let collision_a = compute_internal_signature("secret", "1", b"00body");
|
| 384 |
|
- |
let collision_b = compute_internal_signature("secret", "10", b"0body");
|
|
315 |
+ |
fn signature_separators_are_newlines_not_concat() {
|
|
316 |
+ |
// Without the `\n` delimiters, field-boundary ambiguity would let two
|
|
317 |
+ |
// distinct messages collide (e.g. ts "1"+"00body" vs "10"+"0body").
|
| 385 |
318 |
|
assert_ne!(
|
| 386 |
|
- |
collision_a, collision_b,
|
| 387 |
|
- |
"missing newline separator allows length-ambiguity collision"
|
|
319 |
+ |
v2("s", "1", "POST", "/x", "n", b"00body"),
|
|
320 |
+ |
v2("s", "10", "POST", "/x", "n", b"0body"),
|
|
321 |
+ |
"missing separator allows length-ambiguity collision"
|
| 388 |
322 |
|
);
|
| 389 |
323 |
|
}
|
| 390 |
324 |
|
|
| 391 |
|
- |
// ── verify_internal_signature freshness + signature check ──
|
| 392 |
|
- |
|
| 393 |
|
- |
fn valid(secret: &str, ts: &str, body: &[u8]) -> String {
|
| 394 |
|
- |
compute_internal_signature(secret, ts, body)
|
| 395 |
|
- |
}
|
|
325 |
+ |
// ── verify_signed_request: signature + freshness + mandatory nonce ──
|
| 396 |
326 |
|
|
| 397 |
327 |
|
#[test]
|
| 398 |
328 |
|
fn verify_accepts_valid_signature_at_now() {
|
| 399 |
|
- |
let secret = "s";
|
| 400 |
|
- |
let body = b"abc";
|
| 401 |
|
- |
let ts = "1000";
|
| 402 |
|
- |
let sig = valid(secret, ts, body);
|
| 403 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), body, 1000).is_ok());
|
|
329 |
+ |
let sig = v2("s", "1000", "POST", "/internal/x", "abc", b"body");
|
|
330 |
+ |
assert!(verify_signed_request(
|
|
331 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 1000
|
|
332 |
+ |
)
|
|
333 |
+ |
.is_ok());
|
| 404 |
334 |
|
}
|
| 405 |
335 |
|
|
| 406 |
336 |
|
#[test]
|
| 407 |
337 |
|
fn verify_rejects_wrong_signature() {
|
| 408 |
|
- |
let secret = "s";
|
| 409 |
|
- |
let body = b"abc";
|
| 410 |
|
- |
let ts = "1000";
|
| 411 |
|
- |
// Tamper with one hex char.
|
| 412 |
|
- |
let mut sig = valid(secret, ts, body);
|
|
338 |
+ |
let mut sig = v2("s", "1000", "POST", "/internal/x", "abc", b"body");
|
| 413 |
339 |
|
let first = sig.remove(0);
|
| 414 |
340 |
|
sig.insert(0, if first == '0' { '1' } else { '0' });
|
| 415 |
|
- |
let (status, _) =
|
| 416 |
|
- |
verify_internal_signature(secret, Some(ts), Some(&sig), body, 1000).unwrap_err();
|
|
341 |
+ |
let (status, _) = verify_signed_request(
|
|
342 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 1000,
|
|
343 |
+ |
)
|
|
344 |
+ |
.unwrap_err();
|
| 417 |
345 |
|
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
| 418 |
346 |
|
}
|
| 419 |
347 |
|
|
| 420 |
348 |
|
#[test]
|
| 421 |
349 |
|
fn verify_rejects_wrong_secret() {
|
| 422 |
|
- |
let body = b"abc";
|
| 423 |
|
- |
let ts = "1000";
|
| 424 |
|
- |
let sig = valid("real-secret", ts, body);
|
| 425 |
|
- |
assert!(
|
| 426 |
|
- |
verify_internal_signature("wrong-secret", Some(ts), Some(&sig), body, 1000).is_err()
|
| 427 |
|
- |
);
|
|
350 |
+ |
let sig = v2("real-secret", "1000", "POST", "/internal/x", "abc", b"body");
|
|
351 |
+ |
assert!(verify_signed_request(
|
|
352 |
+ |
"wrong-secret", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 1000
|
|
353 |
+ |
)
|
|
354 |
+ |
.is_err());
|
| 428 |
355 |
|
}
|
| 429 |
356 |
|
|
| 430 |
357 |
|
#[test]
|
| 431 |
358 |
|
fn verify_rejects_tampered_body() {
|
| 432 |
|
- |
let secret = "s";
|
| 433 |
|
- |
let ts = "1000";
|
| 434 |
|
- |
let sig = valid(secret, ts, b"original");
|
| 435 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), b"tampered", 1000).is_err());
|
| 436 |
|
- |
}
|
| 437 |
|
- |
|
| 438 |
|
- |
#[test]
|
| 439 |
|
- |
fn verify_at_window_boundary_accepts_inside_rejects_outside() {
|
| 440 |
|
- |
// Asymmetric window: up to MAX_TIMESTAMP_AGE_SECS (60s) old, but only
|
| 441 |
|
- |
// MAX_FUTURE_SKEW_SECS (5s) into the future. `>` is strict, so exactly
|
| 442 |
|
- |
// at each boundary is accepted.
|
| 443 |
|
- |
let secret = "s";
|
| 444 |
|
- |
let body = b"abc";
|
| 445 |
|
- |
let ts = "1000";
|
| 446 |
|
- |
let sig = valid(secret, ts, body);
|
| 447 |
|
- |
|
| 448 |
|
- |
// now - ts = 60 → accepted (at the age boundary)
|
| 449 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), body, 1060).is_ok());
|
| 450 |
|
- |
// now - ts = 61 → rejected (too old)
|
| 451 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), body, 1061).is_err());
|
| 452 |
|
- |
// ts - now = 5 → accepted (at the future-skew boundary)
|
| 453 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), body, 995).is_ok());
|
| 454 |
|
- |
// ts - now = 6 → rejected (too far in the future)
|
| 455 |
|
- |
assert!(verify_internal_signature(secret, Some(ts), Some(&sig), body, 994).is_err());
|
| 456 |
|
- |
}
|
| 457 |
|
- |
|
| 458 |
|
- |
#[test]
|
| 459 |
|
- |
fn verify_rejects_missing_timestamp_header() {
|
| 460 |
|
- |
let secret = "s";
|
| 461 |
|
- |
let body = b"abc";
|
| 462 |
|
- |
let sig = valid(secret, "1000", body);
|
| 463 |
|
- |
let (status, msg) =
|
| 464 |
|
- |
verify_internal_signature(secret, None, Some(&sig), body, 1000).unwrap_err();
|
| 465 |
|
- |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
| 466 |
|
- |
assert!(msg.contains("Timestamp"));
|
| 467 |
|
- |
}
|
| 468 |
|
- |
|
| 469 |
|
- |
#[test]
|
| 470 |
|
- |
fn verify_rejects_missing_signature_header() {
|
| 471 |
|
- |
let (status, msg) =
|
| 472 |
|
- |
verify_internal_signature("s", Some("1000"), None, b"abc", 1000).unwrap_err();
|
| 473 |
|
- |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
| 474 |
|
- |
assert!(msg.contains("Signature"));
|
| 475 |
|
- |
}
|
| 476 |
|
- |
|
| 477 |
|
- |
#[test]
|
| 478 |
|
- |
fn verify_rejects_unparseable_timestamp() {
|
| 479 |
|
- |
let (status, msg) =
|
| 480 |
|
- |
verify_internal_signature("s", Some("not-an-int"), Some("zz"), b"", 1000).unwrap_err();
|
| 481 |
|
- |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
| 482 |
|
- |
assert!(msg.contains("Invalid timestamp"));
|
| 483 |
|
- |
}
|
| 484 |
|
- |
|
| 485 |
|
- |
#[test]
|
| 486 |
|
- |
fn verify_check_order_missing_timestamp_first() {
|
| 487 |
|
- |
// Both headers missing: timestamp check fires first.
|
| 488 |
|
- |
let (_, msg) = verify_internal_signature("s", None, None, b"", 1000).unwrap_err();
|
| 489 |
|
- |
assert!(msg.contains("Timestamp"), "expected timestamp msg first, got: {msg}");
|
| 490 |
|
- |
}
|
| 491 |
|
- |
|
| 492 |
|
- |
#[test]
|
| 493 |
|
- |
fn verify_check_order_freshness_before_signature() {
|
| 494 |
|
- |
// A stale timestamp must reject even when the (otherwise valid) sig
|
| 495 |
|
- |
// matches. Catches a mutation that runs the freshness check after
|
| 496 |
|
- |
// signature verification.
|
| 497 |
|
- |
let secret = "s";
|
| 498 |
|
- |
let body = b"abc";
|
| 499 |
|
- |
let ts = "1000";
|
| 500 |
|
- |
let sig = valid(secret, ts, body);
|
| 501 |
|
- |
let (_, msg) =
|
| 502 |
|
- |
verify_internal_signature(secret, Some(ts), Some(&sig), body, 9999).unwrap_err();
|
| 503 |
|
- |
assert!(msg.contains("Timestamp"), "expected freshness msg, got: {msg}");
|
| 504 |
|
- |
}
|
| 505 |
|
- |
|
| 506 |
|
- |
// ── v2 (method+path+nonce) verification + nonce replay ──
|
| 507 |
|
- |
|
| 508 |
|
- |
fn v2(secret: &str, ts: &str, method: &str, path: &str, nonce: &str, body: &[u8]) -> String {
|
| 509 |
|
- |
compute_internal_signature_v2(secret, ts, method, path, nonce, body)
|
| 510 |
|
- |
}
|
| 511 |
|
- |
|
| 512 |
|
- |
#[test]
|
| 513 |
|
- |
fn verify_v2_accepts_matching_method_path_nonce() {
|
| 514 |
|
- |
let sig = v2("s", "1000", "POST", "/internal/x", "abc", b"body");
|
|
359 |
+ |
let sig = v2("s", "1000", "POST", "/internal/x", "abc", b"original");
|
| 515 |
360 |
|
assert!(verify_signed_request(
|
| 516 |
|
- |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 1000
|
|
361 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"tampered", 1000
|
| 517 |
362 |
|
)
|
| 518 |
|
- |
.is_ok());
|
|
363 |
+ |
.is_err());
|
| 519 |
364 |
|
}
|
| 520 |
365 |
|
|
| 521 |
366 |
|
#[test]
|
| 522 |
|
- |
fn verify_v2_rejects_wrong_method() {
|
|
367 |
+ |
fn verify_rejects_wrong_method() {
|
| 523 |
368 |
|
let sig = v2("s", "1000", "GET", "/internal/x", "abc", b"body");
|
| 524 |
369 |
|
assert!(verify_signed_request(
|
| 525 |
370 |
|
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 1000
|
| 528 |
373 |
|
}
|
| 529 |
374 |
|
|
| 530 |
375 |
|
#[test]
|
| 531 |
|
- |
fn verify_v2_rejects_wrong_path() {
|
|
376 |
+ |
fn verify_rejects_wrong_path() {
|
| 532 |
377 |
|
let sig = v2("s", "1000", "POST", "/internal/a", "abc", b"body");
|
| 533 |
378 |
|
assert!(verify_signed_request(
|
| 534 |
379 |
|
"s", Some("1000"), Some(&sig), "POST", "/internal/b", Some("abc"), b"body", 1000
|
| 537 |
382 |
|
}
|
| 538 |
383 |
|
|
| 539 |
384 |
|
#[test]
|
| 540 |
|
- |
fn verify_v2_rejects_wrong_nonce() {
|
|
385 |
+ |
fn verify_rejects_wrong_nonce() {
|
| 541 |
386 |
|
let sig = v2("s", "1000", "POST", "/internal/x", "abc", b"body");
|
| 542 |
387 |
|
assert!(verify_signed_request(
|
| 543 |
388 |
|
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("zzz"), b"body", 1000
|
| 546 |
391 |
|
}
|
| 547 |
392 |
|
|
| 548 |
393 |
|
#[test]
|
| 549 |
|
- |
fn verify_v2_freshness_still_enforced() {
|
|
394 |
+ |
fn verify_at_window_boundary_accepts_inside_rejects_outside() {
|
|
395 |
+ |
// Asymmetric window: up to MAX_TIMESTAMP_AGE_SECS (60s) old, but only
|
|
396 |
+ |
// MAX_FUTURE_SKEW_SECS (5s) into the future. `>` is strict, so exactly
|
|
397 |
+ |
// at each boundary is accepted.
|
|
398 |
+ |
let sig = v2("s", "1000", "POST", "/x", "abc", b"abc");
|
|
399 |
+ |
let check = |now| verify_signed_request(
|
|
400 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/x", Some("abc"), b"abc", now,
|
|
401 |
+ |
);
|
|
402 |
+ |
assert!(check(1060).is_ok(), "now-ts=60 accepted (age boundary)");
|
|
403 |
+ |
assert!(check(1061).is_err(), "now-ts=61 rejected (too old)");
|
|
404 |
+ |
assert!(check(995).is_ok(), "ts-now=5 accepted (future-skew boundary)");
|
|
405 |
+ |
assert!(check(994).is_err(), "ts-now=6 rejected (too far future)");
|
|
406 |
+ |
}
|
|
407 |
+ |
|
|
408 |
+ |
#[test]
|
|
409 |
+ |
fn verify_rejects_missing_nonce() {
|
|
410 |
+ |
// A request with no nonce is rejected outright — no v1 downgrade exists.
|
| 550 |
411 |
|
let sig = v2("s", "1000", "POST", "/internal/x", "abc", b"body");
|
| 551 |
|
- |
let (_, msg) = verify_signed_request(
|
| 552 |
|
- |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", Some("abc"), b"body", 9999,
|
|
412 |
+ |
let (status, msg) = verify_signed_request(
|
|
413 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/internal/x", None, b"body", 1000,
|
| 553 |
414 |
|
)
|
| 554 |
415 |
|
.unwrap_err();
|
|
416 |
+ |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
|
417 |
+ |
assert!(msg.contains("Nonce"), "expected nonce msg, got: {msg}");
|
|
418 |
+ |
}
|
|
419 |
+ |
|
|
420 |
+ |
#[test]
|
|
421 |
+ |
fn verify_rejects_missing_timestamp_header() {
|
|
422 |
+ |
let sig = v2("s", "1000", "POST", "/x", "abc", b"abc");
|
|
423 |
+ |
let (status, msg) = verify_signed_request(
|
|
424 |
+ |
"s", None, Some(&sig), "POST", "/x", Some("abc"), b"abc", 1000,
|
|
425 |
+ |
)
|
|
426 |
+ |
.unwrap_err();
|
|
427 |
+ |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
| 555 |
428 |
|
assert!(msg.contains("Timestamp"));
|
| 556 |
429 |
|
}
|
| 557 |
430 |
|
|
| 558 |
431 |
|
#[test]
|
| 559 |
|
- |
fn verify_without_nonce_falls_back_to_v1() {
|
| 560 |
|
- |
// A request with no nonce is verified against the legacy v1 message
|
| 561 |
|
- |
// (timestamp+body), ignoring method/path — the dual-accept path.
|
| 562 |
|
- |
let v1_sig = compute_internal_signature("s", "1000", b"body");
|
| 563 |
|
- |
assert!(verify_signed_request(
|
| 564 |
|
- |
"s", Some("1000"), Some(&v1_sig), "POST", "/internal/x", None, b"body", 1000
|
|
432 |
+ |
fn verify_rejects_missing_signature_header() {
|
|
433 |
+ |
let (status, msg) = verify_signed_request(
|
|
434 |
+ |
"s", Some("1000"), None, "POST", "/x", Some("abc"), b"abc", 1000,
|
| 565 |
435 |
|
)
|
| 566 |
|
- |
.is_ok());
|
|
436 |
+ |
.unwrap_err();
|
|
437 |
+ |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
|
438 |
+ |
assert!(msg.contains("Signature"));
|
|
439 |
+ |
}
|
|
440 |
+ |
|
|
441 |
+ |
#[test]
|
|
442 |
+ |
fn verify_rejects_unparseable_timestamp() {
|
|
443 |
+ |
let (status, msg) = verify_signed_request(
|
|
444 |
+ |
"s", Some("not-an-int"), Some("zz"), "POST", "/x", Some("abc"), b"", 1000,
|
|
445 |
+ |
)
|
|
446 |
+ |
.unwrap_err();
|
|
447 |
+ |
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
|
448 |
+ |
assert!(msg.contains("Invalid timestamp"));
|
|
449 |
+ |
}
|
|
450 |
+ |
|
|
451 |
+ |
#[test]
|
|
452 |
+ |
fn verify_check_order_nonce_before_timestamp() {
|
|
453 |
+ |
// Nonce mandatory: a missing nonce rejects even with all else missing.
|
|
454 |
+ |
let (_, msg) = verify_signed_request("s", None, None, "POST", "/x", None, b"", 1000)
|
|
455 |
+ |
.unwrap_err();
|
|
456 |
+ |
assert!(msg.contains("Nonce"), "expected nonce msg first, got: {msg}");
|
|
457 |
+ |
}
|
|
458 |
+ |
|
|
459 |
+ |
#[test]
|
|
460 |
+ |
fn verify_check_order_freshness_before_signature() {
|
|
461 |
+ |
// A stale timestamp must reject even when the sig is otherwise valid —
|
|
462 |
+ |
// catches a mutation running the freshness check after signature verify.
|
|
463 |
+ |
let sig = v2("s", "1000", "POST", "/x", "abc", b"abc");
|
|
464 |
+ |
let (_, msg) = verify_signed_request(
|
|
465 |
+ |
"s", Some("1000"), Some(&sig), "POST", "/x", Some("abc"), b"abc", 9999,
|
|
466 |
+ |
)
|
|
467 |
+ |
.unwrap_err();
|
|
468 |
+ |
assert!(msg.contains("Timestamp"), "expected freshness msg, got: {msg}");
|
| 567 |
469 |
|
}
|
| 568 |
470 |
|
|
| 569 |
471 |
|
#[test]
|