88 "net/http"
99 "regexp"
1010 "strings"
11+ "unicode"
1112
1213 packages_model "code.gitea.io/gitea/models/packages"
1314 "code.gitea.io/gitea/modules/log"
@@ -18,8 +19,8 @@ import (
1819)
1920
2021var (
21- packageNameRegex = regexp .MustCompile (`\A[-_+.A-Za-z0-9 ]+\z` )
22- filenameRegex = regexp .MustCompile (`\A[-_+=:;.()\[\]{}~!@#$%^& A-Za-z0-9 ]+\z` )
22+ packageNameRegex = regexp .MustCompile (`\A[-_+.\w ]+\z` )
23+ filenameRegex = regexp .MustCompile (`\A[-_+=:;.()\[\]{}~!@#$%^& \w ]+\z` )
2324)
2425
2526func apiError (ctx * context.Context , status int , obj any ) {
@@ -54,20 +55,33 @@ func DownloadPackageFile(ctx *context.Context) {
5455 helper .ServePackageFile (ctx , s , u , pf )
5556}
5657
58+ func isValidPackageName (packageName string ) bool {
59+ if len (packageName ) == 1 && ! unicode .IsLetter (rune (packageName [0 ])) {
60+ return false
61+ }
62+ return packageNameRegex .MatchString (packageName ) && packageName != ".."
63+ }
64+
65+ func isValidFileName (filename string ) bool {
66+ return filenameRegex .MatchString (filename ) &&
67+ strings .TrimSpace (filename ) == filename &&
68+ filename != "." && filename != ".."
69+ }
70+
5771// UploadPackage uploads the specific generic package.
5872// Duplicated packages get rejected.
5973func UploadPackage (ctx * context.Context ) {
6074 packageName := ctx .Params ("packagename" )
6175 filename := ctx .Params ("filename" )
6276
63- if ! packageNameRegex . MatchString (packageName ) || ! filenameRegex . MatchString (filename ) {
64- apiError (ctx , http .StatusBadRequest , errors .New ("Invalid package name or filename" ))
77+ if ! isValidPackageName (packageName ) || isValidFileName (filename ) {
78+ apiError (ctx , http .StatusBadRequest , errors .New ("invalid package name or filename" ))
6579 return
6680 }
6781
6882 packageVersion := ctx .Params ("packageversion" )
6983 if packageVersion != strings .TrimSpace (packageVersion ) {
70- apiError (ctx , http .StatusBadRequest , errors .New ("Invalid package version" ))
84+ apiError (ctx , http .StatusBadRequest , errors .New ("invalid package version" ))
7185 return
7286 }
7387
0 commit comments