diff options
author | Francis Lavoie <lavofr@gmail.com> | 2022-03-23 14:34:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-23 12:34:13 -0600 |
commit | 134b8056444d8f417c0eb4163809f9659ffc3317 (patch) | |
tree | 62bc3fc0af4af24926730f63af56740943e3f36f /caddyconfig/caddyfile/dispenser.go | |
parent | c9b5e7f77b8aac7334d81c552c583af81ba1c400 (diff) |
caddyfile: Prevent bad block opening tokens (#4655)
* caddyfile: Prevent bad block opening tokens
* Clarifying comments
Diffstat (limited to 'caddyconfig/caddyfile/dispenser.go')
-rwxr-xr-x | caddyconfig/caddyfile/dispenser.go | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/caddyconfig/caddyfile/dispenser.go b/caddyconfig/caddyfile/dispenser.go index fbe71ad..8a78f43 100755 --- a/caddyconfig/caddyfile/dispenser.go +++ b/caddyconfig/caddyfile/dispenser.go @@ -458,6 +458,60 @@ func (d *Dispenser) isNewLine() bool { if d.cursor > len(d.tokens)-1 { return false } - return d.tokens[d.cursor-1].File != d.tokens[d.cursor].File || - d.tokens[d.cursor-1].Line+d.numLineBreaks(d.cursor-1) < d.tokens[d.cursor].Line + + prev := d.tokens[d.cursor-1] + curr := d.tokens[d.cursor] + + // If the previous token is from a different file, + // we can assume it's from a different line + if prev.File != curr.File { + return true + } + + // The previous token may contain line breaks if + // it was quoted and spanned multiple lines. e.g: + // + // dir "foo + // bar + // baz" + prevLineBreaks := d.numLineBreaks(d.cursor - 1) + + // If the previous token (incl line breaks) ends + // on a line earlier than the current token, + // then the current token is on a new line + return prev.Line+prevLineBreaks < curr.Line +} + +// isNextOnNewLine determines whether the current token is on a different +// line (higher line number) than the next token. It handles imported +// tokens correctly. If there isn't a next token, it returns true. +func (d *Dispenser) isNextOnNewLine() bool { + if d.cursor < 0 { + return false + } + if d.cursor >= len(d.tokens)-1 { + return true + } + + curr := d.tokens[d.cursor] + next := d.tokens[d.cursor+1] + + // If the next token is from a different file, + // we can assume it's from a different line + if curr.File != next.File { + return true + } + + // The current token may contain line breaks if + // it was quoted and spanned multiple lines. e.g: + // + // dir "foo + // bar + // baz" + currLineBreaks := d.numLineBreaks(d.cursor) + + // If the current token (incl line breaks) ends + // on a line earlier than the next token, + // then the next token is on a new line + return curr.Line+currLineBreaks < next.Line } |