mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
CJ's M3 run with a real login: cursor-agent couldn't chat; it kept
reconnecting to agentn.global.api5.cursor.sh, and the VM's proxy logged
AgentService/Run as refused ("devvm couldn't read the login's token
request or reply"). The CLI's agent client compresses its requests
with gzip (connect-node's sendCompression, read from the 2026.11.11
package, for messages of 1 KiB and more), and a Connect server answers
a gzipped request with gzipped messages whatever the client accepts
(connect-go's negotiateCompression mirrors the request's; the spec has
the server assume the client takes the request's encoding). cursorReply
refused any reply with a Connect-Content-Encoding or Grpc-Encoding but
identity, so it refused Run's reply at its headers.
- frameScanner: a reply whose Content-Type is a Connect or gRPC stream
passes message by message. Each is read whole, at most 16 MiB as it
comes and as it decompresses, gunzipped when flagged compressed, and
checked for a JWT's start before any of it passes, as it came.
Connect's end of stream, and every message of a JSON stream, is also
decoded and its strings checked, so escapes can't hide a token. An
unknown flag, a compressed message without gzip or that isn't gzip,
one over the limit, an envelope cut off, a token in the trailers, and
gRPC-Web's base64 text end the stream before that message. Other
replies keep jwtScanner; requests carry placeholders and pass as
before.
- cursorReply takes gzip only as a stream's own message encoding, and
refuses any other encoding as before.
- TestCursorStreams: a recorded-shape Run stream (gzipped messages, a
plain one, an end of stream), which failed first ("an encoded reply");
gRPC; each refusal; a token compressed, plain, at the end and escaped
there; JSON streams. TestCursorReplyScans now uses a unary reply.
- e2e network: the fake company proxy's api2.cursor.sh answers Run as a
Connect stream with gzipped messages, which passes whole through the
VM's proxy, and one with a token, which ends before it.
- A focused threat review (security-audit skill): no way found for a
token to reach the VM that the old refusal stopped; its memory and
JSON-codec points are taken (no allocation from a declared length,
JSON streams decoded, grpc-web-text refused). Base64 inside messages
stays unread, as before, in internals' network page.
api3.cursor.sh, also blocked in that run, is the CLI's metrics host
(MetricsService, with a fixed key of the CLI's own, fire and forget),
which the allowlist leaves out on purpose; the CLI reaches it with
http2.connect, around HTTPS_PROXY, hence "without proxy settings".
Co-Authored-By: Claude Opus 5.5 <[email protected]>
173 lines
5.7 KiB
Go
173 lines
5.7 KiB
Go
package proxy
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// A Connect or gRPC stream from a host of Cursor's is a run of messages, each in an envelope: a
|
|
// flags byte, a 4-byte big-endian length and the message, which the flags' lowest bit says is
|
|
// compressed with the reply's message encoding (Connect-Content-Encoding, or gRPC's
|
|
// Grpc-Encoding). Connect's last message, its end of stream (flag 0x02), is JSON with the
|
|
// stream's error and trailers. Cursor's agent streams its replies this way, and its messages may
|
|
// come compressed, gzip being the one encoding Connect's and gRPC's clients all take, so the
|
|
// proxy reads each message whole, as it would reach the VM, decompressed, before passing it on.
|
|
const (
|
|
frameCompressed = 0x01
|
|
frameEnd = 0x02
|
|
// frameMax is the most a message holds, compressed or as it decompresses: one message at a
|
|
// time, so this bounds what the proxy keeps of a stream.
|
|
frameMax = 16 << 20
|
|
)
|
|
|
|
// streamKind is what a reply's Content-Type says its body is: Connect's streaming or gRPC's
|
|
// enveloped messages, or neither (""), and whether those messages are JSON. gRPC-Web's text
|
|
// variant, a whole body in base64 that no scanner reads, is "unreadable".
|
|
func streamKind(contentType string) (kind string, isJSON bool) {
|
|
mt, _, err := mime.ParseMediaType(contentType)
|
|
switch {
|
|
case err != nil:
|
|
return "", false
|
|
case strings.HasPrefix(mt, "application/grpc-web-text"):
|
|
return "unreadable", false
|
|
case strings.HasPrefix(mt, "application/connect+"):
|
|
return "connect", mt == "application/connect+json"
|
|
case mt == "application/grpc" || strings.HasPrefix(mt, "application/grpc+"):
|
|
return "grpc", mt == "application/grpc+json"
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// frameScanner is a stream's body that passes message by message, each read whole (at most
|
|
// frameMax), decompressed when its flag says so, and checked for a JWT's start (jwtStart) before
|
|
// any of it passes, as it came; a message holding one, one the proxy can't read (a flag it doesn't
|
|
// know, a compressed one without gzip, one over frameMax, a cut-off envelope) and trailers holding
|
|
// one (tokenIn) end the stream there, so ReverseProxy passes nothing more.
|
|
type frameScanner struct {
|
|
r io.ReadCloser
|
|
grpc bool // gRPC's envelopes, whose flags have no end of stream
|
|
gzip bool // the reply's messages that are compressed are gzip
|
|
json bool // every message is JSON, checked as it decodes
|
|
trailer *http.Header // the reply's trailers, filled in once the body ends
|
|
out []byte // what of the message read last hasn't passed yet
|
|
err error
|
|
}
|
|
|
|
func (f *frameScanner) refuse(why string) error {
|
|
f.err = fmt.Errorf("%w: %s in a stream from a host of Cursor's", errCaptureRefused, why)
|
|
return f.err
|
|
}
|
|
|
|
// Read passes what's left of the message read last, and when that's gone reads the next one
|
|
// (next).
|
|
func (f *frameScanner) Read(p []byte) (int, error) {
|
|
if f.err != nil {
|
|
return 0, f.err
|
|
}
|
|
if len(f.out) == 0 {
|
|
if err := f.next(); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
n := copy(p, f.out)
|
|
f.out = f.out[n:]
|
|
return n, nil
|
|
}
|
|
|
|
// next reads the next message and checks it, ending the stream at its end or at a refusal.
|
|
func (f *frameScanner) next() error {
|
|
var head [5]byte
|
|
if _, err := io.ReadFull(f.r, head[:]); err != nil {
|
|
switch {
|
|
case errors.Is(err, io.EOF) && f.trailer != nil && tokenIn(*f.trailer):
|
|
return f.refuse("a token in a trailer")
|
|
case errors.Is(err, io.EOF):
|
|
f.err = io.EOF
|
|
return io.EOF
|
|
case errors.Is(err, io.ErrUnexpectedEOF):
|
|
return f.refuse("an envelope cut off")
|
|
}
|
|
f.err = err
|
|
return err
|
|
}
|
|
flags, size := head[0], binary.BigEndian.Uint32(head[1:])
|
|
known := frameCompressed
|
|
if !f.grpc {
|
|
known |= frameEnd
|
|
}
|
|
switch {
|
|
case flags&^byte(known) != 0:
|
|
return f.refuse(fmt.Sprintf("a message flagged %#x", flags))
|
|
case size > frameMax:
|
|
return f.refuse("a message over the proxy's limit")
|
|
}
|
|
// The message as it passes, its envelope first, grown as its bytes come rather than as its
|
|
// length says.
|
|
var buf bytes.Buffer
|
|
buf.Write(head[:])
|
|
if n, err := io.CopyN(&buf, f.r, int64(size)); err != nil || n != int64(size) {
|
|
return f.refuse("a message cut off")
|
|
}
|
|
msg := buf.Bytes()[len(head):]
|
|
plain := msg
|
|
if flags&frameCompressed != 0 {
|
|
if !f.gzip {
|
|
return f.refuse("a compressed message without gzip")
|
|
}
|
|
zr, err := gzip.NewReader(bytes.NewReader(msg))
|
|
if err != nil {
|
|
return f.refuse("a compressed message that isn't gzip")
|
|
}
|
|
plain, err = io.ReadAll(io.LimitReader(zr, frameMax+1))
|
|
if err != nil || len(plain) > frameMax {
|
|
return f.refuse("a compressed message the proxy can't read whole")
|
|
}
|
|
}
|
|
if jwtStart.Match(plain) {
|
|
return f.refuse("a token")
|
|
}
|
|
// Connect's end of stream is JSON, whose escapes (eyJ…) would hide a token from the bytes
|
|
// alone: its strings are checked as the VM's client reads them, and one that isn't JSON ends it.
|
|
// So is every message of a stream in JSON (application/connect+json, or gRPC's +json).
|
|
if (!f.grpc && flags&frameEnd != 0) || f.json {
|
|
var v any
|
|
if json.Unmarshal(plain, &v) != nil {
|
|
return f.refuse("a JSON message that isn't JSON")
|
|
}
|
|
if jsonHoldsToken(v) {
|
|
return f.refuse("a token")
|
|
}
|
|
}
|
|
f.out = buf.Bytes()
|
|
return nil
|
|
}
|
|
|
|
// jsonHoldsToken reports whether any string in v, a decoded JSON value, keys included, holds a
|
|
// JWT's start.
|
|
func jsonHoldsToken(v any) bool {
|
|
switch x := v.(type) {
|
|
case string:
|
|
return jwtStart.MatchString(x)
|
|
case []any:
|
|
return slices.ContainsFunc(x, jsonHoldsToken)
|
|
case map[string]any:
|
|
for k, e := range x {
|
|
if jwtStart.MatchString(k) || jsonHoldsToken(e) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (f *frameScanner) Close() error { return f.r.Close() }
|