Skip to content

Commit 8b30211

Browse files
st-grclaude
andcommitted
feat(sandbox): allow AWS Bedrock InvokeModel paths through the L7 router
Adds two patterns to `default_patterns()` so the supervisor's L7 inference router recognizes the Bedrock InvokeModel URL shape and forwards matched requests to the registered upstream: - `POST /model/{modelId}/invoke` → aws_bedrock_invoke - `POST /model/{modelId}/invoke-with-response-stream` → aws_bedrock_invoke_stream The `{modelId}` segment is wildcarded by extending `detect_inference_pattern` to handle one middle `/*/` segment in addition to the existing trailing `/*`. The wildcard is constrained to a single non-empty path segment to avoid path-traversal liabilities — `/model//invoke` and `/model/a/b/invoke` both no-match. Without this, sandboxes running Claude Code in its native Bedrock mode (`CLAUDE_CODE_USE_BEDROCK=1`, `ANTHROPIC_BEDROCK_BASE_URL`, AWS-style auth) hit the supervisor with `403 connection not allowed by policy` because their URL doesn't match `/v1/*` shapes. The fix unblocks operators wanting to register direct AWS Bedrock, an in-cluster Bedrock-compatible bridge, or a Bedrock-emulating LiteLLM as `--type aws-bedrock` providers. Tests cover: positive matches for invoke + invoke-with-response-stream, query-string handling, GET rejection, empty-segment rejection, multi-segment rejection, and unknown-action rejection. Companion changes (provider discovery spec + YAML profile) follow in the next commit. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> Signed-off-by: st-gr <[email protected]>
1 parent e98ea3e commit 8b30211

1 file changed

Lines changed: 97 additions & 1 deletion

File tree

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

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct InferenceApiPattern {
1616
pub kind: String,
1717
}
1818

19-
/// Default patterns for known inference APIs (`OpenAI`, Anthropic).
19+
/// Default patterns for known inference APIs (`OpenAI`, Anthropic, AWS Bedrock).
2020
pub fn default_patterns() -> Vec<InferenceApiPattern> {
2121
vec![
2222
InferenceApiPattern {
@@ -55,10 +55,31 @@ pub fn default_patterns() -> Vec<InferenceApiPattern> {
5555
protocol: "model_discovery".to_string(),
5656
kind: "models_get".to_string(),
5757
},
58+
// AWS Bedrock InvokeModel + InvokeModelWithResponseStream. The `*`
59+
// segment is the Bedrock model id (e.g. `anthropic.claude-opus-4-7`).
60+
InferenceApiPattern {
61+
method: "POST".to_string(),
62+
path_glob: "/model/*/invoke".to_string(),
63+
protocol: "aws_bedrock_invoke".to_string(),
64+
kind: "messages".to_string(),
65+
},
66+
InferenceApiPattern {
67+
method: "POST".to_string(),
68+
path_glob: "/model/*/invoke-with-response-stream".to_string(),
69+
protocol: "aws_bedrock_invoke_stream".to_string(),
70+
kind: "messages".to_string(),
71+
},
5872
]
5973
}
6074

6175
/// Check if an HTTP request matches a known inference API pattern.
76+
///
77+
/// Path globs support two wildcard shapes (one per pattern, not both):
78+
/// - **Trailing `/*`**: `/v1/models/*` matches `/v1/models` and any
79+
/// `/v1/models/<rest>` (one or many path segments).
80+
/// - **Middle `/*/`**: `/model/*/invoke` matches `/model/<id>/invoke`
81+
/// for a single non-empty segment that contains no `/`. Used for
82+
/// AWS Bedrock's `/model/{modelId}/invoke[-with-response-stream]`.
6283
pub fn detect_inference_pattern<'a>(
6384
method: &str,
6485
path: &str,
@@ -78,6 +99,21 @@ pub fn detect_inference_pattern<'a>(
7899
.is_some_and(|suffix| suffix.starts_with('/'));
79100
}
80101

102+
if let Some((before, after)) = p.path_glob.split_once("/*/") {
103+
let Some(rest) = path_only.strip_prefix(before) else {
104+
return false;
105+
};
106+
let Some(rest) = rest.strip_prefix('/') else {
107+
return false;
108+
};
109+
// rest must look like `<id>/<after>` where <id> is non-empty
110+
// and contains no `/` (single path segment).
111+
let Some(slash_at) = rest.find('/') else {
112+
return false;
113+
};
114+
return slash_at > 0 && rest[slash_at + 1..] == *after;
115+
}
116+
81117
path_only == p.path_glob
82118
})
83119
}
@@ -445,6 +481,66 @@ mod tests {
445481
assert!(result.is_none());
446482
}
447483

484+
#[test]
485+
fn detect_aws_bedrock_invoke() {
486+
let patterns = default_patterns();
487+
let result =
488+
detect_inference_pattern("POST", "/model/anthropic.claude-opus-4-7/invoke", &patterns);
489+
assert!(result.is_some());
490+
assert_eq!(result.unwrap().protocol, "aws_bedrock_invoke");
491+
assert_eq!(result.unwrap().kind, "messages");
492+
}
493+
494+
#[test]
495+
fn detect_aws_bedrock_invoke_stream() {
496+
let patterns = default_patterns();
497+
let result = detect_inference_pattern(
498+
"POST",
499+
"/model/anthropic.claude-opus-4-7/invoke-with-response-stream",
500+
&patterns,
501+
);
502+
assert!(result.is_some());
503+
assert_eq!(result.unwrap().protocol, "aws_bedrock_invoke_stream");
504+
}
505+
506+
#[test]
507+
fn aws_bedrock_invoke_with_query_string() {
508+
let patterns = default_patterns();
509+
let result = detect_inference_pattern("POST", "/model/foo.bar/invoke?trace=1", &patterns);
510+
assert!(result.is_some());
511+
assert_eq!(result.unwrap().protocol, "aws_bedrock_invoke");
512+
}
513+
514+
#[test]
515+
fn aws_bedrock_rejects_empty_model_id() {
516+
let patterns = default_patterns();
517+
// `/model//invoke` — empty wildcard segment is not a valid Bedrock id.
518+
assert!(detect_inference_pattern("POST", "/model//invoke", &patterns).is_none());
519+
}
520+
521+
#[test]
522+
fn aws_bedrock_rejects_multi_segment_model_id() {
523+
let patterns = default_patterns();
524+
// The `*` matches a single path segment only; multi-segment ids must
525+
// not match (would be a path-traversal liability otherwise).
526+
assert!(detect_inference_pattern("POST", "/model/foo/bar/invoke", &patterns).is_none());
527+
}
528+
529+
#[test]
530+
fn aws_bedrock_rejects_get() {
531+
let patterns = default_patterns();
532+
assert!(
533+
detect_inference_pattern("GET", "/model/anthropic.claude-opus-4-7/invoke", &patterns)
534+
.is_none()
535+
);
536+
}
537+
538+
#[test]
539+
fn aws_bedrock_rejects_unknown_action() {
540+
let patterns = default_patterns();
541+
assert!(detect_inference_pattern("POST", "/model/foo/converse", &patterns).is_none());
542+
}
543+
448544
#[test]
449545
fn parse_simple_post_request() {
450546
let body = b"{\"hello\":true}";

0 commit comments

Comments
 (0)