package provider import ( "bytes" "context" "errors" "io/fs" "os" "path/filepath" "testing" "time" ) const canaryToken = "sk-ant-oat01-canary-provider-7f3a9c" // tokenFixture is a token home: identity, the token file, no CLI login. func tokenFixture(t *testing.T) Binding { t.Helper() root := t.TempDir() home := filepath.Join(root, "claude-1") if e := os.Mkdir(home, 0700); e != nil { t.Fatal(e) } identity := Identity{AccountID: "token:claude-1", HomeID: "enrolled-home", ProviderVersion: ClaudeVersion} writeMetadata(t, home, identity) writeFake(t, filepath.Join(home, TokenFile), canaryToken) return Binding{ID: "claude-1", Home: home, AccountID: identity.AccountID, Provider: "claude", Token: true, Expected: identity, RuntimeRoot: filepath.Join(root, "homes")} } func openTokenCopy(t *testing.T, b Binding) *AccountLease { t.Helper() 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() }) return l } // The runtime copy of a token home carries no credential: the token is // in no file the session (and so the model) can reach, only in the // launch's environment. func TestTokenCopyHoldsNoToken(t *testing.T) { b := tokenFixture(t) l := openTokenCopy(t, b) runtime := l.Binding().Home err := filepath.WalkDir(filepath.Dir(runtime), func(path string, d fs.DirEntry, err error) error { if err != nil || d.IsDir() { return err } raw, err := os.ReadFile(path) if err != nil { return err } if bytes.Contains(raw, []byte(canaryToken)) { t.Errorf("the token is in %s", path) } return nil }) if err != nil { t.Fatal(err) } if err = l.CheckCopy(context.Background()); err != nil { t.Fatalf("token copy refused: %v", err) } env, err := LaunchSecretEnv(Launch{Binding: l.Binding()}) if err != nil || env[OAuthTokenEnv] != canaryToken || env[ScrubEnv] != "1" || len(env) != 2 { t.Fatalf("launch environment %v", err) } // A launch on a login home gets nothing. plain := l.Binding() plain.Token = false if env, err = LaunchSecretEnv(Launch{Binding: plain}); err != nil || env != nil { t.Fatal("a login home's launch got a secret environment") } if l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }) == nil { t.Fatal("a token copy wrote back to its source") } l.Close() if _, err = LaunchSecretEnv(Launch{Binding: l.Binding()}); err == nil { t.Fatal("a closed lease still gave out its token") } } // A credential appearing in a token copy is containment. func TestTokenCopyRefusesCredentialFile(t *testing.T) { for _, name := range []string{TokenFile, ".credentials.json"} { t.Run(name, func(t *testing.T) { l := openTokenCopy(t, tokenFixture(t)) writeFake(t, filepath.Join(l.Binding().Home, name), "x") if err := l.CheckCopy(context.Background()); !errors.Is(err, ErrContainment) { t.Fatalf("credential in the copy accepted: %v", err) } }) } } // A token home with a damaged token is not staged. func TestTokenHomeRefusesBadToken(t *testing.T) { b := tokenFixture(t) writeFake(t, filepath.Join(b.Home, TokenFile), "two words") if l, err := OpenRuntimeCopy(context.Background(), b, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }, time.Second); err == nil { l.Close() t.Fatal("bad token staged") } } func TestClaudeTokenIdentity(t *testing.T) { id, label, err := ClaudeTokenIdentity([]byte(`{"loggedIn":true,"authMethod":"oauth_token","email":"a@example.com"}`), "claude-2") if err != nil || id.AccountID != "a@example.com" || label != "a@example.com" || id.ProviderVersion != ClaudeVersion { t.Fatal(id, label, err) } id, label, err = ClaudeTokenIdentity([]byte(`{"loggedIn":true,"authMethod":"oauth_token"}`), "claude-2") if err != nil || id.AccountID != "token:claude-2" || label != "" { t.Fatal(id, label, err) } if _, _, err = ClaudeTokenIdentity([]byte(`{"loggedIn":false}`), "claude-2"); !errors.Is(err, ErrLoginRequired) { t.Fatal(err) } } // A containment refusal keeps its fixed category: the runner's log needs // it to say what changed in a copy, and it is never a path. func TestContainmentKeepsItsReason(t *testing.T) { l := openTokenCopy(t, tokenFixture(t)) // A signed-in CLI records the account in its native config. writeFake(t, filepath.Join(l.Binding().Home, ".claude.json"), `{"oauthAccount":{"emailAddress":"a@example.com","accountUuid":"u1"}}`) err := l.CheckCopy(context.Background()) if !errors.Is(err, ErrContainment) { t.Fatalf("changed account metadata accepted: %v", err) } if got := ContainmentReason(err); got != "runtime_claude_identity" { t.Fatalf("reason %q", got) } if got := ContainmentReason(ErrPolicy); got != "" { t.Fatalf("a policy refusal has containment reason %q", got) } }