| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | const canaryToken = "sk-ant-oat01-canary-provider-7f3a9c" |
| 15 | |
| 16 | // tokenFixture is a token home: identity, the token file, no CLI login. |
| 17 | func tokenFixture(t *testing.T) Binding { |
| 18 | t.Helper() |
| 19 | root := t.TempDir() |
| 20 | home := filepath.Join(root, "claude-1") |
| 21 | if e := os.Mkdir(home, 0700); e != nil { |
| 22 | t.Fatal(e) |
| 23 | } |
| 24 | identity := Identity{AccountID: "token:claude-1", HomeID: "enrolled-home", ProviderVersion: ClaudeVersion} |
| 25 | writeMetadata(t, home, identity) |
| 26 | writeFake(t, filepath.Join(home, TokenFile), canaryToken) |
| 27 | return Binding{ID: "claude-1", Home: home, AccountID: identity.AccountID, Provider: "claude", Token: true, Expected: identity, RuntimeRoot: filepath.Join(root, "homes")} |
| 28 | } |
| 29 | |
| 30 | func openTokenCopy(t *testing.T, b Binding) *AccountLease { |
| 31 | t.Helper() |
| 32 | l, err := OpenRuntimeCopy(context.Background(), b, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }, time.Second) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | t.Cleanup(func() { l.Close() }) |
| 37 | return l |
| 38 | } |
| 39 | |
| 40 | // The runtime copy of a token home carries no credential: the token is |
| 41 | // in no file the session (and so the model) can reach, only in the |
| 42 | // launch's environment. |
| 43 | func TestTokenCopyHoldsNoToken(t *testing.T) { |
| 44 | b := tokenFixture(t) |
| 45 | l := openTokenCopy(t, b) |
| 46 | runtime := l.Binding().Home |
| 47 | err := filepath.WalkDir(filepath.Dir(runtime), func(path string, d fs.DirEntry, err error) error { |
| 48 | if err != nil || d.IsDir() { |
| 49 | return err |
| 50 | } |
| 51 | raw, err := os.ReadFile(path) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if bytes.Contains(raw, []byte(canaryToken)) { |
| 56 | t.Errorf("the token is in %s", path) |
| 57 | } |
| 58 | return nil |
| 59 | }) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if err = l.CheckCopy(context.Background()); err != nil { |
| 64 | t.Fatalf("token copy refused: %v", err) |
| 65 | } |
| 66 | env, err := LaunchSecretEnv(Launch{Binding: l.Binding()}) |
| 67 | if err != nil || env[OAuthTokenEnv] != canaryToken || env[ScrubEnv] != "1" || len(env) != 2 { |
| 68 | t.Fatalf("launch environment %v", err) |
| 69 | } |
| 70 | // A launch on a login home gets nothing. |
| 71 | plain := l.Binding() |
| 72 | plain.Token = false |
| 73 | if env, err = LaunchSecretEnv(Launch{Binding: plain}); err != nil || env != nil { |
| 74 | t.Fatal("a login home's launch got a secret environment") |
| 75 | } |
| 76 | if l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }) == nil { |
| 77 | t.Fatal("a token copy wrote back to its source") |
| 78 | } |
| 79 | l.Close() |
| 80 | if _, err = LaunchSecretEnv(Launch{Binding: l.Binding()}); err == nil { |
| 81 | t.Fatal("a closed lease still gave out its token") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // A credential appearing in a token copy is containment. |
| 86 | func TestTokenCopyRefusesCredentialFile(t *testing.T) { |
| 87 | for _, name := range []string{TokenFile, ".credentials.json"} { |
| 88 | t.Run(name, func(t *testing.T) { |
| 89 | l := openTokenCopy(t, tokenFixture(t)) |
| 90 | writeFake(t, filepath.Join(l.Binding().Home, name), "x") |
| 91 | if err := l.CheckCopy(context.Background()); !errors.Is(err, ErrContainment) { |
| 92 | t.Fatalf("credential in the copy accepted: %v", err) |
| 93 | } |
| 94 | }) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // A token home with a damaged token is not staged. |
| 99 | func TestTokenHomeRefusesBadToken(t *testing.T) { |
| 100 | b := tokenFixture(t) |
| 101 | writeFake(t, filepath.Join(b.Home, TokenFile), "two words") |
| 102 | if l, err := OpenRuntimeCopy(context.Background(), b, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }, time.Second); err == nil { |
| 103 | l.Close() |
| 104 | t.Fatal("bad token staged") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestClaudeTokenIdentity(t *testing.T) { |
| 109 | id, label, err := ClaudeTokenIdentity([]byte(`{"loggedIn":true,"authMethod":"oauth_token","email":"a@example.com"}`), "claude-2") |
| 110 | if err != nil || id.AccountID != "a@example.com" || label != "a@example.com" || id.ProviderVersion != ClaudeVersion { |
| 111 | t.Fatal(id, label, err) |
| 112 | } |
| 113 | id, label, err = ClaudeTokenIdentity([]byte(`{"loggedIn":true,"authMethod":"oauth_token"}`), "claude-2") |
| 114 | if err != nil || id.AccountID != "token:claude-2" || label != "" { |
| 115 | t.Fatal(id, label, err) |
| 116 | } |
| 117 | if _, _, err = ClaudeTokenIdentity([]byte(`{"loggedIn":false}`), "claude-2"); !errors.Is(err, ErrLoginRequired) { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // A containment refusal keeps its fixed category: the runner's log needs |
| 123 | // it to say what changed in a copy, and it is never a path. |
| 124 | func TestContainmentKeepsItsReason(t *testing.T) { |
| 125 | l := openTokenCopy(t, tokenFixture(t)) |
| 126 | // A signed-in CLI records the account in its native config. |
| 127 | writeFake(t, filepath.Join(l.Binding().Home, ".claude.json"), `{"oauthAccount":{"emailAddress":"a@example.com","accountUuid":"u1"}}`) |
| 128 | err := l.CheckCopy(context.Background()) |
| 129 | if !errors.Is(err, ErrContainment) { |
| 130 | t.Fatalf("changed account metadata accepted: %v", err) |
| 131 | } |
| 132 | if got := ContainmentReason(err); got != "runtime_claude_identity" { |
| 133 | t.Fatalf("reason %q", got) |
| 134 | } |
| 135 | if got := ContainmentReason(ErrPolicy); got != "" { |
| 136 | t.Fatalf("a policy refusal has containment reason %q", got) |
| 137 | } |
| 138 | } |
| 139 | |