Move some regexp out of functions (#25430)

/cc @KN4CK3R
https://github.com/go-gitea/gitea/pull/25294#discussion_r1237425343

I also searched the codebase and found a few more.

---------

Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
John Olheiser 2023-06-21 14:57:18 -05:00 committed by GitHub
parent 25455bc670
commit 8afc1b1cb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 7 deletions

View file

@ -372,12 +372,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
return generateRepo, nil return generateRepo, nil
} }
var fileNameSanitizeRegexp = regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)
// Sanitize user input to valid OS filenames // Sanitize user input to valid OS filenames
// //
// Based on https://github.com/sindresorhus/filename-reserved-regex // Based on https://github.com/sindresorhus/filename-reserved-regex
// Adds ".." to prevent directory traversal // Adds ".." to prevent directory traversal
func fileNameSanitize(s string) string { func fileNameSanitize(s string) string {
re := regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`) return strings.TrimSpace(fileNameSanitizeRegexp.ReplaceAllString(s, "_"))
return strings.TrimSpace(re.ReplaceAllString(s, "_"))
} }

View file

@ -222,6 +222,8 @@ func isOSWindows() bool {
return runtime.GOOS == "windows" return runtime.GOOS == "windows"
} }
var driveLetterRegexp = regexp.MustCompile("/[A-Za-z]:/")
// FileURLToPath extracts the path information from a file://... url. // FileURLToPath extracts the path information from a file://... url.
func FileURLToPath(u *url.URL) (string, error) { func FileURLToPath(u *url.URL) (string, error) {
if u.Scheme != "file" { if u.Scheme != "file" {
@ -235,8 +237,7 @@ func FileURLToPath(u *url.URL) (string, error) {
} }
// If it looks like there's a Windows drive letter at the beginning, strip off the leading slash. // If it looks like there's a Windows drive letter at the beginning, strip off the leading slash.
re := regexp.MustCompile("/[A-Za-z]:/") if driveLetterRegexp.MatchString(path) {
if re.MatchString(path) {
return path[1:], nil return path[1:], nil
} }
return path, nil return path, nil

View file

@ -77,6 +77,8 @@ func CheckAcceptMediaType(ctx *context.Context) {
} }
} }
var rangeHeaderRegexp = regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)
// DownloadHandler gets the content from the content store // DownloadHandler gets the content from the content store
func DownloadHandler(ctx *context.Context) { func DownloadHandler(ctx *context.Context) {
rc := getRequestContext(ctx) rc := getRequestContext(ctx)
@ -92,8 +94,7 @@ func DownloadHandler(ctx *context.Context) {
toByte = meta.Size - 1 toByte = meta.Size - 1
statusCode := http.StatusOK statusCode := http.StatusOK
if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" { if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" {
regex := regexp.MustCompile(`bytes=(\d+)\-(\d*).*`) match := rangeHeaderRegexp.FindStringSubmatch(rangeHdr)
match := regex.FindStringSubmatch(rangeHdr)
if len(match) > 1 { if len(match) > 1 {
statusCode = http.StatusPartialContent statusCode = http.StatusPartialContent
fromByte, _ = strconv.ParseInt(match[1], 10, 32) fromByte, _ = strconv.ParseInt(match[1], 10, 32)