| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "syscall" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestSessionAuthorizationsPreservesExternalFolderBindingsWithoutAliasing(t *testing.T) { |
| 12 | workspace := t.TempDir() |
| 13 | original := newOwnedTestController(t, Options{WorkspaceRoot: workspace}) |
| 14 | root := externalMigrationRoot(t) |
| 15 | token, display, err := original.RegisterExternalFolderRef(root) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | snapshot := original.SessionAuthorizations() |
| 20 | |
| 21 | laterToken, _, err := original.RegisterExternalFolderRef(externalMigrationRoot(t)) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | registrar := &recordingExternalFolderToolRefs{} |
| 26 | replacement := newOwnedTestController(t, Options{WorkspaceRoot: workspace, ExternalFolderToolRefs: registrar}) |
| 27 | approvalMode := replacement.ToolApprovalMode() |
| 28 | replacement.RestoreSessionAuthorizations(snapshot) |
| 29 | assertMigratedExternalRoot(t, replacement, token, display) |
| 30 | if registrar.token != token || filepath.ToSlash(registrar.root) != display { |
| 31 | t.Fatalf("read-tool registration = (%q, %q), want (%q, %q)", registrar.token, registrar.root, token, display) |
| 32 | } |
| 33 | if _, _, ok := replacement.ExternalFolderRefLocalPath(laterToken); ok { |
| 34 | t.Fatal("source registration after snapshot leaked into replacement") |
| 35 | } |
| 36 | |
| 37 | replacementOnly, _, err := replacement.RegisterExternalFolderRef(externalMigrationRoot(t)) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if _, _, ok := original.ExternalFolderRefLocalPath(replacementOnly); ok { |
| 42 | t.Fatal("replacement registration leaked back into source") |
| 43 | } |
| 44 | another := newOwnedTestController(t, Options{WorkspaceRoot: workspace}) |
| 45 | another.RestoreSessionAuthorizations(snapshot) |
| 46 | assertMigratedExternalRoot(t, another, token, display) |
| 47 | for _, unowned := range []string{laterToken, replacementOnly} { |
| 48 | if _, _, ok := another.ExternalFolderRefLocalPath(unowned); ok { |
| 49 | t.Fatalf("authorization snapshot aliased a controller's later registration %q", unowned) |
| 50 | } |
| 51 | } |
| 52 | for _, ctrl := range []*Controller{original, replacement, another} { |
| 53 | auth := ctrl.SessionAuthorizations() |
| 54 | if len(auth.Grants) != 0 || len(auth.PlanModeReadOnlyCommands) != 0 || len(auth.WriteRoots) != 0 { |
| 55 | t.Fatal("external-folder read authorization widened tool or write grants") |
| 56 | } |
| 57 | } |
| 58 | if replacement.ToolApprovalMode() != approvalMode { |
| 59 | t.Fatal("external-folder restore changed the permission preset") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestRestoreSessionAuthorizationsKeepsExistingExternalFolderBindings(t *testing.T) { |
| 64 | workspace := t.TempDir() |
| 65 | original := newOwnedTestController(t, Options{WorkspaceRoot: workspace}) |
| 66 | oldToken, oldDisplay, err := original.RegisterExternalFolderRef(externalMigrationRoot(t)) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | replacement := newOwnedTestController(t, Options{WorkspaceRoot: workspace}) |
| 71 | newToken, newDisplay, err := replacement.RegisterExternalFolderRef(externalMigrationRoot(t)) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | replacement.RestoreSessionAuthorizations(original.SessionAuthorizations()) |
| 76 | replacement.RestoreSessionAuthorizations(SessionAuthorizations{}) |
| 77 | assertMigratedExternalRoot(t, replacement, oldToken, oldDisplay) |
| 78 | assertMigratedExternalRoot(t, replacement, newToken, newDisplay) |
| 79 | } |
| 80 | |
| 81 | func TestRestoreExternalFolderBindingsKeepsOriginalSymlinkTarget(t *testing.T) { |
| 82 | workspace := t.TempDir() |
| 83 | rootA, rootB := externalMigrationRoot(t), externalMigrationRoot(t) |
| 84 | alias := filepath.Join(t.TempDir(), "dropped-link") |
| 85 | if err := os.Symlink(rootA, alias); err != nil { |
| 86 | if errors.Is(err, os.ErrPermission) || errors.Is(err, syscall.ENOSYS) || errors.Is(err, syscall.ENOTSUP) { |
| 87 | t.Skipf("host does not permit symlinks: %v", err) |
| 88 | } |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | canonicalA, err := filepath.EvalSymlinks(rootA) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | canonicalB, err := filepath.EvalSymlinks(rootB) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | original := newOwnedTestController(t, Options{WorkspaceRoot: workspace}) |
| 100 | token, display, err := original.RegisterExternalFolderRef(alias) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if display != filepath.ToSlash(canonicalA) { |
| 105 | t.Fatalf("registered root = %q, want original canonical target %q", display, canonicalA) |
| 106 | } |
| 107 | snapshot := original.SessionAuthorizations() |
| 108 | if err := os.Remove(alias); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if err := os.Symlink(rootB, alias); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | registrar := &recordingExternalFolderToolRefs{} |
| 115 | replacement := newOwnedTestController(t, Options{WorkspaceRoot: workspace, ExternalFolderToolRefs: registrar}) |
| 116 | replacement.RestoreSessionAuthorizations(snapshot) |
| 117 | assertMigratedExternalRoot(t, replacement, token, display) |
| 118 | if registrar.token != token || registrar.root != canonicalA { |
| 119 | t.Fatalf("restored read-tool registration = (%q, %q), want (%q, %q)", registrar.token, registrar.root, token, canonicalA) |
| 120 | } |
| 121 | if _, _, ok := replacement.ExternalFolderRefLocalPath(externalFolderRefToken(canonicalB)); ok { |
| 122 | t.Fatal("retargeting the dropped alias authorized the new target") |
| 123 | } |
| 124 | if refs := replacement.SessionAuthorizations().externalFolderRefs; len(refs) != 1 || refs[token] != canonicalA { |
| 125 | t.Fatalf("restored folder grants = %v, want only the original canonical root", refs) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func externalMigrationRoot(t *testing.T) string { |
| 130 | t.Helper() |
| 131 | root := filepath.Join(t.TempDir(), "Dropped Folder") |
| 132 | if err := os.Mkdir(root, 0o700); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | return root |
| 136 | } |
| 137 | |
| 138 | func assertMigratedExternalRoot(t *testing.T, ctrl *Controller, token, display string) { |
| 139 | t.Helper() |
| 140 | path, gotDisplay, ok := ctrl.ExternalFolderRefLocalPath(token) |
| 141 | if !ok || filepath.ToSlash(path) != display || gotDisplay != display { |
| 142 | t.Fatalf("original token %q resolves to (%q, %q, %v), want %q", token, path, gotDisplay, ok, display) |
| 143 | } |
| 144 | if _, _, ok := ctrl.ExternalFolderRefLocalPath(token + "/../outside.txt"); ok { |
| 145 | t.Fatal("restored reference permits traversal outside its registered root") |
| 146 | } |
| 147 | } |
| 148 |