bdy 1.26.10-stage → 1.26.11-dev

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.
Files changed (39) hide show
  1. package/distTs/package.json +2 -2
  2. package/distTs/src/command/agent/selftest.js +2 -3
  3. package/distTs/src/index.js +0 -8
  4. package/distTs/src/sshChannelWindow.js +45 -124
  5. package/distTs/src/sshKex.js +32 -0
  6. package/distTs/src/sshKeygen.js +1 -10
  7. package/distTs/src/tunnel/server/sftp.js +0 -24
  8. package/distTs/src/tunnel/server/ssh.js +4 -2
  9. package/distTs/src/tunnel/ssh/client.js +11 -12
  10. package/node_modules/ssh2/.github/workflows/ci.yml +12 -18
  11. package/node_modules/ssh2/.github/workflows/lint.yml +2 -2
  12. package/node_modules/ssh2/README.md +30 -8
  13. package/node_modules/ssh2/lib/Channel.js +38 -3
  14. package/node_modules/ssh2/lib/agent.js +3 -101
  15. package/node_modules/ssh2/lib/client.js +30 -10
  16. package/node_modules/ssh2/lib/keygen.js +6 -7
  17. package/node_modules/ssh2/lib/protocol/Protocol.js +3 -1
  18. package/node_modules/ssh2/lib/protocol/SFTP.js +8 -0
  19. package/node_modules/ssh2/lib/protocol/constants.js +21 -0
  20. package/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +33 -32
  21. package/node_modules/ssh2/lib/protocol/kex.js +122 -0
  22. package/node_modules/ssh2/lib/protocol/zlib.js +11 -1
  23. package/node_modules/ssh2/lib/server.js +57 -12
  24. package/node_modules/ssh2/package.json +9 -6
  25. package/node_modules/ssh2/test/test-channel-window.js +142 -0
  26. package/node_modules/ssh2/test/test-destroy.js +97 -0
  27. package/node_modules/ssh2/test/test-integration-openssh.js +70 -7
  28. package/node_modules/ssh2/test/test-kex-mlkem.js +211 -0
  29. package/node_modules/ssh2/test/test-keygen.js +16 -6
  30. package/node_modules/ssh2/test/test-late-write.js +61 -0
  31. package/node_modules/ssh2/test/test-misc-client-server.js +29 -0
  32. package/node_modules/ssh2/test/test-nodelay.js +54 -0
  33. package/node_modules/ssh2/test/test-protocol-keyparser.js +1 -1
  34. package/node_modules/ssh2/test/test-sftp.js +33 -0
  35. package/node_modules/ssh2/test/test-userauth-signature.js +157 -0
  36. package/package.json +2 -2
  37. package/node_modules/ssh2/util/build_pagent.bat +0 -2
  38. package/node_modules/ssh2/util/pagent.c +0 -88
  39. package/node_modules/ssh2/util/pagent.exe +0 -0
@@ -2,9 +2,23 @@
2
2
 
3
3
  SSH2 client and server modules written in pure JavaScript for [node.js](http://nodejs.org/).
4
4
 
5
- Development/testing is done against OpenSSH (8.7 currently).
5
+ This is `@buddy-works/ssh2`, a fork of [ssh2](https://github.com/mscdex/ssh2) that can be used in its place:
6
+
7
+ ```json
8
+ "ssh2": "npm:@buddy-works/ssh2@1.17.0-pq.1"
9
+ ```
10
+
11
+ The version is the one of ssh2 it is based on, with a suffix of its own. What it adds:
12
+
13
+ * The post-quantum hybrid key exchange `mlkem768x25519-sha256`, preferred where node has ML-KEM (v24.7.0+ built with OpenSSL 3.5+).
14
+ * The `channelWindow` option of clients and servers, and a window that is refilled to it.
15
+ * `accept()` of the `publickey` and `hostbased` authentication contexts checks the signature itself.
16
+ * `destroy()` on server connections, and a client `destroy()` that also frees a half-closed socket.
17
+ * `hostVerifier` is passed the server's identification.
18
+ * `setNoDelay()` works before `connect()`.
19
+ * Fixes: Ed25519 keys generated with a public key starting with a zero byte, the exit status of the SFTP subsystem ([#1492](https://github.com/mscdex/ssh2/pull/1492)), an exception on a write after the connection is gone with compression on.
6
20
 
7
- Changes (breaking or otherwise) in v1.0.0 can be found [here](https://github.com/mscdex/ssh2/issues/935).
21
+ Development/testing is done against OpenSSH (8.7 currently).
8
22
 
9
23
  # Table of Contents
10
24
 
@@ -47,8 +61,7 @@ Changes (breaking or otherwise) in v1.0.0 can be found [here](https://github.com
47
61
 
48
62
  ## Requirements
49
63
 
50
- * [node.js](http://nodejs.org/) -- v10.16.0 or newer
51
- * node v12.0.0 or newer for Ed25519 key support
64
+ * [node.js](http://nodejs.org/) -- v16.0.0 or newer
52
65
  * (Optional) [`cpu-features`](https://github.com/mscdex/cpu-features) is set as an optional package dependency (you do not need to install it explicitly/separately from `ssh2`) that will be automatically built and used if possible. See the project's documentation for its own requirements.
53
66
  * This addon is currently used to help generate an optimal default cipher list
54
67
 
@@ -848,6 +861,7 @@ You can find more examples in the `examples` directory of this repository.
848
861
 
849
862
  * **kex** - _mixed_ - Key exchange algorithms.
850
863
  * Default list (in order from most to least preferable):
864
+ * `mlkem768x25519-sha256` (node v24.7.0+ built with OpenSSL 3.5+)
851
865
  * `curve25519-sha256` (node v14.0.0+)
852
866
  * `curve25519-sha256@libssh.org` (node v14.0.0+)
853
867
  * `ecdh-sha2-nistp256`
@@ -952,7 +966,9 @@ You can find more examples in the `examples` directory of this repository.
952
966
 
953
967
  * **hostHash** - _string_ - Any valid hash algorithm supported by node. The host's key is hashed using this algorithm and passed to the **hostVerifier** function as a hex string. **Default:** (none)
954
968
 
955
- * **hostVerifier** - _function_ - Function with parameters `(key[, callback])` for verifying host keys, where `key` is either a hex _string_ of the hash of the key if `hostHash` was set, otherwise it is the raw host key in _Buffer_ form. Use `utils.parseKey()` to get the host key type. Return `true` to continue with the handshake or `false` to reject and disconnect, or call `callback()` with `true` or `false` if you need to perform asynchronous verification. **Default:** (auto-accept if `hostVerifier` is not set)
969
+ * **hostVerifier** - _function_ - Function with parameters `(key[, callback[, info]])` for verifying host keys, where `key` is either a hex _string_ of the hash of the key if `hostHash` was set, otherwise it is the raw host key in _Buffer_ form. Use `utils.parseKey()` to get the host key type. Return `true` to continue with the handshake or `false` to reject and disconnect, or call `callback()` with `true` or `false` if you need to perform asynchronous verification. `info.header` is the server's identification, the same object a server gets for its clients: `identRaw` (the whole identification line), `versions` (`protocol` and `software`), `comments` and `greeting`. **Default:** (auto-accept if `hostVerifier` is not set)
970
+
971
+ * **channelWindow** - _integer_ - The receive window of each channel in bytes: how much the other side may send before it has to wait for a window adjustment. The throughput of one channel is at most this divided by the round trip time, so a larger window helps on fast links with a long round trip, at the cost of memory for data not consumed yet. From `2 * 1024 * 1024` to `2 ** 32 - 1`. **Default:** `2 * 1024 * 1024`
956
972
 
957
973
  * **keepaliveCountMax** - _integer_ - How many consecutive, unanswered SSH-level keepalive packets that can be sent to the server before disconnection (similar to OpenSSH's ServerAliveCountMax config option). **Default:** `3`
958
974
 
@@ -984,6 +1000,8 @@ You can find more examples in the `examples` directory of this repository.
984
1000
 
985
1001
  * **username** - _string_ - Username for authentication. **Default:** (none)
986
1002
 
1003
+ * **destroy**() - _(void)_ - Drops the connection at once, without waiting for the server to close its side. Unlike `end()` this also frees a socket that is already half-closed, for example after `end()` to a server that no longer answers.
1004
+
987
1005
  * **end**() - _(void)_ - Disconnects the socket.
988
1006
 
989
1007
  * **exec**(< _string_ >command[, < _object_ >options], < _function_ >callback) - _(void)_ - Executes `command` on the server. `callback` has 2 parameters: < _Error_ >err, < _Channel_ >stream. Valid `options` properties are:
@@ -1026,7 +1044,7 @@ You can find more examples in the `examples` directory of this repository.
1026
1044
 
1027
1045
  * **rekey**([< _function_ >callback]) - _(void)_ - Initiates a rekey with the server. If `callback` is supplied, it is added as a one-time handler for the `rekey` event.
1028
1046
 
1029
- * **setNoDelay**([< _boolean_ >noDelay]) - _Client_ - Calls [`setNoDelay()`](https://nodejs.org/docs/latest/api/net.html#socketsetnodelaynodelay) on the underlying socket. Disabling Nagle's algorithm improves latency at the expense of lower throughput.
1047
+ * **setNoDelay**([< _boolean_ >noDelay]) - _Client_ - Calls [`setNoDelay()`](https://nodejs.org/docs/latest/api/net.html#socketsetnodelaynodelay) on the underlying socket. Disabling Nagle's algorithm improves latency at the expense of lower throughput. Can be called before `connect()`, the setting is then applied to the socket `connect()` creates.
1030
1048
 
1031
1049
  * **sftp**([< _object_ >env, ]< _function_ >callback) - _(void)_ - Starts an SFTP session. `env` is an environment to use when executing `sftp` methods. `callback` has 2 parameters: < _Error_ >err, < _SFTP_ >sftp. For methods available on `sftp`, see the [`SFTP` client documentation](https://github.com/mscdex/ssh2/blob/master/SFTP.md).
1032
1050
 
@@ -1095,6 +1113,8 @@ You can find more examples in the `examples` directory of this repository.
1095
1113
 
1096
1114
  * **greeting** - _string_ - A message that is sent to clients immediately upon connection, before handshaking begins. **Note:** Most clients usually ignore this. **Default:** (none)
1097
1115
 
1116
+ * **channelWindow** - _integer_ - The receive window of each channel in bytes: how much the other side may send before it has to wait for a window adjustment. The throughput of one channel is at most this divided by the round trip time, so a larger window helps on fast links with a long round trip, at the cost of memory for data not consumed yet. From `2 * 1024 * 1024` to `2 ** 32 - 1`. **Default:** `2 * 1024 * 1024`
1117
+
1098
1118
  * **highWaterMark** - _integer_ - This is the `highWaterMark` to use for the parser stream. **Default:** `32 * 1024`
1099
1119
 
1100
1120
  * **hostKeys** - _array_ - An array of either Buffers/strings that contain host private keys or objects in the format of `{ key: <Buffer/string>, passphrase: <string> }` for encrypted private keys. (**Required**) **Default:** (none)
@@ -1121,7 +1141,7 @@ You can find more examples in the `examples` directory of this repository.
1121
1141
 
1122
1142
  * **localUsername** - _string_ - The local username provided by the client.
1123
1143
 
1124
- * **signature** - _Buffer_ - This contains a signature to be verified that is passed to (along with the blob) `key.verify()` where `key` is a public key parsed with [`parseKey()`](#utilities).
1144
+ * **signature** - _Buffer_ - This contains a signature to be verified that is passed to (along with the blob) `key.verify()` where `key` is a public key parsed with [`parseKey()`](#utilities). `accept()` checks it as well and rejects the attempt if it was not made with `key`; whether that host key is one to trust is still for the application to decide.
1125
1145
 
1126
1146
  * **hashAlgo** - _mixed_ - This is either `undefined` or a _string_ containing an explicit hash algorithm to be used during verification (passed to `key.verify()`).
1127
1147
 
@@ -1147,7 +1167,7 @@ You can find more examples in the `examples` directory of this repository.
1147
1167
 
1148
1168
  * **data** - _Buffer_ - The actual key data.
1149
1169
 
1150
- * **signature** - _mixed_ - If the value is `undefined`, the client is only checking the validity of the `key`. If the value is a _Buffer_, then this contains a signature to be verified that is passed to (along with the blob) `key.verify()` where `key` is a public key parsed with [`parseKey()`](#utilities).
1170
+ * **signature** - _mixed_ - If the value is `undefined`, the client is only checking the validity of the `key`. If the value is a _Buffer_, then this contains a signature to be verified that is passed to (along with the blob) `key.verify()` where `key` is a public key parsed with [`parseKey()`](#utilities). `accept()` checks it as well: with a signature that was not made with `key` it rejects the attempt instead, so recognizing the public key is enough for the application to do.
1151
1171
 
1152
1172
  * **hashAlgo** - _mixed_ - This is either `undefined` or a _string_ containing an explicit hash algorithm to be used during verification (passed to `key.verify()`).
1153
1173
 
@@ -1213,6 +1233,8 @@ You can find more examples in the `examples` directory of this repository.
1213
1233
 
1214
1234
  #### Connection methods
1215
1235
 
1236
+ * **destroy**() - _(void)_ - Drops the client connection at once, without waiting for the client to close its side. Unlike `end()` this also frees a socket that is already half-closed, for example after `end()` to a client that no longer answers.
1237
+
1216
1238
  * **end**() - _(void)_ - Closes the client connection.
1217
1239
 
1218
1240
  * **forwardOut**(< _string_ >boundAddr, < _integer_ >boundPort, < _string_ >remoteAddr, < _integer_ >remotePort, < _function_ >callback) - _(void)_ - Alert the client of an incoming TCP connection on `boundAddr` on port `boundPort` from `remoteAddr` on port `remotePort`. `callback` has 2 parameters: < _Error_ >err, < _Channel_ >stream.
@@ -12,8 +12,39 @@ const {
12
12
  const { bufferSlice } = require('./protocol/utils.js');
13
13
 
14
14
  const PACKET_SIZE = 32 * 1024;
15
+ // The defaults. A connection can have a larger window, see parseChannelWindow()
15
16
  const MAX_WINDOW = 2 * 1024 * 1024;
16
17
  const WINDOW_THRESHOLD = MAX_WINDOW / 2;
18
+ const MAX_CHANNEL_WINDOW = 2 ** 32 - 1;
19
+
20
+ // The `channelWindow` option of a client or a server: how many bytes a peer
21
+ // may send on a channel before it has to wait for a window adjust. Throughput
22
+ // of one channel is at most window / round trip time, so the default of 2 MB
23
+ // is what limits a fast link with a long round trip.
24
+ function parseChannelWindow(value) {
25
+ if (value === undefined || value === null)
26
+ return MAX_WINDOW;
27
+ if (!Number.isInteger(value)
28
+ || value < MAX_WINDOW
29
+ || value > MAX_CHANNEL_WINDOW) {
30
+ throw new Error(
31
+ `Invalid channelWindow, expected an integer from ${MAX_WINDOW} to `
32
+ + MAX_CHANNEL_WINDOW
33
+ );
34
+ }
35
+ return value;
36
+ }
37
+
38
+ // A connection keeps its window in _maxWindow. The window is topped up once
39
+ // half of it is used, as it is with the default.
40
+ function maxWindow(client) {
41
+ const window = (client && client._maxWindow);
42
+ return (typeof window === 'number' ? window : MAX_WINDOW);
43
+ }
44
+
45
+ function windowThreshold(client) {
46
+ return Math.floor(maxWindow(client) / 2);
47
+ }
17
48
 
18
49
  class ClientStderr extends ReadableStream {
19
50
  constructor(channel, streamOpts) {
@@ -24,8 +55,10 @@ class ClientStderr extends ReadableStream {
24
55
  _read(n) {
25
56
  if (this._channel._waitChanDrain) {
26
57
  this._channel._waitChanDrain = false;
27
- if (this._channel.incoming.window <= WINDOW_THRESHOLD)
58
+ if (this._channel.incoming.window
59
+ <= windowThreshold(this._channel._client)) {
28
60
  windowAdjust(this._channel);
61
+ }
29
62
  }
30
63
  }
31
64
  }
@@ -148,7 +181,7 @@ class Channel extends DuplexStream {
148
181
  _read(n) {
149
182
  if (this._waitChanDrain) {
150
183
  this._waitChanDrain = false;
151
- if (this.incoming.window <= WINDOW_THRESHOLD)
184
+ if (this.incoming.window <= windowThreshold(this._client))
152
185
  windowAdjust(this);
153
186
  }
154
187
  }
@@ -279,7 +312,7 @@ function onEnd() {
279
312
  function windowAdjust(self) {
280
313
  if (self.outgoing.state === 'closed')
281
314
  return;
282
- const amt = MAX_WINDOW - self.incoming.window;
315
+ const amt = maxWindow(self._client) - self.incoming.window;
283
316
  if (amt <= 0)
284
317
  return;
285
318
  self.incoming.window += amt;
@@ -290,6 +323,8 @@ module.exports = {
290
323
  Channel,
291
324
  MAX_WINDOW,
292
325
  PACKET_SIZE,
326
+ parseChannelWindow,
293
327
  windowAdjust,
328
+ windowThreshold,
294
329
  WINDOW_THRESHOLD,
295
330
  };
@@ -2,9 +2,8 @@
2
2
 
3
3
  const { Socket } = require('net');
4
4
  const { Duplex } = require('stream');
5
- const { resolve } = require('path');
6
5
  const { readFile } = require('fs');
7
- const { execFile, spawn } = require('child_process');
6
+ const { execFile } = require('child_process');
8
7
 
9
8
  const { isParsedKey, parseKey } = require('./protocol/keyParser.js');
10
9
 
@@ -157,99 +156,6 @@ class OpenSSHAgent extends BaseAgent {
157
156
  }
158
157
  }
159
158
 
160
- const PageantAgent = (() => {
161
- const RET_ERR_BADARGS = 10;
162
- const RET_ERR_UNAVAILABLE = 11;
163
- const RET_ERR_NOMAP = 12;
164
- const RET_ERR_BINSTDIN = 13;
165
- const RET_ERR_BINSTDOUT = 14;
166
- const RET_ERR_BADLEN = 15;
167
-
168
- const EXEPATH = resolve(__dirname, '..', 'util/pagent.exe');
169
- const ERROR = {
170
- [RET_ERR_BADARGS]: new Error('Invalid pagent.exe arguments'),
171
- [RET_ERR_UNAVAILABLE]: new Error('Pageant is not running'),
172
- [RET_ERR_NOMAP]: new Error('pagent.exe could not create an mmap'),
173
- [RET_ERR_BINSTDIN]: new Error('pagent.exe could not set mode for stdin'),
174
- [RET_ERR_BINSTDOUT]: new Error('pagent.exe could not set mode for stdout'),
175
- [RET_ERR_BADLEN]:
176
- new Error('pagent.exe did not get expected input payload'),
177
- };
178
-
179
- function destroy(stream) {
180
- stream.buffer = null;
181
- if (stream.proc) {
182
- stream.proc.kill();
183
- stream.proc = undefined;
184
- }
185
- }
186
-
187
- class PageantSocket extends Duplex {
188
- constructor() {
189
- super();
190
- this.proc = undefined;
191
- this.buffer = null;
192
- }
193
- _read(n) {}
194
- _write(data, encoding, cb) {
195
- if (this.buffer === null) {
196
- this.buffer = data;
197
- } else {
198
- const newBuffer = Buffer.allocUnsafe(this.buffer.length + data.length);
199
- this.buffer.copy(newBuffer, 0);
200
- data.copy(newBuffer, this.buffer.length);
201
- this.buffer = newBuffer;
202
- }
203
- // Wait for at least all length bytes
204
- if (this.buffer.length < 4)
205
- return cb();
206
-
207
- const len = readUInt32BE(this.buffer, 0);
208
- // Make sure we have a full message before querying pageant
209
- if ((this.buffer.length - 4) < len)
210
- return cb();
211
-
212
- data = this.buffer.slice(0, 4 + len);
213
- if (this.buffer.length > (4 + len))
214
- return cb(new Error('Unexpected multiple agent requests'));
215
- this.buffer = null;
216
-
217
- let error;
218
- const proc = this.proc = spawn(EXEPATH, [ data.length ]);
219
- proc.stdout.on('data', (data) => {
220
- this.push(data);
221
- });
222
- proc.on('error', (err) => {
223
- error = err;
224
- cb(error);
225
- });
226
- proc.on('close', (code) => {
227
- this.proc = undefined;
228
- if (!error) {
229
- if (error = ERROR[code])
230
- return cb(error);
231
- cb();
232
- }
233
- });
234
- proc.stdin.end(data);
235
- }
236
- _final(cb) {
237
- destroy(this);
238
- cb();
239
- }
240
- _destroy(err, cb) {
241
- destroy(this);
242
- cb();
243
- }
244
- }
245
-
246
- return class PageantAgent extends OpenSSHAgent {
247
- getStream(cb) {
248
- cb(null, new PageantSocket());
249
- }
250
- };
251
- })();
252
-
253
159
  const CygwinAgent = (() => {
254
160
  const RE_CYGWIN_SOCK = /^!<socket >(\d+) s ([A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8})/;
255
161
 
@@ -389,11 +295,8 @@ const CygwinAgent = (() => {
389
295
  // being interchangeable
390
296
  const WINDOWS_PIPE_REGEX = /^[/\\][/\\]\.[/\\]pipe[/\\].+/;
391
297
  function createAgent(path) {
392
- if (process.platform === 'win32' && !WINDOWS_PIPE_REGEX.test(path)) {
393
- return (path === 'pageant'
394
- ? new PageantAgent()
395
- : new CygwinAgent(path));
396
- }
298
+ if (process.platform === 'win32' && !WINDOWS_PIPE_REGEX.test(path))
299
+ return new CygwinAgent(path);
397
300
  return new OpenSSHAgent(path);
398
301
  }
399
302
 
@@ -1119,5 +1022,4 @@ module.exports = {
1119
1022
  CygwinAgent,
1120
1023
  isAgent,
1121
1024
  OpenSSHAgent,
1122
- PageantAgent,
1123
1025
  };
@@ -47,10 +47,10 @@ const {
47
47
  const { AgentContext, createAgent, isAgent } = require('./agent.js');
48
48
  const {
49
49
  Channel,
50
- MAX_WINDOW,
51
50
  PACKET_SIZE,
51
+ parseChannelWindow,
52
52
  windowAdjust,
53
- WINDOW_THRESHOLD,
53
+ windowThreshold,
54
54
  } = require('./Channel.js');
55
55
  const {
56
56
  ChannelManager,
@@ -69,6 +69,8 @@ class Client extends EventEmitter {
69
69
  constructor() {
70
70
  super();
71
71
 
72
+ this._maxWindow = parseChannelWindow();
73
+ this._noDelay = undefined;
72
74
  this.config = {
73
75
  host: undefined,
74
76
  port: undefined,
@@ -119,6 +121,7 @@ class Client extends EventEmitter {
119
121
  return this;
120
122
  }
121
123
 
124
+ this._maxWindow = parseChannelWindow(cfg.channelWindow);
122
125
  this.config.host = cfg.hostname || cfg.host || 'localhost';
123
126
  this.config.port = cfg.port || 22;
124
127
  this.config.localAddress = (typeof cfg.localAddress === 'string'
@@ -281,13 +284,20 @@ class Client extends EventEmitter {
281
284
  hostVerifier = (key, verify) => {
282
285
  if (hashAlgo)
283
286
  key = createHash(hashAlgo).update(key).digest('hex');
284
- const ret = hashCb(key, verify);
287
+ // The server's identification is passed too: it is what the server
288
+ // said about itself before the key, and the same object a server gets
289
+ // for its clients as `info.header`
290
+ const ret = hashCb(key, verify, { header: remoteHeader });
285
291
  if (ret !== undefined)
286
292
  verify(ret);
287
293
  };
288
294
  }
289
295
 
296
+ let remoteHeader;
290
297
  const sock = this._sock = (cfg.sock || new Socket());
298
+ // What setNoDelay() was given before there was a socket to give it to
299
+ if (this._noDelay !== undefined && typeof sock.setNoDelay === 'function')
300
+ sock.setNoDelay(this._noDelay);
291
301
  let ready = false;
292
302
  let sawHeader = false;
293
303
  if (this._protocol)
@@ -315,6 +325,7 @@ class Client extends EventEmitter {
315
325
  },
316
326
  onHeader: (header) => {
317
327
  sawHeader = true;
328
+ remoteHeader = header;
318
329
  this._remoteVer = header.versions.software;
319
330
  if (header.greeting)
320
331
  this.emit('greeting', header.greeting);
@@ -542,7 +553,7 @@ class Client extends EventEmitter {
542
553
  type,
543
554
  incoming: {
544
555
  id: info.recipient,
545
- window: MAX_WINDOW,
556
+ window: this._maxWindow,
546
557
  packetSize: PACKET_SIZE,
547
558
  state: 'open'
548
559
  },
@@ -587,7 +598,7 @@ class Client extends EventEmitter {
587
598
  return;
588
599
  }
589
600
 
590
- if (channel.incoming.window <= WINDOW_THRESHOLD)
601
+ if (channel.incoming.window <= windowThreshold(this))
591
602
  windowAdjust(channel);
592
603
  },
593
604
  CHANNEL_EXTENDED_DATA: (p, recipient, data, type) => {
@@ -611,7 +622,7 @@ class Client extends EventEmitter {
611
622
  return;
612
623
  }
613
624
 
614
- if (channel.incoming.window <= WINDOW_THRESHOLD)
625
+ if (channel.incoming.window <= windowThreshold(this))
615
626
  windowAdjust(channel);
616
627
  },
617
628
  CHANNEL_WINDOW_ADJUST: (p, recipient, amount) => {
@@ -1184,8 +1195,14 @@ class Client extends EventEmitter {
1184
1195
  return this;
1185
1196
  }
1186
1197
 
1198
+ // Unlike end(), which sends a disconnect and then waits for the other side
1199
+ // to close, this drops the connection at once. Also for a socket that is no
1200
+ // longer writable: after end() it is only half-closed, and it stays that way
1201
+ // for as long as a peer that is gone does not answer
1187
1202
  destroy() {
1188
- this._sock && isWritable(this._sock) && this._sock.destroy();
1203
+ const sock = this._sock;
1204
+ if (sock && typeof sock.destroy === 'function' && !sock.destroyed)
1205
+ sock.destroy();
1189
1206
  return this;
1190
1207
  }
1191
1208
 
@@ -1633,6 +1650,9 @@ class Client extends EventEmitter {
1633
1650
  }
1634
1651
 
1635
1652
  setNoDelay(noDelay) {
1653
+ // Kept for the socket connect() makes: called before it there is no socket
1654
+ // yet, and the call used to do nothing at all
1655
+ this._noDelay = (noDelay === undefined ? true : !!noDelay);
1636
1656
  if (this._sock && typeof this._sock.setNoDelay === 'function')
1637
1657
  this._sock.setNoDelay(noDelay);
1638
1658
 
@@ -1643,7 +1663,7 @@ class Client extends EventEmitter {
1643
1663
  function openChannel(self, type, opts, cb) {
1644
1664
  // Ask the server to open a channel for some purpose
1645
1665
  // (e.g. session (sftp, exec, shell), or forwarding a TCP connection
1646
- const initWindow = MAX_WINDOW;
1666
+ const initWindow = self._maxWindow;
1647
1667
  const maxPacket = PACKET_SIZE;
1648
1668
 
1649
1669
  if (typeof opts === 'function') {
@@ -1924,7 +1944,7 @@ function onCHANNEL_OPEN(self, info) {
1924
1944
  type: info.type,
1925
1945
  incoming: {
1926
1946
  id: localChan,
1927
- window: MAX_WINDOW,
1947
+ window: self._maxWindow,
1928
1948
  packetSize: PACKET_SIZE,
1929
1949
  state: 'open'
1930
1950
  },
@@ -1940,7 +1960,7 @@ function onCHANNEL_OPEN(self, info) {
1940
1960
 
1941
1961
  self._protocol.channelOpenConfirm(info.sender,
1942
1962
  localChan,
1943
- MAX_WINDOW,
1963
+ self._maxWindow,
1944
1964
  PACKET_SIZE);
1945
1965
  return stream;
1946
1966
  };
@@ -288,13 +288,12 @@ function parseDERs(keyType, pub, priv) {
288
288
  // - Attributes (absent for ED25519)
289
289
 
290
290
  let pubBin = reader.readString(Ber.BitString, true);
291
- {
292
- // Remove leading zero bytes
293
- let i = 0;
294
- for (; i < pubBin.length && pubBin[i] === 0x00; ++i);
295
- if (i > 0)
296
- pubBin = pubBin.slice(i);
297
- }
291
+ // The BIT STRING starts with its number of unused bits, which is always
292
+ // zero here. Only that byte is dropped: the key that follows can start
293
+ // with zero bytes of its own (1 key in 256 does)
294
+ if (pubBin.length !== 33 || pubBin[0] !== 0x00)
295
+ throw new Error('Malformed ED25519 public key');
296
+ pubBin = pubBin.slice(1);
298
297
 
299
298
  // Parse private key
300
299
  reader = new Ber.Reader(priv);
@@ -75,7 +75,9 @@ const {
75
75
  ZlibPacketWriter,
76
76
  } = require('./zlib.js');
77
77
 
78
- const MODULE_VER = require('../../package.json').version;
78
+ // The version is part of the identification string, where a minus sign is not
79
+ // allowed (RFC 4253, 4.2). A prerelease version has one.
80
+ const MODULE_VER = require('../../package.json').version.replace(/-/g, '_');
79
81
 
80
82
  const VALID_DISCONNECT_REASONS = new Map(
81
83
  Object.values(DISCONNECT_REASON).map((n) => [n, 1])
@@ -285,6 +285,14 @@ class SFTP extends EventEmitter {
285
285
  }
286
286
  destroy() {
287
287
  if (this.outgoing.state === 'open' || this.outgoing.state === 'eof') {
288
+ // When running as a server, send exit-status and EOF before closing
289
+ // the channel. This matches OpenSSH behavior where the sftp-server
290
+ // process exits with code 0, triggering exit-status + EOF + close.
291
+ // Without this, clients like SCP/Mutagen see exit code -1.
292
+ if (this.server && this.outgoing.state === 'open') {
293
+ this._protocol.exitStatus(this.outgoing.id, 0);
294
+ this._protocol.channelEOF(this.outgoing.id);
295
+ }
288
296
  this.outgoing.state = 'closing';
289
297
  this._protocol.channelClose(this.outgoing.id);
290
298
  }
@@ -32,6 +32,22 @@ const curve25519Supported = (typeof crypto.diffieHellman === 'function'
32
32
  && typeof crypto.generateKeyPairSync === 'function'
33
33
  && typeof crypto.createPublicKey === 'function');
34
34
 
35
+ // ML-KEM needs a node built against OpenSSL 3.5+ (node v24.7.0+), and having
36
+ // crypto.encapsulate() does not mean the linked OpenSSL knows the algorithm
37
+ const mlkem768x25519Supported = (() => {
38
+ if (!curve25519Supported
39
+ || typeof crypto.encapsulate !== 'function'
40
+ || typeof crypto.decapsulate !== 'function') {
41
+ return false;
42
+ }
43
+ try {
44
+ crypto.generateKeyPairSync('ml-kem-768');
45
+ return true;
46
+ } catch {
47
+ return false;
48
+ }
49
+ })();
50
+
35
51
  const DEFAULT_KEX = [
36
52
  // https://tools.ietf.org/html/rfc5656#section-10.1
37
53
  'ecdh-sha2-nistp256',
@@ -52,6 +68,10 @@ if (curve25519Supported) {
52
68
  DEFAULT_KEX.unshift('curve25519-sha256');
53
69
  DEFAULT_KEX.unshift('curve25519-sha256@libssh.org');
54
70
  }
71
+ if (mlkem768x25519Supported) {
72
+ // https://datatracker.ietf.org/doc/draft-ietf-sshm-mlkem-hybrid-kex/
73
+ DEFAULT_KEX.unshift('mlkem768x25519-sha256');
74
+ }
55
75
  const SUPPORTED_KEX = DEFAULT_KEX.concat([
56
76
  // https://tools.ietf.org/html/rfc4419#section-4
57
77
  'diffie-hellman-group-exchange-sha1',
@@ -348,6 +368,7 @@ module.exports = {
348
368
  SUPPORTED_COMPRESSION,
349
369
 
350
370
  curve25519Supported,
371
+ mlkem768x25519Supported,
351
372
  eddsaSupported,
352
373
  };
353
374