Repository Public
1package ingest23import (4 "bytes"5 "context"6 "crypto/ecdsa"7 "crypto/x509"8 "encoding/base64"9 "errors"10 "io"11 "net"12 "net/http"13 "strconv"14 "sync"15 "time"1617 "github.com/google/uuid"1819 "clustr.spboucher.ai/mcc/internal/httpx"20 "clustr.spboucher.ai/mcc/internal/pki"21 "clustr.spboucher.ai/mcc/internal/protocol"22)2324// Signed-request transport.25//26// Why it exists: macOS Local Network Privacy silently blocks LAN connections27// made by third-party binaries started by launchd, and cannot be granted28// headlessly. Agents therefore reach the service through the public HTTPS29// front (control-api reverse-proxies /agent/v1/ to this loopback listener).30// TLS ends at the front, so a client certificate cannot authenticate the31// connection; instead every request carries the certificate and a signature32// made with its private key (see protocol.RequestSigningString).33//34// Replay protection: timestamp window (±90 s) plus an in-memory nonce cache35// per agent. A restart empties the cache, which shrinks the protection to the36// timestamp window. That is acceptable because every agent call is37// idempotent — reports by (agent, seq), command lifecycle calls by command38// id and state — and commands carry their own Ed25519 signature and nonce,39// verified by the agent.4041func sigErr(reason, message string) *httpx.Error {42 return httpx.Err(http.StatusUnauthorized, reason, message)43}4445var (46 errSigMissing = sigErr("signature_headers_missing", "Signed-request headers are missing or malformed.")47 errSigCert = sigErr("certificate_invalid", "The agent certificate is not valid for this service.")48 errSigClock = sigErr("timestamp_out_of_window", "The request timestamp is outside the accepted window; check the node clock.")49 errSigReplay = sigErr("nonce_replayed", "This request was already received.")50 errSigBad = sigErr("signature_invalid", "The request signature does not verify.")51 errSigThrottle = &httpx.Error{Status: http.StatusTooManyRequests, Code: "rate_limited", Message: "Too many failed authentications.", RetryAfter: 60}52)5354// SignedHandler returns the handler of the signed-request transport (plain55// HTTP, behind the public front). The client IP is taken from56// X-Forwarded-For only when the TCP peer is loopback.57func (s *Server) SignedHandler() http.Handler {58 return httpx.WithRequestContext([]string{"127.0.0.0/8", "::1/128"}, httpx.Recover(s.routes(s.withSignedAgent)))59}6061// withSignedAgent authenticates a request by proof of possession of the62// enrolled certificate key. Nothing of the body is parsed before the63// signature verifies; the body is only read (size-capped) to be hashed.64func (s *Server) withSignedAgent(next http.HandlerFunc) http.HandlerFunc {65 return func(w http.ResponseWriter, r *http.Request) {66 ip := clientIP(r)67 certHdr := r.Header.Get(protocol.HeaderAgentCert)68 // Failures are counted per client IP and presented certificate, so one69 // misbehaving agent behind a shared address cannot lock out the others.70 limKey := ip.String() + "|" + certHdr[max(0, len(certHdr)-24):]71 if s.sigFailures.blocked(limKey) {72 httpx.WriteError(w, r, errSigThrottle)73 return74 }75 fail := func(e error) {76 var he *httpx.Error77 if errors.As(e, &he) && he.Status == http.StatusUnauthorized {78 s.sigFailures.fail(limKey)79 s.log.Warn("ingest: signed request refused", "reason", he.Code, "ip", ip.String(), "path", r.URL.Path)80 }81 s.writeErr(w, r, e)82 }8384 // 1. Headers.85 tsHdr, nonce, sig := r.Header.Get(protocol.HeaderAgentTS), r.Header.Get(protocol.HeaderAgentNonce), r.Header.Get(protocol.HeaderAgentSig)86 if certHdr == "" || tsHdr == "" || sig == "" || len(certHdr) > 8192 || len(sig) > 512 || !validNonce(nonce) {87 fail(errSigMissing)88 return89 }90 ts, err := strconv.ParseInt(tsHdr, 10, 64)91 if err != nil {92 fail(errSigMissing)93 return94 }95 der, err := base64.StdEncoding.DecodeString(certHdr)96 if err != nil {97 fail(errSigMissing)98 return99 }100 // 2. Certificate: issued by our CA for client authentication, currently valid.101 leaf, err := x509.ParseCertificate(der)102 if err != nil {103 fail(errSigCert)104 return105 }106 now := time.Now()107 if _, err := leaf.Verify(x509.VerifyOptions{Roots: s.ca.Pool(), KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, CurrentTime: now}); err != nil {108 fail(errSigCert)109 return110 }111 pub, ok := leaf.PublicKey.(*ecdsa.PublicKey)112 if !ok {113 fail(errSigCert)114 return115 }116 id, err := pki.AgentIDFromCert(leaf)117 if err != nil {118 fail(errSigCert)119 return120 }121 // 3. Serial belongs to a non-revoked agent (same cache as mTLS).122 ctx, cancel := context.WithTimeout(r.Context(), s.reqTimeout)123 a, err := s.lookupAgent(ctx, leaf)124 cancel()125 if err != nil {126 s.writeErr(w, r, err) // database outage: 503, not an authentication failure127 return128 }129 if a == nil || a.ID != id {130 fail(errAgentRevoked)131 return132 }133 // 4. Freshness.134 if d := now.Sub(time.UnixMilli(ts)); d > protocol.MaxRequestSkew || d < -protocol.MaxRequestSkew {135 fail(errSigClock)136 return137 }138 // 5. Signature over method, request URI, timestamp, nonce and body hash.139 body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxReportBody))140 if err != nil {141 fail(bodyErr(err))142 return143 }144 if !protocol.VerifyRequest(pub, r.Method, r.URL.RequestURI(), ts, nonce, body, sig) {145 fail(errSigBad)146 return147 }148 // 6. Replay: only authenticated nonces are remembered.149 if !s.sigNonces.remember(a.ID, nonce, now) {150 fail(errSigReplay)151 return152 }153 r.Body = io.NopCloser(bytes.NewReader(body))154 r.ContentLength = int64(len(body))155 s.markSeen(a.ID)156 next(w, r.WithContext(context.WithValue(r.Context(), agentCtxKey{}, &authed{agent: a, leaf: leaf})))157 }158}159160func validNonce(n string) bool {161 if len(n) < 16 || len(n) > 64 {162 return false163 }164 _, err := base64.RawURLEncoding.DecodeString(n)165 return err == nil166}167168// ServeSigned serves the signed-request transport on ln until ctx ends.169func (s *Server) ServeSigned(ctx context.Context, ln net.Listener) error {170 srv := &http.Server{171 Handler: s.SignedHandler(),172 ReadHeaderTimeout: 10 * time.Second,173 ReadTimeout: 30 * time.Second,174 WriteTimeout: s.maxLongPoll + 20*time.Second,175 IdleTimeout: 120 * time.Second,176 MaxHeaderBytes: 32 << 10,177 }178 errc := make(chan error, 1)179 go func() { errc <- srv.Serve(ln) }()180 select {181 case err := <-errc:182 return err183 case <-ctx.Done():184 }185 s.closeOnce.Do(func() { close(s.closing) })186 sctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)187 defer cancel()188 if err := srv.Shutdown(sctx); err != nil {189 srv.Close()190 }191 return nil192}193194// nonceCache remembers request nonces per agent for twice the skew window.195type nonceCache struct {196 mu sync.Mutex197 seen map[uuid.UUID]map[string]time.Time198 total int199 lastGC time.Time200}201202const (203 nonceTTL = 2*protocol.MaxRequestSkew + 10*time.Second204 noncePerAgentMax = 4096205 nonceTotalMax = 200_000206)207208func newNonceCache() *nonceCache { return &nonceCache{seen: map[uuid.UUID]map[string]time.Time{}} }209210func (c *nonceCache) gcLocked(now time.Time) {211 for id, m := range c.seen {212 for n, t := range m {213 if now.Sub(t) > nonceTTL {214 delete(m, n)215 c.total--216 }217 }218 if len(m) == 0 {219 delete(c.seen, id)220 }221 }222 c.lastGC = now223}224225// remember returns false when the nonce was already used by this agent, or226// when the cache is full (fail closed: an unverifiable request is refused).227func (c *nonceCache) remember(id uuid.UUID, nonce string, now time.Time) bool {228 c.mu.Lock()229 defer c.mu.Unlock()230 if now.Sub(c.lastGC) > 30*time.Second {231 c.gcLocked(now)232 }233 m := c.seen[id]234 if t, ok := m[nonce]; ok && now.Sub(t) <= nonceTTL {235 return false236 }237 if len(m) >= noncePerAgentMax || c.total >= nonceTotalMax {238 c.gcLocked(now)239 if m = c.seen[id]; len(m) >= noncePerAgentMax || c.total >= nonceTotalMax {240 return false241 }242 }243 if m == nil {244 m = map[string]time.Time{}245 c.seen[id] = m246 }247 m[nonce] = now248 c.total++249 return true250}251252// failureLimiter blocks a key after too many failures in a window.253type failureLimiter struct {254 mu sync.Mutex255 max int256 window time.Duration257 m map[string]*failWindow258}259260type failWindow struct {261 start time.Time262 n int263}264265func newFailureLimiter(max int, window time.Duration) *failureLimiter {266 return &failureLimiter{max: max, window: window, m: map[string]*failWindow{}}267}268269func (l *failureLimiter) blocked(key string) bool {270 l.mu.Lock()271 defer l.mu.Unlock()272 fw := l.m[key]273 if fw == nil {274 return false275 }276 if time.Since(fw.start) > l.window {277 delete(l.m, key)278 return false279 }280 return fw.n >= l.max281}282283func (l *failureLimiter) fail(key string) {284 l.mu.Lock()285 defer l.mu.Unlock()286 now := time.Now()287 if len(l.m) > 10000 {288 for k, fw := range l.m {289 if now.Sub(fw.start) > l.window {290 delete(l.m, k)291 }292 }293 }294 fw := l.m[key]295 if fw == nil || now.Sub(fw.start) > l.window {296 l.m[key] = &failWindow{start: now, n: 1}297 return298 }299 fw.n++300}301