Skip to content

Commit 6222f2f

Browse files
committed
net: reject keep-alive delays that cannot be applied
The keep-alive delays are given in milliseconds but the underlying socket options are configured in whole seconds, so a positive value below 1000 ms rounds down to 0. That leaves the system default in place instead of applying the requested timing, and there is nothing to indicate that the value had no effect. uv_tcp_keepalive() already rejects a delay outside [1, 32767] seconds, so the value is treated as invalid one layer down. Throw ERR_OUT_OF_RANGE for a positive initialDelay or interval that cannot be applied as requested, covering both the truncation to zero and the upper bound the socket options can carry. A non-positive value keeps its documented meaning of leaving the current setting unchanged, and Infinity is accepted as "no timeout" by callers such as Agent. Three existing tests passed delays that were silently ignored; test-async-hooks-http-parser-destroy had never configured keep-alive at all despite asking for it. Refs: nodejs#57712 Signed-off-by: Avocado <[email protected]>
1 parent 4b5e86c commit 6222f2f

6 files changed

Lines changed: 124 additions & 5 deletions

File tree

‎doc/api/net.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,11 @@ corresponding system default unchanged.
16691669

16701670
`initialDelay` and `interval` are specified in milliseconds but the
16711671
underlying socket options are configured in whole seconds; the values are
1672-
divided by `1000` and rounded down before being applied.
1672+
divided by `1000` and rounded down before being applied. Sub-second timings
1673+
cannot be expressed, so a positive value below `1000` throws
1674+
[`ERR_OUT_OF_RANGE`][] rather than leaving the corresponding system default in
1675+
place. The largest delay the socket options can carry is `32767` seconds, and a
1676+
value above that throws as well.
16731677

16741678
Enabling the keep-alive functionality will set the following socket options:
16751679

@@ -1688,6 +1692,12 @@ those platforms.
16881692
added:
16891693
- v26.4.0
16901694
- v24.19.0
1695+
changes:
1696+
- version: REPLACEME
1697+
pr-url: https://github.com/nodejs/node/pull/65528
1698+
description: A positive `initialDelay` or `interval` that cannot be applied
1699+
as requested now throws `ERR_OUT_OF_RANGE` instead of being
1700+
silently altered.
16911701
-->
16921702

16931703
* `options` {Object}
@@ -1709,6 +1719,11 @@ socket.setKeepAlive({ enable: true, initialDelay: 1000, interval: 1000, count: 1
17091719
<!-- YAML
17101720
added: v0.1.92
17111721
changes:
1722+
- version: REPLACEME
1723+
pr-url: https://github.com/nodejs/node/pull/65528
1724+
description: A positive `initialDelay` or `interval` that cannot be applied
1725+
as requested now throws `ERR_OUT_OF_RANGE` instead of being
1726+
silently altered.
17121727
- version:
17131728
- v26.4.0
17141729
- v24.19.0
@@ -2590,6 +2605,7 @@ console.log('listening on', server.address().port);
25902605
[`'timeout'`]: #event-timeout
25912606
[`BoundSocket`]: #class-netboundsocket
25922607
[`ERR_INVALID_ARG_VALUE`]: errors.md#err_invalid_arg_value
2608+
[`ERR_OUT_OF_RANGE`]: errors.md#err_out_of_range
25932609
[`ERR_SOCKET_HANDLE_ADOPTED`]: errors.md#err_socket_handle_adopted
25942610
[`EventEmitter`]: events.md#class-eventemitter
25952611
[`child_process.fork()`]: child_process.md#child_processforkmodulepath-args-options

‎lib/net.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ const { kTimeout } = require('internal/timers');
169169
const DEFAULT_IPV4_ADDR = '0.0.0.0';
170170
const DEFAULT_IPV6_ADDR = '::';
171171

172+
// uv_tcp_keepalive() takes its delays in seconds and rejects anything above
173+
// 32767, the largest value the socket options can carry.
174+
const KEEP_ALIVE_MAX_DELAY_MSECS = 32767 * 1000;
175+
172176
const noop = () => {};
173177

174178
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
@@ -847,6 +851,20 @@ Socket.prototype.setNoDelay = function(enable) {
847851
};
848852

849853

854+
// The underlying socket options are configured in whole seconds, so a positive
855+
// value below 1000 ms would be truncated to 0 seconds and leave the system
856+
// default in place instead of applying the requested timing. Reject it rather
857+
// than applying something other than what was asked for. A non-positive value
858+
// keeps its documented meaning of leaving the current setting unchanged.
859+
// Agent passes Infinity to mean "no timeout", which also truncates to 0; it is
860+
// left alone here rather than given a new meaning.
861+
function validateKeepAliveDelay(msecs, name) {
862+
if (msecs === undefined || msecs <= 0 || msecs === Infinity) {
863+
return;
864+
}
865+
validateNumber(msecs, name, 1000, KEEP_ALIVE_MAX_DELAY_MSECS);
866+
}
867+
850868
Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs,
851869
intervalMsecs, count) {
852870
if (enable !== null && typeof enable === 'object') {
@@ -861,6 +879,11 @@ Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs,
861879
const interval = intervalMsecs === undefined ?
862880
undefined : ~~(intervalMsecs / 1000);
863881

882+
if (enable) {
883+
validateKeepAliveDelay(initialDelayMsecs, 'initialDelay');
884+
validateKeepAliveDelay(intervalMsecs, 'interval');
885+
}
886+
864887
if (!this._handle) {
865888
this[kSetKeepAlive] = enable;
866889
this[kSetKeepAliveInitialDelay] = initialDelay;

‎test/parallel/test-async-hooks-http-parser-destroy.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const http = require('http');
99
// parser instances.
1010

1111
const N = 50;
12-
const KEEP_ALIVE = 100;
12+
const KEEP_ALIVE = 1000;
1313

1414
const createdIdsIncomingMessage = [];
1515
const createdIdsClientRequest = [];
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
// The keep-alive delays are given in milliseconds but the underlying socket
8+
// options are configured in whole seconds, and uv_tcp_keepalive() rejects a
9+
// delay outside [1, 32767]. Verifies that a delay which cannot be applied as
10+
// requested is rejected instead of being silently altered.
11+
12+
const MAX_DELAY = 32767 * 1000;
13+
14+
// A positive delay below 1000 ms would be truncated to 0 seconds, which leaves
15+
// the system default in place instead of applying the requested timing.
16+
for (const initialDelay of [1, 400, 999]) {
17+
assert.throws(() => net.Socket.prototype.setKeepAlive.call(
18+
{}, true, initialDelay), {
19+
code: 'ERR_OUT_OF_RANGE',
20+
name: 'RangeError',
21+
message: /The value of "initialDelay" is out of range/,
22+
});
23+
}
24+
25+
// The interval is validated the same way.
26+
assert.throws(() => net.Socket.prototype.setKeepAlive.call(
27+
{}, true, 5000, 500), {
28+
code: 'ERR_OUT_OF_RANGE',
29+
message: /The value of "interval" is out of range/,
30+
});
31+
32+
// A delay above the maximum cannot be carried by the socket options.
33+
assert.throws(() => net.Socket.prototype.setKeepAlive.call(
34+
{}, true, MAX_DELAY + 1000), {
35+
code: 'ERR_OUT_OF_RANGE',
36+
message: /The value of "initialDelay" is out of range/,
37+
});
38+
39+
// The options object form is validated as well.
40+
assert.throws(() => net.Socket.prototype.setKeepAlive.call(
41+
{}, { enable: true, initialDelay: 999 }), {
42+
code: 'ERR_OUT_OF_RANGE',
43+
message: /The value of "initialDelay" is out of range/,
44+
});
45+
46+
// A non-numeric delay is rejected by the same check.
47+
assert.throws(() => net.Socket.prototype.setKeepAlive.call(
48+
{}, true, '1000'), {
49+
code: 'ERR_INVALID_ARG_TYPE',
50+
});
51+
52+
// Delays that can be applied as requested are accepted. A non-positive value
53+
// keeps its documented meaning of leaving the current setting unchanged, and
54+
// so does omitting it entirely.
55+
const server = net.createServer();
56+
server.listen(0, () => {
57+
const client = net.connect({ port: server.address().port }, () => {
58+
for (const args of [
59+
[true, 1000],
60+
[true, MAX_DELAY],
61+
[true, 5000, 1000],
62+
[true, 0],
63+
[true, -1],
64+
[true],
65+
// Agent passes Infinity to mean "no timeout".
66+
[true, Infinity],
67+
[{ enable: true, initialDelay: 1000 }],
68+
// Nothing is configured while keep-alive is disabled, so a delay that
69+
// could not be applied is not rejected either.
70+
[false, 400],
71+
]) {
72+
client.setKeepAlive(...args);
73+
}
74+
75+
client.end();
76+
});
77+
78+
client.on('end', () => server.close());
79+
});

‎test/parallel/test-net-keepalive.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ const echoServer = net.createServer(common.mustCall((connection) => {
3838
}, 1), common.platformTimeout(100));
3939
connection.setTimeout(0);
4040
assert.notStrictEqual(connection.setKeepAlive, undefined);
41-
// Send a keepalive packet after 50 ms
42-
connection.setKeepAlive(true, common.platformTimeout(50));
41+
// Send a keepalive packet after 1 second. Sub-second delays cannot be
42+
// expressed by the underlying socket options.
43+
connection.setKeepAlive(true, 1000);
4344
connection.on('end', function() {
4445
connection.end();
4546
});

‎test/parallel/test-net-persistent-keepalive.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ echoServer.on('listening', common.mustCall(function() {
2727
clientConnection = new net.Socket();
2828
// Send a keepalive packet after 1000 ms
2929
// and make sure it persists
30-
const s = clientConnection.setKeepAlive(true, 400);
30+
const s = clientConnection.setKeepAlive(true, 1000);
3131
assert.ok(s instanceof net.Socket);
3232
clientConnection.connect(this.address().port);
3333
clientConnection.setTimeout(0);

0 commit comments

Comments
 (0)