Skip to content

Commit 2bffc01

Browse files
committed
Fix error propagation, remove string conversions
1 parent 2d71fc4 commit 2bffc01

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/client/auth/aws.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,19 @@ pub async fn compute_aws_sigv4_payload(
272272
let signable_request = SignableRequest::new(
273273
request.method().as_str(),
274274
request.uri().to_string(),
275-
request.headers().iter().map(|(k, v)| {
276-
(
277-
k.as_str(),
278-
std::str::from_utf8(v.as_bytes()).expect("Header value should be valid UTF-8"),
279-
)
280-
}),
275+
request
276+
.headers()
277+
.iter()
278+
.map(|(k, v)| {
279+
let value = std::str::from_utf8(v.as_bytes()).map_err(|_| {
280+
Error::authentication_error(
281+
MECH_NAME,
282+
"Failed to convert header value to valid UTF-8",
283+
)
284+
})?;
285+
Ok((k.as_str(), value))
286+
})
287+
.filter_map(Result::ok),
281288
SignableBody::Bytes(request.body().as_bytes()),
282289
)
283290
.map_err(|e| {
@@ -287,21 +294,21 @@ pub async fn compute_aws_sigv4_payload(
287294
let (signing_instructions, _signature) = sign(signable_request, &signing_params)
288295
.map_err(|e| Error::authentication_error(MECH_NAME, &format!("Signing failed: {e}")))?
289296
.into_parts();
290-
291297
signing_instructions.apply_to_request_http1x(&mut request);
292298

293299
let headers = request.headers();
294300
let authorization_header = headers
295301
.get("authorization")
296302
.ok_or_else(|| Error::authentication_error(MECH_NAME, "Missing authorization header"))?
297303
.to_str()
298-
.map_err(|e| Error::authentication_error(MECH_NAME, &format!("Invalid header value: {e}")))?
299-
.to_string();
304+
.map_err(|e| {
305+
Error::authentication_error(MECH_NAME, &format!("Invalid header value: {e}"))
306+
})?;
300307

301308
let token_header = headers
302309
.get("x-amz-security-token")
303310
.map(|v| {
304-
v.to_str().map(|s| s.to_string()).map_err(|e| {
311+
v.to_str().map_err(|e| {
305312
Error::authentication_error(MECH_NAME, &format!("Invalid token header: {e}"))
306313
})
307314
})

0 commit comments

Comments
 (0)