From 527843272f60cbf07f8be3ec634cfd2d9f1bb4b1 Mon Sep 17 00:00:00 2001 From: awss Date: Sat, 17 Oct 2026 14:22:41 +0100 Subject: [PATCH] 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 719db53e946181dd10a9aae36721c3251f7f8dc1) --- caddyconfig/caddyfile/formatter.go | 2 +- caddyconfig/caddyfile/lexer.go | 21 +++++++++++++++--- caddyconfig/caddyfile/lexer_test.go | 33 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/caddyconfig/caddyfile/formatter.go b/caddyconfig/caddyfile/formatter.go index d25aad2d3..e58c1741d 100644 --- a/caddyconfig/caddyfile/formatter.go +++ b/caddyconfig/caddyfile/formatter.go @@ -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 diff --git a/caddyconfig/caddyfile/lexer.go b/caddyconfig/caddyfile/lexer.go index 6b60627e6..eff0bb401 100644 --- a/caddyconfig/caddyfile/lexer.go +++ b/caddyconfig/caddyfile/lexer.go @@ -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 { diff --git a/caddyconfig/caddyfile/lexer_test.go b/caddyconfig/caddyfile/lexer_test.go index d4b60ec55..09f45e1a6 100644 --- a/caddyconfig/caddyfile/lexer_test.go +++ b/caddyconfig/caddyfile/lexer_test.go @@ -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: "