matcha icon indicating copy to clipboard operation
matcha copied to clipboard

BUG: Unvalidated regex compilation from markdown image syntax

Open andrinoff opened this issue 7 months ago • 2 comments

Describe the bug view/html.go and main.go compile regex patterns in hot paths using regexp.MustCompile() which panics on invalid patterns. While patterns are hardcoded (safe), using MustCompile in non-init code is anti-pattern.

To reproduce

  • main.go:2102, 2939: regexp.MustCompile() in function
  • view/html.go:540, 616, 708, 718, 873: multiple MustCompile in functions

Expected behavior Move MustCompile to package init() or var declarations. Compiled regexes should be package-level constants.

Additional context

  • Good first issue
  • Performance: recompiling regex every call
  • Best practice: compile once at init
  • MustCompile in function is code smell

Suggested fix:

var (
    markdownImageRegex = regexp.MustCompile(`!\[.*?\]\((.*?)\)`)
    // ... other patterns
)

// In function:
matches := markdownImageRegex.FindAllStringSubmatch(body, -1)

OS All platforms

andrinoff avatar Apr 21 '26 09:04 andrinoff

This has had no activity for 30 days. It will be closed in 14 days unless updated. Comment or remove the stale label to keep it open.

floatpanebot avatar May 22 '26 06:05 floatpanebot

Closing due to inactivity. Reopen if still relevant.

floatpanebot avatar Jun 04 '26 19:06 floatpanebot