Skip to content

Authenticate and bound WebSocket connections in the event broadcaster #42

Description

@priscaenoch

Problem

src/ws/eventBroadcaster.ts accepts WebSocket connections and supports query-parameter filters (?contract=CXXX..., ?eventType=token_transfer), but there is no authentication, no origin check, and no cap on concurrent connections or subscriptions. Any client can open unlimited sockets and subscribe to the event stream. Closing this gap properly is cross-cutting: it involves the broadcaster, the WebSocket server setup, shared auth middleware, configuration, tests, and documentation.

What needs to be done

  • Add an authentication/authorization check on the upgrade/connection handshake, consistent with the rest of the API (API key or token), implemented as reusable middleware.
  • Validate the Origin header against an allowlist for browser clients.
  • Enforce a per-client and global connection cap, and a cap on active filters/subscriptions per connection, all configurable via environment variables.
  • Add heartbeat/ping-pong and idle-timeout handling to reap dead sockets, and send a clear close code/reason when a connection is rejected or a limit is exceeded.
  • Document the WebSocket auth model, limits, and configuration.

Files

  • src/ws/eventBroadcaster.ts
  • the WebSocket server setup (src/ws/ and/or src/index.ts)
  • shared auth middleware under src/middleware/
  • src/config/ (limits + allowlist)
  • .env.example, README.md
  • tests under tests/

Acceptance deliverables

  • Unauthenticated or disallowed-origin connections are rejected with a defined close code.
  • Connection and subscription caps are enforced and configurable.
  • The WebSocket auth model and limits are documented.
  • All CI checks pass; the change cannot be merged until CI is green.

Tests to pass

  • Test: a connection without valid auth is rejected.
  • Test: exceeding the connection/subscription cap is rejected.
  • Test: an authenticated connection receives filtered events.

Metadata

Metadata

Assignees

Labels

ADVANCESubstantial, cross-cutting work spanning more than five files. Not a minor fix.GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardThird CampaignCampaign: Third CampaignbugSomething isn't workingenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions