Skip to content

Commit c33d3fb

Browse files
committed
Don't use regex to parse authorization headers
Signed-off-by: Lilly Rose Berner <lilly@lostluma.net>
1 parent e1b62f7 commit c33d3fb

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

echo/src/utils.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use once_cell::sync::Lazy;
21
use rand::{distributions::Alphanumeric, Rng};
32

4-
use regex::Regex;
53
use rocket::{
64
http::Status,
75
request::{FromRequest, Outcome, Request},
@@ -34,14 +32,15 @@ impl<'r> FromRequest<'r> for PasswordHeader<'r> {
3432
None => return Outcome::Error((Status::Unauthorized, "Missing Authorization header.")),
3533
};
3634

37-
static PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i:password) .+").unwrap());
35+
let (prefix, suffix) = match value.split_once(" ") {
36+
Some(values) => values,
37+
None => return Outcome::Error((Status::Unauthorized, "Invalid Authorization header.")),
38+
};
3839

39-
if !PATTERN.is_match(value) {
40-
return Outcome::Error((Status::Unauthorized, "Invalid Authorization header."));
40+
if prefix.to_lowercase() == "password" {
41+
Outcome::Success(PasswordHeader { value: suffix })
42+
} else {
43+
Outcome::Error((Status::Unauthorized, "Invalid Authorization header."))
4144
}
42-
43-
Outcome::Success(PasswordHeader {
44-
value: value.split_once(" ").unwrap().1,
45-
})
4645
}
4746
}

0 commit comments

Comments
 (0)