From 1e8a8efe3e54f4ed49e42c97f16cb78f2a1a3f4e Mon Sep 17 00:00:00 2001 From: kermieisinthehouse Date: Thu, 23 Sep 2021 03:08:08 +0000 Subject: [PATCH] Escape all regexp control characters (#1753) * Escape all regexp control characters * Add #1752 unit tests Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com> --- pkg/autotag/performer_test.go | 4 ++++ pkg/autotag/studio_test.go | 12 ++++++++++++ pkg/autotag/tag_test.go | 12 ++++++++++++ pkg/autotag/tagger.go | 17 ++--------------- .../src/components/Changelog/versions/v0100.md | 1 + 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/pkg/autotag/performer_test.go b/pkg/autotag/performer_test.go index 7d78b9304..6f23baabe 100644 --- a/pkg/autotag/performer_test.go +++ b/pkg/autotag/performer_test.go @@ -23,6 +23,10 @@ func TestPerformerScenes(t *testing.T) { "performer + name", `(?i)(?:^|_|[^\w\d])performer[.\-_ ]*\+[.\-_ ]*name(?:$|_|[^\w\d])`, }, + { + `performer + name\`, + `(?i)(?:^|_|[^\w\d])performer[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + }, } for _, p := range performerNames { diff --git a/pkg/autotag/studio_test.go b/pkg/autotag/studio_test.go index 2d97bc3fa..a172c9360 100644 --- a/pkg/autotag/studio_test.go +++ b/pkg/autotag/studio_test.go @@ -28,6 +28,12 @@ var testStudioCases = []testStudioCase{ "", "", }, + { + `studio + name\`, + `(?i)(?:^|_|[^\w\d])studio[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + "", + "", + }, { "studio name", `(?i)(?:^|_|[^\w\d])studio[.\-_ ]*name(?:$|_|[^\w\d])`, @@ -40,6 +46,12 @@ var testStudioCases = []testStudioCase{ "alias + name", `(?i)(?:^|_|[^\w\d])alias[.\-_ ]*\+[.\-_ ]*name(?:$|_|[^\w\d])`, }, + { + `studio + name\`, + `(?i)(?:^|_|[^\w\d])studio[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + `alias + name\`, + `(?i)(?:^|_|[^\w\d])alias[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + }, } func TestStudioScenes(t *testing.T) { diff --git a/pkg/autotag/tag_test.go b/pkg/autotag/tag_test.go index f23cab47b..b85c104c8 100644 --- a/pkg/autotag/tag_test.go +++ b/pkg/autotag/tag_test.go @@ -28,6 +28,12 @@ var testTagCases = []testTagCase{ "", "", }, + { + `tag + name\`, + `(?i)(?:^|_|[^\w\d])tag[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + "", + "", + }, { "tag name", `(?i)(?:^|_|[^\w\d])tag[.\-_ ]*name(?:$|_|[^\w\d])`, @@ -40,6 +46,12 @@ var testTagCases = []testTagCase{ "alias + name", `(?i)(?:^|_|[^\w\d])alias[.\-_ ]*\+[.\-_ ]*name(?:$|_|[^\w\d])`, }, + { + `tag + name\`, + `(?i)(?:^|_|[^\w\d])tag[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + `alias + name\`, + `(?i)(?:^|_|[^\w\d])alias[.\-_ ]*\+[.\-_ ]*name\\(?:$|_|[^\w\d])`, + }, } func TestTagScenes(t *testing.T) { diff --git a/pkg/autotag/tagger.go b/pkg/autotag/tagger.go index 4bca225c0..c0555d401 100644 --- a/pkg/autotag/tagger.go +++ b/pkg/autotag/tagger.go @@ -25,22 +25,9 @@ import ( const separatorChars = `.\-_ ` -// fixes #1292 -func escapePathRegex(name string) string { - ret := name - - chars := `+*?()|[]{}^$` - for _, c := range chars { - cStr := string(c) - ret = strings.ReplaceAll(ret, cStr, `\`+cStr) - } - - return ret -} - func getPathQueryRegex(name string) string { // escape specific regex characters - name = escapePathRegex(name) + name = regexp.QuoteMeta(name) // handle path separators const separator = `[` + separatorChars + `]` @@ -52,7 +39,7 @@ func getPathQueryRegex(name string) string { func nameMatchesPath(name, path string) bool { // escape specific regex characters - name = escapePathRegex(name) + name = regexp.QuoteMeta(name) name = strings.ToLower(name) path = strings.ToLower(path) diff --git a/ui/v2.5/src/components/Changelog/versions/v0100.md b/ui/v2.5/src/components/Changelog/versions/v0100.md index 1d5125020..caab78f40 100644 --- a/ui/v2.5/src/components/Changelog/versions/v0100.md +++ b/ui/v2.5/src/components/Changelog/versions/v0100.md @@ -18,6 +18,7 @@ * Added sv-SE language option. ([#1691](https://github.com/stashapp/stash/pull/1691)) ### 🐛 Bug fixes +* Fix panic in autotagger when backslash character present in tag/performer/studio name. ([#1753](https://github.com/stashapp/stash/pull/1753)) * Fix Scene Player CLS issue ([#1739](https://github.com/stashapp/stash/pull/1739)) * Fix Scene Edit Panel form layout for mobile and desktop. ([#1737](https://github.com/stashapp/stash/pull/1737)) * Fix Gallery create plugin hook not being invoked when creating Gallery from folder. ([#1731](https://github.com/stashapp/stash/pull/1731))