mirror of
https://github.com/openai/codex.git
synced 2026-11-18 23:02:23 +00:00
Preserve filesystem permission path conventions (#39084)
## Why Filesystem permission paths can use a convention that differs from the host running Codex. Converting them immediately to native absolute paths can change the meaning of ambiguous paths such as `/C:/secret` or Windows UNC paths. ## What changed - Store literal filesystem permission paths as `PathUri` values through the runtime policy and execution protocol. - Keep legacy string-based serialization at explicit protocol boundaries and reject conversions that cannot be represented losslessly. - Encode native paths as opaque URIs when a normal file URI would imply the wrong path convention. ## Testing Added coverage for cross-platform and ambiguous path round trips, UNC path variants, permission-profile serialization, and deny-policy enforcement. GitOrigin-RevId: 5247713796d1f2bb4e02f94eb9fc82d4698060f0
This commit is contained in:
1 parent
a4f37a5b7f
commit
2013e04354
46 files changed
+765
-260
No files matched your search
@@ -20,7 +20,7 @@ use codex_protocol::request_permissions::PermissionGrantScope as CorePermissionG
|
||||
use codex_protocol::request_permissions::RequestPermissionProfile as CoreRequestPermissionProfile;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::LegacyAppPathString;
|
||||
use codex_utils_path_uri::PathConvention;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::io;
|
||||
@@ -69,6 +69,13 @@ pub struct AdditionalFileSystemPermissions {
|
||||
pub entries: Option<Vec<FileSystemSandboxEntry>>,
|
||||
}
|
||||
|
||||
fn permission_path_uri(path: LegacyAppPathString) -> io::Result<PathUri> {
|
||||
if let Ok(path) = AbsolutePathBuf::try_from(path.clone()) {
|
||||
return Ok(path.into());
|
||||
}
|
||||
PathUri::try_from(path).map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))
|
||||
}
|
||||
|
||||
// TODO(anp): Remove this conversion once core permission paths use PathUri.
|
||||
impl From<CoreFileSystemPermissions> for AdditionalFileSystemPermissions {
|
||||
fn from(value: CoreFileSystemPermissions) -> Self {
|
||||
@@ -109,9 +116,27 @@ impl From<CoreFileSystemPermissions> for AdditionalFileSystemPermissions {
|
||||
entries: Some(entries),
|
||||
}
|
||||
} else {
|
||||
let mut read = Vec::new();
|
||||
let mut write = Vec::new();
|
||||
let legacy_compatible = value.glob_scan_max_depth.is_none()
|
||||
&& value.entries.iter().all(|entry| {
|
||||
let CoreFileSystemPath::Path { path } = &entry.path else {
|
||||
return false;
|
||||
};
|
||||
let legacy_path = LegacyAppPathString::from(path.clone());
|
||||
if legacy_path.to_inferred_path_uri().as_ref() != Some(path) {
|
||||
return false;
|
||||
}
|
||||
match entry.access {
|
||||
CoreFileSystemAccessMode::Read => read.push(legacy_path),
|
||||
CoreFileSystemAccessMode::Write => write.push(legacy_path),
|
||||
CoreFileSystemAccessMode::Deny => return false,
|
||||
}
|
||||
true
|
||||
});
|
||||
Self {
|
||||
read: None,
|
||||
write: None,
|
||||
read: (legacy_compatible && !read.is_empty()).then_some(read),
|
||||
write: (legacy_compatible && !write.is_empty()).then_some(write),
|
||||
glob_scan_max_depth: value.glob_scan_max_depth,
|
||||
entries: Some(
|
||||
value
|
||||
@@ -144,11 +169,7 @@ impl TryFrom<AdditionalFileSystemPermissions> for CoreFileSystemPermissions {
|
||||
.map(|paths| {
|
||||
paths
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
path.to_path_uri(PathConvention::native())
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?
|
||||
.to_abs_path()
|
||||
})
|
||||
.map(permission_path_uri)
|
||||
.collect::<io::Result<Vec<_>>>()
|
||||
})
|
||||
.transpose()?;
|
||||
@@ -157,15 +178,11 @@ impl TryFrom<AdditionalFileSystemPermissions> for CoreFileSystemPermissions {
|
||||
.map(|paths| {
|
||||
paths
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
path.to_path_uri(PathConvention::native())
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?
|
||||
.to_abs_path()
|
||||
})
|
||||
.map(permission_path_uri)
|
||||
.collect::<io::Result<Vec<_>>>()
|
||||
})
|
||||
.transpose()?;
|
||||
CoreFileSystemPermissions::from_read_write_roots(read, write)
|
||||
CoreFileSystemPermissions::from_read_write_path_uris(read, write)
|
||||
};
|
||||
permissions.glob_scan_max_depth = value.glob_scan_max_depth;
|
||||
Ok(permissions)
|
||||
@@ -312,9 +329,7 @@ pub enum FileSystemPath {
|
||||
impl From<CoreFileSystemPath> for FileSystemPath {
|
||||
fn from(value: CoreFileSystemPath) -> Self {
|
||||
match value {
|
||||
CoreFileSystemPath::Path { path } => Self::Path {
|
||||
path: LegacyAppPathString::from_abs_path(&path),
|
||||
},
|
||||
CoreFileSystemPath::Path { path } => Self::Path { path: path.into() },
|
||||
CoreFileSystemPath::GlobPattern { pattern } => Self::GlobPattern { pattern },
|
||||
CoreFileSystemPath::Special { value } => Self::Special {
|
||||
value: value.into(),
|
||||
@@ -330,10 +345,7 @@ impl TryFrom<FileSystemPath> for CoreFileSystemPath {
|
||||
fn try_from(value: FileSystemPath) -> Result<Self, Self::Error> {
|
||||
Ok(match value {
|
||||
FileSystemPath::Path { path } => Self::Path {
|
||||
path: path
|
||||
.to_path_uri(PathConvention::native())
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?
|
||||
.to_abs_path()?,
|
||||
path: permission_path_uri(path)?,
|
||||
},
|
||||
FileSystemPath::GlobPattern { pattern } => Self::GlobPattern { pattern },
|
||||
FileSystemPath::Special { value } => Self::Special {
|
||||
|
||||
@@ -839,6 +839,52 @@ fn additional_file_system_permissions_preserves_canonical_entries() {
|
||||
.expect("API paths should convert to native paths"),
|
||||
core_permissions
|
||||
);
|
||||
|
||||
for path in [r"C:\workspace\read-only", r"\\server\share\read-only"] {
|
||||
let path = LegacyAppPathString::from_string(path);
|
||||
let core_permissions = CoreFileSystemPermissions::from_read_write_path_uris(
|
||||
Some(vec![
|
||||
PathUri::try_from(path.clone()).expect("valid foreign permission path"),
|
||||
]),
|
||||
/*write*/ None,
|
||||
);
|
||||
let permissions = AdditionalFileSystemPermissions::from(core_permissions.clone());
|
||||
assert_eq!(permissions.read, Some(vec![path]));
|
||||
assert_eq!(permissions.entries.as_ref().map(Vec::len), Some(1));
|
||||
assert_eq!(
|
||||
CoreFileSystemPermissions::try_from(permissions)
|
||||
.expect("foreign API paths should round-trip"),
|
||||
core_permissions
|
||||
);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
for path in ["//server/share/read-only", r"/\server/share/read-only"] {
|
||||
let path = LegacyAppPathString::from_string(path);
|
||||
let core_permissions =
|
||||
CoreFileSystemPermissions::try_from(AdditionalFileSystemPermissions {
|
||||
read: Some(vec![path.clone()]),
|
||||
write: None,
|
||||
glob_scan_max_depth: None,
|
||||
entries: None,
|
||||
})
|
||||
.expect("native slash UNC permission path");
|
||||
let permissions = AdditionalFileSystemPermissions::from(core_permissions);
|
||||
assert_eq!(
|
||||
permissions.read,
|
||||
Some(vec![LegacyAppPathString::from_string(
|
||||
r"\\server\share\read-only"
|
||||
)])
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
CoreFileSystemPermissions::try_from(AdditionalFileSystemPermissions {
|
||||
read: Some(vec![LegacyAppPathString::from_string(r"\\localhost\share")]),
|
||||
write: None,
|
||||
glob_scan_max_depth: None,
|
||||
entries: None,
|
||||
})
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user