Skip to content

fix(security): harden the gateway, verify billing webhooks, keep bots alive on handler errors - #145

Merged
zVapor-Dev merged 1 commit into
v3from
fix/gateway-webhook-sdk-hardening
Oct 29, 2026
Merged

zVapor-Dev merged 1 commit into
v3from
fix/gateway-webhook-sdk-hardening

Conversation

@zVapor-Dev

Copy link
Copy Markdown
Member

Requested by zVapor · project thread

What this changes

These are the three most serious findings from a security review of 3.1.0-beta.0.

Gateway (packages/core/src/server/gateway/gateway.ts)

  • Before use, frames are checked to be JSON objects, and hello params to have string botId / nonceSig.
  • The async message handler's errors are caught. A failing frame is dropped, and before auth the socket is also closed with 1011.
  • Added a socket error listener. ws reports broken frames (bad UTF-8, oversize) as error events, which were uncaught.
  • Frames are capped at 1 MiB, configurable with the new maxPayload option. ws's default is 100 MiB.

Billing webhooks (routes/billing.ts, platform-discord/billing.ts)

  • /api/webhooks/stripe verifies Stripe-Signature: HMAC-SHA256 over t.rawBody, any v1, 5 minute tolerance. Configured with stripeWebhookSecret / STRIPE_WEBHOOK_SECRET.
  • /api/webhooks/discord verifies Discord's Ed25519 signature (X-Signature-Ed25519 over timestamp + raw body) with the app's public key: discord({ publicKey }) / DISCORD_PUBLIC_KEY. It reads Discord's webhook event envelope (ENTITLEMENT_CREATE, _UPDATE, _DELETE, with ends_at carried as expiresAt) and answers PING and events with an empty 204.
  • New PlatformBilling.verify hook, so other platforms can plug in their own signature check.
  • A webhook that can't be verified now gets 503 instead of being accepted. WEBHOOK_SECRET (the x-webhook-secret header) remains as a fallback for your own relays when no platform signature is configured. It is never accepted in place of a configured Discord signature.
  • Raw bodies are captured by a JSON parser scoped to the webhook routes only.
  • createDashboard gains discord.publicKey and billing: { stripeWebhookSecret, webhookSecret }, with env fallbacks. Before this, lite mode had no way to authenticate webhooks at all.

Bot SDKs

  • sdk-js: handle() errors are caught. A throwing handler now answers the dashboard with a JSON-RPC error (-32603) instead of becoming an unhandled rejection that exits the bot process.
  • sdk-py: same JSON-RPC error reply, instead of tearing down and reconnecting the socket. Non-object frames are skipped.

Docs: .env.example, the asctane-dashboard README and CHANGELOG.

Why

  • Any client that can reach the gateway port could stop the whole server with a single message, before authenticating.
  • Billing webhooks accepted unauthenticated events whenever no secret was set, and lite mode could never set one. The shared-header design also couldn't work with real Stripe or Discord deliveries, since neither can send a custom header.
  • Any logged-in user (owning any Discord server is enough) could crash a bot built on the JS SDK through an action whose handler throws on an unexpected payload.

Breaking for the beta: deployments that relied on unauthenticated webhooks must set STRIPE_WEBHOOK_SECRET / DISCORD_PUBLIC_KEY (or WEBHOOK_SECRET for a relay). The Discord webhook response is now 204 when the signature is verified.

Not in this PR (follow-ups from the same review): server-side validation of role/channel picker values with a role-hierarchy check, declared and gated actions with payload schemas, trustProxy for rate limiting, and a Discord replay window.

Checklist

  • Tests added or updated. New: gateway/hostile-frames.test.ts, webhook-signatures.test.ts, Discord billing signature/envelope tests, a lite-mode webhook test, a Python SDK error-reply test. The new gateway and SDK tests fail against the old code. Old tests that posted unsigned webhooks now send the shared secret or expect 503.
  • The repository's checks pass locally: pnpm lint, pnpm typecheck, pnpm test (283 passed), pytest (14 passed), pnpm build, pnpm smoke:pack (22 packages).
  • Docs or changelog updated

Generated by Claude Code

… alive on handler errors

- gateway: validate frames before use, catch async handler errors, listen
  for socket errors, and cap frames at 1 MiB (maxPayload option)
- billing: verify Stripe-Signature (stripeWebhookSecret) and Discord's
  Ed25519 signature (discord({ publicKey })); refuse unverifiable webhooks
  with 503 instead of accepting them; read Discord's ENTITLEMENT_* event
  envelope and answer with 204; createDashboard gains discord.publicKey and
  billing options
- sdk-js / sdk-py: a throwing handler answers with a JSON-RPC error instead
  of crashing the bot or dropping the connection

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DTq83Lht42mpW1KQWJoXPH
@zVapor-Dev zVapor-Dev self-assigned this Oct 29, 2026
@zVapor-Dev
zVapor-Dev marked this pull request as ready for review October 29, 2026 15:20
@zVapor-Dev
zVapor-Dev merged commit e9a7844 into v3 Oct 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants