feat(gateway): rate limiting + replay/idempotency protection for session + webhook endpoints#47
Merged
aguilar1x merged 12 commits intoJul 19, 2026
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The gateway is the only component that talks to the live Pacto API, holding
pk_/sk_credentials on behalf of every merchant. Before this change,POST /v1/sessionand the outbound webhook path had no rate limiting or replay protection, leaving them open to abuse and duplicate-processing bugs once real merchant traffic hits.This adds three independent, independently-testable protections to
services/connect-gateway, reusing the existing gateway primitives (origin middleware, webhook signature module,toGatewayErrorBody, Prisma).What's included
1. Rate limiting on public endpoints (per publishable key)
src/middleware/rate-limit.ts), configurable viaRATE_LIMIT_WINDOW_MS(default 60000) andRATE_LIMIT_MAX(default 60).originValidationso it keys off the resolvedApiKey; skips/health,/admin/*,OPTIONS, and the inbound webhook path.Retry-Afterheader.2. Replay protection for signed webhooks (new inbound receiver)
t=..,n=..,v1=..), backward compatible with existing outbound signing.POST /v1/webhooks/inbound(src/routes/inbound-webhooks.ts) that verifies the HMAC signature, rejects timestamps outside the tolerance window, and rejects reused nonces (WebhookNoncetable).3. End-to-end idempotency
Idempotency-Keyheader honored onPOST /v1/sessionandPOST /v1/escrows(src/middleware/idempotency.ts+IdempotencyRecord).Idempotent-Replayed: true), without re-running the side effect. Key reuse with a different body returns409 idempotency_key_reuse.WebhookEvent.sourceEventId, so they never double-fire merchant webhooks.Schema / config
IdempotencyRecord,WebhookNonce, andWebhookEvent.sourceEventId..env.exampleandWEBHOOKS.md:RATE_LIMIT_WINDOW_MS,RATE_LIMIT_MAX,PACTO_WEBHOOK_SECRET,WEBHOOK_REPLAY_TOLERANCE_SECONDS.Testing
npm testinservices/connect-gateway: 119 tests passing (18 files). Type-check and Biome lint clean.Retry-After; timestamp-outside-tolerance and reused-nonce → rejected + logged; same idempotency key → original response with the side effect running once.Acceptance criteria
Retry-Afterheader.Known follow-ups (non-blocking)
dispatchEventevent+delivery creation is not transactional (pre-existing).sourceEventId(different nonces) can race to a 500 that self-heals on retry.Closes #34