From 17ac892df5ea0c898e4efe649835e4216315bdc7 Mon Sep 17 00:00:00 2001 From: karnati praveen Date: Fri, 27 Nov 2026 06:35:30 +0000 Subject: [PATCH] fix(glob): treat escaped characters as literals (#40516) The glob compiler wrote a backslash followed by the escaped character into the generated regexp, so `\d`, `\w`, `\s` and `\b` were parsed as regexp constructs instead of the literal character. `a\d` compiled to `^a\d$` and matched "a5" instead of "ad"; `a\b` became a zero-width word boundary. Escaping a character Go does not recognise, e.g. `\C`, made the pattern fail to compile, and protected branch rules then fell back to matching the raw rule text literally. Escape only ASCII punctuation, which Go always reads as a literal, and write letters, digits and non-ASCII characters as-is. This matches gobwas/glob and act. Rules that relied on `\d` matching a digit since https://github.com/go-gitea/gitea/pull/35478 now match a literal `d`. Assisted-by: OpenCode (DeepSeek V4.1 Flash) Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: silverwind --- modules/glob/glob.go | 15 +++++++++++---- modules/glob/glob_test.go | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/glob/glob.go b/modules/glob/glob.go index 363868c8062..0b417ed6a1e 100644 --- a/modules/glob/glob.go +++ b/modules/glob/glob.go @@ -9,6 +9,7 @@ import ( "regexp" "slices" "strings" + "unicode" "gitea.dev/modules/util" ) @@ -35,6 +36,14 @@ type globCompiler struct { negativeFlip bool } +// writeLiteral escapes only ASCII punctuation, an escaped letter like \d is a regexp class +func (g *globCompiler) writeLiteral(r rune) { + if r <= unicode.MaxASCII && !unicode.IsLetter(r) && !unicode.IsDigit(r) { + g.builder.WriteByte('\\') + } + g.builder.WriteRune(r) +} + // compileChars compiles character class patterns like [abc] or [!abc] func (g *globCompiler) compileChars() error { g.builder.WriteByte('[') @@ -56,8 +65,7 @@ func (g *globCompiler) compileChars() error { if g.pos >= len(g.globPattern) { return errors.New("unterminated character class escape") } - g.builder.WriteByte('\\') - g.builder.WriteRune(g.globPattern[g.pos]) + g.writeLiteral(g.globPattern[g.pos]) g.pos++ } else { g.builder.WriteRune(c) @@ -135,8 +143,7 @@ func (g *globCompiler) compile(subPattern bool) error { if g.pos >= len(g.globPattern) { return errors.New("no character to escape") } - g.builder.WriteByte('\\') - g.builder.WriteRune(g.globPattern[g.pos]) + g.writeLiteral(g.globPattern[g.pos]) g.pos++ case '.', '^', '$', '(', ')', '|': g.builder.WriteByte('\\') diff --git a/modules/glob/glob_test.go b/modules/glob/glob_test.go index 0eb101fa14b..c69dfc8535c 100644 --- a/modules/glob/glob_test.go +++ b/modules/glob/glob_test.go @@ -90,6 +90,9 @@ func TestGlob(t *testing.T) { glob(true, "?at", "fat"), glob(true, "*", "abc"), glob(true, `\*`, "*"), + glob(true, `a\d`, "ad"), + glob(true, `[\d]`, "d"), + glob(false, `[a\-z]`, "m"), glob(true, "**", "a.b.c", '.'), glob(false, "?at", "at"),