mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
fix(core) Deduplicate prefix_rules before appending (#10309)
## Summary We ideally shouldn't make it to this point in the first place, but if we do try to append a rule that already exists, we shouldn't append the same rule twice. ## Testing - [x] Added unit test for this case
This commit is contained in:
1 parent
03fcd12e77
commit
6c22360bcb
2 files changed
+37
-23
No files matched your search
@@ -105,35 +105,28 @@ fn append_locked_line(policy_path: &Path, line: &str) -> Result<(), AmendError>
|
|||||||
source,
|
source,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let len = file
|
file.seek(SeekFrom::Start(0))
|
||||||
.metadata()
|
.map_err(|source| AmendError::SeekPolicyFile {
|
||||||
.map_err(|source| AmendError::PolicyMetadata {
|
|
||||||
path: policy_path.to_path_buf(),
|
path: policy_path.to_path_buf(),
|
||||||
source,
|
source,
|
||||||
})?
|
})?;
|
||||||
.len();
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents)
|
||||||
|
.map_err(|source| AmendError::ReadPolicyFile {
|
||||||
|
path: policy_path.to_path_buf(),
|
||||||
|
source,
|
||||||
|
})?;
|
||||||
|
|
||||||
// Ensure file ends in a newline before appending.
|
if contents.lines().any(|existing| existing == line) {
|
||||||
if len > 0 {
|
return Ok(());
|
||||||
file.seek(SeekFrom::End(-1))
|
}
|
||||||
.map_err(|source| AmendError::SeekPolicyFile {
|
|
||||||
|
if !contents.is_empty() && !contents.ends_with('\n') {
|
||||||
|
file.write_all(b"\n")
|
||||||
|
.map_err(|source| AmendError::WritePolicyFile {
|
||||||
path: policy_path.to_path_buf(),
|
path: policy_path.to_path_buf(),
|
||||||
source,
|
source,
|
||||||
})?;
|
})?;
|
||||||
let mut last = [0; 1];
|
|
||||||
file.read_exact(&mut last)
|
|
||||||
.map_err(|source| AmendError::ReadPolicyFile {
|
|
||||||
path: policy_path.to_path_buf(),
|
|
||||||
source,
|
|
||||||
})?;
|
|
||||||
|
|
||||||
if last[0] != b'\n' {
|
|
||||||
file.write_all(b"\n")
|
|
||||||
.map_err(|source| AmendError::WritePolicyFile {
|
|
||||||
path: policy_path.to_path_buf(),
|
|
||||||
source,
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write_all(format!("{line}\n").as_bytes())
|
file.write_all(format!("{line}\n").as_bytes())
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
use std::fs;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
@@ -10,10 +11,12 @@ use codex_execpolicy::Policy;
|
|||||||
use codex_execpolicy::PolicyParser;
|
use codex_execpolicy::PolicyParser;
|
||||||
use codex_execpolicy::RuleMatch;
|
use codex_execpolicy::RuleMatch;
|
||||||
use codex_execpolicy::RuleRef;
|
use codex_execpolicy::RuleRef;
|
||||||
|
use codex_execpolicy::blocking_append_allow_prefix_rule;
|
||||||
use codex_execpolicy::rule::PatternToken;
|
use codex_execpolicy::rule::PatternToken;
|
||||||
use codex_execpolicy::rule::PrefixPattern;
|
use codex_execpolicy::rule::PrefixPattern;
|
||||||
use codex_execpolicy::rule::PrefixRule;
|
use codex_execpolicy::rule::PrefixRule;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
fn tokens(cmd: &[&str]) -> Vec<String> {
|
fn tokens(cmd: &[&str]) -> Vec<String> {
|
||||||
cmd.iter().map(std::string::ToString::to_string).collect()
|
cmd.iter().map(std::string::ToString::to_string).collect()
|
||||||
@@ -46,6 +49,24 @@ fn rule_snapshots(rules: &[RuleRef]) -> Vec<RuleSnapshot> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn append_allow_prefix_rule_dedupes_existing_rule() -> Result<()> {
|
||||||
|
let tmp = tempdir().context("create temp dir")?;
|
||||||
|
let policy_path = tmp.path().join("rules").join("default.rules");
|
||||||
|
let prefix = tokens(&["python3"]);
|
||||||
|
|
||||||
|
blocking_append_allow_prefix_rule(&policy_path, &prefix)?;
|
||||||
|
blocking_append_allow_prefix_rule(&policy_path, &prefix)?;
|
||||||
|
|
||||||
|
let contents = fs::read_to_string(&policy_path).context("read policy")?;
|
||||||
|
assert_eq!(
|
||||||
|
contents,
|
||||||
|
r#"prefix_rule(pattern=["python3"], decision="allow")
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_match() -> Result<()> {
|
fn basic_match() -> Result<()> {
|
||||||
let policy_src = r#"
|
let policy_src = r#"
|
||||||
|
|||||||
Reference in new issue
Block a user