Skip to main content

trust_tasks_https/
status.rs

1//! Mapping from framework error codes to HTTP status codes.
2//!
3//! The mapping is informative — SPEC.md leaves transport-level signalling
4//! to the binding. The authority for this table is the HTTPS binding
5//! specification's §4 status mapping (`bindings/https/0.2/spec.md`); a
6//! client that cannot or does not want to parse the body can still
7//! classify the response broadly.
8//!
9//! | Standard code        | HTTP status |
10//! |----------------------|-------------|
11//! | `malformedRequest`   | 400 Bad Request |
12//! | `permissionDenied`   | 403 Forbidden |
13//! | `unsupportedType`    | 422 Unprocessable Entity |
14//! | `unsupportedVersion` | 422 Unprocessable Entity |
15//! | `expired`            | 422 Unprocessable Entity |
16//! | `proofRequired`      | 422 Unprocessable Entity |
17//! | `proofInvalid`       | 422 Unprocessable Entity |
18//! | `identityMismatch`   | 422 Unprocessable Entity |
19//! | `wrongRecipient`     | 422 Unprocessable Entity |
20//! | `cancelled`          | 422 Unprocessable Entity |
21//! | `taskFailed`         | 422 Unprocessable Entity |
22//! | `idConflict`         | 409 Conflict |
23//! | `unavailable`        | 503 Service Unavailable |
24//! | `internalError`      | 500 Internal Server Error |
25//!
26//! Two outcomes have no framework error code and so no row here: a duplicate
27//! of an execution still in progress (`202 Accepted`) and a duplicate for
28//! which no response was retained (`204 No Content`). They are successes, not
29//! rejections — see the binding spec's §5.1 — and the server emits them
30//! directly rather than through this table.
31//!
32//! The flatness of the 422 bucket is the point, not an oversight. An
33//! earlier version of this table split `proofRequired` / `proofInvalid`
34//! into 401 and `permissionDenied` / `wrongRecipient` / `identityMismatch`
35//! into 403 — which handed an unauthenticated prober a status-code oracle
36//! distinguishing "your proof is bad" from "you are not who you said" from
37//! "this is not addressed to me", without ever reading a body. Collapsing
38//! them to one status removes that signal; the framework error code in the
39//! body remains available to a legitimate producer, which is the party
40//! entitled to it. `permissionDenied` keeps 403 because the binding's table
41//! says so: it is the one outcome that reports on the *authenticated*
42//! caller's authorization, and so tells an unauthenticated prober nothing.
43//!
44//! Extension codes (`<slug>:<local>`) default to **422 Unprocessable
45//! Entity** since they are spec-defined application-layer failures.
46
47use trust_tasks_rs::{StandardCode, TrustTaskCode};
48
49/// Map a [`TrustTaskCode`] to a numeric HTTP status code.
50///
51/// Returns `u16` rather than `http::StatusCode` so the function is
52/// dependency-light and usable from both the server (axum) and client
53/// (reqwest) sides without dragging in extra crates here.
54pub fn status_for_code(code: &TrustTaskCode) -> u16 {
55    match code {
56        TrustTaskCode::Standard(c) => standard_status(*c),
57        TrustTaskCode::Extended { .. } => 422,
58    }
59}
60
61fn standard_status(code: StandardCode) -> u16 {
62    match code {
63        StandardCode::MalformedRequest => 400,
64        StandardCode::PermissionDenied => 403,
65        StandardCode::IdConflict => 409,
66        // The flat 422 bucket of the binding spec's §4 table. Everything
67        // here is "understood, well-formed, and refused"; keeping them
68        // indistinguishable at the status line is deliberate — see the
69        // module docs for the oracle the finer split created.
70        StandardCode::UnsupportedType
71        | StandardCode::UnsupportedVersion
72        | StandardCode::Expired
73        | StandardCode::ProofRequired
74        | StandardCode::ProofInvalid
75        | StandardCode::WrongRecipient
76        | StandardCode::IdentityMismatch
77        | StandardCode::Cancelled
78        | StandardCode::TaskFailed
79        | StandardCode::UnsupportedExtension => 422,
80        StandardCode::Unavailable | StandardCode::Indeterminate => 503,
81        StandardCode::InternalError => 500,
82        // `StandardCode` is `#[non_exhaustive]` (trust-tasks-rs 0.7.0): a
83        // framework revision can add a code without breaking this crate. An
84        // unmapped code is a server-side failure to keep up, not a client
85        // error, so it maps to 500 rather than being silently bucketed as 400.
86        _ => 500,
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    /// REGRESSION: the table must match `bindings/https/0.2/spec.md` §4.
95    /// Before this was fixed, `proofRequired`/`proofInvalid` mapped to 401,
96    /// `permissionDenied`/`wrongRecipient`/`identityMismatch` to 403, and
97    /// `unsupportedType`/`unsupportedVersion`/`expired` to 400 — three rows
98    /// of drift from the binding, and a status-code identity oracle.
99    #[test]
100    fn standard_codes_map_to_binding_spec_statuses() {
101        assert_eq!(status_for_code(&StandardCode::MalformedRequest.into()), 400);
102        assert_eq!(status_for_code(&StandardCode::PermissionDenied.into()), 403);
103        assert_eq!(status_for_code(&StandardCode::IdConflict.into()), 409);
104        assert_eq!(status_for_code(&StandardCode::UnsupportedType.into()), 422);
105        assert_eq!(
106            status_for_code(&StandardCode::UnsupportedVersion.into()),
107            422
108        );
109        assert_eq!(status_for_code(&StandardCode::Expired.into()), 422);
110        assert_eq!(status_for_code(&StandardCode::ProofRequired.into()), 422);
111        assert_eq!(status_for_code(&StandardCode::ProofInvalid.into()), 422);
112        assert_eq!(status_for_code(&StandardCode::WrongRecipient.into()), 422);
113        assert_eq!(status_for_code(&StandardCode::IdentityMismatch.into()), 422);
114        assert_eq!(status_for_code(&StandardCode::Cancelled.into()), 422);
115        assert_eq!(status_for_code(&StandardCode::TaskFailed.into()), 422);
116        assert_eq!(status_for_code(&StandardCode::Unavailable.into()), 503);
117        assert_eq!(status_for_code(&StandardCode::InternalError.into()), 500);
118        assert_eq!(
119            status_for_code(&StandardCode::UnsupportedExtension.into()),
120            422
121        );
122        assert_eq!(status_for_code(&StandardCode::Indeterminate.into()), 503);
123    }
124
125    /// REGRESSION: the codes that report on identity or attribution must be
126    /// indistinguishable at the status line. Asserted as a set property so a
127    /// future edit that re-splits one of them out fails here, not only in
128    /// the per-code table above.
129    #[test]
130    fn identity_related_codes_share_one_status() {
131        let identity_codes = [
132            StandardCode::ProofRequired,
133            StandardCode::ProofInvalid,
134            StandardCode::IdentityMismatch,
135            StandardCode::WrongRecipient,
136        ];
137        for code in identity_codes {
138            assert_eq!(
139                status_for_code(&code.into()),
140                422,
141                "{code:?} must not be distinguishable by status code"
142            );
143        }
144    }
145
146    #[test]
147    fn extension_codes_default_to_422() {
148        let code = TrustTaskCode::Extended {
149            slug: "acl/grant".into(),
150            local: "lastAuthorityProtected".into(),
151        };
152        assert_eq!(status_for_code(&code), 422);
153    }
154}