| 1 |
//go:build !windows |
| 2 |
|
| 3 |
package desync |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func newTestLocalFS(root string) *LocalFS { |
| 16 |
// NoSameOwner/NoSamePermissions keep the tests runnable as a non-root |
| 17 |
// user; the escape protection under test lives in the os.Root-backed |
| 18 |
// Create* methods, not in the permission handling. |
| 19 |
return NewLocalFS(root, LocalFSOptions{NoSameOwner: true, NoSamePermissions: true}) |
| 20 |
} |
| 21 |
|
| 22 |
// TestLocalFSSymlinkWriteEscape reproduces the reported attack: an archive |
| 23 |
// plants a symlink inside the extraction root pointing outside it, then a |
| 24 |
// later entry with a clean name ("evil/passwd", no "..") writes through it. |
| 25 |
// The write must be refused and the out-of-root file left untouched. |
| 26 |
func TestLocalFSSymlinkWriteEscape(t *testing.T) { |
| 27 |
root := t.TempDir() |
| 28 |
outside := t.TempDir() |
| 29 |
sentinel := filepath.Join(outside, "passwd") |
| 30 |
const orig = "original-contents\n" |
| 31 |
require.NoError(t, os.WriteFile(sentinel, []byte(orig), 0644)) |
| 32 |
|
| 33 |
fs := newTestLocalFS(root) |
| 34 |
defer fs.Close() |
| 35 |
|
| 36 |
require.NoError(t, fs.CreateSymlink(NodeSymlink{Name: "evil", Target: outside})) |
| 37 |
|
| 38 |
require.Error(t, fs.CreateFile(NodeFile{Name: "evil/passwd", Data: strings.NewReader("PWNED")}), |
| 39 |
"CreateFile through escaping symlink succeeded, want error") |
| 40 |
|
| 41 |
got, err := os.ReadFile(sentinel) |
| 42 |
require.NoError(t, err) |
| 43 |
require.Equal(t, orig, string(got), "file outside root was modified") |
| 44 |
} |
| 45 |
|
| 46 |
// TestLocalFSLexicalEscape covers the plain ".." traversal at the LocalFS |
| 47 |
// layer (independent of the decoder hardening). |
| 48 |
func TestLocalFSLexicalEscape(t *testing.T) { |
| 49 |
root := t.TempDir() |
| 50 |
fs := newTestLocalFS(root) |
| 51 |
defer fs.Close() |
| 52 |
|
| 53 |
assert.Error(t, fs.CreateFile(NodeFile{Name: "../escape", Data: strings.NewReader("x")}), |
| 54 |
"CreateFile(../escape) succeeded, want error") |
| 55 |
assert.Error(t, fs.CreateDir(NodeDirectory{Name: "../evildir"}), |
| 56 |
"CreateDir(../evildir) succeeded, want error") |
| 57 |
_, err := os.Lstat(filepath.Join(filepath.Dir(root), "escape")) |
| 58 |
assert.True(t, os.IsNotExist(err), "escape file created outside root: err=%v", err) |
| 59 |
} |
| 60 |
|
| 61 |
// TestLocalFSAbsoluteSymlinkTargetVerbatim confirms an absolute symlink target |
| 62 |
// is created verbatim (legitimate, matches GNU tar/casync) but cannot be |
| 63 |
// followed during extraction. |
| 64 |
func TestLocalFSAbsoluteSymlinkTargetVerbatim(t *testing.T) { |
| 65 |
root := t.TempDir() |
| 66 |
fs := newTestLocalFS(root) |
| 67 |
defer fs.Close() |
| 68 |
|
| 69 |
require.NoError(t, fs.CreateSymlink(NodeSymlink{Name: "abs", Target: "/etc"})) |
| 70 |
tgt, err := os.Readlink(filepath.Join(root, "abs")) |
| 71 |
require.NoError(t, err) |
| 72 |
require.Equal(t, "/etc", tgt, "symlink target should be created verbatim") |
| 73 |
|
| 74 |
require.Error(t, fs.CreateFile(NodeFile{Name: "abs/desync-should-not-exist", Data: strings.NewReader("x")}), |
| 75 |
"write through absolute symlink succeeded, want error") |
| 76 |
_, err = os.Lstat("/etc/desync-should-not-exist") |
| 77 |
require.True(t, os.IsNotExist(err), "file created under /etc: err=%v", err) |
| 78 |
} |
| 79 |
|
| 80 |
// TestLocalFSBenignRelativeSymlink is a regression guard: a relative symlink |
| 81 |
// that stays within the root must still be followed so legitimate archives |
| 82 |
// continue to extract correctly. |
| 83 |
func TestLocalFSBenignRelativeSymlink(t *testing.T) { |
| 84 |
root := t.TempDir() |
| 85 |
fs := newTestLocalFS(root) |
| 86 |
defer fs.Close() |
| 87 |
|
| 88 |
require.NoError(t, fs.CreateDir(NodeDirectory{Name: "sub"})) |
| 89 |
require.NoError(t, fs.CreateSymlink(NodeSymlink{Name: "link", Target: "sub"})) |
| 90 |
require.NoError(t, fs.CreateFile(NodeFile{Name: "link/g", Data: strings.NewReader("hello")}), |
| 91 |
"CreateFile through in-root symlink") |
| 92 |
got, err := os.ReadFile(filepath.Join(root, "sub", "g")) |
| 93 |
require.NoError(t, err, "expected file via in-root symlink") |
| 94 |
require.Equal(t, "hello", string(got)) |
| 95 |
} |