Skip to content
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

fix: the server could crash if a client sends invalid frames #293

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/models/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MyWebSocket } from "../services/webSocketServer/webSocket";
import type WebSocket from "ws";

export interface IClient {
getId(): string;

getToken(): string;

getSocket(): MyWebSocket | null;
getSocket(): WebSocket | null;

setSocket(socket: MyWebSocket | null): void;
setSocket(socket: WebSocket | null): void;

getLastPing(): number;

Expand All @@ -19,7 +19,7 @@ export interface IClient {
export class Client implements IClient {
private readonly id: string;
private readonly token: string;
private socket: MyWebSocket | null = null;
private socket: WebSocket | null = null;
private lastPing: number = new Date().getTime();

constructor({ id, token }: { id: string; token: string; }) {
Expand All @@ -35,11 +35,11 @@ export class Client implements IClient {
return this.token;
}

public getSocket(): MyWebSocket | null {
public getSocket(): WebSocket | null {
return this.socket;
}

public setSocket(socket: MyWebSocket | null): void {
public setSocket(socket: WebSocket | null): void {
this.socket = socket;
}

Expand Down
15 changes: 9 additions & 6 deletions src/services/webSocketServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IConfig } from "../../config";
import { Errors, MessageType } from "../../enums";
import { Client, IClient } from "../../models/client";
import { IRealm } from "../../models/realm";
import { MyWebSocket } from "./webSocket";
import type WebSocket from "ws";

export interface IWebSocketServer extends EventEmitter {
readonly path: string;
Expand Down Expand Up @@ -42,11 +42,14 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {

this.socketServer = new WebSocketLib.Server({ path: this.path, server });

this.socketServer.on("connection", (socket: MyWebSocket, req) => this._onSocketConnection(socket, req));
this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req));
this.socketServer.on("error", (error: Error) => this._onSocketError(error));
}

private _onSocketConnection(socket: MyWebSocket, req: IncomingMessage): void {
private _onSocketConnection(socket: WebSocket, req: IncomingMessage): void {
// An unhandled socket error might crash the server. Handle it first.
socket.on("error", error => this._onSocketError(error))

const { query = {} } = url.parse(req.url ?? '', true);

const { id, token, key }: IAuthParams = query;
Expand Down Expand Up @@ -85,7 +88,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {

private _registerClient({ socket, id, token }:
{
socket: MyWebSocket;
socket: WebSocket;
id: string;
token: string;
}): void {
Expand All @@ -103,7 +106,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
this._configureWS(socket, newClient);
}

private _configureWS(socket: MyWebSocket, client: IClient): void {
private _configureWS(socket: WebSocket, client: IClient): void {
client.setSocket(socket);

// Cleanup after a socket closes.
Expand All @@ -130,7 +133,7 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
this.emit("connection", client);
}

private _sendErrorAndClose(socket: MyWebSocket, msg: Errors): void {
private _sendErrorAndClose(socket: WebSocket, msg: Errors): void {
socket.send(
JSON.stringify({
type: MessageType.ERROR,
Expand Down
4 changes: 0 additions & 4 deletions src/services/webSocketServer/webSocket.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/messageHandler/handlers/transmission/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Client } from '../../../../src/models/client';
import { TransmissionHandler } from '../../../../src/messageHandler/handlers';
import { Realm } from '../../../../src/models/realm';
import { MessageType } from '../../../../src/enums';
import { MyWebSocket } from '../../../../src/services/webSocketServer/webSocket';
import type WebSocket from "ws";

const createFakeSocket = (): MyWebSocket => {
const createFakeSocket = (): WebSocket => {
/* eslint-disable @typescript-eslint/no-empty-function */
const sock = {
send: (): void => { },
Expand All @@ -14,7 +14,7 @@ const createFakeSocket = (): MyWebSocket => {
};
/* eslint-enable @typescript-eslint/no-empty-function */

return (sock as unknown as MyWebSocket);
return (sock as unknown as WebSocket);
};

describe('Transmission handler', () => {
Expand Down