Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/origin-src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ zstd.workspace = true
divan.workspace = true
# The benchmarks build their working trees with the fixtures.
origin-src = { workspace = true, features = ["fixture"] }
# Requests the SDK never sends, to test how the server refuses them.
reqwest.workspace = true
tempfile.workspace = true

[[bench]]
Expand Down
63 changes: 61 additions & 2 deletions apps/origin-src/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

use std::sync::Arc;

use axum::Json;
use axum::body::Bytes;
use axum::extract::{DefaultBodyLimit, FromRequestParts, Path, Query, State};
use axum::extract::rejection::{JsonRejection, PathRejection, QueryRejection};
use axum::extract::{DefaultBodyLimit, FromRequest, FromRequestParts, Request, State};
use axum::http::request::Parts;
use axum::http::{StatusCode, header};
use axum::response::{IntoResponse, Response};
Expand All @@ -26,6 +26,7 @@ use origin_shared::api::{
use origin_shared::event::Page;
use origin_shared::landing::Pending;
use origin_shared::pack::{Pack, PackError, Part};
use serde::Serialize;
use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter;
use utoipa_axum::routes;
Expand Down Expand Up @@ -110,6 +111,56 @@ impl FromRequestParts<Arc<Repositories>> for Caller {
}
}

/// Declares extractors like axum's, of the same names, so `utoipa` documents
/// them alike, but refusing a request as an [`ApiError`], as every other
/// failure is, rather than in axum's plain text.
macro_rules! extractors {
($(
$(#[$doc:meta])*
$name:ident: $trait:ident::$method:ident($input:ty)
via $($axum:ident)::+ refusing with $rejection:ty;
)*) => {$(
$(#[$doc])*
struct $name<T>(T);

impl<S: Send + Sync, T> $trait<S> for $name<T>
where
$($axum)::+<T>: $trait<S, Rejection = $rejection>,
{
type Rejection = HttpError;

async fn $method(input: $input, state: &S) -> Result<Self, HttpError> {
let $($axum)::+(value) = <$($axum)::+<T>>::$method(input, state).await?;
Ok(Self(value))
}
}

impl From<$rejection> for HttpError {
fn from(rejection: $rejection) -> Self {
Self::Rejected(rejection.status(), rejection.body_text())
}
}
)*};
}

extractors! {
/// The request's path parameters.
Path: FromRequestParts::from_request_parts(&mut Parts)
via axum::extract::Path refusing with PathRejection;
/// The request's query.
Query: FromRequestParts::from_request_parts(&mut Parts)
via axum::extract::Query refusing with QueryRejection;
/// The request's JSON body; also a JSON response.
Json: FromRequest::from_request(Request)
via axum::Json refusing with JsonRejection;
}

impl<T: Serialize> IntoResponse for Json<T> {
fn into_response(self) -> Response {
axum::Json(self.0).into_response()
}
}

/// Runs a call on the service of repository `repo`, on the blocking thread
/// pool: service calls do synchronous file system work, and event
/// long-polls wait.
Expand All @@ -133,6 +184,9 @@ pub enum HttpError {
Pack(PackError),
/// The caller's credentials are invalid.
Unauthorized(InvalidToken),
/// axum refused the request's path, query or body, with this status:
/// a client error but for a route that doesn't match its handler.
Rejected(StatusCode, String),
/// The service panicked.
Panicked(String),
}
Expand All @@ -147,6 +201,11 @@ impl IntoResponse for HttpError {
Self::Service(error) => (error.code(), error.to_string()),
Self::Pack(error) => (ErrorCode::BadRequest, format!("invalid pack: {error}")),
Self::Unauthorized(error) => (ErrorCode::Unauthorized, error.to_string()),
Self::Rejected(status, message) if status.is_server_error() => {
tracing::error!(%message, "request rejected");
(ErrorCode::Internal, "internal server error".to_owned())
}
Self::Rejected(_, message) => (ErrorCode::BadRequest, message),
Self::Panicked(message) => {
tracing::error!(%message, "request panicked");
(ErrorCode::Internal, "internal server error".to_owned())
Expand Down
26 changes: 23 additions & 3 deletions apps/origin-src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::time::{Duration, Instant};

use origin_client::{Client, Error, Repo};
use origin_shared::api::{
BrowseQuery, ErrorCode, EventsQuery, Expected, FetchRecords, FetchSnapshot, InventoryRequest,
LandRequest, List, LogQuery, NewTarget, PromoteRequest, PutQuery, RepoName, StatementsQuery,
TokenRequest,
ApiError, BrowseQuery, ErrorCode, EventsQuery, Expected, FetchRecords, FetchSnapshot,
InventoryRequest, LandRequest, List, LogQuery, NewTarget, PromoteRequest, PutQuery, RepoName,
StatementsQuery, TokenRequest,
};
use origin_shared::event::EventKind;
use origin_shared::landing::{Blocked, Landing, Proposal};
Expand Down Expand Up @@ -1135,6 +1135,26 @@ fn review(subject: RevisionId) -> EncodedObject {
.encode()
}

/// Requests axum's extractors refuse, for a malformed query or body, are
/// refused as an `ApiError`, as every other failure is.
#[tokio::test]
async fn refuses_malformed_requests_as_api_errors() {
let server = Server::start().await;
let http = reqwest::Client::new();
let repo = format!("{}/api/v1/repos/demo", server.base);
for request in [
http.get(format!("{repo}/events?wait=soon")),
http.post(format!("{repo}/changes/inventory"))
.header("content-type", "application/json")
.body("{"),
] {
let response = request.send().await.unwrap();
assert_eq!(response.status(), reqwest::StatusCode::BAD_REQUEST);
let error: ApiError = response.json().await.unwrap();
assert_eq!(error.code, ErrorCode::BadRequest, "{error}");
}
}

#[tokio::test]
async fn serves_its_openapi_document() {
let server = Server::start().await;
Expand Down
Loading