ssh2-sftp-client icon indicating copy to clipboard operation
ssh2-sftp-client copied to clipboard

Add support of env when establishing a SFTP connection

Open di-ov opened this issue 3 months ago • 5 comments

In the version 1.17.0 of the ssh2 package a new feature was added: https://github.com/mscdex/ssh2/pull/1445

The feature adds support for passing an object of key-value variables when establishing a SFTP connection.

i.e. potentially a basic example would look like:

const config = {
  host: 'arch-vbox',
  port: 22,
  username: 'tim',
  password: 'xxxx',
  env: { MY_VAR: "test value" }
};

const sftp = new Client();

sftp
  .connect(config)
  .then(() => {
    return sftp.list(remoteDir);
  });

Could support for this feature be added?

di-ov avatar Sep 09 '25 13:09 di-ov

dimitar @.***> writes:

di-ov created an issue (theophilusx/ssh2-sftp-client#584)

In the version 1.17.0 of the ssh2 package a new feature was added: mscdex/ssh2#1445

The feature adds support for passing an object of key-value variables when establishing a SFTP connection.

i.e. potentially a basic example would look like:

const config = { host: 'arch-vbox', port: 22, username: 'tim', password: 'xxxx', env: { MY_VAR: "test value" } };

const sftp = new Client();

sftp .connect(config) .then(() => { return sftp.list(remoteDir); });

Could support for this feature be added?

No, this support will not be added.

Reasons:

  • There is no use case. In exactly what scenario would setting remote shell variables impact the limited commands available within sftp?

  • The ability to set environment variables within sftp servers is not part of the sftp (FTP) protocol and is not supported by most sftp servers

  • The extended functionality has been added to ssh and is not an extension to the sftp subsystem of ssh2

Until someone can provide a clear use case for this feature and until ssh2 adds this feature to the sftp subsystem, which is extremely unlikely without an actual use case, this functionality will not be added.

theophilusx avatar Sep 09 '25 22:09 theophilusx

@theophilusx I've initially described the use case here: https://github.com/mscdex/ssh2/issues/1433, but in summary this allows:

  • sending variables to the underlying SSH server through the SFTP module
  • the official OpenSSH SFTP binary supports this functionality via e.g. sftp -o "SendEnv MY_VAR" username@host

Yes, the change is mainly in the SSH component. But, the change done on the the SFTP subsystem is that it added support for passing this env to the underlying SSH server.

di-ov avatar Sep 10 '25 07:09 di-ov

dimitar @.***> writes:

di-ov left a comment (theophilusx/ssh2-sftp-client#584)

@theophilusx I've initially described the use case here: mscdex/ssh2#1433, but in summary this allows:

  • sending variables to the underlying SSH server through the SFTP module
  • the official OpenSSH SFTP binary supports this functionality via e.g. sftp -o "SendEnv MY_VAR" @.***

Yes, the change is mainly in the SSH component. But, the change done on the the SFTP subsystem is that it added support for passing this env to the underlying SSH server.

This is not a use case. The ability to do something doesn't mean it is something beneficial or useful. I'm unwilling to add code that will need to be tested and maintained for something with no clear benefit.

Provide me with a clear example of how you would use environment variables in conjunction with the sftp commands and I will reconsider. As it stands, I cannot see a single use case. I cannot even see how you would test such a feature. Remember, with sftp, you do not have any shell, only a set of well defined commands, none of which have any environment variable support i.e. do not respond to env variables.

theophilusx avatar Sep 10 '25 08:09 theophilusx

Yeah, alright, I get your point. Indeed, my use case might be a bit too specific, but it is the following: I offer a SFTP server solution, which is restricted to receive SFTP commands only. At the same time I also offer a SFTP client solution. When users connect to the SFTP server, this env helps me convey some information specific to each SFTP client instance that I have distributed to my users. This is it in simple terms, unfortunately I can't provide more details.

I am pretty sure there are multiple other use cases where this kind env forwarding functionality is used, but if you think it wound't fit the specific use cases for ssh2-sftp-client I would understand and I can close this.

di-ov avatar Sep 10 '25 15:09 di-ov

dimitar @.***> writes:

di-ov left a comment (theophilusx/ssh2-sftp-client#584)

Yeah, alright, I get your point. Indeed, my use case might be a bit too specific, but it is the following: I offer a SFTP server solution, which is restricted to receive SFTP commands only. At the same time I also offer a SFTP client solution. When users connect to the SFTP server, this env helps me convey some information specific to each SFTP client instance that I have distributed to my users. This is it in simple terms, unfortunately I can't provide more details.

I am pretty sure there are multiple other use cases where this kind env forwarding functionality is used, but if you think it wound't fit the specific use cases for ssh2-sftp-client I would understand and I can close this.

The alternatives are -

  1. You implement the support and provide a PR. Assuming it includes tests and all existing tests continue to pass, I will consider merging.

  2. You either provide an sftp server which will adjust/modify behaviour of sftp commands based on env settings or provide me with a link to one that does. While I still think the use case is weak mainly because of a lack of sftp servers which provide such functionality, if there is one out there I can run and use for testing, I would consider implementing this functionality.

theophilusx avatar Sep 10 '25 21:09 theophilusx