Skip to content

Implement webhook server middleware with HMAC validation and replay attack protection #424

Description

@Kingsman-99

Overview

Build Express/Next.js middleware for receiving StellarSplit invoice webhooks. The middleware must verify HMAC-SHA256 signatures, reject replayed requests using a timestamp + nonce window, and provide typed event handlers.

Requirements

  • createWebhookMiddleware(secret: string, options?: WebhookOptions): RequestHandler
  • WebhookOptions { toleranceSeconds: number, nonceWindowSize: number }
  • Verify X-StellarSplit-Signature header (HMAC-SHA256 of timestamp + body)
  • Reject requests where timestamp is outside toleranceSeconds window
  • Reject replayed nonces using an in-memory LRU nonce cache (size = nonceWindowSize)
  • Export on(event: InvoiceEventType, handler) typed event emitter
  • Write tests: valid webhook passes, expired timestamp rejected, replayed nonce rejected, tampered signature rejected

Acceptance Criteria

  • Middleware validates HMAC signatures correctly
  • Timestamp window enforced
  • Nonce replay rejected
  • Typed event handlers work
  • npm test passes with zero failures
  • TypeScript compiles with zero errors
  • ESLint passes with zero errors

Definition of Done

All CI checks must pass before the PR is reviewed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions