| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
|
| 3 |
// Copyright (C) 2017-2025 SUSE LLC. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package securejoin |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"syscall" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
|
| 20 |
"github.com/cyphar/filepath-securejoin/internal/testutils" |
| 21 |
) |
| 22 |
|
| 23 |
// TODO: These tests won't work on plan9 because it doesn't have symlinks, and |
| 24 |
// also we use '/' here explicitly which probably won't work on Windows. |
| 25 |
|
| 26 |
type input struct { |
| 27 |
root, unsafe string |
| 28 |
expected string |
| 29 |
} |
| 30 |
|
| 31 |
func expandedTempDir(t *testing.T) string { |
| 32 |
dir := t.TempDir() |
| 33 |
dir, err := filepath.EvalSymlinks(dir) |
| 34 |
require.NoError(t, err) |
| 35 |
return dir |
| 36 |
} |
| 37 |
|
| 38 |
// Test basic handling of symlink expansion. |
| 39 |
func TestSymlink(t *testing.T) { |
| 40 |
dir := expandedTempDir(t) |
| 41 |
|
| 42 |
testutils.Symlink(t, "somepath", filepath.Join(dir, "etc")) |
| 43 |
testutils.Symlink(t, "../../../../../../../../../../../../../etc", filepath.Join(dir, "etclink")) |
| 44 |
testutils.Symlink(t, "/../../../../../../../../../../../../../etc/passwd", filepath.Join(dir, "passwd")) |
| 45 |
|
| 46 |
rootOrVol := string(filepath.Separator) |
| 47 |
if vol := filepath.VolumeName(dir); vol != "" { |
| 48 |
rootOrVol = vol + rootOrVol |
| 49 |
} |
| 50 |
|
| 51 |
tc := []input{ |
| 52 |
// Make sure that expansion with a root of '/' proceeds in the expected fashion. |
| 53 |
{rootOrVol, filepath.Join(dir, "passwd"), filepath.Join(rootOrVol, "etc", "passwd")}, |
| 54 |
{rootOrVol, filepath.Join(dir, "etclink"), filepath.Join(rootOrVol, "etc")}, |
| 55 |
|
| 56 |
{rootOrVol, filepath.Join(dir, "etc"), filepath.Join(dir, "somepath")}, |
| 57 |
// Now test scoped expansion. |
| 58 |
{dir, "passwd", filepath.Join(dir, "somepath", "passwd")}, |
| 59 |
{dir, "etclink", filepath.Join(dir, "somepath")}, |
| 60 |
{dir, "etc", filepath.Join(dir, "somepath")}, |
| 61 |
{dir, "etc/test", filepath.Join(dir, "somepath", "test")}, |
| 62 |
{dir, "etc/test/..", filepath.Join(dir, "somepath")}, |
| 63 |
} |
| 64 |
|
| 65 |
for _, test := range tc { |
| 66 |
got, err := SecureJoin(test.root, test.unsafe) |
| 67 |
if err != nil { |
| 68 |
t.Errorf("securejoin(%q, %q): unexpected error: %v", test.root, test.unsafe, err) |
| 69 |
continue |
| 70 |
} |
| 71 |
// This is only for OS X, where /etc is a symlink to /private/etc. In |
| 72 |
// principle, SecureJoin(/, pth) is the same as EvalSymlinks(pth) in |
| 73 |
// the case where the path exists. |
| 74 |
if test.root == "/" { |
| 75 |
if expected, err := filepath.EvalSymlinks(test.expected); err == nil { |
| 76 |
test.expected = expected |
| 77 |
} |
| 78 |
} |
| 79 |
if got != test.expected { |
| 80 |
t.Errorf("securejoin(%q, %q): expected %q, got %q", test.root, test.unsafe, test.expected, got) |
| 81 |
continue |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// In a path without symlinks, SecureJoin is equivalent to Clean+Join. |
| 87 |
func TestNoSymlink(t *testing.T) { |
| 88 |
dir := expandedTempDir(t) |
| 89 |
|
| 90 |
tc := []input{ |
| 91 |
{dir, "somepath", filepath.Join(dir, "somepath")}, |
| 92 |
{dir, "even/more/path", filepath.Join(dir, "even", "more", "path")}, |
| 93 |
{dir, "/this/is/a/path", filepath.Join(dir, "this", "is", "a", "path")}, |
| 94 |
{dir, "also/a/../path/././/with/some/./.././junk", filepath.Join(dir, "also", "path", "with", "junk")}, |
| 95 |
{dir, "yetanother/../path/././/with/some/./.././junk../../../../../../../../../../../../etc/passwd", filepath.Join(dir, "etc", "passwd")}, |
| 96 |
{dir, "/../../../../../../../../../../../../../../../../etc/passwd", filepath.Join(dir, "etc", "passwd")}, |
| 97 |
{dir, "../../../../../../../../../../../../../../../../somedir", filepath.Join(dir, "somedir")}, |
| 98 |
{dir, "../../../../../../../../../../../../../../../../", filepath.Join(dir)}, |
| 99 |
{dir, "./../../.././././../../../../../../../../../../../../../../../../etc passwd", filepath.Join(dir, "etc passwd")}, |
| 100 |
} |
| 101 |
|
| 102 |
if runtime.GOOS == "windows" { |
| 103 |
tc = append(tc, []input{ |
| 104 |
{dir, "d:\\etc\\test", filepath.Join(dir, "etc", "test")}, |
| 105 |
}...) |
| 106 |
} |
| 107 |
|
| 108 |
for _, test := range tc { |
| 109 |
got, err := SecureJoin(test.root, test.unsafe) |
| 110 |
if err != nil { |
| 111 |
t.Errorf("securejoin(%q, %q): unexpected error: %v", test.root, test.unsafe, err) |
| 112 |
} |
| 113 |
if got != test.expected { |
| 114 |
t.Errorf("securejoin(%q, %q): expected %q, got %q", test.root, test.unsafe, test.expected, got) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Make sure that .. is **not** expanded lexically. |
| 120 |
func TestNonLexical(t *testing.T) { |
| 121 |
dir := expandedTempDir(t) |
| 122 |
|
| 123 |
testutils.MkdirAll(t, filepath.Join(dir, "subdir"), 0o755) |
| 124 |
testutils.MkdirAll(t, filepath.Join(dir, "cousinparent", "cousin"), 0o755) |
| 125 |
testutils.Symlink(t, "../cousinparent/cousin", filepath.Join(dir, "subdir", "link")) |
| 126 |
testutils.Symlink(t, "/../cousinparent/cousin", filepath.Join(dir, "subdir", "link2")) |
| 127 |
testutils.Symlink(t, "/../../../../../../../../../../../../../../../../cousinparent/cousin", filepath.Join(dir, "subdir", "link3")) |
| 128 |
|
| 129 |
for _, test := range []input{ |
| 130 |
{dir, "subdir", filepath.Join(dir, "subdir")}, |
| 131 |
{dir, "subdir/link/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 132 |
{dir, "subdir/link2/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 133 |
{dir, "subdir/link3/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 134 |
{dir, "subdir/../test", filepath.Join(dir, "test")}, |
| 135 |
// This is the divergence from a simple filepath.Clean implementation. |
| 136 |
{dir, "subdir/link/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 137 |
{dir, "subdir/link2/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 138 |
{dir, "subdir/link3/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 139 |
} { |
| 140 |
got, err := SecureJoin(test.root, test.unsafe) |
| 141 |
if err != nil { |
| 142 |
t.Errorf("securejoin(%q, %q): unexpected error: %v", test.root, test.unsafe, err) |
| 143 |
continue |
| 144 |
} |
| 145 |
if got != test.expected { |
| 146 |
t.Errorf("securejoin(%q, %q): expected %q, got %q", test.root, test.unsafe, test.expected, got) |
| 147 |
continue |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Make sure that symlink loops result in errors. |
| 153 |
func TestSymlinkLoop(t *testing.T) { |
| 154 |
dir := expandedTempDir(t) |
| 155 |
|
| 156 |
testutils.MkdirAll(t, filepath.Join(dir, "subdir"), 0o755) |
| 157 |
testutils.Symlink(t, "../../../../../../../../../../../../../../../../path", filepath.Join(dir, "subdir", "link")) |
| 158 |
testutils.Symlink(t, "/subdir/link", filepath.Join(dir, "path")) |
| 159 |
testutils.Symlink(t, "/../../../../../../../../../../../../../../../../self", filepath.Join(dir, "self")) |
| 160 |
|
| 161 |
for _, test := range []struct { |
| 162 |
root, unsafe string |
| 163 |
}{ |
| 164 |
{dir, "subdir/link"}, |
| 165 |
{dir, "path"}, |
| 166 |
{dir, "../../path"}, |
| 167 |
{dir, "subdir/link/../.."}, |
| 168 |
{dir, "../../../../../../../../../../../../../../../../subdir/link/../../../../../../../../../../../../../../../.."}, |
| 169 |
{dir, "self"}, |
| 170 |
{dir, "self/.."}, |
| 171 |
{dir, "/../../../../../../../../../../../../../../../../self/.."}, |
| 172 |
{dir, "/self/././.."}, |
| 173 |
} { |
| 174 |
got, err := SecureJoin(test.root, test.unsafe) |
| 175 |
if !errors.Is(err, syscall.ELOOP) { |
| 176 |
t.Errorf("securejoin(%q, %q): expected ELOOP, got %q & %v", test.root, test.unsafe, got, err) |
| 177 |
continue |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Make sure that ENOTDIR is correctly handled. |
| 183 |
func TestEnotdir(t *testing.T) { |
| 184 |
dir := expandedTempDir(t) |
| 185 |
|
| 186 |
testutils.MkdirAll(t, filepath.Join(dir, "subdir"), 0o755) |
| 187 |
testutils.WriteFile(t, filepath.Join(dir, "notdir"), []byte("I am not a directory!"), 0o755) |
| 188 |
testutils.Symlink(t, "/../../../notdir/somechild", filepath.Join(dir, "subdir", "link")) |
| 189 |
|
| 190 |
for _, test := range []struct { |
| 191 |
root, unsafe string |
| 192 |
}{ |
| 193 |
{dir, "subdir/link"}, |
| 194 |
{dir, "notdir"}, |
| 195 |
{dir, "notdir/child"}, |
| 196 |
} { |
| 197 |
_, err := SecureJoin(test.root, test.unsafe) |
| 198 |
if err != nil { |
| 199 |
t.Errorf("securejoin(%q, %q): unexpected error: %v", test.root, test.unsafe, err) |
| 200 |
continue |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// Some silly tests to make sure that all error types are correctly handled. |
| 206 |
func TestIsNotExist(t *testing.T) { |
| 207 |
for _, test := range []struct { |
| 208 |
err error |
| 209 |
expected bool |
| 210 |
}{ |
| 211 |
{&os.PathError{Op: "test1", Err: syscall.ENOENT}, true}, |
| 212 |
{&os.LinkError{Op: "test1", Err: syscall.ENOENT}, true}, |
| 213 |
{&os.SyscallError{Syscall: "test1", Err: syscall.ENOENT}, true}, |
| 214 |
{&os.PathError{Op: "test2", Err: syscall.ENOTDIR}, true}, |
| 215 |
{&os.LinkError{Op: "test2", Err: syscall.ENOTDIR}, true}, |
| 216 |
{&os.SyscallError{Syscall: "test2", Err: syscall.ENOTDIR}, true}, |
| 217 |
{&os.PathError{Op: "test3", Err: syscall.EACCES}, false}, |
| 218 |
{&os.LinkError{Op: "test3", Err: syscall.EACCES}, false}, |
| 219 |
{&os.SyscallError{Syscall: "test3", Err: syscall.EACCES}, false}, |
| 220 |
{errors.New("not a proper error"), false}, |
| 221 |
} { |
| 222 |
got := IsNotExist(test.err) |
| 223 |
if got != test.expected { |
| 224 |
t.Errorf("IsNotExist(%#v): expected %v, got %v", test.err, test.expected, got) |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
type mockVFS struct { |
| 230 |
lstat func(path string) (os.FileInfo, error) |
| 231 |
readlink func(path string) (string, error) |
| 232 |
} |
| 233 |
|
| 234 |
func (m mockVFS) Lstat(path string) (os.FileInfo, error) { return m.lstat(path) } |
| 235 |
func (m mockVFS) Readlink(path string) (string, error) { return m.readlink(path) } |
| 236 |
|
| 237 |
// Make sure that SecureJoinVFS actually does use the given VFS interface. |
| 238 |
func TestSecureJoinVFS(t *testing.T) { |
| 239 |
dir := expandedTempDir(t) |
| 240 |
|
| 241 |
testutils.MkdirAll(t, filepath.Join(dir, "subdir"), 0o755) |
| 242 |
testutils.MkdirAll(t, filepath.Join(dir, "cousinparent", "cousin"), 0o755) |
| 243 |
testutils.Symlink(t, "../cousinparent/cousin", filepath.Join(dir, "subdir", "link")) |
| 244 |
testutils.Symlink(t, "/../cousinparent/cousin", filepath.Join(dir, "subdir", "link2")) |
| 245 |
testutils.Symlink(t, "/../../../../../../../../../../../../../../../../cousinparent/cousin", filepath.Join(dir, "subdir", "link3")) |
| 246 |
|
| 247 |
for _, test := range []input{ |
| 248 |
{dir, "subdir", filepath.Join(dir, "subdir")}, |
| 249 |
{dir, "subdir/link/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 250 |
{dir, "subdir/link2/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 251 |
{dir, "subdir/link3/test", filepath.Join(dir, "cousinparent", "cousin", "test")}, |
| 252 |
{dir, "subdir/../test", filepath.Join(dir, "test")}, |
| 253 |
// This is the divergence from a simple filepath.Clean implementation. |
| 254 |
{dir, "subdir/link/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 255 |
{dir, "subdir/link2/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 256 |
{dir, "subdir/link3/../test", filepath.Join(dir, "cousinparent", "test")}, |
| 257 |
} { |
| 258 |
var nLstat, nReadlink int |
| 259 |
mock := mockVFS{ |
| 260 |
lstat: func(path string) (os.FileInfo, error) { nLstat++; return os.Lstat(path) }, |
| 261 |
readlink: func(path string) (string, error) { nReadlink++; return os.Readlink(path) }, |
| 262 |
} |
| 263 |
|
| 264 |
got, err := SecureJoinVFS(test.root, test.unsafe, mock) |
| 265 |
if err != nil { |
| 266 |
t.Errorf("securejoin(%q, %q): unexpected error: %v", test.root, test.unsafe, err) |
| 267 |
continue |
| 268 |
} |
| 269 |
if got != test.expected { |
| 270 |
t.Errorf("securejoin(%q, %q): expected %q, got %q", test.root, test.unsafe, test.expected, got) |
| 271 |
continue |
| 272 |
} |
| 273 |
if nLstat == 0 && nReadlink == 0 { |
| 274 |
t.Errorf("securejoin(%q, %q): expected to use either lstat or readlink, neither were used", test.root, test.unsafe) |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Make sure that SecureJoinVFS actually does use the given VFS interface, and |
| 280 |
// that errors are correctly propagated. |
| 281 |
func TestSecureJoinVFSErrors(t *testing.T) { |
| 282 |
var ( |
| 283 |
lstatErr = errors.New("lstat error") |
| 284 |
readlinkErr = errors.New("readlink err") |
| 285 |
) |
| 286 |
|
| 287 |
dir := expandedTempDir(t) |
| 288 |
|
| 289 |
// Make a link. |
| 290 |
testutils.Symlink(t, "../../../../../../../../../../../../../../../../path", filepath.Join(dir, "link")) |
| 291 |
|
| 292 |
// Define some fake mock functions. |
| 293 |
lstatFailFn := func(string) (os.FileInfo, error) { return nil, lstatErr } |
| 294 |
readlinkFailFn := func(string) (string, error) { return "", readlinkErr } |
| 295 |
|
| 296 |
// Make sure that the set of {lstat, readlink} failures do propagate. |
| 297 |
for idx, test := range []struct { |
| 298 |
vfs VFS |
| 299 |
expected []error |
| 300 |
}{ |
| 301 |
{ |
| 302 |
expected: []error{nil}, |
| 303 |
vfs: mockVFS{ |
| 304 |
lstat: os.Lstat, |
| 305 |
readlink: os.Readlink, |
| 306 |
}, |
| 307 |
}, |
| 308 |
{ |
| 309 |
expected: []error{lstatErr}, |
| 310 |
vfs: mockVFS{ |
| 311 |
lstat: lstatFailFn, |
| 312 |
readlink: os.Readlink, |
| 313 |
}, |
| 314 |
}, |
| 315 |
{ |
| 316 |
expected: []error{readlinkErr}, |
| 317 |
vfs: mockVFS{ |
| 318 |
lstat: os.Lstat, |
| 319 |
readlink: readlinkFailFn, |
| 320 |
}, |
| 321 |
}, |
| 322 |
{ |
| 323 |
expected: []error{lstatErr, readlinkErr}, |
| 324 |
vfs: mockVFS{ |
| 325 |
lstat: lstatFailFn, |
| 326 |
readlink: readlinkFailFn, |
| 327 |
}, |
| 328 |
}, |
| 329 |
} { |
| 330 |
_, err := SecureJoinVFS(dir, "link", test.vfs) |
| 331 |
|
| 332 |
success := false |
| 333 |
for _, exp := range test.expected { |
| 334 |
if errors.Is(err, exp) { |
| 335 |
success = true |
| 336 |
} |
| 337 |
} |
| 338 |
if !success { |
| 339 |
t.Errorf("SecureJoinVFS.mock%d: expected to get lstatError, got %v", idx, err) |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
func TestUncleanRoot(t *testing.T) { |
| 345 |
root := t.TempDir() |
| 346 |
|
| 347 |
for _, test := range []struct { |
| 348 |
testName, root string |
| 349 |
expectedErr error |
| 350 |
}{ |
| 351 |
{"trailing-dotdot", "foo/..", errUnsafeRoot}, |
| 352 |
{"leading-dotdot", "../foo", errUnsafeRoot}, |
| 353 |
{"middle-dotdot", "../foo", errUnsafeRoot}, |
| 354 |
{"many-dotdot", "foo/../foo/../a", errUnsafeRoot}, |
| 355 |
{"trailing-slash", root + "/foo/bar/", nil}, |
| 356 |
{"trailing-slashes", root + "/foo/bar///", nil}, |
| 357 |
{"many-slashes", root + "/foo///bar////baz", nil}, |
| 358 |
{"plain-dot", root + "/foo/./bar", nil}, |
| 359 |
{"many-dot", root + "/foo/./bar/./.", nil}, |
| 360 |
{"unclean-safe", root + "/foo///./bar/.///.///", nil}, |
| 361 |
{"unclean-unsafe", root + "/foo///./bar/..///.///", errUnsafeRoot}, |
| 362 |
} { |
| 363 |
test := test // copy iterator |
| 364 |
t.Run(test.testName, func(t *testing.T) { |
| 365 |
_, err := SecureJoin(test.root, "foo/bar/baz") |
| 366 |
if test.expectedErr != nil { |
| 367 |
assert.ErrorIsf(t, err, test.expectedErr, "SecureJoin with unsafe root %q", test.root) |
| 368 |
} else { |
| 369 |
assert.NoErrorf(t, err, "SecureJoin with safe but unclean root %q", test.root) |
| 370 |
} |
| 371 |
}) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestHasDotDot(t *testing.T) { |
| 376 |
for _, test := range []struct { |
| 377 |
testName, path string |
| 378 |
expected bool |
| 379 |
}{ |
| 380 |
{"plain-dotdot", "..", true}, |
| 381 |
{"trailing-dotdot", "foo/bar/baz/..", true}, |
| 382 |
{"leading-dotdot", "../foo/bar/baz", true}, |
| 383 |
{"middle-dotdot", "foo/bar/../baz", true}, |
| 384 |
{"dotdot-in-name1", "foo/..bar/baz", false}, |
| 385 |
{"dotdot-in-name2", "foo/bar../baz", false}, |
| 386 |
{"dotdot-in-name3", "foo/b..r/baz", false}, |
| 387 |
{"dotdot-in-name4", "..foo/bar/baz", false}, |
| 388 |
{"dotdot-in-name5", "foo/bar/baz..", false}, |
| 389 |
{"dot1", "./foo/bar/baz", false}, |
| 390 |
{"dot2", "foo/bar/baz/.", false}, |
| 391 |
{"dot3", "foo/././bar/baz", false}, |
| 392 |
{"unclean", "foo//.//bar/baz////", false}, |
| 393 |
} { |
| 394 |
test := test // copy iterator |
| 395 |
t.Run(test.testName, func(t *testing.T) { |
| 396 |
got := hasDotDot(test.path) |
| 397 |
assert.Equalf(t, test.expected, got, "unexpected result for hasDotDot(%q)", test.path) |
| 398 |
}) |
| 399 |
} |
| 400 |
} |