Skip to content

Commit ce53f9c

Browse files
committed
Require password prefix fetching authenticated pastes
Signed-off-by: Lilly Rose Berner <lilly@lostluma.net>
1 parent e90177e commit ce53f9c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

echo/src/utils.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use once_cell::sync::Lazy;
12
use rand::{distributions::Alphanumeric, Rng};
23

4+
use regex::Regex;
35
use rocket::{
46
http::Status,
57
request::{FromRequest, Outcome, Request},
@@ -27,9 +29,19 @@ impl<'r> FromRequest<'r> for PasswordHeader<'r> {
2729
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
2830
let result = request.headers().get_one("Authorization");
2931

30-
match result {
31-
Some(value) => Outcome::Success(PasswordHeader { value }),
32-
None => Outcome::Error((Status::Unauthorized, "Missing Authorization header.")),
32+
let value = match result {
33+
Some(value) => value,
34+
None => return Outcome::Error((Status::Unauthorized, "Missing Authorization header.")),
35+
};
36+
37+
static PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i:password): .+").unwrap());
38+
39+
if !PATTERN.is_match(value) {
40+
return Outcome::Error((Status::Unauthorized, "Invalid Authorization header."));
3341
}
42+
43+
Outcome::Success(PasswordHeader {
44+
value: value.split_once(" ").unwrap().1,
45+
})
3446
}
3547
}

0 commit comments

Comments
 (0)