| 1 |
//go:build !windows |
| 2 |
|
| 3 |
package desync |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"io/fs" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
// makeWritableOnCleanup ensures a tree containing read-only directories can be |
| 18 |
// removed again when the test ends. |
| 19 |
func makeWritableOnCleanup(t *testing.T, root string) { |
| 20 |
t.Helper() |
| 21 |
t.Cleanup(func() { |
| 22 |
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 23 |
if err == nil && d.IsDir() { |
| 24 |
os.Chmod(path, 0755) |
| 25 |
} |
| 26 |
return nil |
| 27 |
}) |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
// TestUnTarReadOnlyDir extracts an archive containing read-only directories |
| 32 |
// that hold further directories and files. The mode from the archive can only |
| 33 |
// be applied once a directory has been populated, see issue #376. |
| 34 |
func TestUnTarReadOnlyDir(t *testing.T) { |
| 35 |
src := t.TempDir() |
| 36 |
makeWritableOnCleanup(t, src) |
| 37 |
|
| 38 |
for _, d := range []string{"ro/sub1", "ro/sub2/deep"} { |
| 39 |
require.NoError(t, os.MkdirAll(filepath.Join(src, d), 0755)) |
| 40 |
} |
| 41 |
require.NoError(t, os.WriteFile(filepath.Join(src, "ro/sub1/file"), []byte("content"), 0644)) |
| 42 |
require.NoError(t, os.Symlink("file", filepath.Join(src, "ro/sub1/link"))) |
| 43 |
// Every directory in the tree is read-only, including the root |
| 44 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro/sub2/deep"), 0555)) |
| 45 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro/sub2"), 0555)) |
| 46 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro/sub1"), 0555)) |
| 47 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro"), 0555)) |
| 48 |
require.NoError(t, os.Chmod(src, 0555)) |
| 49 |
|
| 50 |
b := tarDir(t, src) |
| 51 |
|
| 52 |
dst := filepath.Join(t.TempDir(), "out") |
| 53 |
makeWritableOnCleanup(t, dst) |
| 54 |
lfs := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true}) |
| 55 |
defer lfs.Close() |
| 56 |
require.NoError(t, UnTar(context.Background(), b, lfs)) |
| 57 |
|
| 58 |
// The archive permissions need to be in place afterwards |
| 59 |
for _, d := range []string{".", "ro", "ro/sub1", "ro/sub2", "ro/sub2/deep"} { |
| 60 |
info, err := os.Stat(filepath.Join(dst, d)) |
| 61 |
require.NoError(t, err) |
| 62 |
assert.Equal(t, os.FileMode(0555), info.Mode().Perm(), "mode of dir %s", d) |
| 63 |
} |
| 64 |
|
| 65 |
content, err := os.ReadFile(filepath.Join(dst, "ro/sub1/file")) |
| 66 |
require.NoError(t, err) |
| 67 |
assert.Equal(t, "content", string(content)) |
| 68 |
|
| 69 |
target, err := os.Readlink(filepath.Join(dst, "ro/sub1/link")) |
| 70 |
require.NoError(t, err) |
| 71 |
assert.Equal(t, "file", target) |
| 72 |
} |
| 73 |
|
| 74 |
// TestLocalFSDirModeWhilePopulated confirms a directory is never more |
| 75 |
// permissive than the archive says, not even in the window between its |
| 76 |
// creation and the point where it's complete. Only the permissions needed to |
| 77 |
// write the contents are added. |
| 78 |
func TestLocalFSDirModeWhilePopulated(t *testing.T) { |
| 79 |
dst := filepath.Join(t.TempDir(), "out") |
| 80 |
makeWritableOnCleanup(t, dst) |
| 81 |
|
| 82 |
lfs := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true}) |
| 83 |
defer lfs.Close() |
| 84 |
|
| 85 |
require.NoError(t, lfs.CreateDir(NodeDirectory{Name: ".", Mode: 0755 | os.ModeDir})) |
| 86 |
require.NoError(t, lfs.CreateDir(NodeDirectory{Name: "priv", Mode: 0700 | os.ModeDir})) |
| 87 |
require.NoError(t, lfs.CreateDir(NodeDirectory{Name: "priv/ro", Mode: 0500 | os.ModeDir})) |
| 88 |
|
| 89 |
// Not world-readable at any point, and 0500 only gained what it takes to |
| 90 |
// write into it |
| 91 |
info, err := os.Stat(filepath.Join(dst, "priv")) |
| 92 |
require.NoError(t, err) |
| 93 |
assert.Equal(t, os.FileMode(0700), info.Mode().Perm(), "mode of priv while being populated") |
| 94 |
|
| 95 |
info, err = os.Stat(filepath.Join(dst, "priv/ro")) |
| 96 |
require.NoError(t, err) |
| 97 |
assert.Equal(t, os.FileMode(0700), info.Mode().Perm(), "mode of priv/ro while being populated") |
| 98 |
|
| 99 |
require.NoError(t, lfs.CreateFile(NodeFile{Name: "priv/ro/file", Mode: 0600, Data: strings.NewReader("content")})) |
| 100 |
require.NoError(t, lfs.Finalize()) |
| 101 |
|
| 102 |
for d, want := range map[string]os.FileMode{"priv": 0700, "priv/ro": 0500} { |
| 103 |
info, err := os.Stat(filepath.Join(dst, d)) |
| 104 |
require.NoError(t, err) |
| 105 |
assert.Equal(t, want, info.Mode().Perm(), "final mode of %s", d) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// TestLocalFSDirSetgidWhilePopulated confirms the setgid bit is in place while |
| 110 |
// the contents of a directory are written, so they inherit its group. |
| 111 |
func TestLocalFSDirSetgidWhilePopulated(t *testing.T) { |
| 112 |
dst := filepath.Join(t.TempDir(), "out") |
| 113 |
|
| 114 |
lfs := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true}) |
| 115 |
defer lfs.Close() |
| 116 |
|
| 117 |
require.NoError(t, lfs.CreateDir(NodeDirectory{Name: ".", Mode: 0755 | os.ModeDir})) |
| 118 |
require.NoError(t, lfs.CreateDir(NodeDirectory{Name: "shared", Mode: 0775 | os.ModeDir | os.ModeSetgid})) |
| 119 |
|
| 120 |
info, err := os.Stat(filepath.Join(dst, "shared")) |
| 121 |
require.NoError(t, err) |
| 122 |
assert.NotZero(t, info.Mode()&os.ModeSetgid, "setgid while the directory is populated") |
| 123 |
|
| 124 |
require.NoError(t, lfs.Finalize()) |
| 125 |
|
| 126 |
info, err = os.Stat(filepath.Join(dst, "shared")) |
| 127 |
require.NoError(t, err) |
| 128 |
assert.NotZero(t, info.Mode()&os.ModeSetgid, "setgid after finalizing") |
| 129 |
} |
| 130 |
|
| 131 |
// TestUnTarReadOnlyDirNoSamePermissions extracts the same archive while |
| 132 |
// ignoring the permissions in it. That has to work as well, and must not apply |
| 133 |
// the archive mode to the directories. |
| 134 |
func TestUnTarReadOnlyDirNoSamePermissions(t *testing.T) { |
| 135 |
src := t.TempDir() |
| 136 |
makeWritableOnCleanup(t, src) |
| 137 |
|
| 138 |
require.NoError(t, os.MkdirAll(filepath.Join(src, "ro/sub"), 0755)) |
| 139 |
require.NoError(t, os.WriteFile(filepath.Join(src, "ro/sub/file"), []byte("content"), 0644)) |
| 140 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro/sub"), 0555)) |
| 141 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro"), 0555)) |
| 142 |
|
| 143 |
b := tarDir(t, src) |
| 144 |
|
| 145 |
dst := filepath.Join(t.TempDir(), "out") |
| 146 |
lfs := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true, NoSamePermissions: true}) |
| 147 |
defer lfs.Close() |
| 148 |
require.NoError(t, UnTar(context.Background(), b, lfs)) |
| 149 |
|
| 150 |
// Writable by the current user, i.e. not what the archive says |
| 151 |
for _, d := range []string{"ro", "ro/sub"} { |
| 152 |
info, err := os.Stat(filepath.Join(dst, d)) |
| 153 |
require.NoError(t, err) |
| 154 |
assert.Equal(t, os.FileMode(0700), info.Mode().Perm()&0700, "mode of dir %s", d) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// TestUnTarNoSamePermissionsOverReadOnlyTree extracts over a tree that was |
| 159 |
// written by an earlier extraction and contains read-only directories. |
| 160 |
func TestUnTarNoSamePermissionsOverReadOnlyTree(t *testing.T) { |
| 161 |
src := t.TempDir() |
| 162 |
makeWritableOnCleanup(t, src) |
| 163 |
|
| 164 |
require.NoError(t, os.MkdirAll(filepath.Join(src, "ro/sub"), 0755)) |
| 165 |
require.NoError(t, os.WriteFile(filepath.Join(src, "ro/sub/file"), []byte("content"), 0644)) |
| 166 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro/sub"), 0555)) |
| 167 |
require.NoError(t, os.Chmod(filepath.Join(src, "ro"), 0555)) |
| 168 |
|
| 169 |
dst := filepath.Join(t.TempDir(), "out") |
| 170 |
makeWritableOnCleanup(t, dst) |
| 171 |
|
| 172 |
// First pass, applying the archive permissions |
| 173 |
first := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true}) |
| 174 |
require.NoError(t, UnTar(context.Background(), tarDir(t, src), first)) |
| 175 |
require.NoError(t, first.Close()) |
| 176 |
|
| 177 |
// Second pass over the now read-only tree, ignoring the archive permissions |
| 178 |
second := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true, NoSamePermissions: true}) |
| 179 |
defer second.Close() |
| 180 |
require.NoError(t, UnTar(context.Background(), tarDir(t, src), second)) |
| 181 |
|
| 182 |
content, err := os.ReadFile(filepath.Join(dst, "ro/sub/file")) |
| 183 |
require.NoError(t, err) |
| 184 |
assert.Equal(t, "content", string(content)) |
| 185 |
} |
| 186 |
|
| 187 |
// TestUnTarIntoReadOnlyDir extracts into a target directory that exists |
| 188 |
// already and isn't writable. |
| 189 |
func TestUnTarIntoReadOnlyDir(t *testing.T) { |
| 190 |
src := t.TempDir() |
| 191 |
require.NoError(t, os.WriteFile(filepath.Join(src, "file"), []byte("content"), 0644)) |
| 192 |
|
| 193 |
b := tarDir(t, src) |
| 194 |
|
| 195 |
dst := filepath.Join(t.TempDir(), "out") |
| 196 |
makeWritableOnCleanup(t, dst) |
| 197 |
require.NoError(t, os.Mkdir(dst, 0555)) |
| 198 |
|
| 199 |
lfs := NewLocalFS(dst, LocalFSOptions{NoSameOwner: true}) |
| 200 |
defer lfs.Close() |
| 201 |
require.NoError(t, UnTar(context.Background(), b, lfs)) |
| 202 |
|
| 203 |
content, err := os.ReadFile(filepath.Join(dst, "file")) |
| 204 |
require.NoError(t, err) |
| 205 |
assert.Equal(t, "content", string(content)) |
| 206 |
} |