go: net/http: Filename from FormFile header does not contain slashes
Please answer these questions before submitting your issue. Thanks!
- What version of Go are you using (
go version
)? 1.6.2 window/amd64 - What operating system and processor architecture are you using (
go env
)? windows 7 amd64 - What did you do? If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best. package main
import ( “fmt” “net/http” “os/exec” )
func validate(w http.ResponseWriter, r *http.Request) { file, header, err := r.FormFile(“file”) if err != nil { fmt.Fprintf(w, “%s\n”, err) fmt.Println(err) return } defer file.Close() fmt.Fprintf(w, “%s\n”, header.Header) fmt.Fprintf(w, “%s\n”, header.Filename) fmt.Println(header.Header) fmt.Println(header.Filename) }
func index(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, html) }
func main() { http.HandleFunc(“/”, index) http.HandleFunc(“/validate”, validate) go exec.Command(“rundll32”, “url.dll,FileProtocolHandler”, “http://localhost:8090/”).Start() http.ListenAndServe(“:8090”, nil) }
var html = `<!DOCTYPE html>
<html> <head> <charset="utf-8"> <title>Validation</title> <style type="text/css"> body{font-family:arial;margin-top:4em;margin-left:4em} </style> </head> <body>Validation
Select and submit a file to validate.
<form enctype="multipart/form-data" action="validate" method="post"> </form> </body> </html> ` 1. What did you expect to see? map[Content-Disposition:[form-data; name="file"; filename="C:\Users\sdr\Desktop\test.csv"] Content-Type:[application/vnd.ms-excel]] C:\Users\sdr\Desktop\test.csv 2. What did you see instead? map[Content-Disposition:[form-data; name="file"; filename="C:\Users\sdr\Desktop\test.csv"] Content-Type:[application/vnd.ms-excel]] C:UserssdrDesktoptest.csvAbout this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (8 by maintainers)
I tried debugging this. If I apply
against db82cf4e506938, the program will output
So the problem is in mime/consumeValue. IE does not escape \ in the filename. And, unlike all other browsers I tried, IE sends full path - which is, probably, not secure.
I googled for solutions: https://java.net/jira/si/jira.issueviews:issue-html/JERSEY-759/JERSEY-759.html https://github.com/mscdex/busboy/issues/24 http://jersey.576304.n2.nabble.com/Jersey-truncating-the-slashes-from-the-uploaded-file-name-td5984041.html
Maybe we should try and return the last element of filename path. I am not sure.
I will let @bradfitz decide here.
Alex
@alexbrainman @odeke-em Argh - this appears to be a browser specific problem. When I use IE11 (in a corporate environment) I get ‘filepaths’ with the slashes problem. When I switch to Firefox I get just filenames like you Alex.
I just tested this on Linux/amd64(centos) - go1.6.2 - Firefox and got filenames. Emmanuel, on some flavor of *NIX and browser, got filepaths.
Thanks Scott
edit: Emmanuel didn’t use a browser - sorry.
@sdicker8 I improved your code to produce a working sample that could be used for others to reproduce your bug run in the form of a client and server at https://github.com/odeke-em/bugs/tree/master/golang/15664 or in one place https://gist.github.com/odeke-em/46a8deba3ded6bb4f2169e2e80928442, or inlined below. To run the server, just run
Then for the client
However, I get
filename
to be contain the proper slashes when run on *NIX since I don’t have access to Windows machines.Maybe that’s a Windows thing?
Code inlined