mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-10 21:53:57 +08:00
libcontainer: move CleanPath and StripRoot to internal/pathrs
These helpers will be needed for the compatibility code added in future patches in this series, but because "internal/pathrs" is imported by "libcontainer/utils" we need to move them so that we can avoid circular dependencies. Because the old functions were in a non-internal package it is possible some downstreams use them, so add some wrappers but mark them as deprecated. Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
package pathrs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -32,3 +34,51 @@ func IsLexicallyInRoot(root, path string) bool {
|
||||
path = strings.TrimRight(path, "/")
|
||||
return strings.HasPrefix(path+"/", root+"/")
|
||||
}
|
||||
|
||||
// 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 lexically be a subdirectory of the prefixed path. This is all
|
||||
// done lexically, so paths that include symlinks won't be safe as a result of
|
||||
// using CleanPath.
|
||||
func LexicallyCleanPath(path string) string {
|
||||
// Deal with empty strings nicely.
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Ensure that all paths are cleaned (especially problematic ones like
|
||||
// "/../../../../../" which can cause lots of issues).
|
||||
|
||||
if filepath.IsAbs(path) {
|
||||
return filepath.Clean(path)
|
||||
}
|
||||
|
||||
// If the path isn't absolute, we need to do more processing to fix paths
|
||||
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute
|
||||
// paths to relative ones.
|
||||
path = filepath.Clean(string(os.PathSeparator) + path)
|
||||
// This can't fail, as (by definition) all paths are relative to root.
|
||||
path, _ = filepath.Rel(string(os.PathSeparator), path)
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
// LexicallyStripRoot returns the passed path, stripping the root path if it
|
||||
// was (lexicially) inside it. Note that both passed paths will always be
|
||||
// treated as absolute, and the returned path will also always be absolute. In
|
||||
// addition, the paths are cleaned before stripping the root.
|
||||
func LexicallyStripRoot(root, path string) string {
|
||||
// Make the paths clean and absolute.
|
||||
root, path = LexicallyCleanPath("/"+root), LexicallyCleanPath("/"+path)
|
||||
switch {
|
||||
case path == root:
|
||||
path = "/"
|
||||
case root == "/":
|
||||
// do nothing
|
||||
default:
|
||||
path = strings.TrimPrefix(path, root+"/")
|
||||
}
|
||||
return LexicallyCleanPath("/" + path)
|
||||
}
|
||||
|
||||
@@ -51,3 +51,70 @@ func TestIsLexicallyInRoot(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLexicallyCleanPath(t *testing.T) {
|
||||
path := LexicallyCleanPath("")
|
||||
if path != "" {
|
||||
t.Errorf("expected to receive empty string and received %s", path)
|
||||
}
|
||||
|
||||
path = LexicallyCleanPath("rootfs")
|
||||
if path != "rootfs" {
|
||||
t.Errorf("expected to receive 'rootfs' and received %s", path)
|
||||
}
|
||||
|
||||
path = LexicallyCleanPath("../../../var")
|
||||
if path != "var" {
|
||||
t.Errorf("expected to receive 'var' and received %s", path)
|
||||
}
|
||||
|
||||
path = LexicallyCleanPath("/../../../var")
|
||||
if path != "/var" {
|
||||
t.Errorf("expected to receive '/var' and received %s", path)
|
||||
}
|
||||
|
||||
path = LexicallyCleanPath("/foo/bar/")
|
||||
if path != "/foo/bar" {
|
||||
t.Errorf("expected to receive '/foo/bar' and received %s", path)
|
||||
}
|
||||
|
||||
path = LexicallyCleanPath("/foo/bar/../")
|
||||
if path != "/foo" {
|
||||
t.Errorf("expected to receive '/foo' and received %s", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLexicallyStripRoot(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
root, path, out string
|
||||
}{
|
||||
// Works with multiple components.
|
||||
{"/a/b", "/a/b/c", "/c"},
|
||||
{"/hello/world", "/hello/world/the/quick-brown/fox", "/the/quick-brown/fox"},
|
||||
// '/' must be a no-op.
|
||||
{"/", "/a/b/c", "/a/b/c"},
|
||||
// Must be the correct order.
|
||||
{"/a/b", "/a/c/b", "/a/c/b"},
|
||||
// Must be at start.
|
||||
{"/abc/def", "/foo/abc/def/bar", "/foo/abc/def/bar"},
|
||||
// Must be a lexical parent.
|
||||
{"/foo/bar", "/foo/barSAMECOMPONENT", "/foo/barSAMECOMPONENT"},
|
||||
// Must only strip the root once.
|
||||
{"/foo/bar", "/foo/bar/foo/bar/baz", "/foo/bar/baz"},
|
||||
// Deal with .. in a fairly sane way.
|
||||
{"/foo/bar", "/foo/bar/../baz", "/foo/baz"},
|
||||
{"/foo/bar", "../../../../../../foo/bar/baz", "/baz"},
|
||||
{"/foo/bar", "/../../../../../../foo/bar/baz", "/baz"},
|
||||
{"/foo/bar/../baz", "/foo/baz/bar", "/bar"},
|
||||
{"/foo/bar/../baz", "/foo/baz/../bar/../baz/./foo", "/foo"},
|
||||
// All paths are made absolute before stripping.
|
||||
{"foo/bar", "/foo/bar/baz/bee", "/baz/bee"},
|
||||
{"/foo/bar", "foo/bar/baz/beef", "/baz/beef"},
|
||||
{"foo/bar", "foo/bar/baz/beets", "/baz/beets"},
|
||||
} {
|
||||
got := LexicallyStripRoot(test.root, test.path)
|
||||
if got != test.out {
|
||||
t.Errorf("LexicallyStripRoot(%q, %q) -- got %q, expected %q", test.root, test.path, got, test.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user