@@ -21,12 +21,19 @@ import (
21
21
packages_service "code.gitea.io/gitea/services/packages"
22
22
)
23
23
24
- // https://www .python.org/dev/peps/ pep-0503/#normalized-names
24
+ // https://peps .python.org/pep-0426/#name
25
25
var normalizer = strings .NewReplacer ("." , "-" , "_" , "-" )
26
- var nameMatcher = regexp .MustCompile (`\A[a-zA-Z0-9\.\-_]+\z` )
27
-
28
- // https://www.python.org/dev/peps/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
29
- var versionMatcher = regexp .MustCompile (`^([1-9][0-9]*!)?(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*((a|b|rc)(0|[1-9][0-9]*))?(\.post(0|[1-9][0-9]*))?(\.dev(0|[1-9][0-9]*))?$` )
26
+ var nameMatcher = regexp .MustCompile (`\A(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\.\-_]*[a-zA-Z0-9])\z` )
27
+
28
+ // https://peps.python.org/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
29
+ var versionMatcher = regexp .MustCompile (`\Av?` +
30
+ `(?:[0-9]+!)?` + // epoch
31
+ `[0-9]+(?:\.[0-9]+)*` + // release segment
32
+ `(?:[-_\.]?(?:a|b|c|rc|alpha|beta|pre|preview)[-_\.]?[0-9]*)?` + // pre-release
33
+ `(?:-[0-9]+|[-_\.]?(?:post|rev|r)[-_\.]?[0-9]*)?` + // post release
34
+ `(?:[-_\.]?dev[-_\.]?[0-9]*)?` + // dev release
35
+ `(?:\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*)?` + // local version
36
+ `\z` )
30
37
31
38
func apiError (ctx * context.Context , status int , obj interface {}) {
32
39
helper .LogAndProcessError (ctx , status , obj , func (message string ) {
@@ -121,7 +128,7 @@ func UploadPackageFile(ctx *context.Context) {
121
128
122
129
packageName := normalizer .Replace (ctx .Req .FormValue ("name" ))
123
130
packageVersion := ctx .Req .FormValue ("version" )
124
- if ! nameMatcher . MatchString (packageName ) || ! versionMatcher . MatchString ( packageVersion ) {
131
+ if ! isValidNameAndVersion (packageName , packageVersion ) {
125
132
apiError (ctx , http .StatusBadRequest , "invalid name or version" )
126
133
return
127
134
}
@@ -139,7 +146,7 @@ func UploadPackageFile(ctx *context.Context) {
139
146
Name : packageName ,
140
147
Version : packageVersion ,
141
148
},
142
- SemverCompatible : true ,
149
+ SemverCompatible : false ,
143
150
Creator : ctx .Doer ,
144
151
Metadata : & pypi_module.Metadata {
145
152
Author : ctx .Req .FormValue ("author" ),
@@ -170,3 +177,7 @@ func UploadPackageFile(ctx *context.Context) {
170
177
171
178
ctx .Status (http .StatusCreated )
172
179
}
180
+
181
+ func isValidNameAndVersion (packageName , packageVersion string ) bool {
182
+ return nameMatcher .MatchString (packageName ) && versionMatcher .MatchString (packageVersion )
183
+ }
0 commit comments