302 lines
11 KiB
Go
302 lines
11 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDriveShareRechecksOpenedVolumeBeforeAccess(t *testing.T) {
|
|
srv := NewTransportServer(nil)
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "file"), []byte("drive data"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
activeVolume := "original"
|
|
cfg := VMConfig{
|
|
VMName: "mac", Token: "token", DriveSharingEnabled: true,
|
|
SharedDrives: []string{"C"}, DriveRoots: map[string]string{"C": dir},
|
|
DriveVolumes: map[string]string{"C": "original"},
|
|
VerifyDriveRoot: func(root *os.Root, path, expected string) error {
|
|
if path != dir {
|
|
t.Errorf("unexpected drive path: %s", path)
|
|
}
|
|
if _, err := root.Stat("file"); err != nil {
|
|
return err
|
|
}
|
|
if expected != activeVolume {
|
|
return os.ErrPermission
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
srv.RegisterVM(cfg)
|
|
get := func() *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodGet, "/dav/mac/drives/C/file", nil)
|
|
req.SetBasicAuth("reims", cfg.Token)
|
|
w := httptest.NewRecorder()
|
|
srv.handleWebDAV(w, req)
|
|
return w
|
|
}
|
|
if w := get(); w.Code != http.StatusOK || w.Body.String() != "drive data" {
|
|
t.Fatalf("authorized volume: %d %q", w.Code, w.Body.String())
|
|
}
|
|
activeVolume = "replacement"
|
|
if w := get(); w.Code < 400 || strings.Contains(w.Body.String(), "drive data") {
|
|
t.Fatalf("replacement volume served by cached handler: %d %q", w.Code, w.Body.String())
|
|
}
|
|
cfg.DriveVolumes["C"] = activeVolume
|
|
srv.RegisterVM(cfg)
|
|
if w := get(); w.Code != http.StatusOK || w.Body.String() != "drive data" {
|
|
t.Fatalf("new authorization: %d %q", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestNormalizeDriveLetter(t *testing.T) {
|
|
for _, tc := range []struct{ in, want string }{{"c", "C"}, {"C:", "C"}, {"d:\\", "D"}, {" e: ", "E"}} {
|
|
got, ok := NormalizeDriveLetter(tc.in)
|
|
if !ok || got != tc.want {
|
|
t.Fatalf("NormalizeDriveLetter(%q) = %q, %v; want %q", tc.in, got, ok, tc.want)
|
|
}
|
|
}
|
|
for _, raw := range []string{"", "CC", "1", "C:/", "../C", `\\server\share`} {
|
|
if got, ok := NormalizeDriveLetter(raw); ok {
|
|
t.Fatalf("NormalizeDriveLetter(%q) unexpectedly accepted as %q", raw, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReadOnlyDriveSharesEnforceSelectionAndIsolation(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
rootC := filepath.Join(tmp, "drive-c")
|
|
rootD := filepath.Join(tmp, "drive-d")
|
|
if err := os.MkdirAll(rootC, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(rootD, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(rootC, "hello.txt"), []byte("from-c"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(rootD, "hello.txt"), []byte("from-d"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
srv := NewServer(&memClipboard{})
|
|
port, err := srv.Start(-1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer srv.Close(context.Background())
|
|
base := fmt.Sprintf("http://127.0.0.1:%d", port)
|
|
cfg := VMConfig{
|
|
VMName: "mac", Token: strings.Repeat("ab", 32), Port: port,
|
|
SharedDir: filepath.Join(tmp, "shared"), DriveSharingEnabled: true,
|
|
SharedDrives: []string{"C"}, DriveRoots: map[string]string{"C": rootC, "D": rootD},
|
|
}
|
|
srv.RegisterVM(cfg)
|
|
other := VMConfig{
|
|
VMName: "other", Token: strings.Repeat("cd", 32), Port: port,
|
|
DriveSharingEnabled: true, SharedDrives: []string{"C"},
|
|
DriveRoots: map[string]string{"C": rootD},
|
|
}
|
|
srv.RegisterVM(other)
|
|
|
|
do := func(method, reqPath, token string, body io.Reader) (int, string, http.Header) {
|
|
t.Helper()
|
|
req, err := http.NewRequest(method, base+reqPath, body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
if method == "PROPFIND" {
|
|
req.Header.Set("Depth", "1")
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return resp.StatusCode, string(b), resp.Header
|
|
}
|
|
readC := "/dav/mac/drives/C/hello.txt"
|
|
if code, _, headers := do(http.MethodOptions, "/dav/mac/drives/C/", cfg.Token, nil); code != http.StatusOK ||
|
|
headers.Get("Allow") != "OPTIONS, GET, HEAD, PROPFIND" || headers.Get("DAV") != "1" {
|
|
t.Fatalf("OPTIONS shared C: code=%d headers=%v", code, headers)
|
|
}
|
|
if code, body, _ := do(http.MethodGet, readC, cfg.Token, nil); code != http.StatusOK || body != "from-c" {
|
|
t.Fatalf("read shared C: code=%d body=%q", code, body)
|
|
}
|
|
if code, body, _ := do("PROPFIND", "/dav/mac/drives/C/", cfg.Token, nil); code != http.StatusMultiStatus || !strings.Contains(body, "hello.txt") {
|
|
t.Fatalf("PROPFIND shared C: code=%d body=%s", code, body)
|
|
}
|
|
reqMissing, err := http.NewRequest("PROPFIND", base+"/dav/mac/drives/C/", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reqMissing.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
respMissing, err := http.DefaultClient.Do(reqMissing)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
respMissing.Body.Close()
|
|
if respMissing.StatusCode != http.StatusMultiStatus {
|
|
t.Fatalf("PROPFIND without Depth: code=%d, want 207", respMissing.StatusCode)
|
|
}
|
|
for _, depth := range []string{"infinity", "2"} {
|
|
req, err := http.NewRequest("PROPFIND", base+"/dav/mac/drives/C/", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
if depth != "" {
|
|
req.Header.Set("Depth", depth)
|
|
}
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("PROPFIND Depth %q: code=%d, want 400", depth, resp.StatusCode)
|
|
}
|
|
}
|
|
if code, _, _ := do(http.MethodPut, "/dav/mac/drives/C/new.txt", cfg.Token, strings.NewReader("bad")); code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("PUT read-only C: code=%d, want 405", code)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(rootC, "new.txt")); !os.IsNotExist(err) {
|
|
t.Fatalf("read-only PUT created a host file: %v", err)
|
|
}
|
|
for _, method := range []string{http.MethodDelete, "MKCOL", "MOVE", "COPY", "PROPPATCH", "LOCK"} {
|
|
if code, _, _ := do(method, readC, cfg.Token, nil); code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("%s read-only C: code=%d, want 405", method, code)
|
|
}
|
|
}
|
|
if body, err := os.ReadFile(filepath.Join(rootC, "hello.txt")); err != nil || string(body) != "from-c" {
|
|
t.Fatalf("read-only DELETE changed host file: %q, %v", body, err)
|
|
}
|
|
if code, _, _ := do(http.MethodGet, "/dav/mac/drives/D/hello.txt", cfg.Token, nil); code != http.StatusForbidden {
|
|
t.Fatalf("unselected D: code=%d, want 403", code)
|
|
}
|
|
if code, _, _ := do(http.MethodGet, readC, "", nil); code != http.StatusUnauthorized {
|
|
t.Fatalf("unauthenticated drive read: code=%d, want 401", code)
|
|
}
|
|
if code, body, _ := do(http.MethodGet, "/dav/other/drives/C/hello.txt", other.Token, nil); code != http.StatusOK || body != "from-d" {
|
|
t.Fatalf("per-VM drive root isolation: code=%d body=%q", code, body)
|
|
}
|
|
if code, _, _ := do(http.MethodGet, "/dav/other/drives/C/hello.txt", cfg.Token, nil); code != http.StatusForbidden {
|
|
t.Fatalf("cross-VM drive read: code=%d, want 403", code)
|
|
}
|
|
if _, body, _ := do(http.MethodGet, "/v1/config", cfg.Token, nil); !strings.Contains(body, `"shared_drives":["C"]`) {
|
|
t.Fatalf("enabled config shared_drives missing C: %s", body)
|
|
}
|
|
|
|
cfg.SharedDrives = []string{"C", "D"}
|
|
srv.RegisterVM(cfg)
|
|
if code, body, _ := do(http.MethodGet, "/dav/mac/drives/D/hello.txt", cfg.Token, nil); code != http.StatusOK || body != "from-d" {
|
|
t.Fatalf("newly selected D: code=%d body=%q", code, body)
|
|
}
|
|
cfg.DriveSharingEnabled = false
|
|
srv.RegisterVM(cfg)
|
|
if code, _, _ := do(http.MethodGet, readC, cfg.Token, nil); code != http.StatusForbidden {
|
|
t.Fatalf("disabled master switch: code=%d, want 403", code)
|
|
}
|
|
req, _ := http.NewRequest(http.MethodGet, base+"/v1/config", nil)
|
|
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var live struct {
|
|
SharedDrives []string `json:"shared_drives"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&live); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
if live.SharedDrives == nil || len(live.SharedDrives) != 0 {
|
|
t.Fatalf("disabled config shared_drives = %#v, want empty JSON array", live.SharedDrives)
|
|
}
|
|
}
|
|
|
|
func TestDriveShareConfigPersistsLettersButNotRuntimeRoots(t *testing.T) {
|
|
vmDir := filepath.Join(t.TempDir(), "vm")
|
|
if err := os.Mkdir(vmDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg := VMConfig{VMName: "mac", Token: strings.Repeat("ab", 32), DriveSharingEnabled: true, SharedDrives: []string{"C"}, DriveVolumes: map[string]string{"C": "vol-c"}, DriveRoots: map[string]string{"C": `C:\secret-root`}}
|
|
if err := SaveVMConfig(vmDir, cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(vmDir, ConfigFileName))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(raw)
|
|
if !strings.Contains(text, `"shared_drives": [`) || !strings.Contains(text, `"C"`) || strings.Contains(text, "secret-root") || strings.Contains(text, "DriveRoots") {
|
|
t.Fatalf("persisted drive config leaks runtime roots or omits letters: %s", text)
|
|
}
|
|
loaded, ok := ReadVMConfig(vmDir)
|
|
if !ok || !loaded.DriveSharingEnabled || len(loaded.SharedDrives) != 1 || loaded.SharedDrives[0] != "C" || loaded.DriveVolumes["C"] != "vol-c" || loaded.DriveRoots != nil {
|
|
t.Fatalf("loaded drive config = %#v", loaded)
|
|
}
|
|
}
|
|
func TestReadOnlyDriveFSRejectsWritesAndEscapingSymlink(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
root := filepath.Join(tmp, "root")
|
|
outside := filepath.Join(tmp, "outside")
|
|
if err := os.MkdirAll(root, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(outside, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
victim := filepath.Join(root, "victim.txt")
|
|
if err := os.WriteFile(victim, []byte("safe"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fsys, err := newRootWebDAVFS(sharedRoot{path: root}, true, false, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, flag := range []int{os.O_WRONLY, os.O_RDWR, os.O_APPEND, os.O_CREATE, os.O_EXCL, os.O_TRUNC} {
|
|
if _, err := fsys.OpenFile(context.Background(), "victim.txt", flag, 0); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("write OpenFile flag %d: %v, want permission error", flag, err)
|
|
}
|
|
}
|
|
if err := fsys.Mkdir(context.Background(), "new-dir", 0o755); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("Mkdir: %v, want permission error", err)
|
|
}
|
|
if err := fsys.RemoveAll(context.Background(), "victim.txt"); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("RemoveAll: %v, want permission error", err)
|
|
}
|
|
if err := fsys.Rename(context.Background(), "victim.txt", "renamed.txt"); !errors.Is(err, os.ErrPermission) {
|
|
t.Fatalf("Rename: %v, want permission error", err)
|
|
}
|
|
if body, err := os.ReadFile(victim); err != nil || string(body) != "safe" {
|
|
t.Fatalf("write attempt changed file: %q, %v", body, err)
|
|
}
|
|
makeSharedTestLink(t, outside, filepath.Join(root, "escape"))
|
|
if err := os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("secret"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := fsys.OpenFile(context.Background(), "escape/secret.txt", os.O_RDONLY, 0); err == nil {
|
|
t.Fatal("directory symlink escape unexpectedly succeeded")
|
|
}
|
|
}
|