@@ -4,11 +4,11 @@ package utils
44import (
55 "encoding/json"
66 "io"
7- "os"
8- "path/filepath"
97 "strings"
108
119 "golang.org/x/sys/unix"
10+
11+ "github.com/opencontainers/runc/internal/pathrs"
1212)
1313
1414const (
@@ -37,53 +37,6 @@ func WriteJSON(w io.Writer, v any) error {
3737 return err
3838}
3939
40- // CleanPath makes a path safe for use with filepath.Join. This is done by not
41- // only cleaning the path, but also (if the path is relative) adding a leading
42- // '/' and cleaning it (then removing the leading '/'). This ensures that a
43- // path resulting from prepending another path will always resolve to lexically
44- // be a subdirectory of the prefixed path. This is all done lexically, so paths
45- // that include symlinks won't be safe as a result of using CleanPath.
46- func CleanPath (path string ) string {
47- // Deal with empty strings nicely.
48- if path == "" {
49- return ""
50- }
51-
52- // Ensure that all paths are cleaned (especially problematic ones like
53- // "/../../../../../" which can cause lots of issues).
54-
55- if filepath .IsAbs (path ) {
56- return filepath .Clean (path )
57- }
58-
59- // If the path isn't absolute, we need to do more processing to fix paths
60- // such as "../../../../<etc>/some/path". We also shouldn't convert absolute
61- // paths to relative ones.
62- path = filepath .Clean (string (os .PathSeparator ) + path )
63- // This can't fail, as (by definition) all paths are relative to root.
64- path , _ = filepath .Rel (string (os .PathSeparator ), path )
65-
66- return path
67- }
68-
69- // StripRoot returns the passed path, stripping the root path if it was
70- // (lexicially) inside it. Note that both passed paths will always be treated
71- // as absolute, and the returned path will also always be absolute. In
72- // addition, the paths are cleaned before stripping the root.
73- func StripRoot (root , path string ) string {
74- // Make the paths clean and absolute.
75- root , path = CleanPath ("/" + root ), CleanPath ("/" + path )
76- switch {
77- case path == root :
78- path = "/"
79- case root == "/" :
80- // do nothing
81- default :
82- path = strings .TrimPrefix (path , root + "/" )
83- }
84- return CleanPath ("/" + path )
85- }
86-
8740// SearchLabels searches through a list of key=value pairs for a given key,
8841// returning its value, and the binary flag telling whether the key exist.
8942func SearchLabels (labels []string , key string ) (string , bool ) {
@@ -114,3 +67,23 @@ func Annotations(labels []string) (bundle string, userAnnotations map[string]str
11467 }
11568 return bundle , userAnnotations
11669}
70+
71+ // CleanPath makes a path safe for use with filepath.Join. This is done by not
72+ // only cleaning the path, but also (if the path is relative) adding a leading
73+ // '/' and cleaning it (then removing the leading '/'). This ensures that a
74+ // path resulting from prepending another path will always resolve to lexically
75+ // be a subdirectory of the prefixed path. This is all done lexically, so paths
76+ // that include symlinks won't be safe as a result of using CleanPath.
77+ //
78+ // Deprecated: This function has been moved to internal/pathrs and this wrapper
79+ // will be removed in runc 1.5.
80+ var CleanPath = pathrs .LexicallyCleanPath
81+
82+ // StripRoot returns the passed path, stripping the root path if it was
83+ // (lexicially) inside it. Note that both passed paths will always be treated
84+ // as absolute, and the returned path will also always be absolute. In
85+ // addition, the paths are cleaned before stripping the root.
86+ //
87+ // Deprecated: This function has been moved to internal/pathrs and this wrapper
88+ // will be removed in runc 1.5.
89+ var StripRoot = pathrs .LexicallyStripRoot
0 commit comments