Skip to content

Commit

Permalink
improve regex, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 27, 2024
1 parent f756fc7 commit 609b986
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
24 changes: 19 additions & 5 deletions routers/api/packages/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"regexp"
"strings"
"unicode"

packages_model "code.gitea.io/gitea/models/packages"
"code.gitea.io/gitea/modules/log"
Expand All @@ -18,8 +19,8 @@ import (
)

var (
packageNameRegex = regexp.MustCompile(`\A[-_+.A-Za-z0-9]+\z`)
filenameRegex = regexp.MustCompile(`\A[-_+=:;.()\[\]{}~!@#$%^& A-Za-z0-9]+\z`)
packageNameRegex = regexp.MustCompile(`\A[-_+.\w]+\z`)
filenameRegex = regexp.MustCompile(`\A[-_+=:;.()\[\]{}~!@#$%^& \w]+\z`)
)

func apiError(ctx *context.Context, status int, obj any) {
Expand Down Expand Up @@ -54,20 +55,33 @@ func DownloadPackageFile(ctx *context.Context) {
helper.ServePackageFile(ctx, s, u, pf)
}

func isValidPackageName(packageName string) bool {
if len(packageName) == 1 && !unicode.IsLetter(rune(packageName[0])) {
return false
}
return packageNameRegex.MatchString(packageName) && packageName != ".."
}

func isValidFileName(filename string) bool {
return filenameRegex.MatchString(filename) &&
strings.TrimSpace(filename) == filename &&
filename != "." && filename != ".."
}

// UploadPackage uploads the specific generic package.
// Duplicated packages get rejected.
func UploadPackage(ctx *context.Context) {
packageName := ctx.Params("packagename")
filename := ctx.Params("filename")

if !packageNameRegex.MatchString(packageName) || !filenameRegex.MatchString(filename) {
apiError(ctx, http.StatusBadRequest, errors.New("Invalid package name or filename"))
if !isValidPackageName(packageName) || isValidFileName(filename) {
apiError(ctx, http.StatusBadRequest, errors.New("invalid package name or filename"))
return
}

packageVersion := ctx.Params("packageversion")
if packageVersion != strings.TrimSpace(packageVersion) {
apiError(ctx, http.StatusBadRequest, errors.New("Invalid package version"))
apiError(ctx, http.StatusBadRequest, errors.New("invalid package version"))
return
}

Expand Down
59 changes: 59 additions & 0 deletions routers/api/packages/generic/generic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package generic

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestValidatePackageName(t *testing.T) {
bad := []string{
"",
".",
"..",
"-",
"a?b",
"a b",
"a/b",
}
for _, name := range bad {
assert.False(t, isValidPackageName(name), "bad=%q", name)
}

good := []string{
"a",
"a-",
"a_b",
"c.d+",
}
for _, name := range good {
assert.True(t, isValidPackageName(name), "good=%q", name)
}
}

func TestValidateFileName(t *testing.T) {
bad := []string{
"",
".",
"..",
"a?b",
"a/b",
" a",
"a ",
}
for _, name := range bad {
assert.False(t, isValidFileName(name), "bad=%q", name)
}

good := []string{
"-",
"a",
"a-",
"a_b",
"a b",
"c.d+",
`-_+=:;.()[]{}~!@#$%^& aA1`,
}
for _, name := range good {
assert.True(t, isValidFileName(name), "good=%q", name)
}
}
2 changes: 1 addition & 1 deletion tests/integration/api_packages_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestPackageGeneric(t *testing.T) {
t.Run("InvalidParameter", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

req := NewRequestWithBody(t, "PUT", fmt.Sprintf("/api/packages/%s/generic/%s/%s/%s", user.Name, "invalid+package name", packageVersion, filename), bytes.NewReader(content)).
req := NewRequestWithBody(t, "PUT", fmt.Sprintf("/api/packages/%s/generic/%s/%s/%s", user.Name, "invalid|package name", packageVersion, filename), bytes.NewReader(content)).
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusBadRequest)

Expand Down

0 comments on commit 609b986

Please sign in to comment.