← Back to Pull requests

Limit failed authentication on Cloud's CLI API per client address #3142

Closed
unknown closed this pull request without merging on Oct 23, 2026 00:17 UTC
View on GitHub
unknown unknown commented on Oct 22, 2026 23:59 UTC
Author

Fixes #2942.

Summary

Failed authentication on Cloud's ployz API (/api/cli/ and /api/config/) is now limited per client address: 20 a minute and 100 an hour. A held address gets 429 RATE_LIMITED with Retry-After, whatever credential it presents, so a guess can't land while the address waits.

resolveCaller(headers)                      # ClientAddress set by the route from observedIp
  acquireUseRelease
    acquire: admitWithinWindows("failed-auth:<addr>", 20/60s, 100/h)
               one advisory-locked query over the sliding log; inserts a reservation if admitted
    use:     admitted ? callerOf(headers) : fail RateLimited(retryAfterSeconds)
    release: keep the reservation only when the outcome was Unauthorized; otherwise delete it

  • Reserve, then release. Each attempt holds a place in the limit while it is checked, so a burst of parallel bad tokens can't all pass the check before any failure is recorded. Only a failure keeps its place. acquireUseRelease releases the place even when the request is interrupted while it commits.

  • Address. observedIp honours the configured client IP headers and walks X-Forwarded-For from the right, but only from private or local peers. IPv6 groups by /64.

  • Engine. The CLI reads Cloud's RATE_LIMITED as Unavailable (cloud_account.rs), so it prints the wait instead of an unknown error.

  • Shared limiter. admitWithinWindows now counts all windows in one query, returns the reservation id, and treats its occasional prune of stale rows as best-effort. Agent signup, its other caller, keeps its behaviour, except that a failed prune no longer fails a signup.
  • Evidence

    failed-authentication.postgres.test.ts: 6 tests, all passing:

  • 20 bad tokens get 401, then the 21st request gets 429 with Retry-After, even with a valid token. Successes never count.

  • 60 parallel bad tokens get exactly 20 × 401 and 40 × 429. That is the burst R1 caught getting past the limit.

  • The same limit holds through the real HTTP handler (serveCli), with Retry-After set.

  • A forced prune failure: a valid token still gets 200 and leaves no row. Before the fix it got 500 and left the row.

  • An interrupt held at COMMIT by a deferred trigger leaves no row. Before the fix it left 1.
  • Agent signup tests (18) pass. pnpm typecheck passes. The core test a_cloud_rate_limit_reads_as_unavailable passes.

    Review: R1 Codex+Opus, then 6 findings fixed. R2 Codex found one BUG (a stranded reservation), which is now fixed. R3 Codex on that fix found no BUG.

    Ops

  • New 429s. An address with 20 failed sign-ins in a minute, or 100 in an hour, gets 429 RATE_LIMITED with Retry-After on CLI API calls for up to an hour, including calls with a valid token.

  • The proxy must pass the client's address. Behind a proxy that doesn't set X-Forwarded-For (or the configured client IP header), every client shares the proxy's address, so one token guesser can lock out every ployz user. Caddy and cloudflared set it. docs/user/self-hosting.md says so.

  • Shared NAT. Users behind one NAT share a budget. Both R1 reviewers accepted this trade-off.

  • More than 20 simultaneous in-flight requests from one address, even valid ones, can briefly see 429 while their reservations are held.

  • Storage. It adds rows to the existing rate_limit_event table, with no schema change.
  • Out of scope: the enroll, check-in and MCP bearers, and grouping IPv6 by /48.

    Bets

    None of the Engine bets in core/DESIGN.md change. The core change maps one more Cloud error code. Stable promise: additive. RATE_LIMITED is a new public error code with a new Retry-After header. No RPC, schema or stored-format change.

    Merge Danger

    Door: two-way. Reverting removes the limit. Leftover failed-auth: rows age out with the 24 h retention.

    Blast Radius: CLI auth. If the client address is misconfigured behind a proxy, a guesser can cause a shared lockout. The Ops notes above cover it.

    Diff: 502 lines (+376 / -126) across 6 files Reviewer: nick-potts Review duration: 17m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.71) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 502 lines diff across 6 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.372
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #3142"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 70.7% (HIGH) · Higher = larger blast radius
    70.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.6% · Safe to merge
    30.6%
    Repository