Skip to content

Commit bf1e986

Browse files
russellbjhjaggars
authored andcommitted
fix(sandbox): reject service names as regions in extract_aws_region
Boto3 connects to global S3 endpoints like bucket.s3.amazonaws.com (no region in the hostname). The previous extract_aws_region returned "s3" for this pattern because it took the label at parts[len-3] without checking if it was actually a region. Add looks_like_region() which requires a hyphen followed by a digit (e.g., us-east-1). Service names like "s3" or "bedrock-runtime" are rejected, causing the fallback to us-east-1. Refs NVIDIA#1576
1 parent 715b5f5 commit bf1e986

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

  • crates/openshell-supervisor-network/src

‎crates/openshell-supervisor-network/src/sigv4.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ use aws_smithy_runtime_api::client::identity::Identity;
1010
use miette::{Result, miette};
1111
use std::time::SystemTime;
1212

13+
/// AWS regions contain a hyphen followed by a digit (e.g., `us-east-1`).
14+
/// Service names like `s3` or `bedrock-runtime` do not.
15+
fn looks_like_region(s: &str) -> bool {
16+
let bytes = s.as_bytes();
17+
for i in 0..bytes.len().saturating_sub(1) {
18+
if bytes[i] == b'-' && bytes[i + 1].is_ascii_digit() {
19+
return true;
20+
}
21+
}
22+
false
23+
}
24+
1325
/// Extract the AWS region from an AWS hostname.
1426
///
1527
/// Supports standard, dualstack, FIPS, virtual-hosted, and China partition
@@ -23,7 +35,11 @@ pub fn extract_aws_region(host: &str) -> Option<String> {
2335
&& parts[parts.len() - 2] == "com"
2436
&& parts[parts.len() - 1] == "cn"
2537
{
26-
return Some(parts[parts.len() - 4].to_string());
38+
let candidate = parts[parts.len() - 4];
39+
if looks_like_region(candidate) {
40+
return Some(candidate.to_string());
41+
}
42+
return None;
2743
}
2844
// Standard/dualstack/FIPS/virtual-hosted: *.amazonaws.com
2945
// Scan right-to-left from "amazonaws", skipping non-region labels
@@ -36,7 +52,7 @@ pub fn extract_aws_region(host: &str) -> Option<String> {
3652
while idx > 0 && parts[idx] == "dualstack" {
3753
idx -= 1;
3854
}
39-
if idx > 0 {
55+
if idx > 0 && looks_like_region(parts[idx]) {
4056
return Some(parts[idx].to_string());
4157
}
4258
}
@@ -358,6 +374,11 @@ mod tests {
358374
assert!(extract_aws_region("s3.amazonaws.com").is_none());
359375
}
360376

377+
#[test]
378+
fn virtual_hosted_global_endpoint_returns_none() {
379+
assert!(extract_aws_region("my-bucket.s3.amazonaws.com").is_none());
380+
}
381+
361382
#[test]
362383
fn extract_region_dualstack() {
363384
let region = extract_aws_region("s3.dualstack.us-west-2.amazonaws.com").unwrap();

0 commit comments

Comments
 (0)