From 9df18fdc69de2e71f30d8c1e6bfab2fda2e52eb4 Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Fri, 21 May 2021 15:49:48 +0200 Subject: [PATCH] fixes-20210521 (#373) --- cmd/cmd.go | 2 +- server/handlers.go | 37 +++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 1987d84..e840e32 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -12,7 +12,7 @@ import ( "google.golang.org/api/googleapi" ) -var Version = "1.2.2" +var Version = "1.2.4" var helpTemplate = `NAME: {{.Name}} - {{.Usage}} diff --git a/server/handlers.go b/server/handlers.go index 4cf4238..dbecb1e 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -94,6 +94,27 @@ func healthHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Approaching Neutral Zone, all systems normal and functioning.") } +func canContainsXSS(contentType string) bool { + switch { + case strings.Contains(contentType, "cache-manifest"): + fallthrough + case strings.Contains(contentType, "html"): + fallthrough + case strings.Contains(contentType, "rdf"): + fallthrough + case strings.Contains(contentType, "vtt"): + fallthrough + case strings.Contains(contentType, "xml"): + fallthrough + case strings.Contains(contentType, "xsl"): + return true + case strings.Contains(contentType, "x-mixed-replace"): + return true + } + + return false +} + /* The preview handler will show a preview of the content for browsers (accept type text/html), and referer is not transfer.sh */ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -263,11 +284,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) { for _, fheaders := range r.MultipartForm.File { for _, fheader := range fheaders { filename := sanitize(fheader.Filename) - contentType := fheader.Header.Get("Content-Type") - - if contentType == "" { - contentType = mime.TypeByExtension(filepath.Ext(fheader.Filename)) - } + contentType := mime.TypeByExtension(filepath.Ext(fheader.Filename)) var f io.Reader var err error @@ -474,11 +491,7 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) { return } - contentType := r.Header.Get("Content-Type") - - if contentType == "" { - contentType = mime.TypeByExtension(filepath.Ext(vars["filename"])) - } + contentType := mime.TypeByExtension(filepath.Ext(vars["filename"])) token := Encode(INIT_SEED, s.randomTokenLength) @@ -687,7 +700,7 @@ func (s *Server) CheckDeletionToken(deletionToken, token, filename string) error r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename)) if s.storage.IsNotExist(err) { - return nil + return errors.New("Metadata doesn't exist") } else if err != nil { return err } @@ -1008,7 +1021,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Remaining-Downloads", remainingDownloads) w.Header().Set("X-Remaining-Days", remainingDays) - if disposition == "inline" && strings.Contains(contentType, "html") { + if disposition == "inline" && canContainsXSS(contentType) { reader = ioutil.NopCloser(bluemonday.UGCPolicy().SanitizeReader(reader)) }