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

Allow access to unsanitized file name #800

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,23 @@ func (r *Response) Save(fileName string) error {
return os.WriteFile(fileName, r.Body, 0644)
}

// FileName returns the sanitized file name parsed from "Content-Disposition"
// header or from URL
func (r *Response) FileName() string {
// RawFileName returns the unsanitized file name parsed from "Content-Disposition"
// header or from URL. Be aware that the name may contain unsafe characters.
func (r *Response) RawFileName() string {
_, params, err := mime.ParseMediaType(r.Headers.Get("Content-Disposition"))
if fName, ok := params["filename"]; ok && err == nil {
return SanitizeFileName(fName)
return fName
}
if r.Request.URL.RawQuery != "" {
return SanitizeFileName(fmt.Sprintf("%s_%s", r.Request.URL.Path, r.Request.URL.RawQuery))
return fmt.Sprintf("%s_%s", r.Request.URL.Path, r.Request.URL.RawQuery)
}
return SanitizeFileName(strings.TrimPrefix(r.Request.URL.Path, "/"))
return strings.TrimPrefix(r.Request.URL.Path, "/")
}

// FileName returns the sanitized file name parsed from "Content-Disposition"
// header or from URL
func (r *Response) FileName() string {
return SanitizeFileName(r.RawFileName())
}

func (r *Response) fixCharset(detectCharset bool, defaultEncoding string) error {
Expand Down