-
Notifications
You must be signed in to change notification settings - Fork 575
fix(connection): block reserved internal event types from server messages #3340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,21 @@ const SECONDS_PER_MINUTE = 60 | |||||||||||||
| const TIMEOUT = 20 | ||||||||||||||
| const CONNECTION_TIMEOUT = 5 | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Event names that are emitted internally by Connection to signal local | ||||||||||||||
| * client/socket state transitions. These must never be emitted as a result | ||||||||||||||
| * of a server-supplied `data.type`, otherwise a malicious or compromised | ||||||||||||||
| * rippled server could spoof connection state (e.g. claim the client is | ||||||||||||||
| * `connected` when it is not, or inject a fake `error`/`disconnected`/ | ||||||||||||||
| * `reconnect` to drive the client into a bad state). See DGE-6740. | ||||||||||||||
| */ | ||||||||||||||
| const RESERVED_INTERNAL_EVENTS: ReadonlySet<string> = new Set([ | ||||||||||||||
| 'connected', | ||||||||||||||
| 'disconnected', | ||||||||||||||
| 'error', | ||||||||||||||
| 'reconnect', | ||||||||||||||
| ]) | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * ConnectionOptions is the configuration for the Connection class. | ||||||||||||||
| */ | ||||||||||||||
|
|
@@ -354,7 +369,21 @@ export class Connection extends EventEmitter { | |||||||||||||
| } | ||||||||||||||
| if (data.type) { | ||||||||||||||
| // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Should be true | ||||||||||||||
| this.emit(data.type as string, data) | ||||||||||||||
| const type = data.type as string | ||||||||||||||
|
Comment on lines
371
to
+373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 Severity: HIGH No runtime 💡 Fix SuggestionSuggestion: Add a runtime
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achowdhry-ripple This sounds like a legit comment |
||||||||||||||
| // Refuse to forward server-supplied event names that collide with | ||||||||||||||
| // Connection's internal state events. Otherwise a rogue server could | ||||||||||||||
| // spoof local connection state by sending e.g. `{"type":"connected"}` | ||||||||||||||
| // or `{"type":"error"}`. See DGE-6740. | ||||||||||||||
|
kuan121 marked this conversation as resolved.
Outdated
|
||||||||||||||
| if (RESERVED_INTERNAL_EVENTS.has(type)) { | ||||||||||||||
| this.emit( | ||||||||||||||
| 'error', | ||||||||||||||
| 'badMessage', | ||||||||||||||
| `Server message used reserved internal event type "${type}"; dropping.`, | ||||||||||||||
| message, | ||||||||||||||
| ) | ||||||||||||||
| return | ||||||||||||||
| } | ||||||||||||||
| this.emit(type, data) | ||||||||||||||
| } | ||||||||||||||
| if (data.type === 'response') { | ||||||||||||||
| try { | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -903,6 +903,97 @@ describe('Connection', function () { | |
| TIMEOUT, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. browser tests are failing |
||
| ) | ||
|
|
||
| // DGE-6740: Server messages whose `type` collides with a reserved internal | ||
|
kuan121 marked this conversation as resolved.
Outdated
|
||
| // event name (`connected`, `disconnected`, `error`, `reconnect`) must not | ||
| // be forwarded as that internal event. Otherwise a rogue server could spoof | ||
| // connection state. | ||
| it.each(['connected', 'disconnected', 'reconnect'])( | ||
| 'drops server message with reserved internal event type "%s"', | ||
| async (reservedType) => { | ||
| // If the fix regresses, the reserved listener will fire and we'll fail. | ||
| const spoofTrigger = new Promise<never>((_resolve, reject) => { | ||
| clientContext.client.connection.on(reservedType, () => { | ||
| reject( | ||
| new XrplError( | ||
| `Reserved internal event "${reservedType}" was emitted from a server message`, | ||
| ), | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| const errorReceived = new Promise<void>((resolve) => { | ||
| clientContext.client.connection.on( | ||
| 'error', | ||
| (errorCode, errorMessage) => { | ||
| if ( | ||
| errorCode === 'badMessage' && | ||
| typeof errorMessage === 'string' && | ||
| errorMessage.includes(reservedType) | ||
| ) { | ||
| resolve() | ||
| } | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| // @ts-expect-error -- Testing private member | ||
| clientContext.client.connection.onMessage( | ||
| JSON.stringify({ type: reservedType }), | ||
| ) | ||
|
|
||
| await Promise.race([errorReceived, spoofTrigger]) | ||
| }, | ||
| TIMEOUT, | ||
| ) | ||
|
|
||
| // DGE-6740: A spoofed `{"type":"error"}` payload must not be forwarded to | ||
|
kuan121 marked this conversation as resolved.
Outdated
|
||
| // 'error' listeners as the raw payload. Instead the fix emits an 'error' | ||
| // event whose first arg is the string code 'badMessage'. We verify the | ||
| // 'error' listener never receives the raw spoofed object. | ||
| it( | ||
| 'drops server message with reserved internal event type "error"', | ||
| async () => { | ||
|
kuan121 marked this conversation as resolved.
Outdated
|
||
| const spoofedPayload = { type: 'error', error: 'spoof' } | ||
|
|
||
| const result = new Promise<void>((resolve, reject) => { | ||
| clientContext.client.connection.on( | ||
| 'error', | ||
| (errorCodeOrPayload, errorMessage) => { | ||
| // The fix's emission: emit('error', 'badMessage', '<msg>', rawJson). | ||
| if ( | ||
| errorCodeOrPayload === 'badMessage' && | ||
| typeof errorMessage === 'string' && | ||
| errorMessage.includes('error') | ||
| ) { | ||
| resolve() | ||
| return | ||
| } | ||
| // Anything that looks like the spoofed payload object indicates | ||
| // the raw server message reached 'error' listeners, which is the | ||
| // exact bug being fixed. | ||
| if ( | ||
| errorCodeOrPayload != null && | ||
| typeof errorCodeOrPayload === 'object' && | ||
| (errorCodeOrPayload as { type?: unknown }).type === 'error' | ||
| ) { | ||
| reject( | ||
| new XrplError( | ||
| 'Spoofed server payload was forwarded to error listeners', | ||
| ), | ||
| ) | ||
| } | ||
| }, | ||
| ) | ||
| }) | ||
|
|
||
| // @ts-expect-error -- Testing private member | ||
| clientContext.client.connection.onMessage(JSON.stringify(spoofedPayload)) | ||
|
|
||
| await result | ||
| }, | ||
| TIMEOUT, | ||
| ) | ||
|
|
||
| // it('should clean up websocket connection if error after websocket is opened', async function () { | ||
| // await clientContext.client.disconnect() | ||
| // // fail on connection | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.