From 2be260d41ef7ab56fb69888818b2489bf46b48f4 Mon Sep 17 00:00:00 2001 From: Edson Henrique Date: Wed, 28 Oct 2026 12:25:56 -0400 Subject: [PATCH] fix: acknowledge remote SFTP channel closes --- lib/utils.js | 10 ++-- test/test-sftp-close.js | 112 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 test/test-sftp-close.js diff --git a/lib/utils.js b/lib/utils.js index d9ca6aef..90a52470 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -55,11 +55,13 @@ function onCHANNEL_CLOSE(self, recipient, channel, err, dead) { channel.stderr.push(null); } - if (channel.constructor !== SFTP - && (channel.outgoing.state === 'open' - || channel.outgoing.state === 'eof') + if ((channel.outgoing.state === 'open' + || channel.outgoing.state === 'eof') && !dead) { - channel.close(); + if (channel.constructor === SFTP) + channel.destroy(); + else + channel.close(); } if (channel.outgoing.state === 'closing') channel.outgoing.state = 'closed'; diff --git a/test/test-sftp-close.js b/test/test-sftp-close.js new file mode 100644 index 00000000..86856a4d --- /dev/null +++ b/test/test-sftp-close.js @@ -0,0 +1,112 @@ +'use strict'; + +const assert = require('assert'); +const { generateKeyPairSync } = require('crypto'); + +const { mustCall, setup } = require('./common.js'); +const { SFTP } = require('../lib/protocol/SFTP.js'); +const { ChannelManager, onCHANNEL_CLOSE } = require('../lib/utils.js'); + +const hostKey = generateKeyPairSync('rsa', { + modulusLength: 2048, + privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, +}).privateKey; + +for (const initiator of ['server', 'server-duplicate', 'client']) { + const { client, server } = setup(`SFTP close initiated by ${initiator}`, { + client: { username: 'foo', password: 'bar' }, + server: { hostKeys: [hostKey] }, + }); + let serverSFTP; + let serverCloseReplies = 0; + + server.on('connection', mustCall((conn) => { + const onClose = conn._protocol._handlers.CHANNEL_CLOSE; + conn._protocol._handlers.CHANNEL_CLOSE = (...args) => { + if (serverSFTP && args[1] === serverSFTP.incoming.id) + ++serverCloseReplies; + return onClose(...args); + }; + conn.on('authentication', mustCall((ctx) => ctx.accept())); + conn.on('ready', mustCall(() => { + conn.on('session', mustCall((accept) => { + const session = accept(); + session.on('sftp', (acceptSFTP) => { + serverSFTP = acceptSFTP(); + serverSFTP.on('close', mustCall(() => { + assert.strictEqual(serverSFTP.outgoing.state, 'closed'); + })); + serverSFTP.on('REALPATH', mustCall((id) => { + serverSFTP.name(id, [ + { filename: '/fixture', longname: '/fixture', attrs: {} }, + ]); + if (initiator !== 'client') { + serverSFTP.destroy(); + if (initiator === 'server-duplicate') + conn._protocol.channelClose(serverSFTP.outgoing.id); + } + })); + }); + session.on('exec', (acceptExec) => { + assert.strictEqual( + conn._chanMgr.get(serverSFTP.incoming.id), + undefined, + 'SFTP channel must be released before the next request' + ); + assert.strictEqual(serverCloseReplies, 1); + const channel = acceptExec(); + channel.write('still connected'); + channel.exit(0); + channel.end(); + }); + }, 2)); + })); + })); + + client.on('ready', mustCall(() => { + client.sftp(mustCall((err, sftp) => { + assert.ifError(err); + sftp.on('close', mustCall(() => { + assert.strictEqual(sftp.outgoing.state, 'closed'); + client.exec('after-close', mustCall((err, channel) => { + assert.ifError(err); + let output = ''; + channel.on('data', (data) => { output += data; }); + channel.on('close', mustCall(() => { + assert.strictEqual(output, 'still connected'); + client.end(); + })); + })); + })); + sftp.realpath('/fixture', mustCall((err, path) => { + assert.ifError(err); + assert.strictEqual(path, '/fixture'); + if (initiator === 'client') + sftp.destroy(); + })); + })); + })); +} + +for (const [state, dead] of [ + ['closing', false], + ['closed', false], + ['open', true], +]) { + const client = { + _protocol: { + channelClose: mustCall(() => {}, 0), + }, + }; + client._chanMgr = new ChannelManager(client); + const sftp = new SFTP(client, { + type: 'session', + incoming: { id: 0, state: 'open' }, + outgoing: { id: 0, state }, + }); + const id = client._chanMgr.add(sftp); + sftp.on('close', mustCall()); + onCHANNEL_CLOSE(client, id, sftp, undefined, dead); + onCHANNEL_CLOSE(client, id, sftp, undefined, dead); + assert.strictEqual(sftp.outgoing.state, dead ? state : 'closed'); +}