pkg / sftp

SFTP support for the go.crypto/ssh package
BSD 2-Clause "Simplified" License
1.49k stars 379 forks source link

Can we get file size in the Filewrite handler? #590

Closed neurosnap closed 1 month ago

neurosnap commented 1 month ago

Greetings!

It's unclear from the documentation and code if it's possible to get request.Attributes().Size when a client is uploading a file (e.g. handler.Filewrite)?

Running the ./examples/request-server and logging the r.Attributes() it looks like the answer is no but I wanted to double-check here if I'm missing something obvious.

Our use case is to be able to determine if the user has enough "storage space" to be able to upload the file -- as well as limiting the size for file uploads.

Sorry if this was covered somewhere else and I appreciate the hard work on this package!

puellanivis commented 1 month ago

There is no guarantee that a client might set the SSH_FILEXFER_ATTR_SIZE flag on the attrs field of the SSH_FXP_OPEN packet.

Also, I’m not sure there is even any guarantee that a server would even do anything if such information were included in an SSH_FXP_OPEN packet anyways. We certainly don’t do anything with it in server.go, I think OpenSSH itself would ignore it as well, but the request-server could potentially make use of it, if the client sent it.

I think the way to handle this properly in a standards compliant way, would be to open, setstat with full length, then start writing the data. But this would fill the file with zeros, and remove any ability to know that the file hadn’t completed upload without binary searching for a full zero length, so I’m not sure why any client would really want to do that.

neurosnap commented 1 month ago

Okay it sounds like it's not really possible, thanks for confirming! Feel free to close.