Title: fix(chat): bound deployed-chat callers and stop leaking chat gate config by waleedlatif1 · Pull Request #7525 · simstudioai/sim · GitHub
Open Graph Title: fix(chat): bound deployed-chat callers and stop leaking chat gate config by waleedlatif1 · Pull Request #7525 · simstudioai/sim
X Title: fix(chat): bound deployed-chat callers and stop leaking chat gate config by waleedlatif1 · Pull Request #7525 · simstudioai/sim
Description: Fixes two reported defects on chat deployments. 1. Denial of wallet — POST /api/chat/[identifier] A deployed chat resolves its execution principal from the workflow's workspace, so the plan rate bucket, the usage/credit check and the concurrency reservation all belong to the owner while the request belongs to whoever found the link. Nothing bounded the caller, and aborting the stream refunds none of it — the reservation and the model call have already happened. Two buckets now run after auth and before preprocessExecution, composed from the shared helpers in route-helpers.ts: bucket sustained burst per client IP, per deployment 20/min 40 per deployment, all callers 40/min 80 They apply to every execution regardless of authType — an email or sso visitor is authenticated but still not the payer. The gate-configuration handshake ({password} / {email} with no input) stays unmetered, since it spends nothing. The IP bucket is consulted first and returns on refusal, so one flooding IP cannot drain the deployment's budget and 429 the legitimate audience with it. When the proxy chain resolves to no client IP, the per-deployment bucket is the remaining bound — that is exactly the contract of the existing enforceIpRateLimitWithIndependentBackstop, which this composes with a new sibling enforceResourceRateLimit. The rates are derived, not chosen. A chat execution debits the workspace sync counter — the same counter the owner's API, webhook and scheduled runs draw from. A ceiling at or above that counter never refuses before the shared budget is drained, which is how the billing attack becomes an availability attack on unrelated production workloads. So the ceiling is 80% of the smallest configured plan sync rate: Math.max(1, Math.floor(Math.min(...Object.values(RATE_LIMITS).map((p) => p.sync.refillRate)) * 0.8)) That is 40/min against today's defaults (free 50, pro 150, team 300, enterprise 600), and it tracks operator RATE_LIMIT_*_SYNC overrides, which no hardcoded number could. Tests assert the invariant against every entry in RATE_LIMITS, on burst as well as sustained rate. GET /api/chat/validate answered for any anonymous caller, so available:false inventoried live deployments platform-wide. It now requires a session plus a per-user bucket via the existing enforceUserRateLimit. Its only consumer is the authenticated deploy modal, and proxy.ts already classified it as a workspace-internal credentialed route. 2. Chat gate config at workflow read — GET /api/workflows/[id]/chat/status The route reimplemented the admin-gated detail projection inline behind a bare workflow read check — serving the allowedEmails allow-list, hasPassword and the customization blob to any workspace viewer — and asserted no deploy.chat capability, so it also defeated hideDeployChatbot. It is now a declarative adapter over chat_deployments.list, the same operation GET /api/v2/chat-deployments binds, returning only { isDeployed, deployment: { id, identifier } | null }. That is exactly what the editor reads: useChatDeploymentInfo uses isDeployed for the Launch/Update affordance and deployment.id to fetch the detail from /api/chat/manage/{id}, which was already gated at workspace admin. Narrowing in the use case rather than the adapter means a future adapter cannot reopen it. Deliberate decisions worth reviewing No new env vars. Every sibling deployment throttle — password, OTP, SSO, on chat and on public file shares — is a hardcoded TokenBucketConfig. env.ts and the self-hosting docs are byte-identical to staging. The throttle is at the chat POST route, not inside validateDeploymentAuth. That gate is shared with public file shares, which serve stored bytes and never call preprocessExecution or reserveExecutionSlot — no owner execution budget is spent there, so an executions-per-minute unit does not belong on a download. It also runs on the page-load GET and the config-only handshake, which spend nothing. The buckets fail open. createStorageAdapter caches its adapter on globalThis, so the Postgres fallback only happens at construction — a Redis blip makes every call throw, and failing closed would take down every customer's public chat platform-wide. A briefly unenforceable spend ceiling is bounded by the workspace's own credit cap; every other helper in route-helpers.ts fails open for the same reason. The password/OTP/SSO gates keep failClosed: true because an unenforceable guess limit breaks a secret. .passthrough() → Zod's default strip, not .strict(). The builder responds with schema.parse(body), so stripping already guarantees the narrow wire. .strict() would throw, and since requestJson parses with the same schema, a new bundle reading an older pod mid-rollout would take the chat tab down. WorkflowChatDeploymentStatus types the projection at its source instead. A configured plan rate of 1 is the one value where the derived ceiling lands equal to the plan rather than under it, since no positive integer is below 1. It is inert — a workspace allowed one execution per minute has no capacity left to starve — and documented in the code. Behavior changes worth review /api/workflows/[id]/chat/status now needs a session (it accepted an internal JWT and API keys). No server-side caller exists; the only consumer is the editor hook. A caller with no workspace permission now gets 404 Chat not found or access denied instead of 403, via the domain's shared concealment policy, so the status code no longer reveals that the workflow exists. isDeployed is still the chat row's own isActive, deliberately not toEffectiveChatDeploymentView's "chat and workflow both live" rule — this answers "does this workflow already have a chat to update", which stays true while the workflow is undeployed. Enterprise is held to the same 40/min floor as free, because the ceiling is global rather than per-payer. See below. Known gaps, not fixed here Both are the same class as this fix — an anonymous caller imposing owner-attributed cost — on surfaces this PR does not touch. Both are closed by the same follow-up: a caller-scoped bucket passed into preprocessExecution, where actorSubscription is already resolved two lines above the rate gate. The inactive-chat branch of this same route runs before authentication and does one DB read plus two DB writes (safeStart + safeCompleteWithError) per anonymous request, unbounded. Hammering a disabled chat identifier generates unlimited execution-log rows attributed to the owner. Reaching the execution bucket into a branch that spends no execution would be the wrong fix; it needs its own small bucket. lib/webhooks/processor.ts calls preprocessExecution with userIdIsStoredReference: true the same way, and a generic webhook with requireAuth off has no per-caller bound at all. That same follow-up also lets the chat ceiling scale to the payer's own plan, lifting enterprise above the shared 40/min floor. Testing route-helpers.test.ts — enforceResourceRateLimit keys and refuses; the per-IP bucket's scoped and unscoped key shapes. chat/[identifier]/route.test.ts — each bucket refuses before preprocessExecution, createStreamingResponse and processChatFiles; the deployment bucket is untouched when the IP bucket refuses; the handshake stays unmetered; the ceiling stays under every plan in RATE_LIMITS on both burst and sustained rate; the IP bucket stays under the deployment bucket. workflows/[id]/chat/status/route.test.ts — exact-shape projection for both read and admin, 401/404 paths, inactive and absent deployments, and that chat_deployments.read stays admin-gated. chat/validate/route.test.ts — anonymous refusal before any bucket is consulted, plus the dictionary cap. Every new test was verified to fail when its fix is reverted. bun run type-check, check:audits (45/45), lint:check, and the affected suites pass. 🤖 Generated with Claude Code https://claude.ai/code/session_01QWh9WFMYNZ6uTFQFUzF8Bj
Open Graph Description: Fixes two reported defects on chat deployments. 1. Denial of wallet — POST /api/chat/[identifier] A deployed chat resolves its execution principal from the workflow's workspace, so the plan rat...
X Description: Fixes two reported defects on chat deployments. 1. Denial of wallet — POST /api/chat/[identifier] A deployed chat resolves its execution principal from the workflow's workspace, so the plan...
Opengraph URL: https://github.com/simstudioai/sim/pull/7525
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6c242205-b93f-801f-44e4-40b7939d9fe2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D796:6449C:ACAFBA:EE097F:6AFB5285 |
| html-safe-nonce | 3e58787b5c2d46d1cc3716680694109e0f671f148c736c7c43dbb573587f65ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzk2OjY0NDlDOkFDQUZCQTpFRTA5N0Y6NkFGQjUyODUiLCJ2aXNpdG9yX2lkIjoiMzUzNDkwMDIzNjkzNTQ1MTI3MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3f87463821caa67bb0ff1e9ba7610ca93e2fb65271faca8bbb03005ab6f31bfd |
| hovercard-subject-tag | pull_request:4452728780 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/7525/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Fixes two reported defects on chat deployments. 1. Denial of wallet — POST /api/chat/[identifier] A deployed chat resolves its execution principal from the workflow's workspace, so the plan rat... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7122bb414b542dba14d71a3f0b9f6fd2abce715b9f5912d082800563e6c806d1 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cbf66c024bb6d10e6955baa7413c54cf13a201bb |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width