mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Split in-memory execution policy rules into a separate crate (#62615)
## Why Configuration, protocol, and prompt code use in-memory policies and previously pulled in the Starlark parser through `codex-execpolicy`. ## What changed Move policy construction, rule validation, and evaluation into `codex-execpolicy-rules` and use it directly from those crates. Keep Starlark policy loading and file updates in `codex-execpolicy`, which re-exports the shared rule types and wraps rule validation errors while preserving their source locations. GitOrigin-RevId: 5bc70d3cd387b43a3ad1fcc56af96ad169ac8b13
This commit is contained in:
Generated
+16
-4
@@ -2937,7 +2937,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
"codex-execpolicy",
|
||||
"codex-execpolicy-rules",
|
||||
"codex-features",
|
||||
"codex-file-system",
|
||||
"codex-git-utils",
|
||||
@@ -3519,8 +3519,8 @@ name = "codex-execpolicy"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"caseless",
|
||||
"clap",
|
||||
"codex-execpolicy-rules",
|
||||
"codex-utils-absolute-path",
|
||||
"multimap",
|
||||
"pretty_assertions",
|
||||
@@ -3531,6 +3531,18 @@ dependencies = [
|
||||
"tempfile",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "codex-execpolicy-rules"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"caseless",
|
||||
"codex-utils-absolute-path",
|
||||
"multimap",
|
||||
"serde",
|
||||
"shlex",
|
||||
"thiserror 2.0.18",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
@@ -4713,7 +4725,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"codex-collaboration-mode-templates",
|
||||
"codex-context-fragments",
|
||||
"codex-execpolicy",
|
||||
"codex-execpolicy-rules",
|
||||
"codex-git-utils",
|
||||
"codex-guardian-context",
|
||||
"codex-protocol",
|
||||
@@ -4733,7 +4745,7 @@ dependencies = [
|
||||
"chardetng",
|
||||
"chrono",
|
||||
"codex-async-utils",
|
||||
"codex-execpolicy",
|
||||
"codex-execpolicy-rules",
|
||||
"codex-extension-items",
|
||||
"codex-http-client",
|
||||
"codex-network-proxy",
|
||||
|
||||
@@ -65,6 +65,7 @@ members = [
|
||||
"exec-server-cli",
|
||||
"exec-server/tests/support",
|
||||
"execpolicy",
|
||||
"execpolicy-rules",
|
||||
"ext/agent",
|
||||
"ext/agent-message-board",
|
||||
"ext/connectors",
|
||||
@@ -244,6 +245,7 @@ codex-exec-server = { path = "exec-server" }
|
||||
codex-exec-server-cli = { path = "exec-server-cli" }
|
||||
codex-exec-server-test-support = { path = "exec-server/tests/support" }
|
||||
codex-execpolicy = { path = "execpolicy" }
|
||||
codex-execpolicy-rules = { path = "execpolicy-rules" }
|
||||
codex-extension-api = { path = "ext/extension-api" }
|
||||
codex-extension-items = { path = "ext/items" }
|
||||
codex-multi-env-copy-file = { path = "ext/multi-env-copy-file" }
|
||||
|
||||
@@ -14,7 +14,7 @@ workspace = true
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
codex-execpolicy = { workspace = true }
|
||||
codex-execpolicy-rules = { workspace = true }
|
||||
codex-features = { workspace = true }
|
||||
codex-file-system = { workspace = true }
|
||||
codex-git-utils = { workspace = true }
|
||||
|
||||
@@ -2773,9 +2773,9 @@ mod tests {
|
||||
use crate::McpServerIdentity;
|
||||
use crate::McpServerValueMatcher;
|
||||
use anyhow::Result;
|
||||
use codex_execpolicy::Decision;
|
||||
use codex_execpolicy::Evaluation;
|
||||
use codex_execpolicy::RuleMatch;
|
||||
use codex_execpolicy_rules::Decision;
|
||||
use codex_execpolicy_rules::Evaluation;
|
||||
use codex_execpolicy_rules::RuleMatch;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_absolute_path::AbsolutePathBufGuard;
|
||||
|
||||
@@ -94,7 +94,7 @@ pub use cloud_config_layers::CloudConfigFragment;
|
||||
pub use cloud_config_layers::CloudConfigFragmentSource;
|
||||
pub use cloud_config_layers::CloudConfigLayerError;
|
||||
pub use cloud_config_layers::cloud_config_layers_from_fragments;
|
||||
pub use codex_execpolicy::RequirementsExecPolicy;
|
||||
pub use codex_execpolicy_rules::RequirementsExecPolicy;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub use codex_home_symlink::allowed_symlinked_codex_home;
|
||||
pub use codex_protocol::config_types::ProfileV2Name;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use codex_execpolicy::Decision;
|
||||
use codex_execpolicy::Policy;
|
||||
use codex_execpolicy::RequirementsExecPolicy;
|
||||
use codex_execpolicy::RuleRef;
|
||||
use codex_execpolicy::rule::PatternToken;
|
||||
use codex_execpolicy::rule::PrefixPattern;
|
||||
use codex_execpolicy::rule::PrefixRule;
|
||||
use codex_execpolicy_rules::Decision;
|
||||
use codex_execpolicy_rules::PatternToken;
|
||||
use codex_execpolicy_rules::Policy;
|
||||
use codex_execpolicy_rules::PrefixPattern;
|
||||
use codex_execpolicy_rules::PrefixRule;
|
||||
use codex_execpolicy_rules::RequirementsExecPolicy;
|
||||
use codex_execpolicy_rules::RuleRef;
|
||||
use multimap::MultiMap;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -9,12 +9,12 @@ use codex_config::ConfigLayerSource;
|
||||
use codex_config::ConfigLayerStack;
|
||||
use codex_execpolicy::AmendError;
|
||||
use codex_execpolicy::Decision;
|
||||
use codex_execpolicy::Error as ExecPolicyRuleError;
|
||||
use codex_execpolicy::Evaluation;
|
||||
use codex_execpolicy::MatchOptions;
|
||||
use codex_execpolicy::Policy;
|
||||
use codex_execpolicy::PolicyParser;
|
||||
use codex_execpolicy::RequirementsExecPolicy;
|
||||
use codex_execpolicy::RuleError as ExecPolicyRuleError;
|
||||
use codex_execpolicy::RuleMatch;
|
||||
use codex_execpolicy::blocking_append_allow_prefix_rule;
|
||||
use codex_execpolicy::blocking_append_network_rule;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "execpolicy-rules",
|
||||
crate_name = "codex_execpolicy_rules",
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
[package]
|
||||
name = "codex-execpolicy-rules"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
description = "In-memory execution policy rules and evaluation."
|
||||
|
||||
[lib]
|
||||
name = "codex_execpolicy_rules"
|
||||
path = "src/lib.rs"
|
||||
test = false
|
||||
doctest = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
caseless = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
multimap = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
shlex = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
unicode-normalization = { workspace = true }
|
||||
@@ -0,0 +1,84 @@
|
||||
//! Rule validation failures and their optional locations in the source policy.
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TextPosition {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TextRange {
|
||||
pub start: TextPosition,
|
||||
pub end: TextPosition,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ErrorLocation {
|
||||
pub path: String,
|
||||
pub range: TextRange,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("invalid decision: {0}")]
|
||||
InvalidDecision(String),
|
||||
#[error("invalid pattern element: {0}")]
|
||||
InvalidPattern(String),
|
||||
#[error("invalid example: {0}")]
|
||||
InvalidExample(String),
|
||||
#[error("invalid rule: {0}")]
|
||||
InvalidRule(String),
|
||||
#[error(
|
||||
"expected every example to match at least one rule. rules: {rules:?}; unmatched examples: \
|
||||
{examples:?}"
|
||||
)]
|
||||
ExampleDidNotMatch {
|
||||
rules: Vec<String>,
|
||||
examples: Vec<String>,
|
||||
location: Option<ErrorLocation>,
|
||||
},
|
||||
#[error("expected example to not match rule `{rule}`: {example}")]
|
||||
ExampleDidMatch {
|
||||
rule: String,
|
||||
example: String,
|
||||
location: Option<ErrorLocation>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn with_location(self, location: ErrorLocation) -> Self {
|
||||
match self {
|
||||
Error::ExampleDidNotMatch {
|
||||
rules,
|
||||
examples,
|
||||
location: None,
|
||||
} => Error::ExampleDidNotMatch {
|
||||
rules,
|
||||
examples,
|
||||
location: Some(location),
|
||||
},
|
||||
Error::ExampleDidMatch {
|
||||
rule,
|
||||
example,
|
||||
location: None,
|
||||
} => Error::ExampleDidMatch {
|
||||
rule,
|
||||
example,
|
||||
location: Some(location),
|
||||
},
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn location(&self) -> Option<ErrorLocation> {
|
||||
match self {
|
||||
Error::ExampleDidNotMatch { location, .. }
|
||||
| Error::ExampleDidMatch { location, .. } => location.clone(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -3,7 +3,9 @@ use std::path::Path;
|
||||
#[cfg(windows)]
|
||||
const WINDOWS_EXECUTABLE_SUFFIXES: [&str; 4] = [".exe", ".cmd", ".bat", ".com"];
|
||||
|
||||
pub(crate) fn executable_lookup_key(raw: &str) -> String {
|
||||
/// Return the key shared by host executable declarations and command lookups.
|
||||
/// Windows folds ASCII case and ignores a recognized executable suffix.
|
||||
pub fn executable_lookup_key(raw: &str) -> String {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let raw = raw.to_ascii_lowercase();
|
||||
@@ -22,7 +24,8 @@ pub(crate) fn executable_lookup_key(raw: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn executable_path_lookup_key(path: &Path) -> Option<String> {
|
||||
/// Derive a lookup key from the filename when it is valid UTF-8.
|
||||
pub fn executable_path_lookup_key(path: &Path) -> Option<String> {
|
||||
path.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(executable_lookup_key)
|
||||
@@ -0,0 +1,29 @@
|
||||
//! Construction, validation, and evaluation of in-memory execution policies.
|
||||
//!
|
||||
//! Use `codex-execpolicy` to load rules from a Starlark policy file.
|
||||
|
||||
mod decision;
|
||||
mod error;
|
||||
mod executable_name;
|
||||
mod policy;
|
||||
pub mod rule;
|
||||
|
||||
pub use decision::Decision;
|
||||
pub use error::Error;
|
||||
pub use error::ErrorLocation;
|
||||
pub use error::Result;
|
||||
pub use error::TextPosition;
|
||||
pub use error::TextRange;
|
||||
pub use executable_name::executable_lookup_key;
|
||||
pub use executable_name::executable_path_lookup_key;
|
||||
pub use policy::Evaluation;
|
||||
pub use policy::MatchOptions;
|
||||
pub use policy::Policy;
|
||||
pub use policy::RequirementsExecPolicy;
|
||||
pub use rule::NetworkRuleProtocol;
|
||||
pub use rule::PatternToken;
|
||||
pub use rule::PrefixPattern;
|
||||
pub use rule::PrefixRule;
|
||||
pub use rule::Rule;
|
||||
pub use rule::RuleMatch;
|
||||
pub use rule::RuleRef;
|
||||
@@ -166,7 +166,8 @@ pub struct NetworkRule {
|
||||
pub justification: Option<String>,
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_network_rule_host(raw: &str) -> Result<String> {
|
||||
/// Normalize a host for matching, dropping a port and IPv6 brackets when present.
|
||||
pub fn normalize_network_rule_host(raw: &str) -> Result<String> {
|
||||
let mut host = raw.trim();
|
||||
if host.is_empty() {
|
||||
return Err(Error::InvalidRule(
|
||||
@@ -256,7 +257,7 @@ impl Rule for PrefixRule {
|
||||
}
|
||||
|
||||
/// Count how many rules match each provided example and error if any example is unmatched.
|
||||
pub(crate) fn validate_match_examples(
|
||||
pub fn validate_match_examples(
|
||||
policy: &Policy,
|
||||
rules: &[RuleRef],
|
||||
matches: &[Vec<String>],
|
||||
@@ -292,7 +293,7 @@ pub(crate) fn validate_match_examples(
|
||||
}
|
||||
|
||||
/// Ensure that no rule matches any provided negative example.
|
||||
pub(crate) fn validate_not_match_examples(
|
||||
pub fn validate_not_match_examples(
|
||||
policy: &Policy,
|
||||
_rules: &[RuleRef],
|
||||
not_matches: &[Vec<String>],
|
||||
@@ -18,9 +18,9 @@ path = "src/main.rs"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
caseless = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true, features = ["derive"] }
|
||||
codex-execpolicy-rules = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
multimap = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
@@ -30,7 +30,6 @@ starlark = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tokio = { workspace = true, features = ["fs", "io-util", "macros", "rt"] }
|
||||
unicode-normalization = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = { workspace = true }
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::decision::Decision;
|
||||
use crate::Decision;
|
||||
use crate::rule::NetworkRuleProtocol;
|
||||
use crate::rule::normalize_network_rule_host;
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -1,101 +1,47 @@
|
||||
use starlark::Error as StarlarkError;
|
||||
//! Diagnostics for loading and validating a Starlark policy file.
|
||||
|
||||
use crate::ErrorLocation;
|
||||
use crate::RuleError;
|
||||
use codex_execpolicy_rules::TextPosition;
|
||||
use codex_execpolicy_rules::TextRange;
|
||||
use starlark::codemap::FileSpan;
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TextPosition {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TextRange {
|
||||
pub start: TextPosition,
|
||||
pub end: TextPosition,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ErrorLocation {
|
||||
pub path: String,
|
||||
pub range: TextRange,
|
||||
}
|
||||
|
||||
/// A failure to evaluate a policy file or validate its rules.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("invalid decision: {0}")]
|
||||
InvalidDecision(String),
|
||||
#[error("invalid pattern element: {0}")]
|
||||
InvalidPattern(String),
|
||||
#[error("invalid example: {0}")]
|
||||
InvalidExample(String),
|
||||
#[error("invalid rule: {0}")]
|
||||
InvalidRule(String),
|
||||
#[error(
|
||||
"expected every example to match at least one rule. rules: {rules:?}; unmatched examples: \
|
||||
{examples:?}"
|
||||
)]
|
||||
ExampleDidNotMatch {
|
||||
rules: Vec<String>,
|
||||
examples: Vec<String>,
|
||||
location: Option<ErrorLocation>,
|
||||
},
|
||||
#[error("expected example to not match rule `{rule}`: {example}")]
|
||||
ExampleDidMatch {
|
||||
rule: String,
|
||||
example: String,
|
||||
location: Option<ErrorLocation>,
|
||||
},
|
||||
#[error("starlark error: {0}")]
|
||||
Starlark(StarlarkError),
|
||||
Starlark(starlark::Error),
|
||||
#[error(transparent)]
|
||||
Rule(#[from] RuleError),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn with_location(self, location: ErrorLocation) -> Self {
|
||||
match self {
|
||||
Error::ExampleDidNotMatch {
|
||||
rules,
|
||||
examples,
|
||||
location: None,
|
||||
} => Error::ExampleDidNotMatch {
|
||||
rules,
|
||||
examples,
|
||||
location: Some(location),
|
||||
},
|
||||
Error::ExampleDidMatch {
|
||||
rule,
|
||||
example,
|
||||
location: None,
|
||||
} => Error::ExampleDidMatch {
|
||||
rule,
|
||||
example,
|
||||
location: Some(location),
|
||||
},
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the one-based source range when the failure has a span.
|
||||
pub fn location(&self) -> Option<ErrorLocation> {
|
||||
match self {
|
||||
Error::ExampleDidNotMatch { location, .. }
|
||||
| Error::ExampleDidMatch { location, .. } => location.clone(),
|
||||
Error::Starlark(err) => err.span().map(|span| {
|
||||
let resolved = span.resolve_span();
|
||||
ErrorLocation {
|
||||
path: span.filename().to_string(),
|
||||
range: TextRange {
|
||||
start: TextPosition {
|
||||
line: resolved.begin.line + 1,
|
||||
column: resolved.begin.column + 1,
|
||||
},
|
||||
end: TextPosition {
|
||||
line: resolved.end.line + 1,
|
||||
column: resolved.end.column + 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
}),
|
||||
_ => None,
|
||||
Error::Starlark(error) => error.span().map(error_location_from_file_span),
|
||||
Error::Rule(error) => error.location(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a parser span to the one-based coordinates used for policy diagnostics.
|
||||
pub(crate) fn error_location_from_file_span(span: &FileSpan) -> ErrorLocation {
|
||||
let resolved = span.resolve_span();
|
||||
ErrorLocation {
|
||||
path: span.filename().to_string(),
|
||||
range: TextRange {
|
||||
start: TextPosition {
|
||||
line: resolved.begin.line + 1,
|
||||
column: resolved.begin.column + 1,
|
||||
},
|
||||
end: TextPosition {
|
||||
line: resolved.end.line + 1,
|
||||
column: resolved.end.column + 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,31 @@
|
||||
pub(crate) mod amend;
|
||||
pub(crate) mod decision;
|
||||
pub(crate) mod error;
|
||||
pub(crate) mod execpolicycheck;
|
||||
mod executable_name;
|
||||
pub(crate) mod parser;
|
||||
pub(crate) mod policy;
|
||||
pub mod rule;
|
||||
mod sandbox_migration;
|
||||
|
||||
pub use amend::AmendError;
|
||||
pub use amend::blocking_append_allow_prefix_rule;
|
||||
pub use amend::blocking_append_network_rule;
|
||||
pub use decision::Decision;
|
||||
pub use codex_execpolicy_rules::Decision;
|
||||
pub use codex_execpolicy_rules::Error as RuleError;
|
||||
pub use codex_execpolicy_rules::ErrorLocation;
|
||||
pub use codex_execpolicy_rules::Evaluation;
|
||||
pub use codex_execpolicy_rules::MatchOptions;
|
||||
pub use codex_execpolicy_rules::NetworkRuleProtocol;
|
||||
pub use codex_execpolicy_rules::PatternToken;
|
||||
pub use codex_execpolicy_rules::Policy;
|
||||
pub use codex_execpolicy_rules::PrefixPattern;
|
||||
pub use codex_execpolicy_rules::PrefixRule;
|
||||
pub use codex_execpolicy_rules::RequirementsExecPolicy;
|
||||
pub use codex_execpolicy_rules::Rule;
|
||||
pub use codex_execpolicy_rules::RuleMatch;
|
||||
pub use codex_execpolicy_rules::RuleRef;
|
||||
pub use codex_execpolicy_rules::TextPosition;
|
||||
pub use codex_execpolicy_rules::TextRange;
|
||||
pub use codex_execpolicy_rules::rule;
|
||||
pub use error::Error;
|
||||
pub use error::ErrorLocation;
|
||||
pub use error::Result;
|
||||
pub use error::TextPosition;
|
||||
pub use error::TextRange;
|
||||
pub use execpolicycheck::ExecPolicyCheckCommand;
|
||||
pub use parser::PolicyParser;
|
||||
pub use policy::Evaluation;
|
||||
pub use policy::MatchOptions;
|
||||
pub use policy::Policy;
|
||||
pub use policy::RequirementsExecPolicy;
|
||||
pub use rule::NetworkRuleProtocol;
|
||||
pub use rule::PatternToken;
|
||||
pub use rule::PrefixPattern;
|
||||
pub use rule::PrefixRule;
|
||||
pub use rule::Rule;
|
||||
pub use rule::RuleMatch;
|
||||
pub use rule::RuleRef;
|
||||
pub use sandbox_migration::prefix_rule_migration;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::error::error_location_from_file_span;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use multimap::MultiMap;
|
||||
use starlark::any::ProvidesStaticType;
|
||||
use starlark::codemap::FileSpan;
|
||||
use starlark::environment::GlobalsBuilder;
|
||||
use starlark::environment::Module;
|
||||
use starlark::eval::Evaluator;
|
||||
@@ -18,14 +18,7 @@ use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::decision::Decision;
|
||||
use crate::error::Error;
|
||||
use crate::error::ErrorLocation;
|
||||
use crate::error::Result;
|
||||
use crate::error::TextPosition;
|
||||
use crate::error::TextRange;
|
||||
use crate::executable_name::executable_lookup_key;
|
||||
use crate::executable_name::executable_path_lookup_key;
|
||||
use crate::Decision;
|
||||
use crate::rule::NetworkRule;
|
||||
use crate::rule::NetworkRuleProtocol;
|
||||
use crate::rule::PatternToken;
|
||||
@@ -34,6 +27,11 @@ use crate::rule::PrefixRule;
|
||||
use crate::rule::RuleRef;
|
||||
use crate::rule::validate_match_examples;
|
||||
use crate::rule::validate_not_match_examples;
|
||||
use codex_execpolicy_rules::Error;
|
||||
use codex_execpolicy_rules::ErrorLocation;
|
||||
use codex_execpolicy_rules::Result;
|
||||
use codex_execpolicy_rules::executable_lookup_key;
|
||||
use codex_execpolicy_rules::executable_path_lookup_key;
|
||||
|
||||
pub struct PolicyParser {
|
||||
builder: RefCell<PolicyBuilder>,
|
||||
@@ -54,7 +52,11 @@ impl PolicyParser {
|
||||
|
||||
/// Parses a policy, tagging parser errors with `policy_identifier` so failures include the
|
||||
/// identifier alongside line numbers.
|
||||
pub fn parse(&mut self, policy_identifier: &str, policy_file_contents: &str) -> Result<()> {
|
||||
pub fn parse(
|
||||
&mut self,
|
||||
policy_identifier: &str,
|
||||
policy_file_contents: &str,
|
||||
) -> crate::Result<()> {
|
||||
let pending_validation_count = self.builder.borrow().pending_example_validations.len();
|
||||
let mut dialect = Dialect::Extended.clone();
|
||||
dialect.enable_f_strings = true;
|
||||
@@ -63,14 +65,14 @@ impl PolicyParser {
|
||||
policy_file_contents.to_string(),
|
||||
&dialect,
|
||||
)
|
||||
.map_err(Error::Starlark)?;
|
||||
.map_err(crate::Error::Starlark)?;
|
||||
let globals = GlobalsBuilder::standard().with(policy_builtins).build();
|
||||
Module::with_temp_heap(|module| {
|
||||
let mut eval = Evaluator::new(&module);
|
||||
eval.extra = Some(&self.builder);
|
||||
eval.eval_module(ast, &globals)
|
||||
.map(|_| ())
|
||||
.map_err(Error::Starlark)
|
||||
.map_err(crate::Error::Starlark)
|
||||
})?;
|
||||
self.builder
|
||||
.borrow()
|
||||
@@ -78,7 +80,7 @@ impl PolicyParser {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn build(self) -> crate::policy::Policy {
|
||||
pub fn build(self) -> crate::Policy {
|
||||
self.builder.into_inner().build()
|
||||
}
|
||||
}
|
||||
@@ -137,7 +139,7 @@ impl PolicyBuilder {
|
||||
rules_by_program.insert(rule.program().to_string(), rule.clone());
|
||||
}
|
||||
|
||||
let policy = crate::policy::Policy::from_parts(
|
||||
let policy = crate::Policy::from_parts(
|
||||
rules_by_program,
|
||||
Vec::new(),
|
||||
self.host_executables_by_name.clone(),
|
||||
@@ -151,8 +153,8 @@ impl PolicyBuilder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build(self) -> crate::policy::Policy {
|
||||
crate::policy::Policy::from_parts(
|
||||
fn build(self) -> crate::Policy {
|
||||
crate::Policy::from_parts(
|
||||
self.rules_by_program,
|
||||
self.network_rules,
|
||||
self.host_executables_by_name,
|
||||
@@ -257,23 +259,6 @@ fn parse_network_rule_decision(raw: &str) -> Result<Decision> {
|
||||
}
|
||||
}
|
||||
|
||||
fn error_location_from_file_span(span: FileSpan) -> ErrorLocation {
|
||||
let resolved = span.resolve_span();
|
||||
ErrorLocation {
|
||||
path: span.filename().to_string(),
|
||||
range: TextRange {
|
||||
start: TextPosition {
|
||||
line: resolved.begin.line + 1,
|
||||
column: resolved.begin.column + 1,
|
||||
},
|
||||
end: TextPosition {
|
||||
line: resolved.end.line + 1,
|
||||
column: resolved.end.column + 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn attach_validation_location(error: Error, location: Option<ErrorLocation>) -> Error {
|
||||
match location {
|
||||
Some(location) => error.with_location(location),
|
||||
@@ -378,6 +363,7 @@ fn policy_builtins(builder: &mut GlobalsBuilder) {
|
||||
.unwrap_or_default();
|
||||
let location = eval
|
||||
.call_stack_top_location()
|
||||
.as_ref()
|
||||
.map(error_location_from_file_span);
|
||||
|
||||
let mut builder = policy_builder(eval);
|
||||
|
||||
@@ -7,7 +7,6 @@ use std::sync::Arc;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use codex_execpolicy::Decision;
|
||||
use codex_execpolicy::Error;
|
||||
use codex_execpolicy::Evaluation;
|
||||
use codex_execpolicy::MatchOptions;
|
||||
use codex_execpolicy::NetworkRuleProtocol;
|
||||
@@ -17,6 +16,7 @@ use codex_execpolicy::PolicyParser;
|
||||
use codex_execpolicy::PrefixPattern;
|
||||
use codex_execpolicy::PrefixRule;
|
||||
use codex_execpolicy::RequirementsExecPolicy;
|
||||
use codex_execpolicy::RuleError;
|
||||
use codex_execpolicy::RuleMatch;
|
||||
use codex_execpolicy::RuleRef;
|
||||
use codex_execpolicy::blocking_append_allow_prefix_rule;
|
||||
@@ -288,7 +288,7 @@ fn add_prefix_rule_rejects_empty_prefix() -> Result<()> {
|
||||
let result = policy.add_prefix_rule(&[], Decision::Allow);
|
||||
|
||||
match result.unwrap_err() {
|
||||
Error::InvalidPattern(message) => assert_eq!(message, "prefix cannot be empty"),
|
||||
RuleError::InvalidPattern(message) => assert_eq!(message, "prefix cannot be empty"),
|
||||
other => panic!("expected InvalidPattern(..), got {other:?}"),
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -16,7 +16,7 @@ workspace = true
|
||||
anyhow = { workspace = true }
|
||||
codex-collaboration-mode-templates = { workspace = true }
|
||||
codex-context-fragments = { workspace = true }
|
||||
codex-execpolicy = { workspace = true }
|
||||
codex-execpolicy-rules = { workspace = true }
|
||||
codex-git-utils = { workspace = true }
|
||||
codex-guardian-context = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::model_messages::permissions::ResolvedPermissionMessages;
|
||||
use crate::model_messages::permissions::WORKSPACE_WRITE_TEMPLATE;
|
||||
use approval_instructions::ApprovalInstructions;
|
||||
use codex_context_fragments::ContextualUserFragment;
|
||||
use codex_execpolicy::Policy;
|
||||
use codex_execpolicy_rules::Policy;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::config_types::SandboxMode;
|
||||
use codex_protocol::models::ContentItem;
|
||||
|
||||
@@ -16,7 +16,7 @@ workspace = true
|
||||
chardetng = { workspace = true }
|
||||
chrono = { workspace = true, features = ["serde"] }
|
||||
codex-async-utils = { workspace = true }
|
||||
codex-execpolicy = { workspace = true }
|
||||
codex-execpolicy-rules = { workspace = true }
|
||||
codex-extension-items = { workspace = true }
|
||||
codex-http-client = { workspace = true }
|
||||
codex-network-proxy = { workspace = true }
|
||||
|
||||
@@ -10,7 +10,7 @@ use crate::models::PermissionProfile;
|
||||
use crate::models::PermissionProfileSnapshot;
|
||||
use crate::protocol::AskForApproval;
|
||||
use crate::sandbox::SandboxType;
|
||||
use codex_execpolicy::RequirementsExecPolicy;
|
||||
use codex_execpolicy_rules::RequirementsExecPolicy;
|
||||
use codex_network_proxy::EnvironmentNetworkPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
|
||||
@@ -2549,7 +2549,7 @@ impl std::fmt::Display for FunctionCallOutputPayload {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use anyhow::Result;
|
||||
use codex_execpolicy::Policy;
|
||||
use codex_execpolicy_rules::Policy;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::tempdir;
|
||||
@@ -3436,7 +3436,7 @@ mod tests {
|
||||
exec_policy
|
||||
.add_prefix_rule(
|
||||
&[format!("tool-{i:03}"), "x".repeat(500)],
|
||||
codex_execpolicy::Decision::Allow,
|
||||
codex_execpolicy_rules::Decision::Allow,
|
||||
)
|
||||
.expect("add rule");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user