Skip to content

Commit c9fe195

Browse files
committed
fix(httphandshake): added validation for supported websocket version
1 parent 06b41a2 commit c9fe195

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/libs/custom_lib/validateHttpHandshake.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ class SecWebSocketKeyValidator implements RequestValidator {
9393
}
9494
}
9595

96+
class SecWebSocketVersionValidator implements RequestValidator {
97+
private supportedVersions: number[];
98+
99+
constructor(supportedVersions: number[] = [13]) {
100+
this.supportedVersions = supportedVersions;
101+
}
102+
103+
validate(request: http.IncomingMessage): ValidationResult {
104+
const version = request.headers["sec-websocket-version"];
105+
const isValid =
106+
version !== undefined &&
107+
this.supportedVersions.includes(parseInt(version as string, 10));
108+
109+
return {
110+
isValid,
111+
errors: !isValid
112+
? [
113+
`Unsupported WebSocket version. Required: ${this.supportedVersions.join(", ")}, Got: ${version}`,
114+
]
115+
: [],
116+
};
117+
}
118+
}
119+
96120
class CompositeRequestValidator implements RequestValidator {
97121
private validators: RequestValidator[] = [];
98122

@@ -127,7 +151,8 @@ export default class UpgradeValidatorFactory {
127151
.addValidator(new ConnectionHeaderValidator(config.connectionHeader))
128152
.addValidator(new MethodValidator(config.method))
129153
.addValidator(new OriginValidator(config.allowedOrigins))
130-
.addValidator(new SecWebSocketKeyValidator());
154+
.addValidator(new SecWebSocketKeyValidator())
155+
.addValidator(new SecWebSocketVersionValidator([13]));
131156

132157
return validator;
133158
}

0 commit comments

Comments
 (0)