Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddyconfig: Don't start comments in middle of tokens (#3267)
* caddyconfig: Only parse # as start of comment if preceded by space * caddyconfig: Simplify # logic using len(val), add a test
Francis Lavoie committed
May 5, 2020 at 14:32 UTC
96d6d277a453a602b9ec49131d8e332ce7aefcad
4 files changed
+28
-6
caddyconfig/caddyfile/formatter.go
-3
@@ -131,9 +131,6 @@ func Format(input []byte) []byte {
131
//////////////////////////////////////////////////////////
132
133
if ch == '#' {
134
- if !spacePrior && !beginningOfLine {
135
- write(' ')
136
- }
134
comment = true
135
}
136
caddyconfig/caddyfile/formatter_test.go
+7
-2
@@ -201,7 +201,7 @@ c
201
}
202
203
d {
204
- e #f
204
+ e#f
205
# g
206
}
207
@@ -229,7 +229,7 @@ bar"
229
j {
230
"\"k\" l m"
231
}`,
232
- expect: `"a \"b\" " #c
232
+ expect: `"a \"b\" "#c
233
d
234
235
e {
@@ -305,6 +305,11 @@ bar "{\"key\":34}"`,
305
306
baz`,
307
},
308
+ {
309
+ description: "hash within string is not a comment",
310
+ input: `redir / /some/#/path`,
311
+ expect: `redir / /some/#/path`,
312
+ },
313
} {
314
// the formatter should output a trailing newline,
315
// even if the tests aren't written to expect that
caddyconfig/caddyfile/lexer.go
+1
-1
@@ -141,7 +141,7 @@ func (l *lexer) next() bool {
141
continue
142
}
143
144
- if ch == '#' {
144
+ if ch == '#' && len(val) == 0 {
145
comment = true
146
}
147
if comment {
caddyconfig/caddyfile/lexer_test.go
+20
@@ -77,6 +77,26 @@ func TestLexer(t *testing.T) {
77
{Line: 6, Text: "}"},
78
},
79
},
80
+ {
81
+ input: `host:123 {
82
+ # hash inside string is not a comment
83
+ redir / /some/#/path
84
+ }`,
85
+ expected: []Token{
86
+ {Line: 1, Text: "host:123"},
87
+ {Line: 1, Text: "{"},
88
+ {Line: 3, Text: "redir"},
89
+ {Line: 3, Text: "/"},
90
+ {Line: 3, Text: "/some/#/path"},
91
+ {Line: 4, Text: "}"},
92
+ },
93
+ },
94
+ {
95
+ input: "# comment at beginning of file\n# comment at beginning of line\nhost:123",
96
+ expected: []Token{
97
+ {Line: 3, Text: "host:123"},
98
+ },
99
+ },
100
{
101
input: `a "quoted value" b
102
foobar`,