MCPcopy
hub / github.com/opencontainers/runc / LexicallyCleanPath

Function LexicallyCleanPath

internal/pathrs/path.go:47–68  ·  view source on GitHub ↗

LexicallyCleanPath makes a path safe for use with filepath.Join. This is done by not only cleaning the path, but also (if the path is relative) adding a leading '/' and cleaning it (then removing the leading '/'). This ensures that a path resulting from prepending another path will always resolve to

(path string)

Source from the content-addressed store, hash-verified

45// done lexically, so paths that include symlinks won't be safe as a result of
46// using CleanPath.
47func LexicallyCleanPath(path string) string {
48 // Deal with empty strings nicely.
49 if path == "" {
50 return ""
51 }
52
53 // Ensure that all paths are cleaned (especially problematic ones like
54 // "/../../../../../" which can cause lots of issues).
55
56 if filepath.IsAbs(path) {
57 return filepath.Clean(path)
58 }
59
60 // If the path isn't absolute, we need to do more processing to fix paths
61 // such as "../../../../<etc>/some/path". We also shouldn't convert absolute
62 // paths to relative ones.
63 path = filepath.Clean(string(os.PathSeparator) + path)
64 // This can't fail, as (by definition) all paths are relative to root.
65 path, _ = filepath.Rel(string(os.PathSeparator), path)
66
67 return path
68}
69
70// LexicallyStripRoot returns the passed path, stripping the root path if it
71// was (lexicially) inside it. Note that both passed paths will always be

Callers 10

validateIDFunction · 0.92
needsSetupDevFunction · 0.92
finalizeRootfsFunction · 0.92
createDevicesFunction · 0.92
maskPathsFunction · 0.92
mountPropagateMethod · 0.92
CreateCgroupConfigFunction · 0.92
setProcAttrFunction · 0.92
LexicallyStripRootFunction · 0.85
TestLexicallyCleanPathFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestLexicallyCleanPathFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…