From d08da7594a22d56084a80aec8dfbce47409f6a3f Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 19 Dec 2025 16:58:08 +0200 Subject: [PATCH] cap the file name before normalization --- tools/filesystem/file.go | 7 +++++++ tools/filesystem/file_test.go | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tools/filesystem/file.go b/tools/filesystem/file.go index 6be55bac..6cb54264 100644 --- a/tools/filesystem/file.go +++ b/tools/filesystem/file.go @@ -193,6 +193,13 @@ var extInvalidCharsRegex = regexp.MustCompile(`[^\w\.\*\-\+\=\#]+`) const randomAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789" func normalizeName(fr FileReader, name string) string { + // cut the name even if it is not multibyte safe to avoid operating on too large strings + // --- + originalLength := len(name) + if originalLength > 300 { + name = name[originalLength-300:] + } + // extension // --- originalExt := extractExtension(name) diff --git a/tools/filesystem/file_test.go b/tools/filesystem/file_test.go index a1cf7f55..904c2a07 100644 --- a/tools/filesystem/file_test.go +++ b/tools/filesystem/file_test.go @@ -216,8 +216,9 @@ func TestFileNameNormalizations(t *testing.T) { {"a.b.c.d.tar.gz", `^a_b_c_d_\w{10}\.tar\.gz$`}, {"abcd", `^abcd_\w{10}\.txt$`}, {".abcd.123.", `^abcd_\w{10}\.123$`}, - {"a b! c d . 456", `^a_b_c_d_\w{10}\.456$`}, // normalize spaces - {strings.Repeat("a", 101) + "." + strings.Repeat("b", 21), `^a{100}_\w{10}\.b{20}$`}, // name and extension length trim + {"a b! c d . 456", `^a_b_c_d_\w{10}\.456$`}, // normalize spaces + {strings.Repeat("a", 101) + "." + strings.Repeat("b", 21), `^a{100}_\w{10}\.b{20}$`}, // name and extension length cut + {"abc" + strings.Repeat("d", 290) + "." + strings.Repeat("b", 9), `^d{100}_\w{10}\.b{9}$`}, // initial total lenght cut } for i, s := range scenarios {