Skip to content

Commit

Permalink
Less allocations in flash impl (#627)
Browse files Browse the repository at this point in the history
* restructure cursiv cookie handling

* own cookie signing using blake3

* fix routes
  • Loading branch information
aumetra authored Jan 4, 2025
1 parent 8531dbb commit a32edf5
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 78 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ color-eyre = "0.6.3"
colored_json = "5.0.0"
const_format = "0.2.34"
const-oid = { version = "0.9.6", features = ["db"] }
cookie = { version = "0.18.1", features = [
"key-expansion",
"percent-encode",
"signed",
] }
cookie = { version = "0.18.1", features = ["percent-encode"] }
derive_builder = "0.20.2"
derive_more = { version = "1.0.0", features = ["from"] }
diesel = { version = "2.2.6", default-features = false, features = [
Expand Down
64 changes: 34 additions & 30 deletions kitsune/src/http/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ use tower_http_digest::VerifyDigestLayer;
use tower_stop_using_brave::StopUsingBraveLayer;
use tower_x_clacks_overhead::XClacksOverheadLayer;

#[allow(clippy::too_many_lines)]
pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Result<Router> {
let router = Router::new()
.route(
"/confirm-account/:confirmation_token",
"/confirm-account/{confirmation_token}",
routing::get(handler::confirm_account::get),
)
.route("/emojis/:id", routing::get(handler::custom_emojis::get))
.route("/media/:id", routing::get(handler::media::get))
.route("/emojis/{id}", routing::get(handler::custom_emojis::get))
.route("/media/{id}", routing::get(handler::media::get))
.route(
"/nodeinfo/2.1",
routing::get(handler::nodeinfo::two_one::get),
Expand All @@ -42,27 +43,30 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
.nest(
"/posts",
Router::new()
.route("/:id", routing::get(handler::posts::get))
.route("/:id/activity", routing::get(handler::posts::activity::get)),
.route("/{id}", routing::get(handler::posts::get))
.route(
"/{id}/activity",
routing::get(handler::posts::activity::get),
),
)
.nest(
"/users",
Router::new()
.route("/:user_id", routing::get(handler::users::get))
.route("/{user_id}", routing::get(handler::users::get))
.route(
"/:user_id/followers",
"/{user_id}/followers",
routing::get(handler::users::followers::get),
)
.route(
"/:user_id/following",
"/{user_id}/following",
routing::get(handler::users::following::get),
)
.route(
"/:user_id/inbox",
"/{user_id}/inbox",
routing::post(handler::users::inbox::post).layer(VerifyDigestLayer::default()),
)
.route(
"/:user_id/outbox",
"/{user_id}/outbox",
routing::get(handler::users::outbox::get),
),
)
Expand All @@ -78,7 +82,7 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
routing::get(handler::well_known::webfinger::get),
),
)
.route("/public/*path", routing::get(handler::public::get));
.route("/public/{*path}", routing::get(handler::public::get));

#[cfg(feature = "oidc")]
let router = router.route("/oidc/callback", routing::get(handler::oidc::callback::get));
Expand Down Expand Up @@ -110,19 +114,19 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
"/accounts",
Router::new()
.route(
"/:id",
"/{id}",
routing::get(handler::mastodon::api::v1::accounts::get),
)
.route(
"/:id/follow",
"/{id}/follow",
routing::post(handler::mastodon::api::v1::accounts::follow::post),
)
.route(
"/:id/statuses",
"/{id}/statuses",
routing::get(handler::mastodon::api::v1::accounts::statuses::get),
)
.route(
"/:id/unfollow",
"/{id}/unfollow",
routing::post(handler::mastodon::api::v1::accounts::unfollow::post),
)
.route(
Expand Down Expand Up @@ -164,13 +168,13 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
routing::get(handler::mastodon::api::v1::follow_requests::get),
)
.route(
"/:id/authorize",
"/{id}/authorize",
routing::post(
handler::mastodon::api::v1::follow_requests::accept::post,
),
)
.route(
"/:id/reject",
"/{id}/reject",
routing::post(
handler::mastodon::api::v1::follow_requests::reject::post,
),
Expand All @@ -192,7 +196,7 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
),
)
.route(
"/:id",
"/{id}",
routing::get(handler::mastodon::api::v1::media::get)
.put(handler::mastodon::api::v1::media::put),
),
Expand All @@ -205,11 +209,11 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
routing::get(handler::mastodon::api::v1::notifications::get),
)
.route(
"/:id",
"/{id}",
routing::get(handler::mastodon::api::v1::notifications::get_by_id),
)
.route(
"/:id/dismiss",
"/{id}/dismiss",
routing::post(
handler::mastodon::api::v1::notifications::dismiss::post,
),
Expand All @@ -229,49 +233,49 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
routing::post(handler::mastodon::api::v1::statuses::post),
)
.route(
"/:id",
"/{id}",
routing::delete(handler::mastodon::api::v1::statuses::delete)
.get(handler::mastodon::api::v1::statuses::get)
.put(handler::mastodon::api::v1::statuses::put),
)
.route(
"/:id/context",
"/{id}/context",
routing::get(handler::mastodon::api::v1::statuses::context::get),
)
.route(
"/:id/favourite",
"/{id}/favourite",
routing::post(
handler::mastodon::api::v1::statuses::favourite::post,
),
)
.route(
"/:id/favourited_by",
"/{id}/favourited_by",
routing::get(
handler::mastodon::api::v1::statuses::favourited_by::get,
),
)
.route(
"/:id/reblog",
"/{id}/reblog",
routing::post(handler::mastodon::api::v1::statuses::reblog::post),
)
.route(
"/:id/reblogged_by",
"/{id}/reblogged_by",
routing::get(
handler::mastodon::api::v1::statuses::reblogged_by::get,
),
)
.route(
"/:id/source",
"/{id}/source",
routing::get(handler::mastodon::api::v1::statuses::source::get),
)
.route(
"/:id/unfavourite",
"/{id}/unfavourite",
routing::post(
handler::mastodon::api::v1::statuses::unfavourite::post,
),
)
.route(
"/:id/unreblog",
"/{id}/unreblog",
routing::post(handler::mastodon::api::v1::statuses::unreblog::post),
),
)
Expand Down Expand Up @@ -303,7 +307,7 @@ pub fn create(state: Zustand, server_config: &server::Configuration) -> eyre::Re
),
)
.route(
"/:id",
"/{id}",
routing::get(handler::mastodon::api::v1::media::get)
.put(handler::mastodon::api::v1::media::put),
),
Expand Down
45 changes: 27 additions & 18 deletions lib/cursiv/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,34 @@ where
}

fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let csrf_cookie = req
.headers()
.get_all(header::COOKIE)
.into_iter()
.filter_map(|value| value.to_str().ok()) // Filter out all the values that aren't valid UTF-8
.flat_map(Cookie::split_parse_encoded) // Parse all the cookie headers and flatten the resulting iterator into a contiguous one
.flatten() // Call `.flatten()` to turn `Result<Cookie, Error>` -> `Cookie`, ignoring all the errors
.find(|cookie| cookie.name() == CSRF_COOKIE_NAME); // Find the cookie with the name of our CSRF cookie
let read_data = {
let mut csrf_data = None;
'outer: for header in req.headers().get_all(header::COOKIE) {
let Ok(value_str) = header.to_str() else {
continue;
};

let read_data = if let Some(csrf_cookie) = csrf_cookie {
csrf_cookie
.value_trimmed()
.split_once('.')
.map(|(hash, message)| CsrfData {
hash: hash.into(),
message: message.into(),
})
} else {
None
for cookie in Cookie::split_parse_encoded(value_str) {
let Ok(cookie) = cookie else {
continue;
};

if cookie.name() == CSRF_COOKIE_NAME {
let Some((hash, message)) = cookie.value_trimmed().split_once('.') else {
continue;
};

csrf_data = Some(CsrfData {
hash: hash.into(),
message: message.into(),
});

break 'outer;
}
}
}

csrf_data
};

let handle = CsrfHandle {
Expand Down
5 changes: 5 additions & 0 deletions lib/flashy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ license = "MIT OR Apache-2.0"

[dependencies]
axum-core = { workspace = true, optional = true }
blake3.workspace = true
cookie.workspace = true
futures-test.workspace = true
hex-simd.workspace = true
http.workspace = true
pin-project-lite.workspace = true
rand.workspace = true
serde.workspace = true
sonic-rs.workspace = true
subtle.workspace = true
tower.workspace = true
triomphe.workspace = true
zeroize.workspace = true

[features]
axum = ["dep:axum-core"]
Expand Down
Loading

0 comments on commit a32edf5

Please sign in to comment.