Repository navigation
fix: reject reserved event names on public emits - #253
Conversation
|
Warning Review limit reached
Next review available in: 54 minutes You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (8)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
f997723 to
18ddb37
Compare
Summary
Smocket already kept reserved lifecycle events out of incoming and outgoing catch-all observation, but it did not protect the public send boundary itself. Application code could call
emit('disconnect'),emit('connect_error'), or another reserved name on a server, namespace, broadcast operator, server socket, or client socket. The mock then treated that name as an ordinary packet: it could reach the peer, enter a disconnected client's send buffer, or fireonAnyOutgoing. Socket.IO rejects the same call before any of those effects occur.This adds one public-emit guard for Socket.IO's six reserved names:
connect,connect_error,disconnect,disconnecting,newListener, andremoveListener. Ordinaryemit()throwsError('"name" is a reserved event name')synchronously.emitWithAck()preserves Socket.IO's different observable shape: because upstream callsemit()inside the Promise executor, the same error becomes a rejected Promise rather than escaping synchronously. Rejection happens before delivery, buffering, acknowledgement registration, timeout creation, volatile dropping, or outgoing catch-all notification.The guard covers the concrete send implementations rather than every delegating facade independently.
ServerandNamespaceemissions reachBroadcastOperator, so the operator rejects names even when it has no recipients and carries the rule through room narrowing, timeout, and volatile composition. Direct server and client sockets guard their ownemitpaths. Their timeout and volatile views reject before they arm a timer, inspect connection state, drop a volatile packet, or invoke an outgoing listener. The failed missing-origin client keeps its intentional inert behavior for ordinary application events, but it now rejects reserved names on direct, timeout, volatile, and acknowledgement forms just like a Socket.IO client that has not connected.Internal lifecycle delivery remains separate. Smocket continues to call its private
dispatch()path forconnect,connect_error,disconnecting, anddisconnect, so middleware rejection, missing-server reporting, connection completion, and teardown can still emit the library-owned events.connectionandnew_namespaceare not in the public packet reserved set and remain valid application payload names, matching the measured Socket.IO broadcast path rather than the distinct reserved-listener maps used internally byServerandNamespace.Socket.IO reference
The runtime surface was probed independently against Socket.IO and socket.io-client 4.7.5 and 4.8.3 before the mock changed. Both supported version pairs produced the same results across
Server,Namespace,BroadcastOperator, serverSocket, clientSocket, and their timeout or volatile forms.connect,connect_error,disconnect,disconnecting,newListener,removeListeneremit(reserved)Error('"name" is a reserved event name')emitWithAck(reserved)onAnyOutgoingconnectionandnew_namespaceThe probes exercised every named server surface with all six reserved names, then sent a later application marker in each direction. Once the marker arrived, neither peer had observed
connect_error, and each outgoing catch-all contained only the marker. The same probe also deliveredconnectionandnew_namespacein both directions. This establishes both sides of the boundary without broadening the reserved set from a similarly named internal namespace constant.The installed upstream sources explain the surface sharing observed by the probe. Socket.IO server sockets and broadcast operators both check the same six-name set.
Server.emit()delegates to the root namespace, and namespace delivery constructs a broadcast operator. socket.io-client performs the corresponding check at the start ofSocket.emit(). The implementation here follows those observable entry points while retaining Smocket's own in-memory routing and internal dispatch structure.Rejection ordering
Ordering is part of this fix because several wrappers previously had observable work before delivery. A normal client emit notified
onAnyOutgoingand then either buffered or sent. A timeout emit could create a timer before delegating. A volatile emit inspected whether the peer was connected before deciding to drop. The reserved-name check now precedes all of those branches, so a rejected call leaves no queued packet, outgoing observation, pending acknowledgement, or timeout callback behind.The acknowledgement helper performs its guard inside the Promise executor. That placement is intentional: moving the same check into
ServerSocket.emitWithAck()before constructing a Promise would produce a synchronous throw and diverge from both supported Socket.IO versions. Connected and disconnected acknowledgement paths share the helper, so a reserved name rejects consistently while an allowed disconnected volatile acknowledgement still remains pending under the existing ADR 0016 behavior.The missing-origin
FailedClientSocketfollows the same boundary. Allowed events remain inert and chain as ADR 0005 records; reserved events throw, and reserved acknowledgement calls reject. This keeps the deliberate no-retry client behavior without turning it into an exception to Socket.IO's event-name rule.Type surface
Socket.IO's declarations do not encode the runtime reserved-name rejection. An untyped default event map accepts every string, and a concrete event map that explicitly declares
disconnectmakesemit('disconnect')compile on the server, namespace, broadcast, server socket, client socket, timeout, and volatile surfaces. Both 4.7.5 and 4.8.3 compiled the same consumer fixture.Smocket therefore keeps the same permissive declaration boundary instead of introducing an
Excludethat would make a valid Socket.IO call site fail after substitution. The type fixture now compiles mappeddisconnectcalls through the equivalent Smocket and real Socket.IO generics, including client acknowledgement wrappers. Runtime tests carry the rejection guarantee; the type fixture carries the equally important fact that Smocket is not stricter than upstream where TypeScript cannot represent the runtime rule without breaking compatibility.Tests and documentation
src/reserved-events.test.tsadds real-first coverage for all six names on every supported server and client emit shape. It pairs those invalid calls with accepted application events, covers the pending-connection window, proves non-delivery with later markers, proves outgoing catch-alls remain empty, checks the rejected-Promise acknowledgement shape, and pinsconnectionandnew_namespaceas allowed packet names.src/connect-url.test.tsseparately covers the terminal missing-origin client so its direct, timeout, volatile, and acknowledgement views cannot bypass the guard.src/volatile.test.tsexercises allowedvolatile.emitWithAck()calls in both directions while connected, including the outgoing catch-all that runs before delivery. Its two pre-connect marker cases also issue acknowledgement forms and prove they are dropped before the later normal marker arrives. Together these cases cover both the connected and dropped target branches without waiting for a pending acknowledgement.docs/conformance.mdis regenerated from the green dual run and records the reserved-event cases, the volatile acknowledgement case, and the failed-client Smocket case. The reserved-event entry is removed fromdocs/differences.mdsection C because #232 is no longer an accidental fidelity gap. Under ADR 0019 this is an observable correction toward measured Socket.IO behavior, which lands as the pre-1.0 patch scheduled for v0.4.3.Locally, the Node dual run passes 446 tests across 50 files, the Chromium mock run passes 223 tests across 25 files, and the published dist run passes 192 tests across 22 files. Patch-relevant coverage now executes the connected and pre-connect branches for both server and client volatile acknowledgement paths. TypeScript, ESLint, Prettier, coverage, conformance drift, build, smoke, external-import inspection, publint, and Are the Types Wrong all pass.
Related issue
Closes #232
Checklist