From 8afc1b1cb5ad44ab5dd9597a814b19412702ede3 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Wed, 21 Jun 2023 14:57:18 -0500 Subject: 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 --- modules/repository/generate.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/repository/generate.go') diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 102c5af1c9..cb25daa10b 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -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, "_")) } -- cgit v1.2.3