Skip to content

Commit e7a534d

Browse files
caddyfile: Reject long heredoc markers (#6098)
Co-authored-by: Mohammed Al Sahaf <[email protected]>
1 parent c78ebb3 commit e7a534d

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

caddyconfig/caddyfile/formatter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package caddyfile
1616

1717
import (
1818
"bytes"
19+
"fmt"
1920
"io"
2021
"unicode"
2122

@@ -118,6 +119,10 @@ func Format(input []byte) []byte {
118119
heredoc = heredocClosed
119120
} else {
120121
heredocMarker = append(heredocMarker, ch)
122+
if len(heredocMarker) > 32 {
123+
errorString := fmt.Sprintf("heredoc marker too long: <<%s", string(heredocMarker))
124+
panic(errorString)
125+
}
121126
write(ch)
122127
continue
123128
}

caddyconfig/caddyfile/formatter_test.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package caddyfile
1616

1717
import (
18+
"fmt"
19+
"os"
1820
"strings"
1921
"testing"
2022
)
@@ -24,6 +26,7 @@ func TestFormatter(t *testing.T) {
2426
description string
2527
input string
2628
expect string
29+
panics bool
2730
}{
2831
{
2932
description: "very simple",
@@ -434,18 +437,36 @@ block2 {
434437
}
435438
`,
436439
},
440+
{
441+
description: "very long heredoc from fuzzer",
442+
input: func() string {
443+
bs, _ := os.ReadFile("testdata/clusterfuzz-testcase-minimized-fuzz-format-5806400649363456")
444+
return string(bs)
445+
}(),
446+
panics: true,
447+
},
437448
} {
438-
// the formatter should output a trailing newline,
439-
// even if the tests aren't written to expect that
440-
if !strings.HasSuffix(tc.expect, "\n") {
441-
tc.expect += "\n"
442-
}
443-
444-
actual := Format([]byte(tc.input))
445-
446-
if string(actual) != tc.expect {
447-
t.Errorf("\n[TEST %d: %s]\n====== EXPECTED ======\n%s\n====== ACTUAL ======\n%s^^^^^^^^^^^^^^^^^^^^^",
448-
i, tc.description, string(tc.expect), string(actual))
449-
}
449+
t.Run(fmt.Sprintf("test case %d: %s", i, tc.description), func(t *testing.T) {
450+
if tc.panics {
451+
defer func() {
452+
if r := recover(); r == nil {
453+
t.Errorf("[TEST %d: %s] Expected panic, but got none", i, tc.description)
454+
}
455+
}()
456+
}
457+
458+
// the formatter should output a trailing newline,
459+
// even if the tests aren't written to expect that
460+
if !strings.HasSuffix(tc.expect, "\n") {
461+
tc.expect += "\n"
462+
}
463+
464+
actual := Format([]byte(tc.input))
465+
466+
if !tc.panics && string(actual) != tc.expect {
467+
t.Errorf("\n[TEST %d: %s]\n====== EXPECTED ======\n%s\n====== ACTUAL ======\n%s^^^^^^^^^^^^^^^^^^^^^",
468+
i, tc.description, string(tc.expect), string(actual))
469+
}
470+
})
450471
}
451472
}

caddyconfig/caddyfile/lexer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (l *lexer) next() (bool, error) {
149149
continue
150150
}
151151

152+
if len(val) > 32 {
153+
return false, fmt.Errorf("heredoc marker too long on line #%d: %s", l.line, string(val))
154+
}
155+
152156
// after hitting a newline, we know that the heredoc marker
153157
// is the characters after the two << and the newline.
154158
// we reset the val because the heredoc is syntax we don't

0 commit comments

Comments
 (0)