gin icon indicating copy to clipboard operation
gin copied to clipboard

func (*Context) FormFile returns null in debian 9 (formerly: "File Upload behind nginx proxy fails")

Open dpapathanasiou opened this issue 5 years ago • 2 comments

I've written a simple web app using the single file upload feature by calling func (*Context) FormFile as shown in the example.

I run the app on post 9001, and when I access http://localhost:9001 directly, it works as expected.

But when I try putting the app behind nginx, using a simple proxy_pass config like this:

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass https://127.0.0.1:9001;
}

Suddenly, calls to func (*Context) FormFile result in null pointers:

POST /create HTTP/1.0
Host: 127.0.0.1:9001
Connection: close
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.5
Connection: close
Content-Length: 6857971
Content-Type: multipart/form-data; boundary=---------------------------2758032771652005901044345
Cookie: session=MTUzODg0NzQ4NnxEdi1CQkFFQ180SUFBUkFCRUFBQWZ2LUNBQUlHYzNSeWFXNW5EQWtBQjNObGMzTnBiMjRHYzNSeWFXNW5EQ0lBSUdabFpUSXdZVFJtTnpSak5UYzJaVE0xWm1Vek1XUmxaVEl5TVdVek5HUTBCbk4wY21sdVp3d0pBQWRoWTJOdmRXNTBCbk4wY21sdVp3d2lBQ0EzT1RnMk5XVTVORFl6TkRJMVpXSmtZakEyTlROaU5qWmxOV1ppWmpWbE13PT18X7qRG_LnPiRnGgGsdUbEhcyPkM-74XI12azjpnT4B3U=
Dnt: 1
Referer: https://127.0.0.1/create
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0


runtime error: invalid memory address or nil pointer dereference
/usr/local/go/src/runtime/panic.go:513 (0x433c18)
        gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/go/src/runtime/panic.go:82 (0x432d6d)
        panicmem: panic(memoryError)
/usr/local/go/src/runtime/signal_unix.go:390 (0x449151)
        sigpanic: panicmem()
/root/go/src/myapp/server/create.go:130 (0x9c5710)

Line 130 in the create.go file is simply doing this:

file, fileErr := c.FormFile("file")

which is identical to the example.

I'm not sure if the problem is with gin or nginx, but I also did try setting proxy_request_buffering to off, only to get the same result.

dpapathanasiou avatar Oct 06 '18 19:10 dpapathanasiou

The nginx proxy turned out to be a red herring: proxy_pass is sending all the information to my gin app at port 9001.

The real problem is that under debian 9 and go 1.11, the call to func (*Context) FormFile always returns a null FileHeader.

It's only when I call the func (*Request) FormFile directly from the http Request, rather than using gin's Context does it work properly.

The reason I did not notice this sooner is that my development environment is a linuxmint (ubuntu-based) computer using go 1.10, and my deployment and production servers are both debian 9.

My local builds all run properly, but when I switched it to the debian deployment host it started failing, whether or not I was using nginx as a proxy.

So it seems like a bug in the Context, but I'm not sure where specifically.

dpapathanasiou avatar Oct 07 '18 22:10 dpapathanasiou

have you solve the problem?

MisterChing avatar Dec 06 '22 08:12 MisterChing