1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
package safepath
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestJoinEscapingSymlink(t *testing.T) {
type testCase struct {
name string
target string
}
var cases []testCase
if runtime.GOOS == "windows" {
cases = []testCase{
{name: "root", target: `C:\`},
{name: "absolute file", target: `C:\Windows\System32\cmd.exe`},
}
} else {
cases = []testCase{
{name: "root", target: "/"},
{name: "absolute file", target: "/etc/passwd"},
}
}
cases = append(cases, testCase{name: "relative", target: "../../"})
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tempDir := t.TempDir()
dir, err := filepath.EvalSymlinks(tempDir)
assert.NilError(t, err, "filepath.EvalSymlinks failed for temporary directory %s", tempDir)
err = os.Symlink(tc.target, filepath.Join(dir, "link"))
assert.NilError(t, err, "failed to create symlink to %s", tc.target)
safe, err := Join(context.Background(), dir, "link")
if err == nil {
safe.Close(context.Background())
}
assert.ErrorType(t, err, &ErrEscapesBase{})
})
}
}
func TestJoinGoodSymlink(t *testing.T) {
t.Skip("Debian-local: Test requires root")
tempDir := t.TempDir()
dir, err := filepath.EvalSymlinks(tempDir)
assert.NilError(t, err, "filepath.EvalSymlinks failed for temporary directory %s", tempDir)
assert.Assert(t, os.WriteFile(filepath.Join(dir, "foo"), []byte("bar"), 0o744), "failed to create file 'foo'")
assert.Assert(t, os.Mkdir(filepath.Join(dir, "subdir"), 0o744), "failed to create directory 'subdir'")
assert.Assert(t, os.WriteFile(filepath.Join(dir, "subdir/hello.txt"), []byte("world"), 0o744), "failed to create file 'subdir/hello.txt'")
assert.Assert(t, os.Symlink(filepath.Join(dir, "subdir"), filepath.Join(dir, "subdir_link_absolute")), "failed to create absolute symlink to directory 'subdir'")
assert.Assert(t, os.Symlink("subdir", filepath.Join(dir, "subdir_link_relative")), "failed to create relative symlink to directory 'subdir'")
assert.Assert(t, os.Symlink(filepath.Join(dir, "foo"), filepath.Join(dir, "foo_link_absolute")), "failed to create absolute symlink to file 'foo'")
assert.Assert(t, os.Symlink("foo", filepath.Join(dir, "foo_link_relative")), "failed to create relative symlink to file 'foo'")
for _, target := range []string{
"foo", "subdir",
"subdir_link_absolute", "foo_link_absolute",
"subdir_link_relative", "foo_link_relative",
} {
t.Run(target, func(t *testing.T) {
safe, err := Join(context.Background(), dir, target)
assert.NilError(t, err)
defer safe.Close(context.Background())
if strings.HasPrefix(target, "subdir") {
data, err := os.ReadFile(filepath.Join(safe.Path(), "hello.txt"))
assert.NilError(t, err)
assert.Assert(t, is.Equal(string(data), "world"))
}
})
}
}
func TestJoinWithSymlinkReplace(t *testing.T) {
t.Skip("Debian-local: Test requires root")
tempDir := t.TempDir()
dir, err := filepath.EvalSymlinks(tempDir)
assert.NilError(t, err, "filepath.EvalSymlinks failed for temporary directory %s", tempDir)
link := filepath.Join(dir, "link")
target := filepath.Join(dir, "foo")
err = os.WriteFile(target, []byte("bar"), 0o744)
assert.NilError(t, err, "failed to create test file")
err = os.Symlink(target, link)
assert.Check(t, err, "failed to create symlink to foo")
safe, err := Join(context.Background(), dir, "link")
assert.NilError(t, err)
defer safe.Close(context.Background())
// Delete the link target.
err = os.Remove(target)
if runtime.GOOS == "windows" {
// On Windows it shouldn't be possible.
assert.Assert(t, is.ErrorType(err, &os.PathError{}), "link shouldn't be deletable before cleanup")
} else {
// On Linux we can delete it just fine.
assert.NilError(t, err, "failed to remove symlink")
// Replace target with a symlink to /etc/paswd
err = os.Symlink("/etc/passwd", target)
assert.NilError(t, err, "failed to create symlink")
}
// The returned safe path should still point to the old file.
data, err := os.ReadFile(safe.Path())
assert.NilError(t, err, "failed to read file")
assert.Check(t, is.Equal(string(data), "bar"))
}
func TestJoinCloseInvalidates(t *testing.T) {
t.Skip("Debian-local: Test requires root")
tempDir := t.TempDir()
dir, err := filepath.EvalSymlinks(tempDir)
assert.NilError(t, err)
foo := filepath.Join(dir, "foo")
err = os.WriteFile(foo, []byte("bar"), 0o744)
assert.NilError(t, err, "failed to create test file")
safe, err := Join(context.Background(), dir, "foo")
assert.NilError(t, err)
assert.Check(t, safe.IsValid())
assert.NilError(t, safe.Close(context.Background()))
assert.Check(t, !safe.IsValid())
}
|