Skip to content

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

Merged
merged 5 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion script/init-local-index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cd tmp/index-tmp
cat > config.json <<-EOF
{
"dl": "http://localhost:8888/api/v1/crates",
"api": "http://localhost:8888/"
"api": "http://localhost:4200/"
Copy link
Member

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.

}
EOF
git add config.json
Expand Down
24 changes: 24 additions & 0 deletions src/controllers/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The 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 WEB_ALLOWED_ORIGINS=http://localhost:8888,http://localhost:4200. Any Origin headers in the request would need to be on this list. The example above could be added to .env.sample so that either testing approach works.

Alternatively, you could fall back to self.host() if the other headers aren't set. Note that for this approach you don't want to use self.scheme() because that is for the local socket. You'd probably want something like if self.app().config.env == crate::Env::Production { "https" } else { "http" }.

(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 {
Expand Down