Skip to content

Move some regexp out of functions #25430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
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
//
// Based on https://github.com/sindresorhus/filename-reserved-regex
// Adds ".." to prevent directory traversal
func fileNameSanitize(s string) string {
re := regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)

return strings.TrimSpace(re.ReplaceAllString(s, "_"))
return strings.TrimSpace(fileNameSanitizeRegexp.ReplaceAllString(s, "_"))
}
5 changes: 3 additions & 2 deletions modules/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func isOSWindows() bool {
return runtime.GOOS == "windows"
}

var driveLetterRegexp = regexp.MustCompile("/[A-Za-z]:/")

// FileURLToPath extracts the path information from a file://... url.
func FileURLToPath(u *url.URL) (string, error) {
if u.Scheme != "file" {
Expand All @@ -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.
re := regexp.MustCompile("/[A-Za-z]:/")
if re.MatchString(path) {
if driveLetterRegexp.MatchString(path) {
return path[1:], nil
}
return path, nil
Expand Down
5 changes: 3 additions & 2 deletions services/lfs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func CheckAcceptMediaType(ctx *context.Context) {
}
}

var rangeHeaderRegexp = regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)

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