Skip to content

Commit 77fabf3

Browse files
Duncanwpfleger96
andcommitted
fix(media): align evidence_rejected status to NIP-FI spec table (401→403)
The NIP-FI rejection table (§Public denial classes and transport codes) maps `evidence_rejected` — malformed, invalid, OR expired evidence — to HTTP 403. The previous code mapped signature failures, expired tokens, missing tags, hash/server mismatches, etc. to 401, matching a now-removed oracle-enumeration rationale that the spec does not make. Changes: - Merge the two `IntoResponse` denial branches into one 403 arm covering all structurally present but invalid/malformed/expired proofs. Only absent-header (`MissingAuth`) remains 401. - Update the body string from "authentication failed" / "authorization denied" to the spec's fixed string "evidence rejected" / "authentication required" for the respective classes. - Fix the misleading IntoResponse comment and the now-stale test section header. - Update the `evidence_rejected_errors_permissive_shape_is_json_401` unit test → `evidence_rejected_errors_return_json_403`; add `InvalidAuthKind`, `InvalidAuthVerb`, `InvalidAuthEvent` to coverage. - Fix all five `test_auth_*` assertions in e2e_media_extended.rs: wrong kind, missing t tag, missing expiration, expired token, empty content all expect 403 (not 401). Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent fbd3c93 commit 77fabf3

2 files changed

Lines changed: 32 additions & 35 deletions

File tree

‎crates/buzz-media/src/error.rs‎

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -171,32 +171,28 @@ impl IntoResponse for MediaError {
171171
Self::FileTooLarge { .. } | Self::ImageTooLarge => {
172172
(StatusCode::PAYLOAD_TOO_LARGE, self.to_string())
173173
}
174-
// NIP-FI denial-class split (NIP-FI §Transport and cardinality):
174+
// NIP-FI rejection table (§Public denial classes and transport codes):
175175
// missing_evidence (401) — Authorization header absent
176-
// evidence_rejected (403) — wrong scheme, malformed, duplicate, or
177-
// otherwise structurally invalid proof
176+
// evidence_rejected (403) — structurally present but invalid, malformed,
177+
// expired, or otherwise unacceptable proof
178178
//
179-
// All other authentication failures (signature invalid, expired,
180-
// timestamp out of window, hash mismatch, etc.) return 401 to
181-
// prevent oracle enumeration — they are indistinguishable from an
182-
// absent identity to an unauthenticated caller.
179+
// The spec explicitly maps expired tokens, missing tags, signature failures,
180+
// hash/server mismatches, etc. to `evidence_rejected` → 403. 401 is reserved
181+
// solely for the absent-header case.
183182
Self::MissingAuth => {
184183
tracing::warn!(error = %self, "authentication failed: missing evidence");
185184
(
186185
StatusCode::UNAUTHORIZED,
187-
"authentication failed".to_string(),
186+
"authentication required".to_string(),
188187
)
189188
}
190189
Self::InvalidAuthScheme
191190
| Self::InvalidBase64
192191
| Self::InvalidAuthEvent
193192
| Self::InvalidAuthKind
194193
| Self::InvalidAuthVerb
195-
| Self::DuplicateTag(_) => {
196-
tracing::warn!(error = %self, "authentication failed: evidence rejected");
197-
(StatusCode::FORBIDDEN, "authorization denied".to_string())
198-
}
199-
Self::InvalidSignature
194+
| Self::DuplicateTag(_)
195+
| Self::InvalidSignature
200196
| Self::TokenExpired
201197
| Self::TimestampOutOfWindow
202198
| Self::Unauthorized
@@ -205,11 +201,8 @@ impl IntoResponse for MediaError {
205201
| Self::HashMismatch
206202
| Self::ServerMismatch
207203
| Self::MissingTag(_) => {
208-
tracing::warn!(error = %self, "authentication failed");
209-
(
210-
StatusCode::UNAUTHORIZED,
211-
"authentication failed".to_string(),
212-
)
204+
tracing::warn!(error = %self, "authentication failed: evidence rejected");
205+
(StatusCode::FORBIDDEN, "evidence rejected".to_string())
213206
}
214207
Self::InsufficientScope => (StatusCode::FORBIDDEN, self.to_string()),
215208
Self::RelayMembershipRequired | Self::CommunityWriteFenced => {
@@ -295,9 +288,9 @@ mod tests {
295288
}
296289
}
297290

298-
// ── Legacy IntoResponse shapes (Permissive regression pins) ─────────────
299-
// These pins ensure MediaError::into_response() keeps the old JSON 401
300-
// shape so Off-mode deployments remain unaffected [FI-INV-15].
291+
// ── IntoResponse status code pins ──────────────────────────────────────
292+
// These pins ensure MediaError::into_response() maps each error class to
293+
// the correct HTTP status per the NIP-FI rejection table.
301294

302295
#[test]
303296
fn missing_auth_permissive_shape_is_json_401() {
@@ -321,21 +314,25 @@ mod tests {
321314
}
322315

323316
#[test]
324-
fn evidence_rejected_errors_permissive_shape_is_json_401() {
325-
// In Permissive mode, proof failures also return JSON 401 (no 403 distinction).
317+
fn evidence_rejected_errors_return_json_403() {
318+
// NIP-FI §Public denial classes: `evidence_rejected` maps to HTTP 403.
319+
// This covers all structurally present but invalid/malformed/expired proofs.
326320
for error in [
327321
MediaError::InvalidSignature,
328322
MediaError::TokenExpired,
329323
MediaError::HashMismatch,
330324
MediaError::ServerMismatch,
331325
MediaError::MissingTag("server"),
326+
MediaError::InvalidAuthKind,
327+
MediaError::InvalidAuthVerb,
328+
MediaError::InvalidAuthEvent,
332329
] {
333330
let label = format!("{error:?}");
334331
let resp = error.into_response();
335332
assert_eq!(
336333
resp.status(),
337-
StatusCode::UNAUTHORIZED,
338-
"Permissive: expected 401 for {label}"
334+
StatusCode::FORBIDDEN,
335+
"expected 403 for {label}"
339336
);
340337
let ct = resp
341338
.headers()

‎crates/buzz-test-client/tests/e2e_media_extended.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ async fn test_auth_wrong_kind() {
278278
],
279279
);
280280
let resp = upload_with_auth(&client, &auth, &sha256, &jpeg).await;
281-
assert_eq!(resp.status(), 401, "wrong kind must be 401");
282-
println!("✅ Wrong kind → 401");
281+
assert_eq!(resp.status(), 403, "wrong kind must be 403");
282+
println!("✅ Wrong kind → 403");
283283
}
284284

285285
#[tokio::test]
@@ -300,8 +300,8 @@ async fn test_auth_missing_t_tag() {
300300
],
301301
);
302302
let resp = upload_with_auth(&client, &auth, &sha256, &jpeg).await;
303-
assert_eq!(resp.status(), 401, "missing t tag must be 401");
304-
println!("✅ Missing t tag → 401");
303+
assert_eq!(resp.status(), 403, "missing t tag must be 403");
304+
println!("✅ Missing t tag → 403");
305305
}
306306

307307
#[tokio::test]
@@ -321,8 +321,8 @@ async fn test_auth_missing_expiration() {
321321
],
322322
);
323323
let resp = upload_with_auth(&client, &auth, &sha256, &jpeg).await;
324-
assert_eq!(resp.status(), 401, "missing expiration must be 401");
325-
println!("✅ Missing expiration → 401");
324+
assert_eq!(resp.status(), 403, "missing expiration must be 403");
325+
println!("✅ Missing expiration → 403");
326326
}
327327

328328
#[tokio::test]
@@ -344,8 +344,8 @@ async fn test_auth_expired_token() {
344344
],
345345
);
346346
let resp = upload_with_auth(&client, &auth, &sha256, &jpeg).await;
347-
assert_eq!(resp.status(), 401, "expired token must be 401");
348-
println!("✅ Expired token → 401");
347+
assert_eq!(resp.status(), 403, "expired token must be 403");
348+
println!("✅ Expired token → 403");
349349
}
350350

351351
#[tokio::test]
@@ -367,8 +367,8 @@ async fn test_auth_empty_content() {
367367
],
368368
);
369369
let resp = upload_with_auth(&client, &auth, &sha256, &jpeg).await;
370-
assert_eq!(resp.status(), 401, "empty content must be 401");
371-
println!("✅ Empty content → 401");
370+
assert_eq!(resp.status(), 403, "empty content must be 403");
371+
println!("✅ Empty content → 403");
372372
}
373373

374374
#[tokio::test]

0 commit comments

Comments
 (0)