summaryrefslogtreecommitdiff
path: root/caddyconfig
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2023-08-19 06:32:32 -0400
committerGitHub <noreply@github.com>2023-08-19 10:32:32 +0000
commit10053f7570b113d41c5a5ba40e6c457ab0784ef5 (patch)
treed244fd9b2022c65e92762df1c4890719fd083832 /caddyconfig
parent0a6d3333b2c92553e66c93c14d3258a790aac639 (diff)
caddyfile: Loosen heredoc parsing (#5761)
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/caddyfile/lexer.go16
-rw-r--r--caddyconfig/caddyfile/lexer_test.go60
2 files changed, 61 insertions, 15 deletions
diff --git a/caddyconfig/caddyfile/lexer.go b/caddyconfig/caddyfile/lexer.go
index 47aaa03..6f72b5d 100644
--- a/caddyconfig/caddyfile/lexer.go
+++ b/caddyconfig/caddyfile/lexer.go
@@ -137,18 +137,28 @@ func (l *lexer) next() (bool, error) {
}
// detect whether we have the start of a heredoc
- if !inHeredoc && !heredocEscaped && len(val) > 1 && string(val[:2]) == "<<" {
- if ch == '<' {
- return false, fmt.Errorf("too many '<' for heredoc on line #%d; only use two, for example <<END", l.line)
+ if !(quoted || btQuoted) && !(inHeredoc || heredocEscaped) &&
+ len(val) > 1 && string(val[:2]) == "<<" {
+ // a space means it's just a regular token and not a heredoc
+ if ch == ' ' {
+ return makeToken(0), nil
}
+
+ // skip CR, we only care about LF
if ch == '\r' {
continue
}
+
// after hitting a newline, we know that the heredoc marker
// is the characters after the two << and the newline.
// we reset the val because the heredoc is syntax we don't
// want to keep.
if ch == '\n' {
+ // check if there's too many <
+ if string(val[:3]) == "<<<" {
+ return false, fmt.Errorf("too many '<' for heredoc on line #%d; only use two, for example <<END", l.line)
+ }
+
heredocMarker = string(val[2:])
if !heredocMarkerRegexp.Match([]byte(heredocMarker)) {
return false, fmt.Errorf("heredoc marker on line #%d must contain only alpha-numeric characters, dashes and underscores; got '%s'", l.line, heredocMarker)
diff --git a/caddyconfig/caddyfile/lexer_test.go b/caddyconfig/caddyfile/lexer_test.go
index 801d81e..ce2f533 100644
--- a/caddyconfig/caddyfile/lexer_test.go
+++ b/caddyconfig/caddyfile/lexer_test.go
@@ -322,15 +322,59 @@ EOF same-line-arg
},
},
{
- input: []byte(`heredoc <EOF
+ input: []byte(`escaped-heredoc \<< >>`),
+ expected: []Token{
+ {Line: 1, Text: `escaped-heredoc`},
+ {Line: 1, Text: `<<`},
+ {Line: 1, Text: `>>`},
+ },
+ },
+ {
+ input: []byte(`not-a-heredoc <EOF
content
- EOF same-line-arg
`),
expected: []Token{
- {Line: 1, Text: `heredoc`},
+ {Line: 1, Text: `not-a-heredoc`},
{Line: 1, Text: `<EOF`},
{Line: 2, Text: `content`},
- {Line: 3, Text: `EOF`},
+ },
+ },
+ {
+ input: []byte(`not-a-heredoc <<<EOF content`),
+ expected: []Token{
+ {Line: 1, Text: `not-a-heredoc`},
+ {Line: 1, Text: `<<<EOF`},
+ {Line: 1, Text: `content`},
+ },
+ },
+ {
+ input: []byte(`not-a-heredoc "<<" ">>"`),
+ expected: []Token{
+ {Line: 1, Text: `not-a-heredoc`},
+ {Line: 1, Text: `<<`},
+ {Line: 1, Text: `>>`},
+ },
+ },
+ {
+ input: []byte(`not-a-heredoc << >>`),
+ expected: []Token{
+ {Line: 1, Text: `not-a-heredoc`},
+ {Line: 1, Text: `<<`},
+ {Line: 1, Text: `>>`},
+ },
+ },
+ {
+ input: []byte(`not-a-heredoc <<HERE SAME LINE
+ content
+ HERE same-line-arg
+ `),
+ expected: []Token{
+ {Line: 1, Text: `not-a-heredoc`},
+ {Line: 1, Text: `<<HERE`},
+ {Line: 1, Text: `SAME`},
+ {Line: 1, Text: `LINE`},
+ {Line: 2, Text: `content`},
+ {Line: 3, Text: `HERE`},
{Line: 3, Text: `same-line-arg`},
},
},
@@ -366,14 +410,6 @@ EOF same-line-arg
},
},
{
- input: []byte(`heredoc <<HERE SAME LINE
- content
- HERE same-line-arg
- `),
- expectErr: true,
- errorMessage: "heredoc marker on line #1 must contain only alpha-numeric characters, dashes and underscores; got 'HERE SAME LINE'",
- },
- {
input: []byte(`heredoc <<<EOF
content
EOF same-line-arg