matcha
matcha copied to clipboard
BUG: Unvalidated regex compilation from markdown image syntax
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
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.
Closing due to inactivity. Reopen if still relevant.