Skip to content

regexutil

A tiny, framework-free helper that makes it safe to compile a regular expression whose pattern came from outside your binary (a config file, a CLI flag, a TUI input box, an HTTP payload) by capping its length and bounding how long compilation may run.

import "gitlab.com/phpboyscout/go/regexutil"

The problem: compilation is the DoS surface, not matching

Go's regexp package is built on RE2, which guarantees linear-time matching. It does not suffer the classic catastrophic backtracking that lets (a+)+b hang a Perl- or Java-style engine at match time. It is easy to assume that makes untrusted patterns safe.

It does not. RE2 protects the match; it does not make compilation guaranteed linear. A pathological pattern (in practice a long chain of bounded repetitions, such as (abc|def|ghi){0,999} repeated up to the length cap) can spend measurable wall-clock time inside regexp.Compile and allocate an automaton of many thousands of states. That is enough to hang a CLI's update flow or freeze an interactive TUI while a single compile churns.

So the moment a pattern originates from a user or a config file, the unbounded call is the risk:

// Risky: an attacker (or a fat-fingered config) controls how long this runs
// and how much it allocates.
re, err := regexp.Compile(userPattern)

The two defences

regexutil applies two bounds to every call:

  1. A byte-length cap: MaxPatternLength (1024). Oversize patterns are rejected before any compile work begins. Legitimate filename globs and search queries are short; 1 KiB is generous.
  2. A wall-clock compile timeout: DefaultCompileTimeout (100 ms). This bounds the worst case for anything that passes the length cap. A well-behaved 1 KiB pattern compiles in well under a millisecond, so 100 ms leaves hundreds of times the headroom and is still imperceptible interactively.

Length alone cannot tell a cheap 1 KiB pattern from an expensive one; a timeout alone would still let a huge pattern allocate before the clock runs out. The two together are defence in depth. The threat model has the reasoning.

Framework-free by design

  • One dependency. The runtime graph holds exactly one module beyond the standard library, the toolkit's own go/errors. No config framework, no TUI, no logging stack. depfootprint_test.go fails the build if go-tool-base, the cobra/viper/charmbracelet stack, OpenTelemetry or a cloud SDK enters the graph.
  • Typed sentinel errors. ErrPatternTooLong, ErrPatternCompileTimeout, and ErrPatternInvalid let callers branch precisely with errors.Is.
  • Two entry points. CompileBounded for a call site that already carries a context.Context; CompileBoundedTimeout for one that does not.

Install

go get gitlab.com/phpboyscout/go/regexutil

Quick start

package main

import (
    "context"
    "fmt"

    "gitlab.com/phpboyscout/go/errors"
    "gitlab.com/phpboyscout/go/regexutil"
)

func main() {
    userPattern := `^feature/.*$` // from a config file, flag, or prompt

    re, err := regexutil.CompileBounded(context.Background(), userPattern)
    switch {
    case errors.Is(err, regexutil.ErrPatternTooLong):
        fmt.Println("pattern is too long; shorten it")
        return
    case errors.Is(err, regexutil.ErrPatternCompileTimeout):
        fmt.Println("pattern is too complex to compile safely")
        return
    case errors.Is(err, regexutil.ErrPatternInvalid):
        fmt.Println("pattern is not valid regex syntax")
        return
    case err != nil:
        fmt.Println("unexpected error:", err)
        return
    }

    fmt.Println("compiled:", re.MatchString("feature/login"))
}

When not to reach for this

regexutil is for patterns you did not write. A pattern that is a literal known at build time carries no DoS risk (you control it), so keep using regexp.MustCompile directly:

var branchRE = regexp.MustCompile(`^feature/.*$`) // build-time literal: fine

Where to go next

The documentation follows the Diátaxis framework:

Further reading

The blog carries a curated route through this subject: Building a command-line tool in Go collects everything written about it, ordered so you can start at the beginning rather than newest-first.

Ask phpbotscout

phpbotscout

He answers questions about the projects over on the Discord, citing the docs where they already cover it, and offering to raise an issue where they don't. Bring a bug, an idea, or a questionable engineering decision.

Join the Discord