Skip to content

Commit 1eecc22

Browse files
rumanzoAlexey KostinAndrea Spacca
authored
Normalize upload file name (#606)
* Generate secure token using crypto rand * Normalize names of uploaded files * revert token.go accidentally added to commit * better input filename normalization using transfrom chain * remove unused line --------- Co-authored-by: Alexey Kostin <a.kostin@corp.mail.ru> Co-authored-by: Andrea Spacca <andrea.spacca@gmail.com>
1 parent bedbc81 commit 1eecc22

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329
1717
github.com/dutchcoders/transfer.sh-web v0.0.0-20221119114740-ca3a2621d2a6
1818
github.com/elazarl/go-bindata-assetfs v1.0.1
19+
github.com/emicklei/go-restful v2.16.0+incompatible
1920
github.com/fatih/color v1.14.1
2021
github.com/golang/gddo v0.0.0-20210115222349-20d68f94ee1f
2122
github.com/gorilla/handlers v1.5.1
@@ -29,6 +30,7 @@ require (
2930
golang.org/x/crypto v0.17.0
3031
golang.org/x/net v0.17.0
3132
golang.org/x/oauth2 v0.7.0
33+
golang.org/x/text v0.14.0
3234
google.golang.org/api v0.114.0
3335
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
3436
storj.io/common v0.0.0-20230301105927-7f966760c100
@@ -86,7 +88,6 @@ require (
8688
go.opencensus.io v0.24.0 // indirect
8789
golang.org/x/sync v0.1.0 // indirect
8890
golang.org/x/sys v0.15.0 // indirect
89-
golang.org/x/text v0.14.0 // indirect
9091
google.golang.org/appengine v1.6.7 // indirect
9192
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
9293
google.golang.org/grpc v1.56.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ github.com/dutchcoders/transfer.sh-web v0.0.0-20221119114740-ca3a2621d2a6 h1:7uT
8585
github.com/dutchcoders/transfer.sh-web v0.0.0-20221119114740-ca3a2621d2a6/go.mod h1:F6Q37CxDh2MHr5KXkcZmNB3tdkK7v+bgE+OpBY+9ilI=
8686
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
8787
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
88+
github.com/emicklei/go-restful v2.16.0+incompatible h1:rgqiKNjTnFQA6kkhFe16D8epTksy9HQ1MyrbDXSdYhM=
89+
github.com/emicklei/go-restful v2.16.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
8890
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
8991
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
9092
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

server/handlers.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"sync"
5252
textTemplate "text/template"
5353
"time"
54+
"unicode"
5455

5556
"github.com/ProtonMail/go-crypto/openpgp"
5657
"github.com/ProtonMail/go-crypto/openpgp/armor"
@@ -66,6 +67,9 @@ import (
6667
blackfriday "github.com/russross/blackfriday/v2"
6768
qrcode "github.com/skip2/go-qrcode"
6869
"golang.org/x/net/idna"
70+
"golang.org/x/text/runes"
71+
"golang.org/x/text/transform"
72+
"golang.org/x/text/unicode/norm"
6973
)
7074

7175
const getPathPart = "get"
@@ -418,7 +422,24 @@ func (s *Server) notFoundHandler(w http.ResponseWriter, _ *http.Request) {
418422
}
419423

420424
func sanitize(fileName string) string {
421-
return path.Base(fileName)
425+
t := transform.Chain(
426+
norm.NFD,
427+
runes.Remove(runes.In(unicode.Cc)),
428+
runes.Remove(runes.In(unicode.Cf)),
429+
runes.Remove(runes.In(unicode.Co)),
430+
runes.Remove(runes.In(unicode.Cs)),
431+
runes.Remove(runes.In(unicode.Other)),
432+
runes.Remove(runes.In(unicode.Zl)),
433+
runes.Remove(runes.In(unicode.Zp)),
434+
norm.NFC)
435+
newName, _, err := transform.String(t, fileName)
436+
if err != nil {
437+
return path.Base(fileName)
438+
}
439+
if len(newName) == 0 {
440+
newName = "_"
441+
}
442+
return path.Base(newName)
422443
}
423444

424445
func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)