return 403 on anonymous query
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
Trinity Pointard 2022-01-17 20:10:59 +01:00
parent 2735dfae46
commit e3e80396ed
2 changed files with 22 additions and 8 deletions

View file

@ -91,6 +91,9 @@ async fn handler(
async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> { async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> {
let (api_key, content_sha256) = check_payload_signature(&garage, &req).await?; let (api_key, content_sha256) = check_payload_signature(&garage, &req).await?;
let api_key = api_key.ok_or_else(|| {
Error::Forbidden("Garage does not support anonymous access yet".to_string())
})?;
let authority = req let authority = req
.headers() .headers()

View file

@ -20,7 +20,7 @@ use crate::error::*;
pub async fn check_payload_signature( pub async fn check_payload_signature(
garage: &Garage, garage: &Garage,
request: &Request<Body>, request: &Request<Body>,
) -> Result<(Key, Option<Hash>), Error> { ) -> Result<(Option<Key>, Option<Hash>), Error> {
let mut headers = HashMap::new(); let mut headers = HashMap::new();
for (key, val) in request.headers() { for (key, val) in request.headers() {
headers.insert(key.to_string(), val.to_str()?.to_string()); headers.insert(key.to_string(), val.to_str()?.to_string());
@ -34,8 +34,19 @@ pub async fn check_payload_signature(
let authorization = if let Some(authorization) = headers.get("authorization") { let authorization = if let Some(authorization) = headers.get("authorization") {
parse_authorization(authorization, &headers)? parse_authorization(authorization, &headers)?
} else if let Some(algorithm) = headers.get("x-amz-algorithm") {
parse_query_authorization(algorithm, &headers)?
} else { } else {
parse_query_authorization(&headers)? let content_sha256 = headers.get("x-amz-content-sha256");
if let Some(content_sha256) = content_sha256.filter(|c| "UNSIGNED-PAYLOAD" != c.as_str()) {
let sha256 = hex::decode(content_sha256)
.ok()
.and_then(|bytes| Hash::try_from(&bytes))
.ok_or_bad_request("Invalid content sha256 hash")?;
return Ok((None, Some(sha256)));
} else {
return Ok((None, None));
}
}; };
let scope = format!( let scope = format!(
@ -93,7 +104,7 @@ pub async fn check_payload_signature(
Some(Hash::try_from(&bytes).ok_or_bad_request("Invalid content sha256 hash")?) Some(Hash::try_from(&bytes).ok_or_bad_request("Invalid content sha256 hash")?)
}; };
Ok((key, content_sha256)) Ok((Some(key), content_sha256))
} }
struct Authorization { struct Authorization {
@ -165,11 +176,11 @@ fn parse_authorization(
Ok(auth) Ok(auth)
} }
fn parse_query_authorization(headers: &HashMap<String, String>) -> Result<Authorization, Error> { fn parse_query_authorization(
let algo = headers algorithm: &str,
.get("x-amz-algorithm") headers: &HashMap<String, String>,
.ok_or_bad_request("X-Amz-Algorithm not found in query parameters")?; ) -> Result<Authorization, Error> {
if algo != "AWS4-HMAC-SHA256" { if algorithm != "AWS4-HMAC-SHA256" {
return Err(Error::BadRequest( return Err(Error::BadRequest(
"Unsupported authorization method".to_string(), "Unsupported authorization method".to_string(),
)); ));