From 2dd3852416e2b04bc90b98643673ec9e131c32e6 Mon Sep 17 00:00:00 2001 From: prettysunflower Date: Tue, 3 Mar 2026 13:16:21 -0500 Subject: [PATCH] fix(caddyfile): Prevent parser to panic when no token were added by empty {block} (#7543) --- caddyconfig/caddyfile/parse.go | 2 +- ...ced_block_from_separate_file.caddyfiletest | 52 +++++++++++++++++++ ...ssue_7518_unused_block_panic_snippets.conf | 15 ++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_block_from_separate_file.caddyfiletest create mode 100644 caddytest/integration/testdata/issue_7518_unused_block_panic_snippets.conf diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go index d11582392..e9f27dfbf 100644 --- a/caddyconfig/caddyfile/parse.go +++ b/caddyconfig/caddyfile/parse.go @@ -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. if !maybeSnippet && nesting == 0 { // first of the line - if i == 0 || isNextOnNewLine(tokensCopy[i-1], token) { + if i == 0 || isNextOnNewLine(tokensCopy[len(tokensCopy)-1], token) { index = 0 } else { index++ diff --git a/caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_block_from_separate_file.caddyfiletest b/caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_block_from_separate_file.caddyfiletest new file mode 100644 index 000000000..b42a84a09 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/import_block_snippet_non_replaced_block_from_separate_file.caddyfiletest @@ -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 + } + ] + } + } + } + } +} \ No newline at end of file diff --git a/caddytest/integration/testdata/issue_7518_unused_block_panic_snippets.conf b/caddytest/integration/testdata/issue_7518_unused_block_panic_snippets.conf new file mode 100644 index 000000000..0f3e53a2b --- /dev/null +++ b/caddytest/integration/testdata/issue_7518_unused_block_panic_snippets.conf @@ -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 +} \ No newline at end of file