| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/google/uuid" |
| 11 | ) |
| 12 | |
| 13 | func windowsBindingFixture(root string) WindowsProfile { |
| 14 | return WindowsProfile{Version: 1, VMID: uuid.MustParse("2a6f8208-3906-4e16-8233-2b9d28ecf7be"), GUIUserSID: "S-1-5-21-123-456-789-1001", GUIUser: "Guest", InstanceDirectory: filepath.Join(root, "vm"), RelayExecutable: filepath.Join(root, "agent.exe"), PairingFile: filepath.Join(root, "pairing.dpapi")} |
| 15 | } |
| 16 | |
| 17 | func TestWindowsBindingPreservesIdentityAndHasNoSecretArgument(t *testing.T) { |
| 18 | root := t.TempDir() |
| 19 | binding := windowsBindingFixture(root) |
| 20 | if err := BindWindowsProfile(root, binding); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | loaded, err := LoadWindowsProfile(root) |
| 24 | if err != nil || *loaded != binding { |
| 25 | t.Fatalf("binding changed: %v", err) |
| 26 | } |
| 27 | cmd, err := WindowsRelayCommand(context.Background(), root, "export", "--paths-stdin") |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if len(cmd.Args) != 5 || cmd.Args[1] != "--relay-profile" || cmd.Args[2] != WindowsProfilePath(root) || cmd.Args[3] != "export" || cmd.Args[4] != "--paths-stdin" { |
| 32 | t.Fatal("relay command did not preserve the profile boundary") |
| 33 | } |
| 34 | different := binding |
| 35 | different.GUIUserSID = "S-1-5-21-123-456-789-1002" |
| 36 | if err := BindWindowsProfile(root, different); err == nil { |
| 37 | t.Fatal("another GUI user replaced the binding") |
| 38 | } |
| 39 | if _, err := WindowsRelayCommand(context.Background(), root, "export", `["C:\\private"]`); err == nil { |
| 40 | t.Fatal("paths accepted in argv") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestWindowsBindingRejectsUnknownFieldsAndUnsafeIdentity(t *testing.T) { |
| 45 | for _, kind := range []string{"system", "empty-vm", "relative", "relative-network", "unknown", "trailing"} { |
| 46 | t.Run(kind, func(t *testing.T) { |
| 47 | root := t.TempDir() |
| 48 | binding := windowsBindingFixture(root) |
| 49 | switch kind { |
| 50 | case "system": |
| 51 | binding.GUIUserSID = "S-1-5-18" |
| 52 | case "empty-vm": |
| 53 | binding.VMID = uuid.Nil |
| 54 | case "relative": |
| 55 | binding.PairingFile = "relative" |
| 56 | case "relative-network": |
| 57 | binding.NetworkPairingFile = "packet.dpapi" |
| 58 | } |
| 59 | data, _ := json.Marshal(binding) |
| 60 | if kind == "unknown" { |
| 61 | data = append(data[:len(data)-1], []byte(`,"secret":"must not be accepted"}`)...) |
| 62 | } |
| 63 | if kind == "trailing" { |
| 64 | data = append(data, []byte(` {}`)...) |
| 65 | } |
| 66 | if err := os.WriteFile(WindowsProfilePath(root), data, 0600); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if _, err := LoadWindowsProfile(root); err == nil { |
| 70 | t.Fatal("unsafe binding accepted") |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestWindowsBindingKeepsPacketPairingOutOfArguments(t *testing.T) { |
| 77 | root := t.TempDir() |
| 78 | binding := windowsBindingFixture(root) |
| 79 | binding.NetworkPairingFile = filepath.Join(root, "packet.dpapi") |
| 80 | if err := BindWindowsProfile(root, binding); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | loaded, err := LoadWindowsProfile(root) |
| 84 | if err != nil || loaded.NetworkPairingFile != binding.NetworkPairingFile { |
| 85 | t.Fatal("packet pairing binding lost") |
| 86 | } |
| 87 | cmd, err := WindowsRelayCommand(context.Background(), root, "network-stream") |
| 88 | if err != nil || len(cmd.Args) != 4 || cmd.Args[3] != "network-stream" { |
| 89 | t.Fatal("packet transport bypassed the protected profile") |
| 90 | } |
| 91 | } |