Details
Vulnerable code
packages/executors/legacy-ws/src/index.ts, lines 103-167 at b5d1c36ebb8ed3868260ffee5bbcbb423ad5c1f1:
const id = Date.now().toString();
return observableToAsyncIterable({
subscribe(observer) {
ensureWebsocket();
...
websocket.onmessage = event => { // sole handler: overwritten by every new subscribe()
const data = JSON.parse(event.data.toString('utf-8'));
switch (data.type) {
...
case LEGACY_WS.DATA: {
observer.next(data.payload); // SINK: data.id is never compared with id
break;
}
}
};
},
});
Two independent defects are visible here: the single mutable onmessage assignment (which detaches the earlier subscriber), and the missing data.id === id check in the DATA branch (which misroutes the payload). The operation id itself is also derived from Date.now(), so it is not collision-resistant.
Reproduction
Verified dynamically against the real published package @graphql-tools/[email protected] (npm tarball with matching source at d304214db06e8a4ce9cb41138f199e3bc6ed36f2). The harness was a protocol-correct legacy graphql-ws server (ws v8) that tags every DATA frame with the operation id of the operation that produced it, plus two callers sharing one executor instance.
- Scenario A (control, single subscription): the subscriber received its own payload, confirming the harness is sound.
- Scenario B (the claim): caller 1 ("victim") subscribed; the server ACKed and the client sent
start id=1790247741390. Caller 2 ("attacker") then invoked the same executor whilereadyStatewasOPEN. The server observed only one socket (ensureWebsocketreused it) and only one started operation — the attacker's operation was never started, becauseCONNECTION_ACKhad already fired. The server then emitted a correctly-taggedDATAframe for the victim's id. The attacker's iterator received{"data":{"victimSecret":"VICTIM-PRIVATE-DATA-a1b2c3"}}and the victim's iterator received nothing. - Scenario C: a
DATAframe tagged with the id"id-that-was-never-started"was still delivered to the subscriber, proving the protocol operation id is never validated. - Realistic wiring: with the executor wired into
wrapSchema()the way a stitching gateway does, two concurrent users subscribing to the same field produced a verbatim cross-tenant leak — User B received{"data":{"accountBalance":"USER-A-PRIVATE-BALANCE-$91402.11"}}while User A received nothing. When the two users select different fields, the wrap layer's result transform masks the foreign field (B sawweather:null), but the misrouting still occurs and the victim is still starved; at the raw executor API the payload leaks verbatim regardless. - Causality control: patching only the
DATAbranch to checkdata.id === ideliminated the leak (the attacker received nothing, and the foreign-id frame was rejected), pinning the leak to the missing id check. Under that patch the victim still received nothing, confirming the second, independent defect: the single sharedwebsocket.onmessageis overwritten on eachsubscribe(), detaching the earlier subscriber's handler entirely. - Scope caveat: the identical proof of concept also reproduces on the current latest release
1.1.35.
Minimal client-side shape:
import WebSocket from 'isomorphic-ws';
import { parse } from 'graphql';
import { buildWSLegacyExecutor } from '@graphql-tools/executor-legacy-ws';
const exec = buildWSLegacyExecutor('ws://localhost:8081/graphql', WebSocket);
const victim = exec({ document: parse('subscription { privateEvent { secret } }') });
const victimPending = victim.next(); // causes connection + START; wait until the server has ACKed
await new Promise(r => setTimeout(r, 100));
const attacker = exec({ document: parse('subscription { publicEvent { id } }') });
const attackerPending = attacker.next(); // overwrites socket.onmessage
// When the server now emits DATA for the victim's id, this prints the victim's secret.
console.log(await attackerPending);
Steps:
- Start a normal legacy
subscriptions-transport-wsserver with a private event subscription. - Build one executor and call
victimIterator.next()so the socket receivesconnection_ackandSTARTfor the victim. - While that socket is
OPEN, callattackerIterator.next()using the same executor. - Emit one victim event from the server.
- Expected: a
DATAframe is delivered only to the observer registered fordata.id; the attacker's pendingnext()must never resolve with the victim's payload, and the victim keeps receiving their own events. - Actual: the attacker's
next()resolves with the victim's payload, and the victim's iterator receives nothing.
Suggested remediation
Dispatch protocol frames by operation id to the observer that started that operation, instead of routing every frame to whichever subscriber most recently overwrote the socket's sole onmessage handler.
// 1. Stop the misrouting: ignore frames that belong to another operation.
case LEGACY_WS.DATA: {
if (data.id !== id) {
break;
}
observer.next(data.payload);
break;
}
// 2. Stop the handler overwrite: keep a registry of observers keyed by operation id
// and install exactly one shared message listener for the socket.
const observers = new Map<string, Observer<any>>();
function ensureMessageHandler() {
if (messageHandlerInstalled) return;
messageHandlerInstalled = true;
websocket.onmessage = event => {
const data = JSON.parse(event.data.toString('utf-8'));
const target = data.id != null ? observers.get(data.id) : undefined;
// ... dispatch DATA / ERROR / COMPLETE to `target` only, and remove it on COMPLETE
};
}
An alternative that keeps the current structure is to create a separate socket (and therefore a separate listener) per execution, as the in-repo UrlLoader wrapper effectively does today.
Notes:
- The same id check is needed on the
ERRORandCOMPLETEbranches, not justDATA; otherwise one operation's termination still tears down another's iterator. - The operation id is generated as
Date.now().toString()(line 103), which is not collision-resistant — two subscriptions created in the same millisecond would share an id even after the dispatch fix. Use a counter or a random component. - The missing id check and the overwritten
onmessageare two independent defects; fixing only the id check stops the leak but leaves the earlier subscriber permanently starved, as the causality control above showed. packages/loaders/url/src/index.ts:262-295already builds a fresh legacy executor inside each request, so that caller needs no change; the fix belongs in the executor itself, because the exportedbuildWSLegacyExecutorreturns a general reusableDisposableExecutor.- No upstream advisory (CVE/GHSA), fix commit, or fixed version is known for this issue: unknown / not verified. The set of affected downstream consumers has likewise not been enumerated.