Files
w3cjandClaude Opus 5.5 3101c85260 fix(vm): a failed agent import leaves the store as it was; the import's bounds count every entry and cap a file; a login's terminal is put back
devvm agent import wrote each settings file into the store as it read
it, so a file that failed to read partway (changed since it was listed,
or past the bounds) left the store half replaced. The import now reads
and writes every agent's settings into a staging folder of the agent's
store first (.devvm-import-<random>, beside the agent's folder, so no
VM mounts it), and only once all of them are staged, and the run isn't
interrupted, moves each file into place, prunes what the home folder no
longer has and then saves the logins. A failure or an interrupt while
staging leaves the store as it was: the staging folder goes, through
discard, or through a cleanup for an interrupted run, which reaches it
by a root of its own. A failure after that (a rename, a login) can
still leave the store partly replaced, which internals/agents.md says.
A killed import's leftover staging folder counts as nothing of the
agent's (inStore), so it never makes the import ask, and the next
import of that agent takes it out first (sweepStages).

The import's bounds on an agent's own folder now match devvm agent
save's: the walk counts every entry it meets, links, sockets and FIFOs
included, not only the folders and files it keeps, and a file of more
than 16 MiB, as listed or as read, fails the import as the 128 MiB
does. Claude Code's plugin records are copied with the store's longer
paths, and what the copy grows counts against the same bounds
(hostAgent.grew), naming the folder, or the link a file came through,
so the store never gets more than the import checked. The two handles
on an agent's folder, the os.Root it lists through and the folder
opened one folder at a time that it reads through, are checked to be
one folder (sameFolder), failing the import otherwise. A VM state the
import can't read is a warning (agent_import_states_unread), naming the
file, rather than silently dropping that VM from the project-inside
warnings and the link checks; the follower takes the VMs the import
read at its start instead of reading them again.

state.WriteFileIn and writeFileMode share one tail (finishTemp).
clearKind takes the root the import opened once, rather than opening
the store for every path, and pruneStore's walk is pruneIn, which the
import calls on the same root.

Cursor's changing verbs (proxy's cursorChanges), checked again against
the 1,463 methods of DashboardService, AutomationsService and
BackgroundComposerService in 2026.11.17-f532c45, read and not run.
generate goes: it matched only GenerateGrokCollectionDescription, which
only returns a draft, and GenerateTeamGrokBotScimIdpSigningKey, which
cursorCredential refuses anyway; no other method changes between
refused and passed. test stays, justified in its comment: it matches
TestAutomationFilter, a dry run that only reads, but also
TestTeamCustomerTelemetryDestinationConnection, which has Cursor's
servers send a request with any headers to any address. park, hand,
decline, replace and resend match only methods that cursorCredential
refuses too (as rotate, link and invite do), and stay, justified in
the comment, for the methods Cursor adds. Every cursorRoutes path the
CLI's login goes to is checked against the proxy by agentlogin's
TestCursorCapture (proxy can't import agentlogin, which imports it);
TestCursorChangingVerbs now says so.

devvm agent login on a terminal saves the terminal's state before the
login (ui.Terminal, through vm.Env.Terminal, which now returns a
restore) and puts it back once the login and endLogin are done, or in
a UI cleanup if the run ends first, so an ssh that a signal killed in
raw mode doesn't leave the host terminal raw.

Tests that fail without their fix: TestAgentImportFailsWhole (a failed
import leaves the store's own files and no staging folder),
TestAgentImportTooMuch (rewritten: its dead want field gone; exactly
20,000 entries and 128 MiB list whole, one more entry or byte fails, a
link and a FIFO count, a file of 16 MiB and a byte fails),
TestAgentImportPluginsGrow, TestSameFolder (the helper, and the import
with its folder swapped between the two opens),
TestAgentImportStageLeftover, TestAgentImportInterrupted (the ctx check
before apply, and the interrupt cleanup on its own),
TestAgentImportProjectInside (beside a
broken state), TestCursorChangingVerbs (GenerateGrokCollectionDescription
passes), TestAgentLoginTerminal (put back once, after the login ended,
interrupted included), TestTerminalRestores. Tests that pin what
already held: TestNarrowNotesBesideBroken (devvm network's -g
allowlists beside a VM state that can't be read), TestWriteFileIn (a
failed rename leaves no temporary file), and TestCursorChangingVerbs'
TestTeamCustomerTelemetryDestinationConnection.

The finder now checks fst with found, so nilaway no longer reports
fst's flow, and all of fst's lines in its baseline go.

Docs: using/coding-agents.md, internals/agents.md,
internals/network.md; CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-19 10:02:01 -07:00

246 lines
14 KiB
Go

package proxy
// Cursor's CLI: the proxy puts its login's token in for the RPCs the CLI calls on its API,
// api2.cursor.sh, its agent's hosts, api5.cursor.sh's, and its indexing's, repo42.cursor.sh
// (agentlogin's cursor format), which serve them, many of them streamed, beside its login's poll
// at api2's /auth/poll, the one request below /auth/ it captures. So on those hosts it refuses
// what would hand the VM a token or make a credential with the login (cursorMints): any other
// request below /auth/, on any of them, an RPC that makes a credential, and a path it can't read as
// Cursor's hosts do (oddPath). And it reads every reply it can without breaking a stream
// (cursorReply), refusing one that holds a token's shape.
import (
"errors"
"fmt"
"io"
"net/http"
"regexp"
"slices"
"strings"
)
// cursorAPI is Cursor's API's host, where its login polls.
const cursorAPI = "api2.cursor.sh"
// cursorHost reports whether host is one the proxy puts Cursor's login in for: its API's, its
// agent's or its indexing's.
func cursorHost(host string) bool {
return host == cursorAPI || host == "api5.cursor.sh" || strings.HasSuffix(host, ".api5.cursor.sh") || host == "repo42.cursor.sh"
}
// cursorMinting are the RPCs of Cursor's that make, show or exchange a credential with the login,
// by their paths, which every method of Connect's goes to, as the CLI's 2026.11.17-f532c45
// package defines them (read, not run): its CLI calls none of them, and the login's bindings
// (agentlogin's cursorRoutes) name none of them.
var cursorMinting = []string{
"/aiserver.v1.AiService/MintRealtimeVoiceSecret",
"/aiserver.v1.AutomationsService/GetSentryAuthUrl",
"/aiserver.v1.BackgroundComposerService/GetGithubAccessTokenForRepos",
"/aiserver.v1.BackgroundComposerService/RedeemSlackSignInLink",
"/aiserver.v1.BackgroundComposerService/CompleteSlackSignIn",
"/aiserver.v1.DashboardService/ApproveOAuthAuthorizationRequest",
"/aiserver.v1.DashboardService/CreateGrokSignInLink",
"/aiserver.v1.DashboardService/CreateSupportImpersonationSessionInternal",
"/aiserver.v1.DashboardService/CompleteMcpOAuth",
"/aiserver.v1.DashboardService/HandOffMcpOAuthCode",
}
// cursorCredential matches a method's name that says it makes, shows or hands over a credential, a
// sign-in or an invitation, which the proxy refuses on every Cursor host, as the package defines
// more of them than cursorMinting names (DashboardService's API keys, service tokens and OAuth
// applications, BackgroundComposerService's secrets and keyring) and Cursor can add more.
var cursorCredential = regexp.MustCompile(`(?i)(key|secret|token|oauth|signin|login|session|auth|cookie|password|jwt|credential|impersonat|scim|invite|link)`)
// cursorStore matches Cursor's agent store's service, aiserver.v1.AgentStoreService, and, as
// defence in depth, any service of any package or version whose name starts with AgentStore, in
// any case: every method of each the proxy refuses, as the store isn't the VM's alone. The account's user store is mounted read-write by its CLIs on every
// machine and by its cloud agents, which load the skills in it as instructions; a team's store by
// every member's; and the store can share a path with the team or the public. Its methods take a
// token of its own (MintAgentStoreToken's), which a VM could send out and use from elsewhere until
// it expires, for whatever Cursor lets that token do, so the VM gets neither the token nor the
// store. The CLI keeps its sessions in a local store.db without it.
var cursorStore = regexp.MustCompile(`(?i)^/[^/]*\.agentstore[^/]*service/`)
// cursorPersists are the methods of Cursor's that change the account in ways that outlast the VM
// and act outside it, as GitHub's keys and webhooks do (githubMinting), which the CLI calls but its
// login never goes to in a VM: plugins and marketplaces installed, updated or removed; cloud
// agents' environments set or deleted, and a cloud agent started; Bedrock keys checked, which the
// store doesn't keep. In the services where the account's own state is, DashboardService,
// AutomationsService and BackgroundComposerService, any method whose name starts with a verb that
// changes something is refused too (cursorChanges), as the package defines many more.
var cursorPersists = []string{
"/aiserver.v1.DashboardService/InstallUserPlugin",
"/aiserver.v1.DashboardService/UninstallUserPlugin",
"/aiserver.v1.DashboardService/UpdateUserPluginInstall",
"/aiserver.v1.DashboardService/RegisterMarketplaceAndPlugins",
"/aiserver.v1.DashboardService/RefreshMarketplace",
"/aiserver.v1.DashboardService/RemoveMarketplace",
"/aiserver.v1.AutomationsService/ApplyAutomationSpec",
"/aiserver.v1.AutomationsService/DeleteAutomation",
"/aiserver.v1.AutomationsService/UpdateAutomationAuthoringMode",
"/aiserver.v1.AutomationsService/TestAutomation",
"/aiserver.v1.BackgroundComposerService/StartBackgroundComposerFromSnapshot",
"/aiserver.v1.BackgroundComposerService/SetPersonalEnvironmentJson",
"/aiserver.v1.BackgroundComposerService/SetTeamEnvironmentJson",
"/aiserver.v1.BackgroundComposerService/DeletePersonalEnvironmentJson",
"/aiserver.v1.BackgroundComposerService/DeleteTeamEnvironment",
"/aiserver.v1.AiService/TestBedrockCredentials",
}
// cursorAccount are the services where the account's and its teams' own state is.
var cursorAccount = []string{"/aiserver.v1.DashboardService/", "/aiserver.v1.AutomationsService/", "/aiserver.v1.BackgroundComposerService/"}
// cursorChanges matches a method's name that starts with a verb that changes something. Each verb
// was checked against the release's 1,463 methods of cursorAccount's services: every method it
// matches changes something or acts outside the VM, and the CLI calls none of them. So verbs whose
// methods also read, such as Check, Batch, Resolve, Generate and Submit, which the CLI's
// SubmitFeedback starts with, aren't among them. One method that test matches only reads,
// TestAutomationFilter, a dry run of an automation's filter on a sample: test stays, for
// TestTeamCustomerTelemetryDestinationConnection, which has Cursor's servers send a request with
// any headers to any address. A verb that matches nothing in the release, such as launch or edit,
// is there for the methods Cursor adds, and so is one that matches only methods that
// cursorCredential refuses too, such as rotate, park or replace.
var cursorChanges = regexp.MustCompile(`(?i)^(apply|create|update|upsert|set|delete|remove|enable|disable|install|uninstall|register|add|invite|start|launch|trigger|run|test|pause|resume|rotate|revoke|refresh|toggle|archive|restore|move|rename|edit|modify|change|approve|accept|join|leave|transfer|grant|connect|disconnect|unlink|link|share|publish|import|sync|redeem|complete|schedule|cancel|stop|kill|send|post|put|patch|save|store|upload|attach|detach|assign|unassign|promote|demote|reset|clear|purge|migrate|claim|mark|clone|commit|mute|unmute|reindex|retry|merge|fork|write|pin|unpin|rerun|confirm|request|release|acquire|activate|deactivate|notify|make|open|ensure|park|copy|bulk|advance|abort|wake|snapshot|seed|reply|opt|hand|fail|use|skip|decide|unshare|undelete|unpublish|unprepare|unresolve|enroll|unenroll|upgrade|suspend|switch|duplicate|dismiss|deregister|deliver|decline|convert|close|append|execute|provision|mutate|inject|finish|finalize|reassign|reinstall|reject|renew|reorder|reparent|replace|resend|rmdir|report|record)`)
// cursorAccountChange reports whether p, a method's clean path, changes the account (cursorPersists,
// or a changing verb in cursorAccount's services).
func cursorAccountChange(p string) bool {
lower := strings.ToLower(p)
return slices.ContainsFunc(cursorPersists, func(m string) bool { return strings.EqualFold(p, m) }) ||
slices.ContainsFunc(cursorAccount, func(s string) bool { return strings.HasPrefix(lower, strings.ToLower(s)) }) &&
cursorChanges.MatchString(p[strings.LastIndex(p, "/")+1:])
}
// cursorMints is what a request to a Cursor host would get or make with the login the proxy puts
// in, or "": a login's tokens, below /auth/ but for the poll it captures (GET api2's
// /auth/poll), a credential, from one of cursorMinting's RPCs or any whose name says so
// (cursorCredential), or below /cloud-agent/, where cursor-agent worker gets its identity tokens
// (the CLI's interactive commands call nothing there), the agent store (cursorStore), a part of a reply, or anything at a path it
// can't read as the host does.
func cursorMints(host string, r *http.Request) string {
method, u := r.Method, r.URL
p := cleanPath(u.Path)
lower := strings.ToLower(p)
switch {
case oddPath(u):
return "a request at a path devvm can't read as Cursor's hosts do"
case r.Header.Get("Range") != "":
return "a part of a reply, which devvm can't read whole"
case strings.HasPrefix(lower+"/", "/auth/") && (host != cursorAPI || method != http.MethodGet || lower != "/auth/poll"):
return "a Cursor login's tokens"
case strings.HasPrefix(lower+"/", "/cloud-agent/"):
return "a Cursor cloud agent's credential"
case slices.ContainsFunc(cursorMinting, func(m string) bool { return strings.EqualFold(p, m) }),
cursorCredential.MatchString(p[strings.LastIndex(p, "/")+1:]):
return "a Cursor credential"
case cursorStore.MatchString(p):
return "Cursor's agent store, which other machines', cloud and team agents share"
case cursorAccountChange(p):
return "a change to your Cursor account that outlasts the VM (plugins, automations, cloud agents)"
}
return ""
}
// cursorUnencoded drops what of a request to a Cursor host asks for a compressed reply, as the
// proxy reads every reply's body as it passes (cursorReply): HTTP's Accept-Encoding, which Go's
// transport sets again for gzip alone and undoes, and Connect's and gRPC's for their messages, as
// every client of theirs takes identity.
func cursorUnencoded(h http.Header) {
for _, k := range []string{"Accept-Encoding", "Connect-Accept-Encoding", "Grpc-Accept-Encoding"} {
h.Del(k)
}
}
// cursorReply checks a reply from a Cursor host outside the guard of its login's poll, whatever its
// status, length or type, streams included: no part of a body (206, which the proxy's refusal of
// Range already rules out), no encoding the proxy can't read (its HTTP Content-Encoding, which Go's
// transport undoes for gzip, or its Connect or gRPC messages' but gzip), no token in a header, a
// redirect's Location included, and a body that the proxy reads as it passes, cut off at the first
// token in it: a Connect or gRPC stream message by message, each decompressed when it comes
// compressed (frameScanner), and any other body as its bytes come (jwtScanner).
func (s *server) cursorReply(resp *http.Response) error {
refuse := func(why string) error {
_ = resp.Body.Close()
return fmt.Errorf("%w: %s from a host of Cursor's", errCaptureRefused, why)
}
h := resp.Header
h.Del("Set-Cookie") // the CLI keeps no cookies, and a cookie could hold a session
kind, isJSON := streamKind(h.Get("Content-Type"))
connectEnc, grpcEnc := h.Get("Connect-Content-Encoding"), h.Get("Grpc-Encoding")
// A stream's messages may be gzip, in its own protocol's header alone.
gz := kind == "connect" && strings.EqualFold(connectEnc, "gzip") && identity(grpcEnc) ||
kind == "grpc" && strings.EqualFold(grpcEnc, "gzip") && identity(connectEnc)
switch {
case resp.StatusCode == http.StatusPartialContent:
return refuse("a part of a reply")
case encoded(resp) || kind == "unreadable" || !gz && (!identity(connectEnc) || !identity(grpcEnc)):
return refuse("an encoded reply")
case tokenIn(h):
return refuse("a token in a header")
}
if kind != "" {
resp.Body = &frameScanner{r: resp.Body, grpc: kind == "grpc", gzip: gz, json: isJSON, trailer: &resp.Trailer}
return nil
}
resp.Body = &jwtScanner{r: resp.Body, trailer: &resp.Trailer}
return nil
}
// tokenIn reports whether any of h's values, or names, holds a JWT's start (jwtStart).
func tokenIn(h http.Header) bool {
for k, vs := range h {
if jwtStart.MatchString(k) || slices.ContainsFunc(vs, jwtStart.MatchString) {
return true
}
}
return false
}
// identity reports whether a message encoding is none: unset, or identity.
func identity(enc string) bool { return enc == "" || strings.EqualFold(enc, "identity") }
// jwtStart is a JWT's start, a header and the start of its claims, which the scanner finds before
// any of the claims or the signature that make it a token pass: a base64url part of a JSON object
// ({" is eyJ), a dot, and another one's start.
var jwtStart = regexp.MustCompile(`eyJ[A-Za-z0-9_-]{8,}\.eyJ`)
// jwtWindow is how much of what a body has passed the scanner keeps, so a JWT's start split between
// two reads is found: more than any JWT header's.
const jwtWindow = 4 << 10
// jwtScanner is a reply's body that passes as it comes, without buffering, and fails at the read
// that would complete a JWT's start (jwtStart), passing none of that read: ReverseProxy then ends
// the reply there, so a token's claims and signature never reach the VM.
type jwtScanner struct {
r io.ReadCloser
trailer *http.Header // the reply's trailers, HTTP/2's and gRPC's, filled in once the body ends
tail []byte // the last jwtWindow bytes passed
err error
}
// Read fails at the read that completes a JWT's start, and at the body's end when its trailers hold
// one (tokenIn), so ReverseProxy, which copies them after the body, never sends them.
func (j *jwtScanner) Read(p []byte) (int, error) {
if j.err != nil {
return 0, j.err
}
n, err := j.r.Read(p)
if n > 0 {
j.tail = append(j.tail, p[:n]...)
if jwtStart.Match(j.tail) {
j.err = fmt.Errorf("%w: a token from a host of Cursor's", errCaptureRefused)
return 0, j.err
}
if over := len(j.tail) - jwtWindow; over > 0 {
j.tail = append(j.tail[:0], j.tail[over:]...)
}
}
if errors.Is(err, io.EOF) && j.trailer != nil && tokenIn(*j.trailer) {
j.err = fmt.Errorf("%w: a token in a trailer from a host of Cursor's", errCaptureRefused)
return n, j.err
}
return n, err
}
func (j *jwtScanner) Close() error { return j.r.Close() }