package provider import ( "context" "errors" "os" "path/filepath" "strings" "testing" "time" "golang.org/x/sys/unix" ) const syncedAtFixture = "1795305600000" // writeRemoteSettings writes the files 2.1.312 leaves after an empty // managed-settings sync: the object, then the time of the sync. func writeRemoteSettings(t *testing.T, home, settings string, syncedAt bool) { t.Helper() writeFake(t, filepath.Join(home, "remote-settings.json"), settings) if syncedAt { writeFake(t, filepath.Join(home, "remote-settings.json.synced-at"), syncedAtFixture) } } func TestClaudeEmptyRemoteSettingsAccepted(t *testing.T) { for _, tc := range []struct { name, settings string syncedAt bool }{ {"object", "{}", false}, {"object+synced-at", "{}", true}, {"newline", "{}\n", true}, {"surrounding", " \t\r\n{}\r\n \t", true}, {"at-cap", "{}" + strings.Repeat(" ", 62), true}, } { t.Run(tc.name, func(t *testing.T) { l := claudeRuntimeFixture(t) writeRemoteSettings(t, l.runtime, tc.settings, tc.syncedAt) if err := l.validateRuntimeEntries(); err != nil { t.Fatal(err) } }) } } func TestClaudePolicyStampCap(t *testing.T) { for _, size := range []int{225, 4096, 4097} { l := claudeRuntimeFixture(t) writeFake(t, filepath.Join(l.runtime, "policy-limits.json.stamp.json"), strings.Repeat("x", size)) err := l.validateRuntimeEntries() if size <= 4096 && err != nil { t.Fatalf("%d bytes: %v", size, err) } if size > 4096 && ContainmentReason(err) != "runtime_claude_policy_limits" { t.Fatalf("%d bytes: %v", size, err) } } } // Anything but the empty object, as anything but a private regular file, // or from another CLI version stays refused, and the refusal names it. func TestClaudeRemoteSettingsOtherwiseRefused(t *testing.T) { const settings, synced = "remote-settings.json", "remote-settings.json.synced-at" for _, tc := range []struct { name, target, kind, content string syncedAt bool want string }{ {"permissions", settings, "file", `{"permissions":{}}`, true, "remote_settings"}, {"array", settings, "file", "[]", false, "remote_settings"}, {"null", settings, "file", "null", false, "remote_settings"}, {"empty-file", settings, "file", "", false, "remote_settings"}, {"inner-space", settings, "file", "{ }", false, "remote_settings"}, {"nul", settings, "file", "{}\x00", false, "remote_settings"}, {"oversize", settings, "file", "{}" + strings.Repeat(" ", 63), false, "remote_settings"}, {"directory", settings, "directory", "", false, "remote_settings"}, {"symlink", settings, "symlink", "", false, "remote_settings"}, {"hardlink", settings, "hardlink", "", false, "remote_settings"}, {"fifo", settings, "fifo", "", false, "remote_settings"}, {"mode", settings, "mode", "", false, "remote_settings"}, {"setuid", settings, "setuid", "", false, "remote_settings"}, {"version", settings, "version", "", true, "remote_settings"}, {"synced-alone", synced, "alone", "", false, "remote_settings_synced_at"}, {"synced-oversize", synced, "file", strings.Repeat("1", 33), false, "remote_settings_synced_at"}, {"synced-directory", synced, "directory", "", false, "remote_settings_synced_at"}, {"synced-symlink", synced, "symlink", "", false, "remote_settings_synced_at"}, {"synced-hardlink", synced, "hardlink", "", false, "remote_settings_synced_at"}, {"synced-fifo", synced, "fifo", "", false, "remote_settings_synced_at"}, {"synced-mode", synced, "mode", "", false, "remote_settings_synced_at"}, {"synced-setgid", synced, "setgid", "", false, "remote_settings_synced_at"}, {"synced-nested", "projects/" + synced, "file", syncedAtFixture, false, "remote_settings_synced_at"}, } { t.Run(tc.name, func(t *testing.T) { l := claudeRuntimeFixture(t) must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } if tc.target == synced || strings.HasSuffix(tc.target, "/"+synced) { if tc.kind != "alone" { // A nested copy is refused even beside an accepted root pair. writeRemoteSettings(t, l.runtime, "{}", tc.target != synced) } } else if tc.syncedAt { writeFake(t, filepath.Join(l.runtime, synced), syncedAtFixture) } path := filepath.Join(l.runtime, tc.target) must(os.MkdirAll(filepath.Dir(path), 0700)) valid := "{}" if tc.target != settings { valid = syncedAtFixture } switch tc.kind { case "file": writeFake(t, path, tc.content) case "directory": must(os.Mkdir(path, 0700)) case "symlink": target := filepath.Join(t.TempDir(), "state") writeFake(t, target, valid) must(os.Symlink(target, path)) case "hardlink": target := filepath.Join(t.TempDir(), "state") writeFake(t, target, valid) must(os.Link(target, path)) case "fifo": must(unix.Mkfifo(path, 0600)) case "mode": writeFake(t, path, valid) must(os.Chmod(path, 0644)) case "setuid": writeFake(t, path, valid) must(os.Chmod(path, 0600|os.ModeSetuid)) case "setgid": writeFake(t, path, valid) must(os.Chmod(path, 0600|os.ModeSetgid)) case "version": writeFake(t, path, valid) l.source.Expected.ProviderVersion = "other" case "alone": writeFake(t, path, valid) } err := l.validateRuntimeEntries() if !errors.Is(err, ErrContainment) || ContainmentReason(err) != "runtime_claude_"+tc.want { t.Fatalf("want runtime_claude_%s: %v", tc.want, err) } }) } } // writeManagedTurn writes what 2.1.312 left in a runtime home during a // first turn on an org-managed account (production 2026-11-22, top-level // names and sizes captured), less the token-only scrub stubs. func writeManagedTurn(t *testing.T, home string) { t.Helper() for _, dir := range []string{"projects", "backups", "dev-mods", "seed-admin", "sessions", ".claude", ".config", ".npm", ".cache"} { if err := os.MkdirAll(filepath.Join(home, dir), 0700); err != nil { t.Fatal(err) } } for name, size := range map[string]int{"policy-limits.json": 409, "policy-limits.json.signature.json": 3006, "policy-limits.json.stamp.json": 225} { writeFake(t, filepath.Join(home, name), strings.Repeat("x", size)) } writeRemoteSettings(t, home, "{}", true) } // Production 2026-11-22: a warm coordinator on an org-managed account // was refused before its second turn, runtime_claude_unexpected_entry: // unlisted. This is the top-level set captured from that runtime home. func TestTokenCopySecondTurnManagedAccount(t *testing.T) { l := openTokenCopy(t, tokenFixture(t)) ctx := context.Background() if err := l.CheckCopy(ctx); err != nil { t.Fatalf("first turn: %v", err) } home := l.Binding().Home scrubStubs(t, home, 0600, 0700) writeManagedTurn(t, home) if err := l.CheckCopy(ctx); err != nil { t.Fatalf("second turn: %v", err) } } // The same second turn on a subscription copy: the CLI's own credential // file is staged and no scrub stubs are written (those come only with a // token launch), and a stub there stays refused. func TestSubscriptionCopySecondTurnManagedAccount(t *testing.T) { b, _ := accountFixture(t) b.Provider = "claude" b.Expected.ProviderVersion = ClaudeVersion writeMetadata(t, b.Home, b.Expected) writeFake(t, filepath.Join(b.Home, credentialName("claude")), `{}`) l, err := OpenRuntimeCopy(context.Background(), b, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }, time.Second) if err != nil { t.Fatal(err) } t.Cleanup(func() { l.Close() }) ctx := context.Background() if err = l.CheckCopy(ctx); err != nil { t.Fatalf("first turn: %v", err) } home := l.Binding().Home if _, err = os.Lstat(filepath.Join(home, credentialName("claude"))); err != nil { t.Fatalf("subscription copy has no credential file: %v", err) } writeManagedTurn(t, home) if err = l.CheckCopy(ctx); err != nil { t.Fatalf("second turn: %v", err) } writeFake(t, filepath.Join(home, ".bashrc"), "") if err = l.CheckCopy(ctx); ContainmentReason(err) != "runtime_claude_unexpected_entry:.bashrc" { t.Fatalf("scrub stub on a subscription copy: %v", err) } } // The account's own home never feeds these into the staged one. func TestClaudeRemoteSettingsNotStaged(t *testing.T) { l := claudeRuntimeFixture(t) names := []string{"policy-limits.json.stamp.json", "remote-settings.json", "remote-settings.json.synced-at"} for _, name := range names { writeFake(t, filepath.Join(l.source.Home, name), "{}") } preflightFixture(t, l) for _, name := range names { if _, err := os.Lstat(filepath.Join(l.runtime, name)); !os.IsNotExist(err) { t.Fatalf("source state staged: %s: %v", name, err) } } }