8
8
"net/http"
9
9
"regexp"
10
10
"strings"
11
+ "unicode"
11
12
12
13
packages_model "code.gitea.io/gitea/models/packages"
13
14
"code.gitea.io/gitea/modules/log"
@@ -18,8 +19,8 @@ import (
18
19
)
19
20
20
21
var (
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` )
23
24
)
24
25
25
26
func apiError (ctx * context.Context , status int , obj any ) {
@@ -54,20 +55,33 @@ func DownloadPackageFile(ctx *context.Context) {
54
55
helper .ServePackageFile (ctx , s , u , pf )
55
56
}
56
57
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
+
57
71
// UploadPackage uploads the specific generic package.
58
72
// Duplicated packages get rejected.
59
73
func UploadPackage (ctx * context.Context ) {
60
74
packageName := ctx .Params ("packagename" )
61
75
filename := ctx .Params ("filename" )
62
76
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" ))
65
79
return
66
80
}
67
81
68
82
packageVersion := ctx .Params ("packageversion" )
69
83
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" ))
71
85
return
72
86
}
73
87
0 commit comments