Skip to content

Commit 90a16a9

Browse files
committed
fix: harden websocket limits
1 parent 80225df commit 90a16a9

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ WEBSOCKET_NETWORKS=mainnet
1515
BLOCKCHAIN_POLL_INTERVAL=10000
1616
WEBSOCKET_HEARTBEAT_INTERVAL=30000
1717
WEBSOCKET_MAX_CONNECTIONS_PER_IP=5
18+
WEBSOCKET_MAX_PAYLOAD_BYTES=65536
1819

1920
# App Configuration
2021
# Public origin allowed by the API CORS allowlist (server-side). Defaults to

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ WEBSOCKET_NETWORKS=mainnet # comma-separated; add testnet if you run a
6565
BLOCKCHAIN_POLL_INTERVAL=10000
6666
WEBSOCKET_HEARTBEAT_INTERVAL=30000
6767
WEBSOCKET_MAX_CONNECTIONS_PER_IP=5
68+
WEBSOCKET_MAX_PAYLOAD_BYTES=65536
6869

6970
# Public origin allowed by the API CORS allowlist
7071
PUBLIC_BASE_URL=https://explorer.fairco.in

server/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import packageJson from '../package.json' with { type: 'json' }
2929
const __dirname = path.dirname(fileURLToPath(import.meta.url))
3030
const app = express()
3131
const PORT = parseInt(process.env.PORT || '8080', 10)
32+
const WEBSOCKET_MAX_PAYLOAD_BYTES = parseInt(process.env.WEBSOCKET_MAX_PAYLOAD_BYTES || '65536', 10)
3233

3334
// Trust X-Forwarded-* headers only when the direct TCP peer is an explicitly
3435
// configured reverse proxy. Leave TRUSTED_PROXY_CIDRS empty when the backend can
@@ -638,7 +639,12 @@ app.use((_req, res) => {
638639
const server = createServer(app)
639640

640641
// WebSocket setup
641-
const wss = new WebSocketServer({ noServer: true })
642+
const wss = new WebSocketServer({
643+
noServer: true,
644+
maxPayload: Number.isFinite(WEBSOCKET_MAX_PAYLOAD_BYTES) && WEBSOCKET_MAX_PAYLOAD_BYTES > 0
645+
? WEBSOCKET_MAX_PAYLOAD_BYTES
646+
: 65536,
647+
})
642648

643649
server.on('upgrade', (request, socket, head) => {
644650
const { pathname } = parse(request.url || '', true)

0 commit comments

Comments
 (0)