Skip to content

Commit

Permalink
Fixes #1017 Logs an error and closes connection when invalid frame re…
Browse files Browse the repository at this point in the history
…ceived
  • Loading branch information
yasserf committed Oct 21, 2019
1 parent f99b62f commit bb05335
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
29 changes: 29 additions & 0 deletions conf/config.yml
Expand Up @@ -63,6 +63,14 @@ httpServer:
headers:
- user-agent

# type: uws
# options:
# # url path for http health-checks, GET requests to this path will return 200 if deepstream is alive
# healthCheckPath: /health-check
# # Headers to copy over from websocket
# headers:
# - user-agent

# Connection Endpoint Configuration
# to disable, replace configuration with null eg. `http: null`
connectionEndpoints:
Expand Down Expand Up @@ -106,6 +114,27 @@ connectionEndpoints:
# maximum allowed size of an individual message in bytes
maxMessageSize: 1048576

- type: ws-json
options:
# url path websocket connections connect to
urlPath: /deepstream-json
# the amount of milliseconds between each ping/heartbeat message
heartbeatInterval: 30000
# the amount of milliseconds that writes to sockets are buffered
outgoingBufferTimeout: 10
# the maximum amount of bytes to buffer before flushing, stops the client from large enough packages
# to block its responsiveness
maxBufferByteSize: 100000

# Security
# amount of time a connection can remain open while not being logged in
unauthenticatedClientTimeout: 180000
# invalid login attempts before the connection is cut
maxAuthAttempts: 3
# maximum allowed size of an individual message in bytes
maxMessageSize: 1048576


- type: http
options:
# allow 'authData' parameter in POST requests, if disabled only token and OPEN auth is
Expand Down
2 changes: 1 addition & 1 deletion ds-types
5 changes: 5 additions & 0 deletions src/connection-endpoint/base/socket-wrapper.ts
Expand Up @@ -32,6 +32,11 @@ export abstract class WSSocketWrapper<SerializedType extends { length: number }>
return this.isClosed !== true
}

protected invalidTypeReceived () {
this.services.logger.error(EVENT.ERROR, `Received an invalid message type on ${this.uuid}`)
this.destroy()
}

/**
* Called by the connection endpoint to flush all buffered writes.
* A buffered write is a write that is not a high priority, such as an ack
Expand Down
Expand Up @@ -15,6 +15,11 @@ export class WSBinarySocketWrapper extends WSSocketWrapper<Uint8Array> {
}

public parseMessage (message: ArrayBuffer): ParseResult[] {
if (typeof message === 'string') {
this.invalidTypeReceived()
return []
}

/* we copy the underlying buffer (since a shallow reference won't be safe
* outside of the callback)
* the copy could be avoided if we make sure not to store references to the
Expand Down
12 changes: 11 additions & 1 deletion src/connection-endpoint/websocket/json/socket-wrapper-factory.ts
Expand Up @@ -13,7 +13,17 @@ export class JSONSocketWrapper extends WSSocketWrapper<string> {
}

public parseMessage (message: string): ParseResult[] {
return [JSON.parse(message)]
if (typeof message !== 'string') {
this.invalidTypeReceived()
return []
}

try {
return [JSON.parse(message)]
} catch (e) {
this.invalidTypeReceived()
return []
}
}

public parseData (message: Message): true | Error {
Expand Down
Expand Up @@ -15,6 +15,11 @@ export class TextWSSocketWrapper extends WSSocketWrapper<string> {
}

public parseMessage (message: string): ParseResult[] {
if (typeof message !== 'string') {
this.invalidTypeReceived()
return []
}

return textMessageParse.parse(message)
}

Expand Down

0 comments on commit bb05335

Please sign in to comment.