Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC of introducing FromSpoofableRequestParts and Spoofable #3001

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
34 changes: 34 additions & 0 deletions axum-extra/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ mod optional_path;
pub mod rejection;
mod with_rejection;

/// Private mod, public trait trick
mod spoof {
pub trait FromSpoofableRequestParts<S>: Sized {
type Rejection: axum::response::IntoResponse;

fn from_request_parts(
parts: &mut axum::http::request::Parts,
state: &S,
) -> impl std::future::Future<Output = Result<Self, Self::Rejection>> + Send;
}
}

/// Wrap spoofable extractor
#[derive(Debug)]
pub struct Spoofable<E>(pub E);

/// Allow `Spoofable` to be used with spoofable extractors in handlers
impl<S, E> FromRequestParts<S> for Spoofable<E>
where
E: spoof::FromSpoofableRequestParts<S>,
S: Sync,
{
type Rejection = E::Rejection;

async fn from_request_parts(
parts: &mut axum::http::request::Parts,
state: &S,
) -> Result<Self, Self::Rejection> {
E::from_request_parts(parts, state).await.map(Spoofable)
}
}

#[cfg(feature = "form")]
mod form;

Expand All @@ -24,6 +56,8 @@ pub mod multipart;
#[cfg(feature = "scheme")]
mod scheme;

use axum::extract::FromRequestParts;

pub use self::{
cached::Cached, host::Host, optional_path::OptionalPath, with_rejection::WithRejection,
};
Expand Down
11 changes: 4 additions & 7 deletions axum-extra/src/extract/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Extractor that parses the scheme of a request.
//! See [`Scheme`] for more details.

use axum::{
extract::FromRequestParts,
response::{IntoResponse, Response},
};
use axum::response::{IntoResponse, Response};
use http::{
header::{HeaderMap, FORWARDED},
request::Parts,
Expand Down Expand Up @@ -34,7 +31,7 @@ impl IntoResponse for SchemeMissing {
}
}

impl<S> FromRequestParts<S> for Scheme
impl<S> super::spoof::FromSpoofableRequestParts<S> for Scheme
where
S: Send + Sync,
{
Expand Down Expand Up @@ -83,12 +80,12 @@ fn parse_forwarded(headers: &HeaderMap) -> Option<&str> {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::TestClient;
use crate::{extract::Spoofable, test_helpers::TestClient};
use axum::{routing::get, Router};
use http::header::HeaderName;

fn test_client() -> TestClient {
async fn scheme_as_body(Scheme(scheme): Scheme) -> String {
async fn scheme_as_body(Spoofable(Scheme(scheme)): Spoofable<Scheme>) -> String {
scheme
}

Expand Down
13 changes: 13 additions & 0 deletions examples/spoofable-scheme/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "spoofable-scheme"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
axum = { path = "../../axum" }
axum-extra = { path = "../../axum-extra", features = ["scheme"] }
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

40 changes: 40 additions & 0 deletions examples/spoofable-scheme/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Example of application using spoofable extractors
//!
//! Run with
//!
//! ```not_rust
//! cargo run -p spoofable-scheme
//! ```
//!
//! Test with curl:
//!
//! ```not_rust
//! curl -i http://localhost:3000/ -H "X-Forwarded-Proto: http"
//! ```

use axum::{routing::get, Router};
use axum_extra::extract::{Scheme, Spoofable};
use tokio::net::TcpListener;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
)
.with(tracing_subscriber::fmt::layer())
.init();

// build our application with some routes
let app = Router::new().route("/", get(f));

let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}

async fn f(Spoofable(Scheme(scheme)): Spoofable<Scheme>) -> String {
scheme
}
Loading