fix(httpsign): recover original url in axum nested routes
All checks were successful
/ build (push) Successful in 11m12s

This commit is contained in:
əlemi 2025-02-02 13:45:30 +01:00
parent 3210a3a2d5
commit 95bb2e60dc
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 20 additions and 10 deletions

2
Cargo.lock generated
View file

@ -1720,7 +1720,7 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "httpsign"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"axum 0.8.1",
"base64",

View file

@ -1,6 +1,6 @@
[package]
name = "httpsign"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = [ "alemi <me@alemi.dev>" ]
description = "fediverse-friendly implementation of http signaures in rust"

View file

@ -85,16 +85,26 @@ impl HttpSignature {
#[cfg(feature = "axum")]
pub fn build_from_parts(&mut self, parts: &axum::http::request::Parts) -> &mut Self {
let mut out = Vec::new();
let method = parts.method.to_string().to_lowercase();
// since we're using nested routes, the request uri gets trimmed at each nesting
// this breaks http signatures! we need to maintain the original uri, so we try extracting it
let uri = match parts.extensions.get::<axum::extract::OriginalUri>() {
Some(original) => original.path_and_query(),
None => parts.uri.path_and_query(),
}
.map(|x| x.as_str())
.unwrap_or("/");
for header in self.headers.iter() {
match header.as_str() {
"(request-target)" => out.push(
format!(
"(request-target): {} {}",
parts.method.to_string().to_lowercase(),
parts.uri.path_and_query().map(|x| x.as_str()).unwrap_or("/")
)
),
// TODO other pseudo-headers,
// pseudo-headers
"(request-target)" => out.push(format!("(request-target): {method} {uri}")),
// TODO handle other pseudo-headers,
// normal headers
_ => out.push(format!("{}: {}",
header.to_lowercase(),
parts.headers.get(header).map(|x| x.to_str().unwrap_or("")).unwrap_or("")