Skip to content

Commit e7a978c

Browse files
committed
fix(providers): reject caller-supplied STS endpoint override
The aws_sts_assume_role refresh read an sts_endpoint_url from refresh material and, when it pointed at a loopback host, redirected the AWS-signed AssumeRole request there. That let a caller aim a signed STS request at an arbitrary loopback service (CWE-918 SSRF) via undocumented production material. Compile the endpoint override out of production builds: it is now a test-only helper that unit tests use to target a local mock STS. Reject the sts_endpoint_url material key at the configure boundary so it can never reach a stored refresh state outside tests. Production always resolves the STS endpoint from the region. Signed-off-by: Russell Bryant <[email protected]>
1 parent 2c6dfb8 commit e7a978c

2 files changed

Lines changed: 119 additions & 4 deletions

File tree

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,15 @@ pub(super) async fn handle_configure_provider_refresh(
20752075
"refresh token endpoints must be defined by the provider profile, not material",
20762076
));
20772077
}
2078+
// The STS endpoint is always resolved from the region in production. Reject
2079+
// any caller-supplied override so an AWS-signed AssumeRole request cannot be
2080+
// redirected at an arbitrary service (CWE-918). Tests inject a mock endpoint
2081+
// through test-only code paths, not this boundary.
2082+
if request.material.contains_key("sts_endpoint_url") {
2083+
return Err(Status::invalid_argument(
2084+
"sts_endpoint_url material is not permitted",
2085+
));
2086+
}
20782087
if request
20792088
.expires_at_ms
20802089
.is_some_and(|expires_at_ms| expires_at_ms < 0)
@@ -6199,6 +6208,90 @@ mod tests {
61996208
);
62006209
}
62016210

6211+
#[tokio::test]
6212+
async fn configure_aws_sts_rejects_endpoint_override_material() {
6213+
use crate::grpc::StoredSettingValue;
6214+
use crate::grpc::StoredSettings;
6215+
use crate::grpc::policy::save_global_settings;
6216+
6217+
let state = test_server_state().await;
6218+
let global_settings = StoredSettings {
6219+
revision: 1,
6220+
settings: std::iter::once((
6221+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY.to_string(),
6222+
StoredSettingValue::Bool(true),
6223+
))
6224+
.collect(),
6225+
..Default::default()
6226+
};
6227+
save_global_settings(state.store.as_ref(), &global_settings)
6228+
.await
6229+
.unwrap();
6230+
6231+
create_provider_record(
6232+
state.store.as_ref(),
6233+
Provider {
6234+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
6235+
id: String::new(),
6236+
name: "aws-endpoint-override".to_string(),
6237+
created_at_ms: 0,
6238+
labels: HashMap::new(),
6239+
resource_version: 0,
6240+
}),
6241+
r#type: "aws".to_string(),
6242+
credentials: HashMap::new(),
6243+
config: HashMap::new(),
6244+
credential_expires_at_ms: HashMap::new(),
6245+
},
6246+
)
6247+
.await
6248+
.unwrap();
6249+
6250+
let err = handle_configure_provider_refresh(
6251+
&state,
6252+
Request::new(ConfigureProviderRefreshRequest {
6253+
provider: "aws-endpoint-override".to_string(),
6254+
credential_key: "AWS_ACCESS_KEY_ID".to_string(),
6255+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole as i32,
6256+
material: HashMap::from([
6257+
(
6258+
"role_arn".to_string(),
6259+
"arn:aws:iam::123456789012:role/Test".to_string(),
6260+
),
6261+
(
6262+
"sts_endpoint_url".to_string(),
6263+
"http://127.0.0.1:1/".to_string(),
6264+
),
6265+
]),
6266+
secret_material_keys: Vec::new(),
6267+
expires_at_ms: None,
6268+
}),
6269+
)
6270+
.await
6271+
.unwrap_err();
6272+
6273+
assert_eq!(err.code(), Code::InvalidArgument);
6274+
assert!(err.message().contains("sts_endpoint_url"));
6275+
6276+
// The rejected configuration must not leave a refresh state behind.
6277+
let provider = state
6278+
.store
6279+
.get_message_by_name::<Provider>("aws-endpoint-override")
6280+
.await
6281+
.unwrap()
6282+
.unwrap();
6283+
assert!(
6284+
crate::provider_refresh::get_refresh_state(
6285+
state.store.as_ref(),
6286+
provider.object_id(),
6287+
"AWS_ACCESS_KEY_ID",
6288+
)
6289+
.await
6290+
.unwrap()
6291+
.is_none()
6292+
);
6293+
}
6294+
62026295
#[tokio::test]
62036296
async fn configure_aws_sts_persists_resolved_additional_output_keys() {
62046297
use crate::grpc::StoredSettingValue;

‎crates/openshell-server/src/provider_refresh.rs‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,11 @@ async fn mint_aws_sts_assume_role(
600600
let sdk_config = config_loader.load().await;
601601
let sts_config = {
602602
let mut builder = aws_sdk_sts::config::Builder::from(&sdk_config);
603-
if let Some(endpoint) = material_value(&state.material, &["sts_endpoint_url"])
604-
&& let Ok(parsed) = reqwest::Url::parse(&endpoint)
605-
&& parsed.host_str().is_some_and(is_loopback_host)
606-
{
603+
// Endpoint overrides exist only to point tests at a local mock STS. In
604+
// production the endpoint is always resolved from the region so a caller
605+
// cannot redirect an AWS-signed AssumeRole request at an arbitrary
606+
// service (CWE-918). See `test_sts_endpoint_override`.
607+
if let Some(endpoint) = test_sts_endpoint_override(state) {
607608
builder = builder.endpoint_url(endpoint);
608609
}
609610
builder.build()
@@ -815,6 +816,27 @@ fn is_loopback_host(host: &str) -> bool {
815816
matches!(host, "localhost" | "127.0.0.1" | "::1")
816817
}
817818

819+
/// Test-only STS endpoint override. Reads the `sts_endpoint_url` material and
820+
/// accepts it only when it is a loopback URL, so unit tests can target a local
821+
/// mock STS. Compiled out of production builds entirely: the configure boundary
822+
/// also rejects the `sts_endpoint_url` material key, so it can never reach a
823+
/// stored refresh state outside tests.
824+
#[cfg(test)]
825+
fn test_sts_endpoint_override(state: &StoredProviderCredentialRefreshState) -> Option<String> {
826+
material_value(&state.material, &["sts_endpoint_url"]).filter(|endpoint| {
827+
reqwest::Url::parse(endpoint)
828+
.ok()
829+
.and_then(|parsed| parsed.host_str().map(is_loopback_host))
830+
.unwrap_or(false)
831+
})
832+
}
833+
834+
#[cfg(not(test))]
835+
#[allow(clippy::missing_const_for_fn)]
836+
fn test_sts_endpoint_override(_state: &StoredProviderCredentialRefreshState) -> Option<String> {
837+
None
838+
}
839+
818840
pub fn spawn_refresh_worker(state: std::sync::Arc<crate::ServerState>, interval: Duration) {
819841
info!(
820842
interval_seconds = interval.as_secs(),

0 commit comments

Comments
 (0)