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

request-server doesn't handle uploads of empty files #214

Closed
an2deg opened this issue Dec 21, 2017 · 5 comments
Closed

request-server doesn't handle uploads of empty files #214

an2deg opened this issue Dec 21, 2017 · 5 comments
Assignees

Comments

@an2deg
Copy link

an2deg commented Dec 21, 2017

Hi!

I figured out that request-server has a problem with uploading empty files. Uploading of an empty file finishes without errors but no file has created.
It happens because the file supposed to be created after the first SSH2_FXP_WRITE packet but SFTP client just sends SSH2_FXP_OPEN and than SSH2_FXP_CLOSE.
Possible solution:

  1. Extend isOpener interface by methods isOpenedForWrite() bool and isOpenedForRead() bool.

  2. In packetWorker() in case of isOpener packet if the file is opening for write, create empty sshFxpWritePacket structure and process it. Dirty and hacky example:

     case isOpener:
     	handle := rs.nextRequest(requestFromPacket(pkt))
     	if pkt.isOpenedForWrite() {
     		request, _ := rs.getRequest(handle)
     		wpkt := &sshFxpWritePacket{}
     		uerr := request.updateMethod(wpkt)
     		if uerr != nil {
     			rpkt = statusFromError(pkt, syscall.EBADF)
     		} else {
     			rpkt = request.call(rs.Handlers, wpkt)
     		}
     	}
     	rpkt = sshFxpHandlePacket{pkt.id(), handle}
    

In this case will be invoked a write of empty buffer and the file will be created. In case of already existent file the file content will no be overwritten because we use empty byte buffer.

It will also cover scenario when some SFTP client creates a file using SSH2_FXP_OPEN and then sends immediately SSH2_FXP_STAT packet just to check file existents.

P.S. Adding methods isOpenedForWrite() and isOpenedForRead() to the isOpener interface doesn't cover situation when SFTP client opens a file with O_TRUNC flag.

@puellanivis
Copy link
Collaborator

I don’t think injecting a sshFxpWritePacket is the right thing to do… by specification, if an SSH_FXP_OPEN request has the O_CREAT flag set, it is the server’s responsibility to create that file if it does not already exist. So, I am unsure why one would want to inject a write like that…

@an2deg
Copy link
Author

an2deg commented Dec 21, 2017

Then I think we need special handler for SSH_FXP_OPEN packet.

@eikenb
Copy link
Member

eikenb commented Dec 21, 2017

First let me say Thanks @an2deg for the report. It never crossed my mind to upload a empty file, but it makes perfect sense to want/need to do it. Having people to point out this kind of personal blindness is one of my favorite things about free software projects.

I'd really rather not add another Handler. I'm playing with either adding an "Open" request method [1] too the FileCmd handler or do something a bit like @an2deg first suggested but instead of creating an empty write packet, adding the necessary support for the open packet in the request handling code in fileput(), packetData(), etc. Looking through the code I'm leaning toward the latter solution as I think it makes more sense and doesn't require a new request method.

I'll create a new branch for this work and post it here so you can follow along if you'd like.

[1] a 'request method' here means the plain text http-like methods attached to the request objects. Things like "Get", "Put", "Rename", "Remove", etc. See request.call() for all of them in a switch:

sftp/request.go

Line 139 in 9e9438a

func (r *Request) call(handlers Handlers, pkt requestPacket) responsePacket {

@eikenb eikenb self-assigned this Dec 21, 2017
eikenb added a commit that referenced this issue Dec 24, 2017
When the SSH_FXP_OPEN packet has the SSH_FXF_CREAT pflag set the file
should get created even if no data is sent. We do this by having the
Open method call through to the FilePut Handler with empty packet data.

Fixes #214
@eikenb
Copy link
Member

eikenb commented Dec 24, 2017

@an2deg @puellanivis, I just pushed a branch with a fix for this and opened a PR with the branch. If you have any thoughts, please comment here or on the PR, whichever is most convenient.

@an2deg
Copy link
Author

an2deg commented Dec 29, 2017

@eikenb sorry for a late reply. MR looks good.
Thank you!

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

No branches or pull requests

3 participants