Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions crates/openshell-core/src/provider_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! Runtime provider credential snapshots.

use crate::secrets::{CredentialPlacement, SecretResolver};
use crate::secrets::{CredentialPlacement, PathTemplate, SecretResolver};
use crate::{
endpoint_path::EndpointPathPattern,
host_pattern::HostPattern,
Expand Down Expand Up @@ -101,8 +101,6 @@ impl std::error::Error for StaticCredentialBindingError {}
/// Reserved entries in the existing credential map carry static placement only.
pub const STATIC_CREDENTIAL_METADATA_PREFIX: &str = "static-env:";

const CREDENTIAL_PATH_SLOT: &str = "{credential}";

fn split_static_credential_metadata(
credentials: HashMap<String, crate::proto::ProviderProfileCredential>,
) -> (
Expand Down Expand Up @@ -1175,14 +1173,7 @@ fn compile_credential_placement(
{
CredentialPlacement::QueryParam(metadata.query_param.trim().to_string())
}
"path"
if metadata.path_template.starts_with('/')
&& metadata.path_template.matches(CREDENTIAL_PATH_SLOT).count() == 1
&& !metadata
.path_template
.chars()
.any(|c| c.is_control() || "?#".contains(c)) =>
{
"path" if PathTemplate::parse(&metadata.path_template).is_some() => {
CredentialPlacement::PathTemplate(metadata.path_template.clone())
}
_ => {
Expand Down Expand Up @@ -1687,6 +1678,52 @@ mod tests {
}
}

#[test]
fn subtree_path_template_serves_every_method_with_one_credential() {
let env = HashMap::from([("API_KEY".into(), "123456:synthetic".into())]);
let bindings =
HashMap::from([("API_KEY".into(), binding("bot-api.example.com", 443, "/**"))]);
let state = ProviderCredentialState::from_bound_environment(
7,
env.clone(),
HashMap::new(),
HashMap::new(),
bindings.clone(),
vec![],
)
.unwrap();
let mut metadata = placement_metadata("path");
metadata.values_mut().next().unwrap().path_template = "/bot{credential}/**".into();
assert_eq!(
state
.install_bound_environment(8, env, HashMap::new(), metadata, bindings, vec![])
.unwrap(),
1
);
let reference = state.snapshot().child_env["API_KEY"].clone();
for method in ["sendMessage", "getUpdates", "editMessageText"] {
let target = format!("/bot{reference}/{method}");
let resolver = state
.resolver_for_endpoint("bot-api.example.com", 443, &target)
.unwrap();
assert_eq!(
crate::secrets::rewrite_target_for_eval(&target, &resolver)
.unwrap()
.resolved,
format!("/bot123456:synthetic/{method}")
);
}
let moved = format!("/v1/{reference}/getMe");
let resolver = state
.resolver_for_endpoint("bot-api.example.com", 443, &moved)
.unwrap();
assert!(
crate::secrets::rewrite_target_for_eval(&moved, &resolver)
.unwrap_err()
.is_placement_mismatch()
);
}

#[test]
fn malformed_static_metadata_revokes_static_material_and_preserves_real_grants() {
for malformed in 0..11 {
Expand Down
254 changes: 252 additions & 2 deletions crates/openshell-core/src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,76 @@ pub enum CredentialPlacement {
PathTemplate(String),
}

/// Authored `path_template` of a credential placed in the request path.
///
/// A template is a request path with exactly one `{credential}` slot. It
/// matches only that path, unless it ends in `/**`. The terminal `/**` also
/// matches everything below the path through the slot, so one credential serves
/// every method of an API that keeps its token in a path prefix. A `*` anywhere
/// else is an ordinary character.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathTemplate {
/// The template without a terminal `/**`.
prefix: String,
/// Byte offset of the credential slot within `prefix`.
slot_offset: usize,
subtree: bool,
}

impl PathTemplate {
/// The slot a credential placeholder occupies.
pub const SLOT: &'static str = "{credential}";
const SUBTREE_SUFFIX: &'static str = "/**";

/// Parse an authored template, or `None` when it cannot place a credential.
#[must_use]
pub fn parse(template: &str) -> Option<Self> {
if !template.starts_with('/')
|| template.matches(Self::SLOT).count() != 1
|| template.chars().any(|c| c.is_control() || "?#".contains(c))
{
return None;
}
let (prefix, subtree) = template
.strip_suffix(Self::SUBTREE_SUFFIX)
.map_or((template, false), |prefix| (prefix, true));
Some(Self {
slot_offset: prefix.find(Self::SLOT)?,
prefix: prefix.to_string(),
subtree,
})
}

/// Whether the template ends in `/**`.
#[must_use]
pub fn is_subtree(&self) -> bool {
self.subtree
}

/// Match a request path whose own credential token was replaced by the slot.
///
/// `slot_at` is where that replacement happened in `normalized`. It must
/// equal the declared slot: `{credential}` text the client wrote itself is
/// not a slot, and would otherwise let the real token sit in the wildcard.
fn permits_subtree(&self, normalized: &str, slot_at: usize) -> bool {
if slot_at != self.slot_offset {
return false;
}
let Some(tail) = normalized.strip_prefix(self.prefix.as_str()) else {
return false;
};
if tail.is_empty() {
return true;
}
// Descendants only: the slot's segment cannot continue past the template.
tail.strip_prefix('/').is_some_and(|descendants| {
descendants
.split('/')
.all(|segment| segment != "." && segment != "..")
})
}
}

/// The actual destination of a workload-provided credential placeholder.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RequestCredentialLocation {
Expand Down Expand Up @@ -513,9 +583,11 @@ impl SecretResolver {
Some(RequestCredentialLocation::Path(actual)),
) => {
// Normalize only this credential's slot; all other path text must match.
let subtree = PathTemplate::parse(expected).filter(PathTemplate::is_subtree);
let mut normalized = String::new();
let mut pos = 0;
let mut slots = 0;
let mut slot_at = 0;
while pos < actual.len() {
let next_canonical = actual[pos..]
.find(PLACEHOLDER_PREFIX)
Expand All @@ -533,14 +605,19 @@ impl SecretResolver {
return false;
};
if placeholder_env_key(token) == Some(key) {
normalized.push_str("{credential}");
slot_at = normalized.len();
normalized.push_str(PathTemplate::SLOT);
slots += 1;
} else {
normalized.push_str(token);
}
pos = end;
}
slots == 1 && normalized == *expected
slots == 1
&& subtree.as_ref().map_or_else(
|| normalized == *expected,
|template| template.permits_subtree(&normalized, slot_at),
)
}
_ => false,
}
Expand Down Expand Up @@ -1862,6 +1939,179 @@ mod tests {
);
}

#[test]
fn path_subtree_template_permits_the_base_and_every_descendant() {
let resolver = placement_resolver(CredentialPlacement::PathTemplate(
"/bot{credential}/**".into(),
));
let token = "openshell:resolve:env:v1_TOKEN";
for (suffix, resolved) in [
("/sendMessage", "/botsecret-value/sendMessage"),
("/getUpdates", "/botsecret-value/getUpdates"),
("/files/a/b", "/botsecret-value/files/a/b"),
("/", "/botsecret-value/"),
("", "/botsecret-value"),
] {
let result = rewrite_target_for_eval(&format!("/bot{token}{suffix}"), &resolver)
.unwrap_or_else(|error| panic!("{suffix:?} must be permitted: {error}"));
assert_eq!(result.resolved, resolved);
assert_eq!(result.redacted, format!("/bot[CREDENTIAL]{suffix}"));
}
let encoded = format!("/bot{}/getMe", fully_percent_encoded(token));
assert!(rewrite_target_for_eval(&encoded, &resolver).is_ok());
}

#[test]
fn path_subtree_template_keeps_the_credential_in_its_declared_slot() {
let resolver = placement_resolver(CredentialPlacement::PathTemplate(
"/bot{credential}/**".into(),
));
let token = "openshell:resolve:env:v1_TOKEN";
let forged_slot = "{credential}";
let encoded_forged_slot = "%7Bcredential%7D";
for target in [
// The fixed text around the slot must match.
format!("/other{token}/getMe"),
format!("/prefix/bot{token}/getMe"),
// The slot ends at a path boundary, so a sibling segment is not a descendant.
format!("/bot{token}.suffix/getMe"),
format!("/bot{token}-extra/getMe"),
// One occurrence only, even when the second one is in the wildcard tail.
format!("/bot{token}/echo/{token}"),
// Literal `{credential}` text is not a credential slot. The real token
// would otherwise land in the wildcard tail.
format!("/bot{forged_slot}/echo/{token}"),
format!("/bot{encoded_forged_slot}/echo/{token}"),
// The credential may not appear where the template declares no slot.
format!("/{token}/getMe"),
] {
assert!(
rewrite_target_for_eval(&target, &resolver)
.unwrap_err()
.is_placement_mismatch(),
"{target} must be a placement mismatch"
);
}
}

#[test]
fn path_subtree_template_rejects_dot_segments_in_the_wildcard_tail() {
let resolver = placement_resolver(CredentialPlacement::PathTemplate(
"/bot{credential}/**".into(),
));
let token = "openshell:resolve:env:v1_TOKEN";
for target in [
format!("/bot{token}/../admin"),
format!("/bot{token}/a/./b"),
format!("/bot{token}/%2e%2e/admin"),
] {
assert!(
rewrite_target_for_eval(&target, &resolver)
.unwrap_err()
.is_placement_mismatch(),
"{target} must be a placement mismatch"
);
}
// A dot inside a segment name is an ordinary character.
assert!(rewrite_target_for_eval(&format!("/bot{token}/file..txt"), &resolver).is_ok());
}

#[test]
fn path_subtree_template_does_not_widen_other_credentials() {
let (_, resolver) = SecretResolver::from_provider_env_for_revision(
HashMap::from([
("TOKEN".to_string(), "secret-value".to_string()),
("OTHER".to_string(), "other-value".to_string()),
]),
HashMap::new(),
1,
);
let resolver = resolver.unwrap().with_placements(HashMap::from([
(
"TOKEN".to_string(),
CredentialPlacement::PathTemplate("/bot{credential}/**".into()),
),
(
"OTHER".to_string(),
CredentialPlacement::Header("x-other".into()),
),
]));
// OTHER declares a header, so the TOKEN wildcard tail cannot carry it.
assert!(
rewrite_target_for_eval(
"/botopenshell:resolve:env:v1_TOKEN/openshell:resolve:env:v1_OTHER",
&resolver
)
.unwrap_err()
.is_placement_mismatch()
);
}

#[test]
fn path_template_grammar_recognizes_only_a_terminal_subtree_marker() {
let exact = PathTemplate::parse("/bot{credential}/sendMessage").unwrap();
assert!(!exact.is_subtree());

let subtree = PathTemplate::parse("/bot{credential}/**").unwrap();
assert!(subtree.is_subtree());
assert!(
PathTemplate::parse("/{credential}/**")
.unwrap()
.is_subtree()
);

// `*` elsewhere, and a single `*`, are ordinary characters.
for template in [
"/bot{credential}/*",
"/bot{credential}/**/x",
"/*/{credential}",
"/bot{credential}**",
] {
assert!(
!PathTemplate::parse(template).unwrap().is_subtree(),
"{template}"
);
}

// The slot appears exactly once and the path stays a bare path.
for template in [
"/bot/**",
"/bot{credential}/{credential}/**",
"bot{credential}/**",
"/bot{credential}/**?x=1",
"/bot{credential}/**#x",
"/bot{credential}/\n**",
"",
] {
assert!(PathTemplate::parse(template).is_none(), "{template:?}");
}
}

#[test]
fn literal_star_in_an_exact_path_template_stays_literal() {
// Only a terminal `/**` is a wildcard. Every other template keeps its
// exact meaning, including the `*` characters it already contained.
for template in [
"/bot{credential}/*",
"/bot{credential}/a*",
"/*/{credential}",
] {
let resolver = placement_resolver(CredentialPlacement::PathTemplate(template.into()));
let token = "openshell:resolve:env:v1_TOKEN";
let literal = template.replace("{credential}", token);
assert!(
rewrite_target_for_eval(&literal, &resolver).is_ok(),
"{template} must still match itself literally"
);
assert!(
rewrite_target_for_eval(&format!("/bot{token}/sendMessage"), &resolver)
.unwrap_err()
.is_placement_mismatch(),
"{template} must not match other paths"
);
}
}

fn fully_percent_encoded(marker: &str) -> String {
const HEX: &[u8; 16] = b"0123456789ABCDEF";
let mut encoded = String::with_capacity(marker.len() * 3);
Expand Down
Loading
Loading