Skip to content

Commit bfae6df

Browse files
authored
fix: don't crash server when a non HTTP method was provided (#236)
1 parent 6a47bf4 commit bfae6df

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

packages/handlersjs-http/lib/servers/node/node-http-request-response.handler.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ describe('NodeHttpRequestResponseHandler', () => {
120120

121121
});
122122

123+
it('should return 405 when request method is not an HTTP method', async () => {
124+
125+
// This is a valid header in the WebDAV protocol
126+
streamMock.requestStream.method = 'PROPFIND';
127+
await lastValueFrom(handler.handle(streamMock));
128+
129+
expect(streamMock.responseStream.writeHead).toHaveBeenCalledWith(
130+
405,
131+
'Only HTTP methods are allowed!',
132+
);
133+
134+
});
135+
123136
it('throws when headers is null/undefined', async () => {
124137

125138
streamMock.requestStream.headers = null;

packages/handlersjs-http/lib/servers/node/node-http-request-response.handler.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
110110

111111
if (!nodeHttpStreams.requestStream) {
112112

113+
// No request was received, this path is technically impossible to reach
113114
this.logger.error('No request stream received', { nodeHttpStreams });
114115

115116
return throwError(() => new Error('request stream cannot be null or undefined.'));
@@ -118,6 +119,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
118119

119120
if (!nodeHttpStreams.requestStream.headers) {
120121

122+
// No request headers were received, this path is technically impossible to reach
121123
this.logger.error('No request headers received', { requestStream: nodeHttpStreams.requestStream });
122124

123125
return throwError(() => new Error('headers of the request cannot be null or undefined.'));
@@ -138,6 +140,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
138140

139141
if (!nodeHttpStreams.responseStream) {
140142

143+
// No response was received, this path is technically impossible to reach
141144
this.logger.error('No response stream received', { nodeHttpStreams });
142145

143146
return throwError(() => new Error('response stream cannot be null or undefined.'));
@@ -148,19 +151,36 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
148151

149152
if (!url) {
150153

154+
// No request url was received, this path is technically impossible to reach
151155
this.logger.warn('No url received', { requestStream: nodeHttpStreams.requestStream });
152156

153157
return throwError(() => new Error('url of the request cannot be null or undefined.'));
154158

155159
}
156160

161+
// Check if the request method is an HTTP method + this ensures typing throughout the file
157162
const method = Object.values(HttpMethods).find((m) => m === nodeHttpStreams.requestStream.method);
158163

159164
if (!method) {
160165

161-
this.logger.warn('No method received', { requestStream: nodeHttpStreams.requestStream });
166+
if (nodeHttpStreams.requestStream.method) {
162167

163-
return throwError(() => new Error('method of the request cannot be null or undefined.'));
168+
// An unsupported method was received
169+
this.logger.warn('Invalid method received', { method: nodeHttpStreams.requestStream.method });
170+
this.logger.clearVariables();
171+
nodeHttpStreams.responseStream.writeHead(405, 'Only HTTP methods are allowed!');
172+
nodeHttpStreams.responseStream.end();
173+
174+
return of(void 0);
175+
176+
} else {
177+
178+
// No request method was received, this path is technically impossible to reach
179+
this.logger.warn('No method received', { requestStream: nodeHttpStreams.requestStream });
180+
181+
return throwError(() => new Error('method of the request cannot be null or undefined.'));
182+
183+
}
164184

165185
}
166186

packages/handlersjs-http/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"coverageThreshold": {
8686
"global": {
8787
"statements": 99.79,
88-
"branches": 99.1,
88+
"branches": 99.11,
8989
"functions": 99.15,
9090
"lines": 100
9191
}
@@ -99,4 +99,4 @@
9999
"<rootDir>/lib/main.ts"
100100
]
101101
}
102-
}
102+
}

0 commit comments

Comments
 (0)