tr-apisrv 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +4 -1
- package/index.js +29 -17
- package/package.json +1 -1
- package/test/test.js +21 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Changelog
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
3.1.0
|
|
5
|
+
-----
|
|
6
|
+
|
|
7
|
+
Tested on Node.js 18, 20, 22, 24 and 26.
|
|
8
|
+
|
|
9
|
+
### Changed behaviour
|
|
10
|
+
|
|
11
|
+
- `maxBodySize: 0` now allows only empty bodies, as documented.
|
|
12
|
+
Before, 0 disabled the limit.
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- A request rejected because of its body (413 too large, 400 longer
|
|
17
|
+
than Content-Length) had its connection destroyed right after the
|
|
18
|
+
response was queued, so clients could get a reset instead of the
|
|
19
|
+
response (seen on Node 20 and 22). The response is now sent with
|
|
20
|
+
`Connection: close` and the connection is closed only after it has
|
|
21
|
+
been written. If an `errorCallback` takes over and never responds,
|
|
22
|
+
the connection is closed after `bodyReadTimeoutMs`.
|
package/README.md
CHANGED
|
@@ -262,7 +262,10 @@ path. Later sources override earlier ones in `request.params`. When this occurs,
|
|
|
262
262
|
`application/x-www-form-urlencoded`. Other types result in
|
|
263
263
|
`400 Bad Request (POST or PUT body must be in JSON or www-form-urlencoded format.)`.
|
|
264
264
|
* `GET` and `DELETE` requests must not include a body.
|
|
265
|
-
* Bodies larger than `maxBodySize` yield `413 Payload Too Large
|
|
265
|
+
* Bodies larger than `maxBodySize` yield `413 Payload Too Large`; with
|
|
266
|
+
`maxBodySize: 0` any non-empty body does. The response to a rejected
|
|
267
|
+
body is sent with `Connection: close` and the connection is closed
|
|
268
|
+
after it.
|
|
266
269
|
* Slow clients that exceed `bodyReadTimeoutMs` cause `408 Request Timeout`.
|
|
267
270
|
|
|
268
271
|
## Path rewriting
|
package/index.js
CHANGED
|
@@ -734,6 +734,30 @@ class ApiSrv {
|
|
|
734
734
|
var declaredContentLength;
|
|
735
735
|
// What an errorCallback is told about an error of this request.
|
|
736
736
|
const ctx = (source, extra) => Object.assign({ source, req, res, request: r }, extra);
|
|
737
|
+
// Reject a request whose body is not read. The connection is
|
|
738
|
+
// closed, but only once the response has been written:
|
|
739
|
+
// destroying it at once could lose the response.
|
|
740
|
+
const rejectBody = (code, text, source) => {
|
|
741
|
+
const socket = req.socket;
|
|
742
|
+
const kill = () => {
|
|
743
|
+
if (socket && !socket.destroyed) {
|
|
744
|
+
socket.destroy();
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
// Also when an errorCallback never responds.
|
|
748
|
+
const fallback = setTimeout(kill, this.bodyReadTimeoutMs);
|
|
749
|
+
fallback.unref();
|
|
750
|
+
res.once('finish', () => {
|
|
751
|
+
clearTimeout(fallback);
|
|
752
|
+
if (socket && !socket.destroyed) {
|
|
753
|
+
socket.end();
|
|
754
|
+
setTimeout(kill, 1000).unref();
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
res.once('close', () => clearTimeout(fallback));
|
|
758
|
+
res.setHeader('Connection', 'close');
|
|
759
|
+
error(res, code, text, undefined, ctx(source));
|
|
760
|
+
};
|
|
737
761
|
if (req.headers['transfer-encoding'] && req.headers['content-length']) {
|
|
738
762
|
error(res, 400, 'Both Transfer-Encoding and Content-Length defined.', undefined, ctx('request'));
|
|
739
763
|
return;
|
|
@@ -745,12 +769,8 @@ class ApiSrv {
|
|
|
745
769
|
return;
|
|
746
770
|
}
|
|
747
771
|
declaredContentLength = cl;
|
|
748
|
-
if (
|
|
749
|
-
|
|
750
|
-
try {
|
|
751
|
-
req.destroy();
|
|
752
|
-
} catch (ignored) {
|
|
753
|
-
}
|
|
772
|
+
if (cl > this.maxBodySize) {
|
|
773
|
+
rejectBody(413, 'Request body too large.', 'limit');
|
|
754
774
|
return;
|
|
755
775
|
}
|
|
756
776
|
}
|
|
@@ -765,24 +785,16 @@ class ApiSrv {
|
|
|
765
785
|
clearTimeout(timeout);
|
|
766
786
|
timeout = undefined;
|
|
767
787
|
}
|
|
768
|
-
|
|
769
|
-
try {
|
|
770
|
-
req.destroy();
|
|
771
|
-
} catch (ignored) {
|
|
772
|
-
}
|
|
788
|
+
rejectBody(400, 'Request body longer than Content-Length.', 'request');
|
|
773
789
|
return;
|
|
774
790
|
}
|
|
775
|
-
if (
|
|
791
|
+
if (bodySize > this.maxBodySize) {
|
|
776
792
|
completed = true;
|
|
777
793
|
if (timeout) {
|
|
778
794
|
clearTimeout(timeout);
|
|
779
795
|
timeout = undefined;
|
|
780
796
|
}
|
|
781
|
-
|
|
782
|
-
try {
|
|
783
|
-
req.destroy();
|
|
784
|
-
} catch (ignored) {
|
|
785
|
-
}
|
|
797
|
+
rejectBody(413, 'Request body too large.', 'limit');
|
|
786
798
|
return;
|
|
787
799
|
}
|
|
788
800
|
chunks.push(data);
|
package/package.json
CHANGED
package/test/test.js
CHANGED
|
@@ -1148,6 +1148,26 @@ test('rejects requests exceeding Content-Length limit without reading body', asy
|
|
|
1148
1148
|
}
|
|
1149
1149
|
});
|
|
1150
1150
|
|
|
1151
|
+
test('maxBodySize 0 allows only empty bodies', async () => {
|
|
1152
|
+
const port = 12422;
|
|
1153
|
+
const srv = new ApiSrv({
|
|
1154
|
+
port,
|
|
1155
|
+
maxBodySize: 0,
|
|
1156
|
+
callback: (r) => r.jsonResponse({ ok: true })
|
|
1157
|
+
});
|
|
1158
|
+
try {
|
|
1159
|
+
assert.strictEqual(await rawRequest(port,
|
|
1160
|
+
'POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}'), 413);
|
|
1161
|
+
assert.strictEqual(await rawRequest(port,
|
|
1162
|
+
'POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\nTransfer-Encoding: chunked\r\n\r\n2\r\n{}\r\n0\r\n\r\n'), 413);
|
|
1163
|
+
assert.strictEqual(await rawRequest(port,
|
|
1164
|
+
'POST / HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 0\r\nConnection: close\r\n\r\n'), 200);
|
|
1165
|
+
assert.strictEqual((await httpRequest(port, { path: '/?a=b' })).status, 200);
|
|
1166
|
+
} finally {
|
|
1167
|
+
await srv.shutdown();
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
|
|
1151
1171
|
test('enforces Content-Length to match body length', async () => {
|
|
1152
1172
|
const port = 12358;
|
|
1153
1173
|
const srv = new ApiSrv({
|
|
@@ -1500,6 +1520,7 @@ test('errorCallback handles body size and unsupported method errors', async () =
|
|
|
1500
1520
|
});
|
|
1501
1521
|
assert.strictEqual(res.status, 413);
|
|
1502
1522
|
assert.deepStrictEqual(JSON.parse(res.data), { custom: 413 });
|
|
1523
|
+
assert.strictEqual(res.headers.connection, 'close');
|
|
1503
1524
|
res = await httpRequest(port, { method: 'PATCH', path: '/' });
|
|
1504
1525
|
assert.strictEqual(res.status, 405);
|
|
1505
1526
|
assert.deepStrictEqual(JSON.parse(res.data), { custom: 405 });
|