feat: add security load test and regression tooling - #462
Conversation
cameri
left a comment
There was a problem hiding this comment.
Please add documentation on how to use npm run test:load
There was a problem hiding this comment.
Pull request overview
Adds a new manual security/load-testing utility to reproduce and guard against WebSocket “zombie connection” heartbeat regressions, and wires it into the repo’s npm scripts.
Changes:
- Added
scripts/security-load-test.jsto generate zombie WebSocket connections and optionally flood the relay with valid signed Nostr events. - Added
npm run test:loadentry inpackage.jsonto run the new tooling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| scripts/security-load-test.js | New load/security test script to simulate zombie clients + optional event spammer. |
| package.json | Adds test:load script to execute the new tool. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const args = process.argv.slice(2).reduce((acc, arg, i, arr) => { | ||
| if (arg.startsWith('--')) acc[arg.slice(2)] = arr[i + 1]; | ||
| return acc; | ||
| }, {}); | ||
| const RELAY_URL = args.url || 'ws://localhost:8008'; | ||
| const TOTAL_ZOMBIES = parseInt(args.zombies || '5000', 10); | ||
| const SPAM_RATE = parseInt(args['spam-rate'] || '0', 10); |
There was a problem hiding this comment.
The CLI parsing logic treats every --flag as having a value at arr[i + 1] (even if the next token is another flag), which can lead to TOTAL_ZOMBIES / SPAM_RATE becoming NaN (e.g., --zombies --spam-rate 100). Consider using a more robust parser (or at least validating parseInt(...) results and exiting with a helpful message when values are missing/invalid).
| constargs=process.argv.slice(2).reduce((acc,arg,i,arr)=>{ | |
| if(arg.startsWith('--'))acc[arg.slice(2)]=arr[i+1]; | |
| returnacc; | |
| },{}); | |
| constRELAY_URL=args.url||'ws://localhost:8008'; | |
| constTOTAL_ZOMBIES=parseInt(args.zombies||'5000',10); | |
| constSPAM_RATE=parseInt(args['spam-rate']||'0',10); | |
| functionparseCliArgs(argv){ | |
| constacc={}; | |
| for(leti=0;i<argv.length;i++){ | |
| constarg=argv[i]; | |
| if(!arg.startsWith('--'))continue; | |
| constkey=arg.slice(2); | |
| constvalue=argv[i+1]; | |
| if(value===undefined||value.startsWith('--')){ | |
| console.error(`Missing value for --${key}`); | |
| process.exit(1); | |
| } | |
| acc[key]=value; | |
| i++; | |
| } | |
| returnacc; | |
| } | |
| functionparseIntegerArg(value,defaultValue,flagName){ | |
| if(value===undefined)returndefaultValue; | |
| constparsed=Number.parseInt(value,10); | |
| if(Number.isNaN(parsed)){ | |
| console.error(`Invalid value for --${flagName}: ${value}. Expected an integer.`); | |
| process.exit(1); | |
| } | |
| returnparsed; | |
| } | |
| constargs=parseCliArgs(process.argv.slice(2)); | |
| constRELAY_URL=args.url||'ws://localhost:8008'; | |
| constTOTAL_ZOMBIES=parseIntegerArg(args.zombies,5000,'zombies'); | |
| constSPAM_RATE=parseIntegerArg(args['spam-rate'],0,'spam-rate'); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
YashIIT0909
commented
Apr 18, 2026
I have migrated the script to typescript. |
cameri
commented
Apr 18, 2026
@YashIIT0909 you will have to reset your branch to your last own commit, then merge latest origin/main back into it. |
c3182c3 to
8d2c6f9CompareUh oh!
There was an error while loading. Please reload this page.
Description
This PR introduces a specialized security and load-testing tool, scripts/security-load-test.js. These assets are designed to reproduce, verify, and permanently safeguard the relay against memory leaks in the WebSocket heartbeat mechanism (specifically the "zombie connection" issue).
The tool has been integrated into package.json via the
npm run test:loadcommand to allow for easy execution during manual security audits.Related Issue
Fixes#429
Motivation and Context
While the primary heartbeat bug (eviction blocked by active subscriptions) has been addressed, the relay previously lacked a reliable way to verify that dead connections are actually being evicted from both the
wsclient set and the internalWeakMapadapters.This change is required to:
How Has This Been Tested?
The tooling was tested in a Linux environment using a 1-worker cluster configuration:
Screenshots
WeakMapwhile the socket remains in the server's active client set.Types of changes
Checklist: