Skip to content

Commit 3144d27

Browse files
authored
fix(uws): discard any write to an aborted uWS response (#682)
This bug only exists for polling transport connections running on top of uWS. If the remote client abruptly disconnects (thus aborting the request) while the server is waiting on an asynchronous operation such as compression, the server may attempt to write a response via the aborted response object. This causes an uncaught exception to be thrown.
1 parent 7bd7775 commit 3144d27

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/userver.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export class uServer extends BaseServer {
258258
class ResponseWrapper {
259259
private statusWritten: boolean = false;
260260
private headers = [];
261+
private isAborted = false;
261262

262263
constructor(readonly res: HttpResponse) {}
263264

@@ -291,13 +292,17 @@ class ResponseWrapper {
291292
public getHeader() {}
292293

293294
public writeStatus(status: string) {
295+
if (this.isAborted) return;
296+
294297
this.res.writeStatus(status);
295298
this.statusWritten = true;
296299
this.writeBufferedHeaders();
297300
return this;
298301
}
299302

300303
public writeHeader(key: string, value: string) {
304+
if (this.isAborted) return;
305+
301306
if (key === "Content-Length") {
302307
// the content length is automatically added by uWebSockets.js
303308
return;
@@ -316,6 +321,8 @@ class ResponseWrapper {
316321
}
317322

318323
public end(data) {
324+
if (this.isAborted) return;
325+
319326
if (!this.statusWritten) {
320327
// status will be inferred as "200 OK"
321328
this.writeBufferedHeaders();
@@ -324,10 +331,18 @@ class ResponseWrapper {
324331
}
325332

326333
public onData(fn) {
334+
if (this.isAborted) return;
335+
327336
this.res.onData(fn);
328337
}
329338

330339
public onAborted(fn) {
331-
this.res.onAborted(fn);
340+
if (this.isAborted) return;
341+
342+
this.res.onAborted(() => {
343+
// Any attempt to use the UWS response object after abort will throw!
344+
this.isAborted = true;
345+
fn();
346+
});
332347
}
333348
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy