Skip to content

Commit d01af66

Browse files
committed
fix(sandbox): address code review — preserve headers, narrow scope, improve docs
SigV4 header preservation (P1): - Preserve all original headers except AWS auth headers and hop-by-hop headers instead of dropping everything except host/content-type/ content-length. S3 operations that depend on x-amz-acl, Range, x-amz-server-side-encryption, etc. now work correctly. - Extract is_aws_auth_header and is_hop_by_hop_header helpers, reuse in strip_aws_headers. - Add test proving S3 headers are preserved and hop-by-hop stripped. aws-s3 endpoint scope (P2): - Document that **.amazonaws.com is broader than S3 due to the host wildcard validator only allowing wildcards in the first DNS label. - Recommend the generic 'aws' profile with custom policy for tighter host scoping. Chunked limitation visibility (P2): - Add chunked transfer encoding limitation to manage-providers.mdx so it's visible wherever AWS STS is documented. Example doc fixes: - Remove branch-specific prerequisite. - Fix inconsistent path references for the gateway config. Refs NVIDIA#1576
1 parent e159f28 commit d01af66

5 files changed

Lines changed: 68 additions & 30 deletions

File tree

‎crates/openshell-sandbox/src/l7/rest.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ fn hex_value(byte: u8) -> Option<u8> {
824824
}
825825
}
826826

827-
828827
async fn collect_chunked_body<C: AsyncRead + Unpin>(
829828
client: &mut C,
830829
already_read: &[u8],

‎crates/openshell-sandbox/src/sigv4.rs‎

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ pub fn extract_aws_region(host: &str) -> Option<String> {
2323
}
2424
}
2525

26+
fn is_aws_auth_header(lower: &str) -> bool {
27+
matches!(
28+
lower,
29+
"authorization" | "x-amz-date" | "x-amz-security-token" | "x-amz-content-sha256"
30+
)
31+
}
32+
33+
fn is_hop_by_hop_header(lower: &str) -> bool {
34+
matches!(
35+
lower,
36+
"connection"
37+
| "proxy-connection"
38+
| "keep-alive"
39+
| "transfer-encoding"
40+
| "te"
41+
| "trailer"
42+
| "upgrade"
43+
| "proxy-authorization"
44+
| "proxy-authenticate"
45+
| "accept-encoding"
46+
)
47+
}
48+
2649
/// Strip AWS auth headers from raw HTTP request bytes.
2750
///
2851
/// Removes Authorization, X-Amz-Date, X-Amz-Security-Token, and
@@ -48,11 +71,8 @@ pub fn strip_aws_headers(raw: &[u8]) -> Vec<u8> {
4871
if line.is_empty() {
4972
break;
5073
}
51-
let lower = line.to_ascii_lowercase();
52-
if lower.starts_with("authorization:")
53-
|| lower.starts_with("x-amz-date:")
54-
|| lower.starts_with("x-amz-security-token:")
55-
|| lower.starts_with("x-amz-content-sha256:")
74+
if let Some((k, _)) = line.split_once(':')
75+
&& is_aws_auth_header(&k.trim().to_ascii_lowercase())
5676
{
5777
continue;
5878
}
@@ -105,21 +125,20 @@ pub fn apply_sigv4_to_request(
105125
}
106126
});
107127

108-
// Collect only headers that should be included in the SigV4 signature.
109-
// The old hand-rolled code only signed host, content-type, and
110-
// content-length. Signing all headers causes failures when the proxy
111-
// or transport modifies unsigned-by-convention headers (Connection,
112-
// Accept-Encoding, etc.) between signing and delivery.
128+
// Preserve all original headers except AWS auth headers (which we
129+
// regenerate) and hop-by-hop headers (which the proxy or transport may
130+
// modify between signing and delivery, breaking the signature).
113131
let mut existing_headers: Vec<(String, String)> = Vec::new();
114132
for line in lines.iter().skip(1) {
115133
if line.is_empty() {
116134
break;
117135
}
118136
if let Some((k, v)) = line.split_once(':') {
119137
let lower = k.trim().to_ascii_lowercase();
120-
if lower == "host" || lower == "content-type" || lower == "content-length" {
121-
existing_headers.push((lower, v.trim().to_string()));
138+
if is_aws_auth_header(&lower) || is_hop_by_hop_header(&lower) {
139+
continue;
122140
}
141+
existing_headers.push((lower, v.trim().to_string()));
123142
}
124143
}
125144

@@ -207,8 +226,7 @@ mod tests {
207226

208227
#[test]
209228
fn extract_region_from_s3_virtual_hosted_hostname() {
210-
let region =
211-
extract_aws_region("my-bucket.s3.us-east-1.amazonaws.com").unwrap();
229+
let region = extract_aws_region("my-bucket.s3.us-east-1.amazonaws.com").unwrap();
212230
assert_eq!(region, "us-east-1");
213231
}
214232

@@ -255,6 +273,28 @@ mod tests {
255273
assert!(result_str.contains("x-amz-security-token: FwoGZXIvYXdzEBYaDH+session+token"));
256274
}
257275

276+
#[test]
277+
fn sign_preserves_s3_headers_and_strips_hop_by_hop() {
278+
let raw = b"PUT /object.txt HTTP/1.1\r\nHost: my-bucket.s3.us-east-1.amazonaws.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\nx-amz-acl: private\r\nx-amz-server-side-encryption: AES256\r\nRange: bytes=0-99\r\nConnection: keep-alive\r\nAccept-Encoding: gzip\r\nProxy-Connection: keep-alive\r\n\r\nhello";
279+
let result = apply_sigv4_to_request(
280+
raw,
281+
"my-bucket.s3.us-east-1.amazonaws.com",
282+
"us-east-1",
283+
"s3",
284+
"AKIATEST",
285+
"secret",
286+
None,
287+
);
288+
let result_str = String::from_utf8_lossy(&result);
289+
assert!(result_str.contains("x-amz-acl: private"));
290+
assert!(result_str.contains("x-amz-server-side-encryption: AES256"));
291+
assert!(result_str.contains("range: bytes=0-99"));
292+
assert!(!result_str.contains("connection:"));
293+
assert!(!result_str.contains("accept-encoding:"));
294+
assert!(!result_str.contains("proxy-connection:"));
295+
assert!(result_str.contains("authorization: AWS4-HMAC-SHA256"));
296+
}
297+
258298
#[test]
259299
fn apply_sigv4_rewrites_request() {
260300
let raw = b"POST /model/test/invoke HTTP/1.1\r\nHost: bedrock-runtime.us-east-2.amazonaws.com\r\nContent-Type: application/json\r\nAuthorization: AWS4-HMAC-SHA256 old-invalid-sig\r\nX-Amz-Date: old-date\r\n\r\n{}";

‎docs/sandboxes/manage-providers.mdx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ openshell provider refresh configure my-aws \
182182
Use the generic `aws` profile type for multi-service access and scope endpoints
183183
via sandbox network policy. Use `aws-s3` for S3-specific endpoint rules.
184184

185+
The proxy re-signs requests using SigV4 before forwarding to AWS. Clients that
186+
send `Content-Length` bodies (curl, most CLI tools) work. Clients that use
187+
`Transfer-Encoding: chunked` (e.g., boto3 `put_object`) are not yet supported
188+
by the SigV4 relay and will fail.
189+
185190
External refresh systems should continue to push new current credentials through
186191
`openshell provider update`. The `--credential-expires-at` option works for
187192
static credentials, externally refreshed credentials, and gateway-managed

‎examples/aws-s3-sts.md‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ placeholders and signs requests on the fly.
99

1010
- AWS CLI authenticated (`aws sts get-caller-identity` succeeds)
1111
- Podman running (`podman info` succeeds)
12-
- OpenShell built from the `feat/1576-aws-sts-with-sigv4` branch (or later,
13-
once merged)
12+
- OpenShell built from source with AWS STS refresh and SigV4 signing support
1413

1514
## 1. Create AWS test resources
1615

@@ -79,9 +78,9 @@ Export them before starting:
7978
eval "$(aws configure export-credentials --format env)"
8079
```
8180

82-
The gateway must use the local supervisor image. Edit the gateway config
83-
(`~/.cache/gateway-podman/gateway.toml` or equivalent) so `supervisor_image`
84-
appears under `[openshell.gateway]`:
81+
The gateway must use the locally built supervisor image. Set the
82+
`supervisor_image` field under `[openshell.gateway]` in the gateway config
83+
(typically `.cache/gateway-podman/gateway.toml` in the repo root):
8584

8685
```toml
8786
[openshell.gateway]
@@ -94,16 +93,6 @@ Then start the gateway:
9493
mise run gateway
9594
```
9695

97-
Alternatively, start the gateway binary directly with the correct config:
98-
99-
```shell
100-
eval "$(aws configure export-credentials --format env)"
101-
./target/debug/openshell-gateway \
102-
--config .cache/gateway-podman/gateway.toml \
103-
--port 18080 --log-level info --drivers podman --disable-tls \
104-
--db-url "sqlite:.cache/gateway-podman/gateway.db?mode=rwc"
105-
```
106-
10796
## 4. Configure the provider
10897

10998
In a separate terminal:

‎providers/aws-s3.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ credentials:
4848
env_vars: [AWS_SESSION_TOKEN]
4949
required: true
5050
endpoints:
51+
# S3 uses virtual-hosted URLs like bucket.s3.region.amazonaws.com.
52+
# The host wildcard validator only allows wildcards in the first DNS
53+
# label, so we cannot write *.s3.**.amazonaws.com. Using **.amazonaws.com
54+
# is broader than S3; use the generic 'aws' profile with a custom policy
55+
# if you need tighter host scoping.
5156
- host: "**.amazonaws.com"
5257
port: 443
5358
protocol: rest

0 commit comments

Comments
 (0)