Skip to content

Commit cdecc42

Browse files
aws-sdk-rust-cilandonxjamesShaharNaveh
committed
[smithy-rs] Rollup of 2 commits
Includes commits: ec429200 Add `generateBuildEnvironmentConstants` as a dependency of `sourcesJar` (#4005) 771f7173 Fix Sigv4 signing bug for endpoints with default ports (#4006) Co-authored-by: Landon James <[email protected]> Co-authored-by: Shahar Naveh <[email protected]>
1 parent 4c6c511 commit cdecc42

File tree

16 files changed

+95
-32
lines changed

16 files changed

+95
-32
lines changed

sdk/aws-runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ version = "1.2.1"
3838
[dependencies.aws-sigv4]
3939
path = "../aws-sigv4"
4040
features = ["http0-compat"]
41-
version = "1.2.8"
41+
version = "1.2.9"
4242

4343
[dependencies.aws-smithy-async]
4444
path = "../aws-smithy-async"

sdk/aws-sigv4/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ required-features = ["sigv4a"]
1010

1111
[package]
1212
name = "aws-sigv4"
13-
version = "1.2.8"
13+
version = "1.2.9"
1414
authors = ["AWS Rust SDK Team <[email protected]>", "David Barsky <[email protected]>"]
1515
description = "SigV4 signer for HTTP requests and Event Stream messages."
1616
edition = "2021"

sdk/aws-sigv4/src/http_request/canonical_request.rs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::sign::v4::sha256_hex_string;
1616
use crate::SignatureVersion;
1717
use aws_smithy_http::query_writer::QueryWriter;
1818
use http0::header::{AsHeaderName, HeaderName, HOST};
19+
use http0::uri::{Port, Scheme};
1920
use http0::{HeaderMap, HeaderValue, Uri};
2021
use std::borrow::Cow;
2122
use std::cmp::Ordering;
@@ -389,10 +390,28 @@ impl<'a> CanonicalRequest<'a> {
389390
match canonical_headers.get(&HOST) {
390391
Some(header) => header.clone(),
391392
None => {
393+
let port = uri.port();
394+
let scheme = uri.scheme();
392395
let authority = uri
393396
.authority()
394-
.expect("request uri authority must be set for signing");
395-
let header = HeaderValue::try_from(authority.as_str())
397+
.expect("request uri authority must be set for signing")
398+
.as_str();
399+
let host = uri
400+
.host()
401+
.expect("request uri host must be set for signing");
402+
403+
// Check if port is default (80 for HTTP, 443 for HTTPS) and if so exclude it from the
404+
// Host header when signing since RFC 2616 indicates that the default port should not be
405+
// sent in the Host header (and Hyper strips default ports if they are present)
406+
// https://datatracker.ietf.org/doc/html/rfc2616#section-14.23
407+
// https://github.com/awslabs/aws-sdk-rust/issues/1244
408+
let header_value = if is_port_scheme_default(scheme, port) {
409+
host
410+
} else {
411+
authority
412+
};
413+
414+
let header = HeaderValue::try_from(header_value)
396415
.expect("endpoint must contain valid header characters");
397416
canonical_headers.insert(HOST, header.clone());
398417
header
@@ -475,6 +494,15 @@ fn normalize_header_value(header_value: &str) -> Result<HeaderValue, CanonicalRe
475494
HeaderValue::from_str(&trimmed_value).map_err(CanonicalRequestError::from)
476495
}
477496

497+
#[inline]
498+
fn is_port_scheme_default(scheme: Option<&Scheme>, port: Option<Port<&str>>) -> bool {
499+
if let (Some(scheme), Some(port)) = (scheme, port) {
500+
return [("http", "80"), ("https", "443")].contains(&(scheme.as_str(), port.as_str()));
501+
}
502+
503+
false
504+
}
505+
478506
#[derive(Debug, PartialEq, Default)]
479507
pub(crate) struct SignedHeaders {
480508
headers: Vec<CanonicalHeaderName>,
@@ -692,6 +720,40 @@ mod tests {
692720
);
693721
}
694722

723+
#[test]
724+
fn test_host_header_properly_handles_ports() {
725+
fn host_header_test_setup(endpoint: String) -> String {
726+
let mut req = test::v4::test_request("get-vanilla");
727+
req.uri = endpoint;
728+
let req = SignableRequest::from(&req);
729+
let settings = SigningSettings {
730+
payload_checksum_kind: PayloadChecksumKind::XAmzSha256,
731+
session_token_mode: SessionTokenMode::Exclude,
732+
..Default::default()
733+
};
734+
let identity = Credentials::for_tests().into();
735+
let signing_params = signing_params(&identity, settings);
736+
let creq = CanonicalRequest::from(&req, &signing_params).unwrap();
737+
creq.header_values_for("host")
738+
}
739+
740+
// HTTP request with 80 port should not be signed with that port
741+
let http_80_host_header = host_header_test_setup("http://localhost:80".into());
742+
assert_eq!(http_80_host_header, "localhost",);
743+
744+
// HTTP request with non-80 port should be signed with that port
745+
let http_1234_host_header = host_header_test_setup("http://localhost:1234".into());
746+
assert_eq!(http_1234_host_header, "localhost:1234",);
747+
748+
// HTTPS request with 443 port should not be signed with that port
749+
let https_443_host_header = host_header_test_setup("https://localhost:443".into());
750+
assert_eq!(https_443_host_header, "localhost",);
751+
752+
// HTTPS request with non-443 port should be signed with that port
753+
let https_1234_host_header = host_header_test_setup("https://localhost:1234".into());
754+
assert_eq!(https_1234_host_header, "localhost:1234",);
755+
}
756+
695757
#[test]
696758
fn test_set_xamz_sha_256() {
697759
let req = test::v4::test_request("get-vanilla-query-order-key-case");

sdk/dsql/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

sdk/ebs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

sdk/glacier/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

sdk/lexruntime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

sdk/lexruntimev2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ version = "1.5.5"
2525

2626
[dependencies.aws-sigv4]
2727
path = "../aws-sigv4"
28-
version = "1.2.8"
28+
version = "1.2.9"
2929

3030
[dependencies.aws-smithy-async]
3131
path = "../aws-smithy-async"

sdk/mediastoredata/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

sdk/omics/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ version = "1.5.5"
2424

2525
[dependencies.aws-sigv4]
2626
path = "../aws-sigv4"
27-
version = "1.2.8"
27+
version = "1.2.9"
2828

2929
[dependencies.aws-smithy-async]
3030
path = "../aws-smithy-async"

0 commit comments

Comments
 (0)