-
Notifications
You must be signed in to change notification settings - Fork 650
Restrict authentication to same-origin requests. #2627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8788546
346cd98
04ddd4b
8523b79
1540521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,30 @@ impl AuthenticatedUser { | |
impl<'a> UserAuthenticationExt for dyn RequestExt + 'a { | ||
/// Obtain `AuthenticatedUser` for the request or return an `Unauthorized` error | ||
fn authenticate(&self, conn: &PgConnection) -> AppResult<AuthenticatedUser> { | ||
let forwarded_host = self.headers().get("x-forwarded-host"); | ||
let forwarded_proto = self.headers().get("x-forwarded-proto"); | ||
let expected_origin = match (forwarded_host, forwarded_proto) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose having a comma separated list of origins in an environment variable like Alternatively, you could fall back to |
||
(Some(host), Some(proto)) => format!( | ||
"{}://{}", | ||
proto.to_str().unwrap_or_default(), | ||
host.to_str().unwrap_or_default() | ||
), | ||
_ => "".to_string(), | ||
}; | ||
|
||
let bad_origin = self | ||
.headers() | ||
.get_all(header::ORIGIN) | ||
.iter() | ||
.find(|h| h.to_str().unwrap_or_default() != expected_origin); | ||
if let Some(bad_origin) = bad_origin { | ||
let error_message = format!( | ||
"only same-origin requests can be authenticated. expected {}, got {:?}", | ||
expected_origin, bad_origin | ||
); | ||
return Err(internal(&error_message)) | ||
.chain_error(|| Box::new(Unauthorized) as Box<dyn AppError>); | ||
} | ||
if let Some(id) = self.extensions().find::<TrustedUserId>() { | ||
// A trusted user_id was provided by a signed cookie (or a test `MockCookieUser`) | ||
Ok(AuthenticatedUser { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose dropping this change.