MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / lexical_clean_absolute_path

Function lexical_clean_absolute_path

crates/openshell-cli/src/ssh.rs:829–852  ·  view source on GitHub ↗

Lexically clean a POSIX-style absolute path by resolving `.` and `..` components, collapsing repeated separators, and stripping any trailing slash. Returns `None` if the input is empty or relative — the caller is expected to reject those before reaching this helper. This is *lexical* only: it does not consult the filesystem and so cannot follow symlinks. That trade-off is intentional — the functi

(path: &str)

Source from the content-addressed store, hash-verified

827/// SSH command. Symlink-based escapes inside the sandbox must be addressed
828/// server-side.
829fn lexical_clean_absolute_path(path: &str) -> Option<String> {
830 if !path.starts_with('/') {
831 return None;
832 }
833 let mut stack: Vec<&str> = Vec::new();
834 for component in path.split('/') {
835 match component {
836 "" | "." => {}
837 ".." => {
838 stack.pop();
839 }
840 other => stack.push(other),
841 }
842 }
843 if stack.is_empty() {
844 return Some("/".to_string());
845 }
846 let mut out = String::with_capacity(path.len());
847 for component in stack {
848 out.push('/');
849 out.push_str(component);
850 }
851 Some(out)
852}
853
854/// Validate that a sandbox-side source path passed to `sandbox download`
855/// resolves under the sandbox writable root.

Callers 1

Calls 3

pushMethod · 0.80
lenMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected