mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
codex: harden dynamic hook naming
This commit is contained in:
@@ -109,7 +109,7 @@ pub(crate) fn matcher_pattern_for_event(
|
||||
}
|
||||
|
||||
pub(crate) fn validate_matcher_pattern(matcher: &str) -> Result<(), regex::Error> {
|
||||
if is_match_all_matcher(matcher) || is_exact_matcher(matcher) {
|
||||
if is_match_all_matcher(matcher) || is_literal_tool_name_matcher(matcher) {
|
||||
return Ok(());
|
||||
}
|
||||
regex::Regex::new(matcher).map(|_| ())
|
||||
@@ -119,7 +119,7 @@ pub(crate) fn matches_matcher(matcher: Option<&str>, input: Option<&str>) -> boo
|
||||
match matcher {
|
||||
None => true,
|
||||
Some(matcher) if is_match_all_matcher(matcher) => true,
|
||||
Some(matcher) if is_exact_matcher(matcher) => input
|
||||
Some(matcher) if is_literal_tool_name_matcher(matcher) => input
|
||||
.map(|input| matcher.split('|').any(|candidate| candidate == input))
|
||||
.unwrap_or(false),
|
||||
Some(matcher) => input
|
||||
@@ -147,10 +147,19 @@ fn is_match_all_matcher(matcher: &str) -> bool {
|
||||
matcher.is_empty() || matcher == "*"
|
||||
}
|
||||
|
||||
fn is_exact_matcher(matcher: &str) -> bool {
|
||||
fn is_literal_tool_name_matcher(matcher: &str) -> bool {
|
||||
matcher
|
||||
.chars()
|
||||
.all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '|')
|
||||
.split('|')
|
||||
.all(|candidate| !candidate.is_empty() && !contains_regex_syntax(candidate))
|
||||
}
|
||||
|
||||
fn contains_regex_syntax(candidate: &str) -> bool {
|
||||
candidate.chars().any(|ch| {
|
||||
matches!(
|
||||
ch,
|
||||
'\\' | '^' | '$' | '*' | '(' | ')' | '[' | ']' | '{' | '}'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -194,6 +203,18 @@ mod tests {
|
||||
fn literal_matcher_uses_exact_matching() {
|
||||
assert!(matches_matcher(Some("Bash"), Some("Bash")));
|
||||
assert!(!matches_matcher(Some("Bash"), Some("BashOutput")));
|
||||
assert!(matches_matcher(
|
||||
Some("tool-search.v2"),
|
||||
Some("tool-search.v2")
|
||||
));
|
||||
assert!(!matches_matcher(
|
||||
Some("tool-search.v2"),
|
||||
Some("tool-searchXv2")
|
||||
));
|
||||
assert!(matches_matcher(
|
||||
Some("lookup+ticket"),
|
||||
Some("lookup+ticket")
|
||||
));
|
||||
assert!(matches_matcher(
|
||||
Some("dynamic__codex_app__automation_update"),
|
||||
Some("dynamic__codex_app__automation_update")
|
||||
@@ -210,6 +231,8 @@ mod tests {
|
||||
Some("mcp__memory"),
|
||||
Some("mcp__memory__create_entities")
|
||||
));
|
||||
assert_eq!(validate_matcher_pattern("tool-search.v2"), Ok(()));
|
||||
assert_eq!(validate_matcher_pattern("lookup+ticket"), Ok(()));
|
||||
assert_eq!(validate_matcher_pattern("mcp__memory"), Ok(()));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user