github.com/moby/docker@v26.1.3+incompatible/internal/safepath/common_test.go (about)

     1  package safepath
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestIsLocalTo(t *testing.T) {
    11  	for _, tc := range []struct {
    12  		name    string
    13  		subpath string
    14  		result  bool
    15  	}{
    16  		{name: "same", subpath: "/volume", result: true},
    17  		{name: "1 level subpath", subpath: "/volume/sub", result: true},
    18  		{name: "2 level subpath", subpath: "/volume/sub/path", result: true},
    19  		{name: "absolute", subpath: "/etc/passwd", result: false},
    20  		{name: "backtrack", subpath: "/volume/../", result: false},
    21  		{name: "backtrack inside", subpath: "/volume/sub/../", result: true},
    22  		{name: "relative path", subpath: "./rel", result: false},
    23  		{name: "file with dots", subpath: "/volume/file..with.dots", result: true},
    24  		{name: "file starting with dots", subpath: "/volume/..file", result: true},
    25  	} {
    26  		t.Run(tc.name, func(t *testing.T) {
    27  			result := isLocalTo(tc.subpath, "/volume")
    28  			assert.Check(t, is.Equal(result, tc.result))
    29  		})
    30  	}
    31  }