}
private async requestHandler(messageEvent: MessageEvent): Promise<void> {
- if (this.isResponse(messageEvent.data) === true) {
+ const validatedMessageEvent = this.validateMessageEvent(messageEvent);
+ if (validatedMessageEvent === false) {
return;
}
- const [uuid, command, requestPayload] = this.validateMessageEvent(messageEvent)
- .data as BroadcastChannelRequest;
+ if (this.isResponse(validatedMessageEvent.data) === true) {
+ return;
+ }
+ const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest;
if (
requestPayload?.hashIds !== undefined &&
}
private responseHandler(messageEvent: MessageEvent): void {
- if (this.isRequest(messageEvent.data) === true) {
+ const validatedMessageEvent = this.validateMessageEvent(messageEvent);
+ if (validatedMessageEvent === false) {
return;
}
- const [uuid, responsePayload] = this.validateMessageEvent(messageEvent)
- .data as BroadcastChannelResponse;
+ if (this.isRequest(validatedMessageEvent.data) === true) {
+ return;
+ }
+ const [uuid, responsePayload] = validatedMessageEvent.data as BroadcastChannelResponse;
if (this.responses.has(uuid) === false) {
this.responses.set(uuid, {
responsesExpected: this.uiService.getBroadcastChannelExpectedResponses(uuid),
import { BroadcastChannel } from 'worker_threads';
-import BaseError from '../exception/BaseError';
+import * as uuid from 'uuid';
+
import type { JsonType } from '../types/JsonType';
import type {
BroadcastChannelRequest,
BroadcastChannelResponse,
MessageEvent,
} from '../types/WorkerBroadcastChannel';
+import logger from '../utils/Logger';
+import Utils from '../utils/Utils';
+
+const moduleName = 'WorkerBroadcastChannel';
export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
protected constructor() {
return Array.isArray(message) && message.length === 2;
}
- protected validateMessageEvent(messageEvent: MessageEvent): MessageEvent {
+ protected validateMessageEvent(messageEvent: MessageEvent): MessageEvent | false {
if (Array.isArray(messageEvent.data) === false) {
- throw new BaseError('Worker broadcast channel protocol message event data is not an array');
+ logger.error(
+ this.logPrefix(moduleName, 'validateMessageEvent') +
+ ' Worker broadcast channel protocol message event data is not an array'
+ );
+ return false;
+ }
+ if (uuid.validate(messageEvent.data[0]) === false) {
+ logger.error(
+ this.logPrefix(moduleName, 'validateMessageEvent') +
+ ' Worker broadcast channel protocol message event data UUID field is invalid'
+ );
+ return false;
}
return messageEvent;
}
+
+ private logPrefix(modName: string, methodName: string): string {
+ return Utils.logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`);
+ }
}
import type internal from 'stream';
import { StatusCodes } from 'http-status-codes';
+import * as uuid from 'uuid';
import WebSocket, { RawData, WebSocketServer } from 'ws';
-import BaseError from '../../exception/BaseError';
import type { UIServerConfiguration } from '../../types/ConfigurationData';
import type { ProtocolRequest, ProtocolResponse } from '../../types/UIProtocol';
import { WebSocketCloseEventStatusCode } from '../../types/WebSocket';
this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
}
ws.on('message', (rawData) => {
- const [messageId, procedureName, payload] = this.validateRawDataRequest(rawData);
+ const request = this.validateRawDataRequest(rawData);
+ if (request === false) {
+ ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
+ return;
+ }
+ const [messageId, procedureName, payload] = request as ProtocolRequest;
this.uiServices
.get(version)
.requestHandler(this.buildProtocolRequest(messageId, procedureName, payload))
}
}
- private validateRawDataRequest(rawData: RawData): ProtocolRequest {
+ private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
// logger.debug(
// `${this.logPrefix(
// moduleName,
const request = JSON.parse(rawData.toString()) as ProtocolRequest;
if (Array.isArray(request) === false) {
- throw new BaseError('UI protocol request is not an array');
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'validateRawDataRequest'
+ )} UI protocol request is not an array:`,
+ request
+ );
+ return false;
}
if (request.length !== 3) {
- throw new BaseError('UI protocol request is malformed');
+ logger.error(
+ `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
+ request
+ );
+ return false;
+ }
+
+ if (uuid.validate(request[0]) === false) {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'validateRawDataRequest'
+ )} UI protocol request UUID field is invalid:`,
+ request
+ );
+ return false;
}
return request;