Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFTP Permission denied error crashes the whole connection #1436

Closed
Coding-Kiwi opened this issue Dec 31, 2024 · 2 comments
Closed

SFTP Permission denied error crashes the whole connection #1436

Coding-Kiwi opened this issue Dec 31, 2024 · 2 comments
Labels

Comments

@Coding-Kiwi
Copy link

I recently updated ssh2 from version 0.8.9 to 1.16 in my app, and I'm working on getting everything up and running again. One thing I noticed is that if there's an error inside sftp, it ends up crashing the whole client and closing the connection.

Is this something that's meant to happen? Do you think there's a way I can prevent this from happening?

This is my example, I try to create a writestream on a file in the homefolder of the user test but the file is owned by root, so writing should fail.

import { Client } from 'ssh2';

const client = new Client();

client.on('ready', () => {
    client.sftp((err, sftp) => {
        if (err) throw (err);

        var writeStream = sftp.createWriteStream("/home/test/asd");

        writeStream.write("this is a test", (e) => {
            if (e) throw (e);

            console.log("DATA WRITTEN");
        });

        writeStream.end();
        console.log("stream end");
    });
}).on('end', () => {
    console.log("END event");
}).on('error', (err) => {
    console.log("ERROR event", err);
}).on('close', () => {
    console.log("CLOSE event");
}).connect({
    host: "....",
    port: 22,
    username: "....",
    password: "...."
});

outputs:

stream end
ERROR event Error: Permission denied
    at 101 (project\node_modules\ssh2\lib\protocol\SFTP.js:2854:19)
    at SFTP.push (project\node_modules\ssh2\lib\protocol\SFTP.js:278:11)
    at CHANNEL_DATA (project\node_modules\ssh2\lib\client.js:585:23)
    at 94 (project\node_modules\ssh2\lib\protocol\handlers.misc.js:930:16)
    at Protocol.onPayload (project\node_modules\ssh2\lib\protocol\Protocol.js:2059:10)
    at AESGCMDecipherNative.decrypt (project\node_modules\ssh2\lib\protocol\crypto.js:987:26)
    at Protocol.parsePacket [as _parse] (project\node_modules\ssh2\lib\protocol\Protocol.js:2028:25)
    at Protocol.parse (project\node_modules\ssh2\lib\protocol\Protocol.js:313:16)
    at Socket.<anonymous> (project\node_modules\ssh2\lib\client.js:773:21)
    at Socket.emit (node:events:518:28) {
  code: 3
}
END event
CLOSE event

My usecase / app is an ssh terminal with sftp functionality and if an sftp error occurs this now means I have to create a whole new client, clear the terminal, reauthenticate and so on.
In the old version the sftp subsystem would fail seperately from the "main" ssh connection and leave the client alone. My expectation would be that the write callback gets called with an error instead or something like that?

Thank you

@Coding-Kiwi Coding-Kiwi changed the title SFTP Permission denied error crashed the whole connection SFTP Permission denied error crashes the whole connection Dec 31, 2024
@mscdex
Copy link
Owner

mscdex commented Dec 31, 2024

You're missing an 'error' event handler on the stream object.

@Coding-Kiwi
Copy link
Author

omg.. yes that fixes the issue
thank you for the incredibly fast response!
have a nice day sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants