mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Reduce repeated work in filesystem sandbox path checks (#65069)
## Why Checking multiple paths against the same sandbox policy repeats root resolution and protected metadata path construction. URI containment checks also allocate segment lists and check for escaped separators even when paths contain no escapes. ## What changed - Lazily cache resolved policy roots, lexical depths, protected metadata paths, and the full-disk write decision in the path matcher. Reuse the matcher across checks against the same prepared local policy. - Compare URI path segments directly from iterators and skip decoding for plain POSIX segment comparisons. Keep checking the full path for encoded separators, including the suffix beyond a matched prefix. ## Testing Exercise one prepared matcher against protected metadata and an ordinary writable file. Extend URI prefix tests to cover a candidate shorter than its base and paths with repeated or trailing separators. GitOrigin-RevId: 34425d018516e24c36e1c87ea51bfbd344137cd8
This commit is contained in:
committed by
copyberry
parent
848d7ffbe3
commit
18e2471c33
@@ -28,10 +28,12 @@ use crate::protocol::WritableRoot;
|
||||
mod deny_read_validator;
|
||||
mod enforcement;
|
||||
mod local_aliases;
|
||||
mod path_access;
|
||||
mod target;
|
||||
mod windows_glob;
|
||||
|
||||
use local_aliases::LocalPolicyContext;
|
||||
use path_access::PathAccessMatcher;
|
||||
|
||||
pub use deny_read_validator::DenyReadValidator;
|
||||
pub use deny_read_validator::DenyReadViolation;
|
||||
@@ -53,13 +55,32 @@ const PROTECTED_METADATA_AGENTS_PATH_NAME: &str = ".agents";
|
||||
const PROTECTED_METADATA_CODEX_PATH_NAME: &str = ".codex";
|
||||
const PROTECTED_METADATA_AWS_PATH_NAME: &str = ".aws";
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum ProtectedMetadata {
|
||||
Git,
|
||||
Agents,
|
||||
Codex,
|
||||
Aws,
|
||||
}
|
||||
|
||||
impl ProtectedMetadata {
|
||||
const ALL: [Self; 4] = [Self::Git, Self::Agents, Self::Codex, Self::Aws];
|
||||
|
||||
const fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Git => PROTECTED_METADATA_GIT_PATH_NAME,
|
||||
Self::Agents => PROTECTED_METADATA_AGENTS_PATH_NAME,
|
||||
Self::Codex => PROTECTED_METADATA_CODEX_PATH_NAME,
|
||||
Self::Aws => PROTECTED_METADATA_AWS_PATH_NAME,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Top-level workspace metadata paths that stay protected under writable roots.
|
||||
pub const PROTECTED_METADATA_PATH_NAMES: &[&str] = &[
|
||||
PROTECTED_METADATA_GIT_PATH_NAME,
|
||||
PROTECTED_METADATA_AGENTS_PATH_NAME,
|
||||
PROTECTED_METADATA_CODEX_PATH_NAME,
|
||||
PROTECTED_METADATA_AWS_PATH_NAME,
|
||||
];
|
||||
pub const PROTECTED_METADATA_PATH_NAMES: &[&str] = &{
|
||||
let [git, agents, codex, aws] = ProtectedMetadata::ALL;
|
||||
[git.as_str(), agents.as_str(), codex.as_str(), aws.as_str()]
|
||||
};
|
||||
|
||||
/// Returns true when a path basename is one of the protected workspace metadata names.
|
||||
pub fn is_protected_metadata_name(name: &OsStr) -> bool {
|
||||
@@ -83,9 +104,11 @@ pub fn forbidden_agent_metadata_write(
|
||||
}
|
||||
|
||||
with_local_policy_context(path, cwd, |path, context| {
|
||||
file_system_sandbox_policy
|
||||
.metadata_write_denial(path, context)
|
||||
.filter(|_| !file_system_sandbox_policy.can_write_path(path, context))
|
||||
let matching = PathAccessMatcher::new(Cow::Borrowed(file_system_sandbox_policy), *context);
|
||||
matching
|
||||
.metadata_write_denial(path)
|
||||
.filter(|_| !matching.can_write_path(path))
|
||||
.map(ProtectedMetadata::as_str)
|
||||
})?
|
||||
}
|
||||
|
||||
@@ -1037,43 +1060,7 @@ impl FileSystemSandboxPolicy {
|
||||
path: &PathUri,
|
||||
context: &FileSystemSandboxPolicyContext<'_>,
|
||||
) -> FileSystemAccessMode {
|
||||
match self.kind {
|
||||
FileSystemSandboxKind::Unrestricted | FileSystemSandboxKind::ExternalSandbox => {
|
||||
return FileSystemAccessMode::Write;
|
||||
}
|
||||
FileSystemSandboxKind::Restricted => {}
|
||||
}
|
||||
|
||||
let Some(convention) = context.cwd.infer_path_convention() else {
|
||||
return FileSystemAccessMode::Deny;
|
||||
};
|
||||
if path.infer_path_convention() != Some(convention)
|
||||
|| path.lexical_depth().is_none()
|
||||
|| context.cwd.lexical_depth().is_none()
|
||||
{
|
||||
return FileSystemAccessMode::Deny;
|
||||
}
|
||||
|
||||
let Some(entries) = self.resolved_entries(context).into_iter().try_fold(
|
||||
Vec::new(),
|
||||
|mut entries, (root, access)| {
|
||||
match root.lexical_depth() {
|
||||
Some(depth) => entries.push((root, access, depth)),
|
||||
None if root.is_opaque() => {}
|
||||
None => return None,
|
||||
}
|
||||
Some(entries)
|
||||
},
|
||||
) else {
|
||||
return FileSystemAccessMode::Deny;
|
||||
};
|
||||
|
||||
entries
|
||||
.into_iter()
|
||||
.filter(|(root, _, _)| path.starts_with(root))
|
||||
.max_by_key(|(_, access, depth)| (*depth, *access))
|
||||
.map(|(_, access, _)| access)
|
||||
.unwrap_or(FileSystemAccessMode::Deny)
|
||||
PathAccessMatcher::new(Cow::Borrowed(self), *context).resolve_access(path)
|
||||
}
|
||||
|
||||
pub fn can_write_path(
|
||||
@@ -1081,38 +1068,7 @@ impl FileSystemSandboxPolicy {
|
||||
path: &PathUri,
|
||||
context: &FileSystemSandboxPolicyContext<'_>,
|
||||
) -> bool {
|
||||
if !self.resolve_access(path, context).can_write() {
|
||||
return false;
|
||||
}
|
||||
self.has_full_disk_write_access_with_context(context)
|
||||
|| self.metadata_write_denial(path, context).is_none()
|
||||
}
|
||||
|
||||
fn metadata_write_denial(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
context: &FileSystemSandboxPolicyContext<'_>,
|
||||
) -> Option<&'static str> {
|
||||
if !matches!(self.kind, FileSystemSandboxKind::Restricted) {
|
||||
return None;
|
||||
}
|
||||
let entries = self.resolved_entries(context);
|
||||
let (protected, metadata_name) = entries
|
||||
.iter()
|
||||
.filter(|(_, access)| access.can_write())
|
||||
.find_map(|(root, _)| {
|
||||
PROTECTED_METADATA_PATH_NAMES
|
||||
.iter()
|
||||
.find_map(|metadata_name| {
|
||||
let protected = root.join_descendant(metadata_name).ok()?;
|
||||
path.starts_with(&protected)
|
||||
.then_some((protected, *metadata_name))
|
||||
})
|
||||
})?;
|
||||
(!entries.iter().any(|(root, access)| {
|
||||
access.can_write() && path.starts_with(root) && root.starts_with(&protected)
|
||||
}))
|
||||
.then_some(metadata_name)
|
||||
PathAccessMatcher::new(Cow::Borrowed(self), *context).can_write_path(path)
|
||||
}
|
||||
|
||||
fn prepare_deny_read_matcher(
|
||||
|
||||
@@ -12,6 +12,7 @@ use super::FileSystemSpecialPath;
|
||||
#[cfg(target_os = "macos")]
|
||||
use super::PROTECTED_METADATA_PATH_NAMES;
|
||||
use super::local_temporary_directories;
|
||||
use super::path_access::PathAccessMatcher;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use std::borrow::Cow;
|
||||
@@ -57,8 +58,7 @@ impl LocalPolicyContext {
|
||||
/// environment bindings change. Metadata checks also account for native ASCII
|
||||
/// case aliases without changing executor-independent path comparisons.
|
||||
pub struct LocalFileSystemPolicyMatcher<'a> {
|
||||
policy: Cow<'a, FileSystemSandboxPolicy>,
|
||||
context: FileSystemSandboxPolicyContext<'a>,
|
||||
matching: PathAccessMatcher<'a>,
|
||||
}
|
||||
|
||||
impl FileSystemSandboxPolicy {
|
||||
@@ -105,8 +105,7 @@ impl FileSystemSandboxPolicy {
|
||||
}
|
||||
}
|
||||
Ok(LocalFileSystemPolicyMatcher {
|
||||
policy,
|
||||
context: *context,
|
||||
matching: PathAccessMatcher::new(policy, *context),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -115,16 +114,12 @@ impl LocalFileSystemPolicyMatcher<'_> {
|
||||
/// Check write access, preserving failures to establish native path equivalence.
|
||||
pub fn can_write_path(&self, path: &PathUri) -> io::Result<bool> {
|
||||
self.with_path(path, |path| -> io::Result<bool> {
|
||||
let writable = self.policy.can_write_path(path, &self.context);
|
||||
let writable = self.matching.can_write_path(path);
|
||||
#[cfg(target_os = "macos")]
|
||||
if writable
|
||||
&& !self
|
||||
.policy
|
||||
.has_full_disk_write_access_with_context(&self.context)
|
||||
{
|
||||
if writable && !self.matching.has_full_disk_write_access() {
|
||||
let native_path = path.to_abs_path()?;
|
||||
for (root, access) in self.policy.resolved_entries(&self.context) {
|
||||
if !access.can_write() || root.is_opaque() {
|
||||
for root in self.matching.writable_paths() {
|
||||
if root.is_opaque() {
|
||||
continue;
|
||||
}
|
||||
let root = root.to_abs_path()?;
|
||||
@@ -135,7 +130,7 @@ impl LocalFileSystemPolicyMatcher<'_> {
|
||||
// This second policy view may only deny an already allowed write.
|
||||
// Normalize both candidate and entries so explicit metadata grants
|
||||
// and narrower restrictions keep their precedence.
|
||||
let mut policy = self.policy.clone().into_owned();
|
||||
let mut policy = self.matching.policy.as_ref().clone();
|
||||
for entry in &mut policy.entries {
|
||||
if let FileSystemPath::Path { path } = &mut entry.path
|
||||
&& let Some(normalized) =
|
||||
@@ -144,7 +139,7 @@ impl LocalFileSystemPolicyMatcher<'_> {
|
||||
*path = PathUri::from(normalized);
|
||||
}
|
||||
}
|
||||
if !policy.can_write_path(&PathUri::from(candidate), &self.context) {
|
||||
if !policy.can_write_path(&PathUri::from(candidate), &self.matching.context) {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
@@ -153,8 +148,8 @@ impl LocalFileSystemPolicyMatcher<'_> {
|
||||
})?
|
||||
}
|
||||
|
||||
pub(super) fn resolve_access(&self, path: &PathUri) -> FileSystemAccessMode {
|
||||
self.with_path(path, |path| self.policy.resolve_access(path, &self.context))
|
||||
pub(crate) fn resolve_access(&self, path: &PathUri) -> FileSystemAccessMode {
|
||||
self.with_path(path, |path| self.matching.resolve_access(path))
|
||||
.unwrap_or(FileSystemAccessMode::Deny)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
//! Match lexical policy paths against one executor context.
|
||||
//! Resolve symbolic roots, precedence depths, and metadata paths once within an operation.
|
||||
|
||||
use super::FileSystemAccessMode;
|
||||
use super::FileSystemSandboxKind;
|
||||
use super::FileSystemSandboxPolicy;
|
||||
use super::FileSystemSandboxPolicyContext;
|
||||
use super::ProtectedMetadata;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use std::borrow::Cow;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub(crate) struct PathAccessMatcher<'a> {
|
||||
pub(crate) policy: Cow<'a, FileSystemSandboxPolicy>,
|
||||
pub(crate) context: FileSystemSandboxPolicyContext<'a>,
|
||||
paths: OnceLock<ResolvedPaths>,
|
||||
full_disk_write_access: OnceLock<bool>,
|
||||
}
|
||||
|
||||
struct ResolvedPaths {
|
||||
entries: Vec<PathAccessEntry>,
|
||||
has_ambiguous_root: bool,
|
||||
protected_metadata: OnceLock<Vec<(PathUri, ProtectedMetadata)>>,
|
||||
}
|
||||
|
||||
struct PathAccessEntry {
|
||||
root: PathUri,
|
||||
access: FileSystemAccessMode,
|
||||
depth: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> PathAccessMatcher<'a> {
|
||||
pub(crate) fn new(
|
||||
policy: Cow<'a, FileSystemSandboxPolicy>,
|
||||
context: FileSystemSandboxPolicyContext<'a>,
|
||||
) -> Self {
|
||||
Self {
|
||||
policy,
|
||||
context,
|
||||
paths: OnceLock::new(),
|
||||
full_disk_write_access: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn paths(&self) -> &ResolvedPaths {
|
||||
self.paths.get_or_init(|| {
|
||||
let entries: Vec<_> = self
|
||||
.policy
|
||||
.resolved_entries(&self.context)
|
||||
.into_iter()
|
||||
.map(|(root, access)| PathAccessEntry {
|
||||
depth: root.lexical_depth(),
|
||||
root,
|
||||
access,
|
||||
})
|
||||
.collect();
|
||||
let has_ambiguous_root = entries
|
||||
.iter()
|
||||
.any(|entry| entry.depth.is_none() && !entry.root.is_opaque());
|
||||
ResolvedPaths {
|
||||
entries,
|
||||
has_ambiguous_root,
|
||||
protected_metadata: OnceLock::new(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_access(&self, path: &PathUri) -> FileSystemAccessMode {
|
||||
match self.policy.kind {
|
||||
FileSystemSandboxKind::Unrestricted | FileSystemSandboxKind::ExternalSandbox => {
|
||||
return FileSystemAccessMode::Write;
|
||||
}
|
||||
FileSystemSandboxKind::Restricted => {}
|
||||
}
|
||||
let convention = self.context.cwd.infer_path_convention();
|
||||
if convention.is_none()
|
||||
|| path.infer_path_convention() != convention
|
||||
|| path.lexical_depth().is_none()
|
||||
|| self.context.cwd.lexical_depth().is_none()
|
||||
{
|
||||
return FileSystemAccessMode::Deny;
|
||||
}
|
||||
|
||||
let paths = self.paths();
|
||||
if paths.has_ambiguous_root {
|
||||
return FileSystemAccessMode::Deny;
|
||||
}
|
||||
paths
|
||||
.entries
|
||||
.iter()
|
||||
.filter_map(|entry| {
|
||||
let depth = entry.depth?;
|
||||
path.starts_with(&entry.root)
|
||||
.then_some((depth, entry.access))
|
||||
})
|
||||
.max()
|
||||
.map(|(_, access)| access)
|
||||
.unwrap_or(FileSystemAccessMode::Deny)
|
||||
}
|
||||
|
||||
pub(crate) fn has_full_disk_write_access(&self) -> bool {
|
||||
*self.full_disk_write_access.get_or_init(|| {
|
||||
self.policy
|
||||
.has_full_disk_write_access_with_context(&self.context)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn can_write_path(&self, path: &PathUri) -> bool {
|
||||
self.resolve_access(path).can_write()
|
||||
&& (self.has_full_disk_write_access() || self.metadata_write_denial(path).is_none())
|
||||
}
|
||||
|
||||
pub(crate) fn writable_paths(&self) -> impl Iterator<Item = &PathUri> {
|
||||
self.paths()
|
||||
.entries
|
||||
.iter()
|
||||
.filter(|entry| entry.access.can_write())
|
||||
.map(|entry| &entry.root)
|
||||
}
|
||||
|
||||
pub(crate) fn metadata_write_denial(&self, path: &PathUri) -> Option<ProtectedMetadata> {
|
||||
if !matches!(self.policy.kind, FileSystemSandboxKind::Restricted) {
|
||||
return None;
|
||||
}
|
||||
let paths = self.paths();
|
||||
let protected_metadata = paths.protected_metadata.get_or_init(|| {
|
||||
self.writable_paths()
|
||||
.flat_map(|root| {
|
||||
ProtectedMetadata::ALL.into_iter().filter_map(|metadata| {
|
||||
root.join_descendant(metadata.as_str())
|
||||
.ok()
|
||||
.map(|protected| (protected, metadata))
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
let (protected, metadata) = protected_metadata
|
||||
.iter()
|
||||
.find(|(root, _)| path.starts_with(root))?;
|
||||
(!self
|
||||
.writable_paths()
|
||||
.any(|root| path.starts_with(root) && root.starts_with(protected)))
|
||||
.then_some(*metadata)
|
||||
}
|
||||
}
|
||||
@@ -394,8 +394,9 @@ fn uri_deny_and_metadata_matcher_use_executor_paths() {
|
||||
}
|
||||
assert!(!policy.can_write_path(&path("file:///C:/workspace/.codex/config"), &context));
|
||||
assert_eq!(
|
||||
policy.metadata_write_denial(&path("file:///C:/workspace/.codex/config"), &context),
|
||||
Some(".codex"),
|
||||
PathAccessMatcher::new(Cow::Borrowed(&policy), context)
|
||||
.metadata_write_denial(&path("file:///C:/workspace/.codex/config")),
|
||||
Some(ProtectedMetadata::Codex),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -832,16 +833,28 @@ fn filesystem_policy_blocks_protected_metadata_path_writes_by_default() {
|
||||
let dot_codex_config = cwd.path().join(".codex").join("config.toml");
|
||||
let dot_aws_config = cwd.path().join(".aws").join("config");
|
||||
let root = AbsolutePathBuf::from_absolute_path(cwd.path()).expect("absolute cwd");
|
||||
let context = LocalPolicyContext::new(cwd.path()).expect("local policy context");
|
||||
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
|
||||
path: root.into(),
|
||||
access: FileSystemAccessMode::Write,
|
||||
missing_path_behavior: None,
|
||||
}]);
|
||||
|
||||
assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_git_config, cwd.path()));
|
||||
assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_agents_config, cwd.path()));
|
||||
assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_codex_config, cwd.path()));
|
||||
assert!(!file_system_policy.can_write_local_path_with_cwd(&dot_aws_config, cwd.path()));
|
||||
let matching = file_system_policy
|
||||
.prepare_local_matching(&context.as_context())
|
||||
.expect("prepare local permissions");
|
||||
let results = [
|
||||
dot_git_config.as_path(),
|
||||
dot_agents_config.as_path(),
|
||||
dot_codex_config.as_path(),
|
||||
dot_aws_config.as_path(),
|
||||
cwd.path().join("ordinary.txt").as_path(),
|
||||
]
|
||||
.map(|path| {
|
||||
let path = PathUri::from_host_native_path(path).expect("native path");
|
||||
matching.can_write_path(&path).expect("match permissions")
|
||||
});
|
||||
assert_eq!(results, [false, false, false, false, true]);
|
||||
|
||||
let writable_roots = file_system_policy.get_writable_roots_with_cwd(cwd.path());
|
||||
assert_eq!(writable_roots.len(), 1);
|
||||
|
||||
@@ -348,13 +348,13 @@ impl PathUri {
|
||||
return false;
|
||||
}
|
||||
let convention = convention.unwrap_or(PathConvention::Posix);
|
||||
let Some(path_segments) = containment_path_segments(&self.0, convention) else {
|
||||
let Some(mut path_segments) = containment_path_segments(&self.0, convention) else {
|
||||
return false;
|
||||
};
|
||||
let Some(base_segments) = containment_path_segments(&base.0, convention) else {
|
||||
return false;
|
||||
};
|
||||
native_path_segments_start_with(&path_segments, &base_segments, convention)
|
||||
strip_native_path_segments_prefix(&mut path_segments, base_segments, convention)
|
||||
}
|
||||
|
||||
/// Returns whether the lexical subtrees rooted at these URIs overlap.
|
||||
@@ -385,7 +385,7 @@ impl PathUri {
|
||||
return None;
|
||||
}
|
||||
let convention = self.infer_path_convention()?;
|
||||
containment_path_segments(&self.0, convention).map(|segments| segments.len())
|
||||
containment_path_segments(&self.0, convention).map(Iterator::count)
|
||||
}
|
||||
|
||||
/// Returns the decoded relative path from `base` to this URI.
|
||||
@@ -410,20 +410,18 @@ impl PathUri {
|
||||
}
|
||||
|
||||
let convention = self.infer_path_convention()?;
|
||||
let path_segments = containment_path_segments(&self.0, convention)?;
|
||||
let mut path_segments = containment_path_segments(&self.0, convention)?;
|
||||
let base_segments = containment_path_segments(&base.0, convention)?;
|
||||
if !native_path_segments_start_with(&path_segments, &base_segments, convention) {
|
||||
if !strip_native_path_segments_prefix(&mut path_segments, base_segments, convention) {
|
||||
return None;
|
||||
}
|
||||
let relative_segments = &path_segments[base_segments.len()..];
|
||||
let separator = match convention {
|
||||
PathConvention::Posix => "/",
|
||||
PathConvention::Windows => "\\",
|
||||
};
|
||||
Some(
|
||||
relative_segments
|
||||
.iter()
|
||||
.map(|segment| decode_uri_path(segment))
|
||||
path_segments
|
||||
.map(decode_uri_path)
|
||||
.collect::<Vec<_>>()
|
||||
.join(separator),
|
||||
)
|
||||
@@ -771,40 +769,48 @@ fn is_windows_drive_uri_segment(segment: &str) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
fn containment_path_segments(url: &Url, convention: PathConvention) -> Option<Vec<&str>> {
|
||||
let segments = url
|
||||
.path_segments()?
|
||||
.filter(|segment| !segment.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
(!segments.iter().any(|segment| {
|
||||
urlencoding::decode_binary(segment.as_bytes())
|
||||
.iter()
|
||||
.any(|byte| *byte == b'/' || (convention == PathConvention::Windows && *byte == b'\\'))
|
||||
}))
|
||||
.then_some(segments)
|
||||
fn containment_path_segments(
|
||||
url: &Url,
|
||||
convention: PathConvention,
|
||||
) -> Option<impl Iterator<Item = &str>> {
|
||||
let segments = url.path_segments()?.filter(|segment| !segment.is_empty());
|
||||
let windows = convention == PathConvention::Windows;
|
||||
// Check the whole path, including any suffix beyond a matched prefix. Plain URI
|
||||
// paths already use slash boundaries and do not need per-segment decoding.
|
||||
if url
|
||||
.path()
|
||||
.bytes()
|
||||
.any(|byte| byte == b'%' || (windows && byte == b'\\'))
|
||||
&& segments.clone().any(|segment| {
|
||||
urlencoding::decode_binary(segment.as_bytes())
|
||||
.iter()
|
||||
.any(|byte| *byte == b'/' || (windows && *byte == b'\\'))
|
||||
})
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(segments)
|
||||
}
|
||||
|
||||
fn native_path_segments_start_with(
|
||||
path_segments: &[&str],
|
||||
base_segments: &[&str],
|
||||
fn strip_native_path_segments_prefix<'a, 'b>(
|
||||
path: &mut impl Iterator<Item = &'a str>,
|
||||
mut base: impl Iterator<Item = &'b str>,
|
||||
convention: PathConvention,
|
||||
) -> bool {
|
||||
match convention {
|
||||
PathConvention::Posix => {
|
||||
path_segments.len() >= base_segments.len()
|
||||
&& path_segments.iter().zip(base_segments).all(|(path, base)| {
|
||||
urlencoding::decode_binary(path.as_bytes())
|
||||
== urlencoding::decode_binary(base.as_bytes())
|
||||
})
|
||||
}
|
||||
PathConvention::Windows => {
|
||||
path_segments.len() >= base_segments.len()
|
||||
&& path_segments.iter().zip(base_segments).all(|(path, base)| {
|
||||
path.eq_ignore_ascii_case(base)
|
||||
|| decode_uri_path(path).eq_ignore_ascii_case(&decode_uri_path(base))
|
||||
})
|
||||
}
|
||||
}
|
||||
base.all(|base| {
|
||||
path.next().is_some_and(|path| match convention {
|
||||
PathConvention::Posix => {
|
||||
path == base
|
||||
|| ((path.contains('%') || base.contains('%'))
|
||||
&& urlencoding::decode_binary(path.as_bytes())
|
||||
== urlencoding::decode_binary(base.as_bytes()))
|
||||
}
|
||||
PathConvention::Windows => {
|
||||
path.eq_ignore_ascii_case(base)
|
||||
|| decode_uri_path(path).eq_ignore_ascii_case(&decode_uri_path(base))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn infer_opaque_path_convention(path_bytes: &[u8]) -> Option<PathConvention> {
|
||||
|
||||
@@ -1292,6 +1292,12 @@ fn join_uses_the_base_uri_path_convention() {
|
||||
fn starts_with_uses_uri_segment_boundaries() {
|
||||
for (path, base, expected) in [
|
||||
("file:///workspace/plugin", "file:///", true),
|
||||
("file:///workspace", "file:///workspace/plugin", false),
|
||||
(
|
||||
"file:///workspace//plugin/assets",
|
||||
"file:///workspace/plugin/",
|
||||
true,
|
||||
),
|
||||
("file:///workspace/plugin", "file:///workspace/plugin", true),
|
||||
(
|
||||
"file:///workspace/plugin/assets/icon.svg",
|
||||
|
||||
Reference in New Issue
Block a user