caddyfile: check heredoc markers without a regexp (#8249)

The formatter checked every token starting with "<<" against the heredoc
marker regexp. In a libFuzzer build each string comparison copies both
strings, and the regexp compares the remaining input on every step, so a
long marker-shaped token made the fuzz-format target time out.

A byte loop accepts the same markers in linear time. The lexer uses it
too, so there is one definition of a valid marker.

(cherry picked from commit 719db53e94)
This commit is contained in:
awss 2026-10-17 14:22:41 +01:00 • committed by Francis Lavoie
parent 02eaa93307
commit 527843272f
No known key found for this signature in database
3 changed files with 52 additions and 4 deletions

View file

@ -153,7 +153,7 @@ func Format(input []byte) []byte {
if heredoc == heredocOpening {
if ch == '\n' {
if len(heredocMarker) > 0 && heredocMarkerRegexp.MatchString(string(heredocMarker)) {
if len(heredocMarker) > 0 && isHeredocMarker(string(heredocMarker)) {
heredoc = heredocOpened
} else {
heredocMarker = nil

View file

@ -19,7 +19,6 @@ import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
"unicode"
)
@ -166,7 +165,7 @@ func (l *lexer) next() (bool, error) {
}
heredocMarker = string(val[2:])
if !heredocMarkerRegexp.Match([]byte(heredocMarker)) {
if !isHeredocMarker(heredocMarker) {
return false, fmt.Errorf("heredoc marker on line #%d must contain only alphanumeric characters, dashes and underscores; got '%s'", l.line, heredocMarker)
}
@ -406,7 +405,23 @@ func (t Token) Clone() Token {
}
}
var heredocMarkerRegexp = regexp.MustCompile("^[A-Za-z0-9_-]+$")
// isHeredocMarker reports whether marker is a valid heredoc marker, made of
// one or more ASCII letters, digits, dashes and underscores. It scans bytes
// instead of matching a regexp because regexp matching of a long string is
// quadratic under libFuzzer instrumentation.
func isHeredocMarker(marker string) bool {
if len(marker) == 0 {
return false
}
for i := range len(marker) {
c := marker[i]
isAlnum := 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9'
if !isAlnum && c != '_' && c != '-' {
return false
}
}
return true
}
// isNextOnNewLine tests whether t2 is on a different line from t1
func isNextOnNewLine(t1, t2 Token) bool {

View file

@ -586,3 +586,36 @@ func BenchmarkLexerLongHeredoc(b *testing.B) {
}
}
}
// TestIsHeredocMarker covers the marker check shared by the lexer and the
// formatter, including a long marker like the one that made the formatter time
// out under libFuzzer when the check was a regexp.
func TestIsHeredocMarker(t *testing.T) {
long := strings.Repeat("a", 500000)
for i, tc := range []struct {
marker string
want bool
}{
{marker: "END", want: true},
{marker: "a-b_C9", want: true},
{marker: "09AZaz", want: true},
{marker: long, want: true},
{marker: "", want: false},
{marker: "EN D", want: false},
{marker: "END\n", want: false},
{marker: "<END", want: false},
{marker: "/", want: false},
{marker: ":", want: false},
{marker: "@", want: false},
{marker: "[", want: false},
{marker: "`", want: false},
{marker: "{", want: false},
{marker: "ÉND", want: false},
{marker: "END\xff", want: false},
{marker: long + "<<<<", want: false},
} {
if got := isHeredocMarker(tc.marker); got != tc.want {
t.Errorf("Test %d: isHeredocMarker(%.20q) = %v, want %v", i, tc.marker, got, tc.want)
}
}
}