caddyfile: parser accepts site blocks with unbalanced braces
Issue Details
The Caddyfile parser accepts some site blocks with unbalanced braces, and caddy adapt converts them without any error or warning. Tested on master at b3d67c86.
1. A missing closing } is not detected when the last directive has its own block
:8080 {
header {
X-Foo "bar"
}
caddy adapt correctly parses it. The same happens with other directives (file_server { browse }), with deeper nesting (handle { header { … } }), and with an unclosed global options block ({ servers { protocols h1 } ).
For comparison, this is rejected:
:8080 {
file_server
syntax error: unexpected token 'file_server', expecting '}', at Caddyfile:2
2. An extra } after a site without braces is silently ignored
:8080
file_server
}
This is accepted too. It is also possible to declare two site blocks without curly braces if a } separates them, i.e. the following slightly reworded example from the documentation is accepted:
:8080
root /www/example.com
file_server
}
:3000
reverse_proxy localhost:9000
, even though
To configure multiple sites with the same Caddyfile, you must use curly braces around each one to separate their configurations
Assistance Disclosure
AI used
If AI was used, describe the extent to which it was used.
I found the bug in my own Caddyfiles, came up with the example configs myself and used Claude to understand where the problem lied in code and to draft this issue.
Hi, I have a fix ready for this and will open a PR shortly. It covers both cases from the report:
- A braced site block missing its closing brace now returns
unexpected EOF, including when the last directive has its own block (that directive's}was being counted as the site block's close). - A stray
}after a braceless site now returns an error instead of being treated as the end of the block.
I reproduced both shapes on master (b3d67c8) before the fix and added table cases to TestParseAll; caddy adapt on the patched binary rejects both examples from the issue and still adapts a valid nested config.