| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "reflect" |
| 7 | "sort" |
| 8 | |
| 9 | "golang.org/x/sys/unix" |
| 10 | ) |
| 11 | |
| 12 | // Claude's sign-in with no terminal (Paul, 2026-11-10): the unmodified |
| 13 | // `claude setup-token` runs contained like every provider launch, in a |
| 14 | // bubblewrap namespace whose only writable home is the launch's own |
| 15 | // scratch home (the connection's dedicated home is not mounted: the |
| 16 | // runner writes the token there itself), with no workspace, no tool |
| 17 | // socket, no other home and no runner state, and egress only through the |
| 18 | // relay to SignInHosts. Its policy is built here like a check's: no |
| 19 | // account, no binding, a pinned binary. |
| 20 | |
| 21 | const signInPolicyVersion = "signin-v1" |
| 22 | |
| 23 | // SignInHosts are the hosts setup-token reaches, read from the pinned |
| 24 | // CLI 2.1.312 statically (its production OAuth config): TOKEN_URL |
| 25 | // https://platform.claude.com/v1/oauth/token, where the code is |
| 26 | // exchanged for the token, and BASE_API_URL https://api.anthropic.com. |
| 27 | // The authorize page (claude.com) is opened by the person's browser, |
| 28 | // never by the CLI. Sorted. |
| 29 | var SignInHosts = []string{"api.anthropic.com", "platform.claude.com"} |
| 30 | |
| 31 | // SignInBrowser stands in for a browser inside the namespace: setup-token |
| 32 | // runs it with the URL and it opens nothing. |
| 33 | const SignInBrowser = "/usr/bin/true" |
| 34 | |
| 35 | // SignInArgv is the CLI's argv inside the namespace. |
| 36 | var SignInArgv = []string{"/opt/claude", "setup-token"} |
| 37 | |
| 38 | // SignInPolicyInputs are the pinned CLI and the host's bubblewrap. |
| 39 | type SignInPolicyInputs struct { |
| 40 | Binary, Bwrap string |
| 41 | } |
| 42 | |
| 43 | // signInEnv is the CLI's environment: its config in the scratch home, |
| 44 | // no nonessential traffic, no updater, a terminal, no browser. |
| 45 | func signInEnv() map[string]string { |
| 46 | return map[string]string{ |
| 47 | "CLAUDE_CONFIG_DIR": "/home/runner", |
| 48 | "CLAUDE_SECURESTORAGE_CONFIG_DIR": "/home/runner", |
| 49 | "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1", |
| 50 | "DISABLE_AUTOUPDATER": "1", |
| 51 | "TERM": "xterm-256color", |
| 52 | "BROWSER": SignInBrowser, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // BuildSignInPolicy pins the CLI (mounted read-only at /opt/claude) and |
| 57 | // the host's bubblewrap (certified, not mounted). |
| 58 | func BuildSignInPolicy(in SignInPolicyInputs) (Policy, error) { |
| 59 | m := effectiveManifest{Version: signInPolicyVersion, Provider: "claude", Argv: append([]string(nil), SignInArgv...), Env: signInEnv(), NamespaceArgs: NamespaceArguments(), EgressHosts: append([]string(nil), SignInHosts...)} |
| 60 | sort.Strings(m.EgressHosts) |
| 61 | cli := Mount{Source: in.Binary, Target: "/opt/claude", ReadOnly: true} |
| 62 | m.OuterMounts = []Mount{cli} |
| 63 | for _, mount := range []Mount{cli, {Source: in.Bwrap, Target: "/opt/bwrap", ReadOnly: true}} { |
| 64 | fd, err := openNoLinks(mount.Source, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK) |
| 65 | if err != nil { |
| 66 | return Policy{}, ErrPolicy |
| 67 | } |
| 68 | f := os.NewFile(uintptr(fd), mount.Source) |
| 69 | info, err := f.Stat() |
| 70 | f.Close() |
| 71 | if err != nil || !info.Mode().IsRegular() { |
| 72 | return Policy{}, ErrPolicy |
| 73 | } |
| 74 | raw, err := os.ReadFile(mount.Source) |
| 75 | if err != nil { |
| 76 | return Policy{}, err |
| 77 | } |
| 78 | m.Files = append(m.Files, fileDigest{Mount: mount, SHA256: digest(raw)}) |
| 79 | } |
| 80 | raw, hash, err := hashJSON(m) |
| 81 | if err != nil { |
| 82 | return Policy{}, err |
| 83 | } |
| 84 | return Policy{Hash: hash, ManifestHash: hash, Manifest: raw, Mounts: m.OuterMounts, Env: signInEnv()}, nil |
| 85 | } |
| 86 | |
| 87 | // IsSignIn says the policy is a sign-in's (BuildSignInPolicy). |
| 88 | func (p Policy) IsSignIn() bool { |
| 89 | var m effectiveManifest |
| 90 | return p.ManifestHash != "" && digest(p.Manifest) == p.ManifestHash && json.Unmarshal(p.Manifest, &m) == nil && m.Version == signInPolicyVersion |
| 91 | } |
| 92 | |
| 93 | // attestSignIn binds a sign-in launch to its policy: Claude and nothing |
| 94 | // else selected, no binding, workspace, tool socket or session, the |
| 95 | // policy's own argv, environment and mounts, the pinned files unchanged. |
| 96 | func attestSignIn(l Launch, argv []string) (string, error) { |
| 97 | p := l.Policy |
| 98 | var m effectiveManifest |
| 99 | if l.Selection != (Selection{Provider: "claude"}) || l.Binding != (Binding{}) || l.Workspace != "" || l.ToolSocket != "" || l.ResumeSessionID != "" || l.AttemptKey == "" || json.Unmarshal(p.Manifest, &m) != nil || digest(p.Manifest) != p.ManifestHash || p.Hash != p.ManifestHash || !reflect.DeepEqual(argv, m.Argv) || !reflect.DeepEqual(argv, SignInArgv) || !reflect.DeepEqual(p.Env, m.Env) || !reflect.DeepEqual(m.Env, signInEnv()) || !reflect.DeepEqual(p.Mounts, m.OuterMounts) || !reflect.DeepEqual(m.NamespaceArgs, NamespaceArguments()) || !reflect.DeepEqual(m.EgressHosts, SignInHosts) { |
| 100 | return "", ErrPolicy |
| 101 | } |
| 102 | for _, f := range m.Files { |
| 103 | raw, err := os.ReadFile(f.Mount.Source) |
| 104 | if err != nil || digest(raw) != f.SHA256 { |
| 105 | return "", ErrPolicy |
| 106 | } |
| 107 | } |
| 108 | _, hash, err := hashJSON(struct{ Manifest, Attempt string }{p.ManifestHash, l.AttemptKey}) |
| 109 | return hash, err |
| 110 | } |
| 111 | |