Skip to main content

vti_common/
error.rs

1use affinidi_tdk::secrets_resolver::errors::SecretsResolverError;
2use axum::http::StatusCode;
3use axum::response::{IntoResponse, Response};
4use tracing::{debug, warn};
5
6#[derive(Debug, thiserror::Error)]
7pub enum AppError {
8    #[error("configuration error: {0}")]
9    Config(String),
10
11    #[error("io error: {0}")]
12    Io(#[from] std::io::Error),
13
14    #[error("store error: {0}")]
15    Store(#[from] fjall::Error),
16
17    #[error("serialization error: {0}")]
18    Serialization(#[from] serde_json::Error),
19
20    #[error("internal error: {0}")]
21    Internal(String),
22
23    #[error("secret store error: {0}")]
24    SecretStore(String),
25
26    #[error("not found: {0}")]
27    NotFound(String),
28
29    #[error("conflict: {0}")]
30    Conflict(String),
31
32    /// The resource existed but has been permanently, irreversibly consumed
33    /// or removed — rendered as **410 Gone**, distinct from [`Self::NotFound`]
34    /// (never existed / not visible to this caller) and [`Self::Conflict`]
35    /// (a transient state mismatch a retry or different request could
36    /// resolve). Canonical use: the TEE Mode B bootstrap carve-out after it
37    /// has been claimed — a second `/bootstrap/request` cannot succeed no
38    /// matter what the caller sends, ever again for this VTA. The SDK's
39    /// `vta_sdk::error::VtaError::Gone` mirrors this on the client side
40    /// (`from_http` maps 410 → `Gone`) with an operator-facing hint.
41    #[error("gone: {0}")]
42    Gone(String),
43
44    #[error("secrets error: {0}")]
45    Secrets(#[from] SecretsResolverError),
46
47    #[error("authentication error: {0}")]
48    Authentication(String),
49
50    #[error("unauthorized: {0}")]
51    Unauthorized(String),
52
53    #[error("forbidden: {0}")]
54    Forbidden(String),
55
56    /// Operation requires a stepped-up (`acr=aal2`) session, but
57    /// the caller's JWT carries a lower acr (typically `aal1`).
58    /// Distinct from [`Self::Forbidden`] so wallets can react —
59    /// `step_up_required` is the operator-friendly signal to
60    /// trigger a passkey-login or VTA-approval ceremony, not a
61    /// hard rejection.
62    ///
63    /// Rendered as **403 Forbidden** with body
64    /// `{ "error": "step_up_required", "message": "...",
65    ///   "requiredAcr": "aal2" }` so clients can distinguish it
66    /// from a role-based rejection without parsing English.
67    #[error("step-up required: {0}")]
68    StepUpRequired(String),
69
70    /// The Policy Decision Point refused the request until an approval is
71    /// obtained, and is handing back what obtaining it requires.
72    ///
73    /// Distinct from [`Self::StepUpRequired`] because that variant can only say
74    /// *that* elevation is needed — it renders `{error, message, requiredAcr}`
75    /// and has nowhere to put the `approveRequest` the caller must get signed,
76    /// nor any way to express a consent requirement (approver set, threshold,
77    /// challenge). A REST caller receiving it could learn it was blocked but
78    /// not what to do about it, while the trust-task caller for the very same
79    /// decision received the full document. That asymmetry is what this closes.
80    ///
81    /// `code` is the stable machine-readable reason (`auth:step_up_required`,
82    /// `auth:consent_required`) — the field a client keys on, rather than the
83    /// HTTP status or the English message. `details` is merged into the body,
84    /// so the REST response carries exactly what the trust-task reject's
85    /// `details` carries.
86    ///
87    /// Rendered as **403 Forbidden**: the request was understood and the caller
88    /// authenticated; it is refused pending a decision they can still obtain.
89    #[error("approval required: {code}")]
90    ApprovalRequired {
91        code: &'static str,
92        details: serde_json::Value,
93    },
94
95    #[error("validation error: {0}")]
96    Validation(String),
97
98    /// The request did not carry a required `Trust-Task` header. Routes
99    /// registered via [`crate::trust_task::TrustTaskRouter::route_with_task`]
100    /// reject missing headers with this variant (400). Only `/health` is
101    /// allowed to omit it.
102    #[error("request is missing required Trust-Task header")]
103    TrustTaskMissing,
104
105    /// The request's `Trust-Task` header did not match the handler's
106    /// registered task. Returned as 415 per spec §16.2; the response body
107    /// carries the expected + received task URLs so clients can diagnose
108    /// without re-reading the route table.
109    #[error("Trust-Task header does not match handler (expected {expected})")]
110    TrustTaskMismatch {
111        expected: String,
112        received: Option<String>,
113    },
114
115    /// The supplied Trust-Task value was not a well-formed identifier
116    /// (empty, non-`https://`, or contained header-injection control
117    /// characters). Returned as 400.
118    #[error("malformed Trust-Task identifier: {0}")]
119    TrustTaskMalformed(String),
120
121    /// A request reused an `Idempotency-Key` it had previously sent
122    /// with a *different* body hash. The cached response is preserved
123    /// for the original requester; the conflicting retry is rejected
124    /// with 422 so clients don't silently get a stale response from a
125    /// drifting payload.
126    #[error("Idempotency-Key conflict: same key, different request body")]
127    IdempotencyKeyConflict,
128
129    /// A pagination cursor failed integrity verification — either the
130    /// HMAC tag didn't validate (tampered, forged, or signed under a
131    /// different community's audit_key) or the encoded form was
132    /// malformed. Returned as 400 with no extra detail so an attacker
133    /// can't learn whether their guessed cursor was structurally
134    /// close to a valid one.
135    #[error("invalid pagination cursor")]
136    InvalidCursor,
137
138    /// A bounded computation aborted because it hit a resource ceiling
139    /// (time/instruction budget or an input-size cap) before completing.
140    /// Used by the Rego policy evaluator to refuse pathological policies
141    /// or adversarial inputs on the unauthenticated join path rather than
142    /// burning CPU unbounded. Rendered as **503 Service Unavailable** — the
143    /// evaluation did not complete, and the message is generic so an
144    /// attacker can't probe the exact limits.
145    #[error("resource limit exceeded: {0}")]
146    ResourceExhausted(String),
147
148    /// Catch-all for service-specific errors (e.g., KeyDerivation, BadGateway, TeeAttestation).
149    /// Services create helper functions to construct these with appropriate status codes.
150    #[error("{message}")]
151    ServiceError { status: StatusCode, message: String },
152
153    /// An I/O failure in a vsock operation. Preserves the underlying
154    /// `std::io::Error` via `#[source]` while adding a human-readable
155    /// label of which operation failed (connect / read / write / flush).
156    ///
157    /// Construct via [`AppError::vsock`] for ergonomic `.map_err(...)`.
158    #[error("{operation} failed: {source}")]
159    Vsock {
160        operation: &'static str,
161        #[source]
162        source: std::io::Error,
163    },
164}
165
166impl AppError {
167    /// Build a closure suitable for `.map_err(...)` that wraps an
168    /// `std::io::Error` into [`AppError::Vsock`] with the given operation
169    /// label. Keeps the source chain intact for downstream error walkers
170    /// while giving log readers the operation name.
171    pub fn vsock(operation: &'static str) -> impl FnOnce(std::io::Error) -> AppError {
172        move |source| AppError::Vsock { operation, source }
173    }
174}
175
176/// Convert the canonical auth-flow errors into [`AppError`] so
177/// the route layer's existing `IntoResponse` plumbing renders
178/// them without backend-specific glue. Each variant lands on the
179/// HTTP status reflected in the [`crate::auth::AuthError`]
180/// doc-comments:
181///
182/// - `Forbidden`, `DidMethodRejected` → 403
183/// - `PendingChallengeLimitReached` → 429 via the Validation arm
184///   (route layer can return a typed 429 if needed; the canonical
185///   variant carries the rate-limit signal in the message).
186/// - `SessionNotFound`, `SessionStateMismatch`, `ChallengeMismatch`,
187///   `ChallengeExpired`, `SignerMismatch`, `StaleMessage`,
188///   `RefreshTokenInvalid`, `RefreshTokenExpired` → 401
189/// - `AttestationFailed` → 503 via Internal (TEE outages are not
190///   the caller's fault).
191/// - `Internal` → 500.
192impl From<crate::auth::backend::AuthError> for AppError {
193    fn from(e: crate::auth::backend::AuthError) -> Self {
194        use crate::auth::backend::AuthError as A;
195        match e {
196            A::Forbidden | A::DidMethodRejected => AppError::Forbidden(e.to_string()),
197            A::PendingChallengeLimitReached => AppError::Validation(e.to_string()),
198            // One message for every failure reachable *without* the
199            // subject's private key. These variants differ in ways a caller
200            // must not be able to observe: "session not found" versus
201            // "signer mismatch" tells a party probing identifiers whether
202            // the subject it named is enrolled here, which is precisely what
203            // `handle_challenge` stops disclosing at the other end of the
204            // flow. The variant is preserved for the log line; the caller is
205            // told that authentication failed.
206            A::SessionNotFound
207            | A::SessionStateMismatch
208            | A::ChallengeMismatch
209            | A::ChallengeExpired
210            | A::SignerMismatch
211            | A::StaleMessage
212            | A::RefreshTokenInvalid
213            | A::RefreshTokenExpired => {
214                tracing::debug!(reason = %e, "authentication failed");
215                AppError::Authentication("authentication failed".into())
216            }
217            A::AttestationFailed(msg) => AppError::Internal(format!("tee attestation: {msg}")),
218            A::Internal(msg) => AppError::Internal(msg),
219        }
220    }
221}
222
223impl IntoResponse for AppError {
224    fn into_response(self) -> Response {
225        let status = match &self {
226            AppError::Config(_) => StatusCode::INTERNAL_SERVER_ERROR,
227            AppError::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
228            AppError::Store(_) => StatusCode::INTERNAL_SERVER_ERROR,
229            AppError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
230            AppError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
231            AppError::SecretStore(_) => StatusCode::INTERNAL_SERVER_ERROR,
232            AppError::NotFound(_) => StatusCode::NOT_FOUND,
233            AppError::Conflict(_) => StatusCode::CONFLICT,
234            AppError::Gone(_) => StatusCode::GONE,
235            AppError::Secrets(_) => StatusCode::INTERNAL_SERVER_ERROR,
236            AppError::Authentication(_) => StatusCode::UNAUTHORIZED,
237            AppError::Unauthorized(_) => StatusCode::UNAUTHORIZED,
238            AppError::Forbidden(_) => StatusCode::FORBIDDEN,
239            AppError::StepUpRequired(_) => StatusCode::FORBIDDEN,
240            AppError::ApprovalRequired { .. } => StatusCode::FORBIDDEN,
241            AppError::Validation(_) => StatusCode::BAD_REQUEST,
242            AppError::TrustTaskMissing => StatusCode::BAD_REQUEST,
243            AppError::TrustTaskMismatch { .. } => StatusCode::UNSUPPORTED_MEDIA_TYPE,
244            AppError::TrustTaskMalformed(_) => StatusCode::BAD_REQUEST,
245            AppError::IdempotencyKeyConflict => StatusCode::UNPROCESSABLE_ENTITY,
246            AppError::InvalidCursor => StatusCode::BAD_REQUEST,
247            AppError::ResourceExhausted(_) => StatusCode::SERVICE_UNAVAILABLE,
248            AppError::ServiceError { status, .. } => *status,
249            AppError::Vsock { .. } => StatusCode::INTERNAL_SERVER_ERROR,
250        };
251
252        if status.is_server_error() {
253            warn!(status = %status.as_u16(), error = %self, "server error");
254        } else {
255            debug!(status = %status.as_u16(), error = %self, "client error");
256        }
257
258        // Trust-Task variants get structured payloads so clients can
259        // diagnose without re-reading the route table. Every other
260        // variant retains the existing `{ "error": "<display>" }` shape
261        // for backwards-compat with the workspace's existing consumers.
262        let body = match &self {
263            AppError::TrustTaskMissing => serde_json::json!({
264                "error": "TrustTaskMissing",
265                "message": self.to_string(),
266            }),
267            AppError::TrustTaskMismatch { expected, received } => serde_json::json!({
268                "error": "TrustTaskMismatch",
269                "message": self.to_string(),
270                "expected": expected,
271                "received": received,
272            }),
273            AppError::TrustTaskMalformed(value) => serde_json::json!({
274                "error": "TrustTaskMalformed",
275                "message": self.to_string(),
276                "received": value,
277            }),
278            AppError::IdempotencyKeyConflict => serde_json::json!({
279                "error": "IdempotencyKeyConflict",
280                "message": self.to_string(),
281            }),
282            AppError::StepUpRequired(msg) => serde_json::json!({
283                "error": "step_up_required",
284                "message": msg,
285                "requiredAcr": "aal2",
286            }),
287            // `details` is merged at the top level rather than nested, so the
288            // body reads the same as the trust-task reject's `details` object
289            // and a client can key on one shape across both transports. `error`
290            // is written last so a `details` carrying that key cannot displace
291            // the code the caller switches on.
292            AppError::ApprovalRequired { code, details } => {
293                let mut body = match details {
294                    serde_json::Value::Object(map) => map.clone(),
295                    _ => serde_json::Map::new(),
296                };
297                body.insert("error".to_string(), serde_json::json!(code));
298                serde_json::Value::Object(body)
299            }
300            _ => serde_json::json!({ "error": self.to_string() }),
301        };
302        (status, axum::Json(body)).into_response()
303    }
304}
305
306/// Helper to create a service-specific error for key derivation failures.
307pub fn key_derivation_error(msg: impl Into<String>) -> AppError {
308    AppError::ServiceError {
309        status: StatusCode::BAD_REQUEST,
310        message: format!("key derivation error: {}", msg.into()),
311    }
312}
313
314/// Helper to create a service-specific error for bad gateway responses.
315pub fn bad_gateway_error(msg: impl Into<String>) -> AppError {
316    AppError::ServiceError {
317        status: StatusCode::BAD_GATEWAY,
318        message: format!("bad gateway: {}", msg.into()),
319    }
320}
321
322/// Helper to create a service-specific error for TEE attestation failures.
323pub fn tee_attestation_error(msg: impl Into<String>) -> AppError {
324    AppError::ServiceError {
325        status: StatusCode::SERVICE_UNAVAILABLE,
326        message: format!("TEE attestation error: {}", msg.into()),
327    }
328}
329
330#[cfg(test)]
331mod approval_required_tests {
332    use super::*;
333    use axum::body::to_bytes;
334    use axum::response::IntoResponse;
335
336    async fn body_of(err: AppError) -> serde_json::Value {
337        let resp = err.into_response();
338        let bytes = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
339        serde_json::from_slice(&bytes).expect("json body")
340    }
341
342    /// The point of the variant: a REST caller must receive the same actionable
343    /// payload the trust-task caller gets, not merely "you were blocked".
344    #[tokio::test]
345    async fn details_are_merged_alongside_the_code() {
346        let body = body_of(AppError::ApprovalRequired {
347            code: "auth:step_up_required",
348            details: serde_json::json!({
349                "requiredAcr": "aal2",
350                "approveRequest": { "id": "urn:uuid:abc" },
351            }),
352        })
353        .await;
354
355        assert_eq!(body["error"], "auth:step_up_required");
356        assert_eq!(body["requiredAcr"], "aal2");
357        assert_eq!(body["approveRequest"]["id"], "urn:uuid:abc");
358    }
359
360    #[tokio::test]
361    async fn consent_details_survive_the_round_trip() {
362        let body = body_of(AppError::ApprovalRequired {
363            code: "auth:consent_required",
364            details: serde_json::json!({
365                "approverSet": "ops",
366                "minApprovals": 2,
367                "excludeRequester": true,
368            }),
369        })
370        .await;
371
372        assert_eq!(body["error"], "auth:consent_required");
373        assert_eq!(body["minApprovals"], 2);
374        assert_eq!(body["excludeRequester"], true);
375    }
376
377    /// `details` is assembled from a policy decision, so a stray `error` key in
378    /// it must not be able to displace the code a client switches on.
379    #[tokio::test]
380    async fn details_cannot_overwrite_the_code() {
381        let body = body_of(AppError::ApprovalRequired {
382            code: "auth:consent_required",
383            details: serde_json::json!({ "error": "allow", "approverSet": "ops" }),
384        })
385        .await;
386
387        assert_eq!(body["error"], "auth:consent_required");
388        assert_eq!(body["approverSet"], "ops");
389    }
390
391    /// A non-object `details` must still yield a well-formed body rather than
392    /// panicking or emitting a bare scalar.
393    #[tokio::test]
394    async fn a_non_object_details_still_renders_the_code() {
395        let body = body_of(AppError::ApprovalRequired {
396            code: "auth:step_up_required",
397            details: serde_json::Value::Null,
398        })
399        .await;
400
401        assert_eq!(body["error"], "auth:step_up_required");
402        assert!(body.as_object().is_some_and(|m| m.len() == 1));
403    }
404
405    #[tokio::test]
406    async fn renders_as_forbidden() {
407        let resp = AppError::ApprovalRequired {
408            code: "auth:consent_required",
409            details: serde_json::json!({}),
410        }
411        .into_response();
412        assert_eq!(resp.status(), StatusCode::FORBIDDEN);
413    }
414}
415
416#[cfg(test)]
417mod gone_tests {
418    use super::*;
419
420    /// The whole point of the variant: a consumed single-use resource (the
421    /// canonical example being the TEE Mode B bootstrap carve-out) must
422    /// render 410, not 403/409 — the SDK's `VtaError::from_http` keys its
423    /// `Gone` mapping off exactly this status code.
424    #[test]
425    fn renders_as_410_gone() {
426        let resp =
427            AppError::Gone("TEE first-boot carve-out has already been used".into()).into_response();
428        assert_eq!(resp.status(), StatusCode::GONE);
429    }
430
431    #[tokio::test]
432    async fn body_carries_the_message() {
433        use axum::body::to_bytes;
434
435        let resp = AppError::Gone("carve-out closed".into()).into_response();
436        let bytes = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
437        let body: serde_json::Value = serde_json::from_slice(&bytes).expect("json body");
438        assert_eq!(body["error"], "gone: carve-out closed");
439    }
440}