From 2648d475d751b47755958f47a366e300b6b6de0a Mon Sep 17 00:00:00 2001 From: Percy Wegmann Date: Tue, 30 Apr 2024 20:31:49 -0500 Subject: [PATCH] drive: don't allow DELETE on read-only shares Fixes tailscale/corp#19646 Signed-off-by: Percy Wegmann --- drive/driveimpl/drive_test.go | 32 +++++++++++++++++++++++++++++++- drive/driveimpl/remote_impl.go | 1 + 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/drive/driveimpl/drive_test.go b/drive/driveimpl/drive_test.go index 6dc4c7ce5..66405648e 100644 --- a/drive/driveimpl/drive_test.go +++ b/drive/driveimpl/drive_test.go @@ -88,11 +88,33 @@ func TestFileManipulation(t *testing.T) { s.checkFileContents(remote1, share11, file112) s.addShare(remote1, share12, drive.PermissionReadOnly) - s.writeFile("writing file to read-only remote should fail", remote1, share12, file111, "hello world", false) s.writeFile("writing file to non-existent remote should fail", "non-existent", share11, file111, "hello world", false) s.writeFile("writing file to non-existent share should fail", remote1, "non-existent", file111, "hello world", false) } +func TestPermissions(t *testing.T) { + s := newSystem(t) + + s.addRemote(remote1) + s.addShare(remote1, share12, drive.PermissionReadOnly) + + s.writeFile("writing file to read-only remote should fail", remote1, share12, file111, "hello world", false) + if err := s.client.Mkdir(path.Join(remote1, share12), 0644); err == nil { + t.Error("making directory on read-only remote should fail") + } + + // Now, write file directly to file system so that we can test permissions + // on other operations. + s.write(remote1, share12, file111, "hello world") + if err := s.client.Remove(pathTo(remote1, share12, file111)); err == nil { + t.Error("deleting file from read-only remote should fail") + } + if err := s.client.Rename(pathTo(remote1, share12, file111), pathTo(remote1, share12, file112), true); err == nil { + t.Error("moving file on read-only remote should fail") + } + +} + type local struct { l net.Listener fs *FileSystemForLocal @@ -324,6 +346,14 @@ func (s *system) read(remoteName, shareName, name string) string { return string(b) } +func (s *system) write(remoteName, shareName, name, contents string) { + filename := filepath.Join(s.remotes[remoteName].shares[shareName], name) + err := os.WriteFile(filename, []byte(contents), 0644) + if err != nil { + s.t.Fatalf("failed to WriteFile: %s", err) + } +} + func (s *system) readViaWebDAV(remoteName, shareName, name string) string { path := pathTo(remoteName, shareName, name) b, err := s.client.Read(path) diff --git a/drive/driveimpl/remote_impl.go b/drive/driveimpl/remote_impl.go index 9f45dac05..446e0ed6c 100644 --- a/drive/driveimpl/remote_impl.go +++ b/drive/driveimpl/remote_impl.go @@ -380,6 +380,7 @@ func (s *userServer) run() error { "MKCOL": true, "MOVE": true, "PROPPATCH": true, + "DELETE": true, } // canSudo checks wether we can sudo -u the configured executable as the