fix(caddyfile): Prevent parser to panic when no token were added by empty {block} (#7543)

This commit is contained in:
prettysunflower
2026-03-03 13:16:21 -05:00
committed by GitHub
parent 11b56c6cfc
commit 2dd3852416
3 changed files with 68 additions and 1 deletions

View File

@@ -507,7 +507,7 @@ func (p *parser) doImport(nesting int) error {
// format, won't check for nesting correctness or any other error, that's what parser does. // format, won't check for nesting correctness or any other error, that's what parser does.
if !maybeSnippet && nesting == 0 { if !maybeSnippet && nesting == 0 {
// first of the line // first of the line
if i == 0 || isNextOnNewLine(tokensCopy[i-1], token) { if i == 0 || isNextOnNewLine(tokensCopy[len(tokensCopy)-1], token) {
index = 0 index = 0
} else { } else {
index++ index++

View File

@@ -0,0 +1,52 @@
import testdata/issue_7518_unused_block_panic_snippets.conf
example.com {
import snippet
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"example.com"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "headers",
"response": {
"set": {
"Reverse_proxy": [
"localhost:3000"
]
}
}
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
# Used by import_block_snippet_non_replaced_block_from_separate_file.caddyfiletest
(snippet) {
header {
reverse_proxy localhost:3000
{block}
}
}
# This snippet being unused by the test Caddyfile is intentional.
# This is to test that a panic runtime error triggered by an out-of-range slice index access
# will not happen again, please see issue #7518 and pull request #7543 for more information
(unused_snippet) {
header SomeHeader SomeValue
}