-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "csurf" | ||
authors.workspace = true | ||
edition.workspace = true | ||
version.workspace = true | ||
|
||
[dependencies] | ||
http = "0.2.11" | ||
tower = { version = "0.4.13", default-features = false } | ||
zeroize = { version = "1.7.0", features = ["derive"] } | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# csurf | ||
|
||
Small stateless CSRF crate for tower-based frameworks | ||
|
||
Implements the [Signed Double-Submit Cookie](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#signed-double-submit-cookie-recommended) pattern, as recommended by OWASP. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use http::{Request, Response}; | ||
use std::task::{self, Poll}; | ||
use tower::{Layer, Service}; | ||
use zeroize::{Zeroize, ZeroizeOnDrop}; | ||
|
||
#[derive(Zeroize, ZeroizeOnDrop)] | ||
pub struct Signer {} | ||
|
||
#[derive(Clone, Zeroize, ZeroizeOnDrop)] | ||
pub struct CsrfLayer { | ||
key: Vec<u8>, | ||
} | ||
|
||
impl<S> Layer<S> for CsrfLayer { | ||
type Service = CsrfService<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
CsrfService::new(inner, self.key.clone()) | ||
} | ||
} | ||
|
||
#[derive(Clone, Zeroize, ZeroizeOnDrop)] | ||
pub struct CsrfService<S> { | ||
#[zeroize(skip)] | ||
inner: S, | ||
key: Vec<u8>, | ||
} | ||
|
||
impl<S> CsrfService<S> { | ||
pub fn new(inner: S, key: Vec<u8>) -> Self { | ||
Self { inner, key } | ||
} | ||
} | ||
|
||
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for CsrfService<S> | ||
where | ||
S: Service<Request<ReqBody>, Response = Response<ResBody>>, | ||
{ | ||
type Error = S::Error; | ||
type Future = S::Future; | ||
type Response = S::Response; | ||
|
||
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: Request<ReqBody>) -> Self::Future { | ||
self.inner.call(req) | ||
} | ||
} |