Skip to content

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

Merged
francislavoie merged 2 commits intocaddyserver:masterfrom
prettysunflower:master
Mar 3, 2026
Merged

fix(caddyfile): Prevent parser to panic when no token were added by empty {block}#7543
francislavoie merged 2 commits intocaddyserver:masterfrom
prettysunflower:master

Conversation

@prettysunflower
Copy link
Contributor

@prettysunflower prettysunflower commented Mar 3, 2026

Nyallo~

In #7206, some code was changed to make the parser ignore {block} tokens when there is no body to replace it with. As such, no tokens were appended to tokensCopy after this change when this condition was fulfilled.

Unfortunately, the parser code assumed in line 510 that tokens were added to tokensCopy on every iteration of importedTokens. This caused the program to panic with an "index out of range" runtime error.

As a fix, and since this code is supposed to be checking the last index of the slice, we're now checking the length of tokensCopy instead of solely relying on the loop's range index variable (i).

This should fix #7518?

Assistance Disclosure

No AI/LLM was used.

In #7206, some code was changed to make the parser ignore `{block}` tokens when there is no body to replace it with.
As such, no tokens were appended to `tokensCopy` after this change when this condition was fulfilled.

Unfortunately, the parser code assumed in line 510 that tokens were added on every iteration of `importedTokens`.
This caused the program to panic with an "index out of range" runtime error.

As a fix, and since this code is supposed to be checking the last index of the slice, we're now checking the length of `tokensCopy` instead of solely relying on the loop's range index variable.

Signed-off-by: prettysunflower <me@prettysunflower.moe>
@CLAassistant
Copy link

CLAassistant commented Mar 3, 2026

CLA assistant check
All committers have signed the CLA.

@prettysunflower prettysunflower changed the title fix(caddyfile): Prevent parser to panic when no token was added fix(caddyfile): Prevent parser to panic when no token were added by empty {block} Mar 3, 2026
@francislavoie francislavoie added the bug 🐞 Something isn't working label Mar 3, 2026
@francislavoie francislavoie added this to the v2.11.2 milestone Mar 3, 2026
@francislavoie
Copy link
Member

Interesting. Could you add the reproduce case from the issue as a testcase to prove that it fixes it? Thanks!

Signed-off-by: prettysunflower <me@prettysunflower.moe>
@prettysunflower
Copy link
Contributor Author

Sure, I've added a test case (slightly modified from the issue), it panics without this fix, and passes with the fix.

@francislavoie
Copy link
Member

Perfect, thanks!

@ZingyAwesome if you'd like to build from this branch to play around with it to confirm that would be great :)

@francislavoie francislavoie merged commit 2dd3852 into caddyserver:master Mar 3, 2026
27 checks passed
@ZingyAwesome
Copy link

It's working now, thank you for such a fast fix 🎉

@github-actions github-actions bot mentioned this pull request Mar 6, 2026
4 tasks
prettysunflower added a commit to prettysunflower/caddy that referenced this pull request Mar 10, 2026
Resolve issue caddyserver#7557

So, here is the situation:
- Pull request caddyserver#7206 included some changes to the doImport's function of
  Caddyfile's parser. What it does is that if there is no token within a
  block that follows the import, and the import contains `{block}`, then
  the `{block}` token is discarded.
- This implementation had a few flaws:
  - Issue caddyserver#7518 noticed that in cases that `{block}` was not imported,
    a runtime error was raised due to the assumption that tokens were
    always added to `tokensCopy` on every iteration of `importedTokens`.
    This was fixed by pull request caddyserver#7543.
  - Issue caddyserver#7557 notices that {block} can be ignored when imported from a
    certain file. There, it's again an issue with how the import works.
    When `import snippets` is called, this import instruction doesn't
    contains any nested blocks. And when the argument replacer that is
    the `importedTokens` loop is called and finds `{block}`, it uses the
    block from the file's import (which in this case is nothing),
    `{block}` is erased, and unavailable when the import directive is
    called for the imported snippet.

These changes fix the issues raised in caddyserver#7557 and tries to stabilize
the situation by adding a `Disposable` field to `Token`. It works like
this:
- When a `{block}` is encountered in the argument replacer loop, instead
  of ignoring it and not copying it, the token is now copied (instead of
  being erased), but with the `Disposable` field toggled on.
- When subsequent imports happens, the "disposable" `{block}` token is
  available for the processing of the new import, and can be used to
  call snippets from other files.
- When all tokens are processed within a server block, disposable tokens
  are removed from the keys and segments of the blocks.

This way, `{block}` tokens are there for subsequent inputs, and are
removed if unused.

Tests added in pull requests caddyserver#7206 and caddyserver#7543 passes with this new
implementation, confirming that unused `{block}` are accepted if nothing
is passed to `import`, as well as the other usual tests.
A new test was also added based on issue caddyserver#7557 reporting, and also passes.

... hopefully this commit description is clear?

Signed-off-by: prettysunflower <me@prettysunflower.moe>
prettysunflower added a commit to prettysunflower/caddy that referenced this pull request Mar 10, 2026
Resolve issue caddyserver#7557

So, here is the situation:
- Pull request caddyserver#7206 included some changes to the doImport's function of
  Caddyfile's parser. What it does is that if there is no token within a
  block that follows the import, and the import contains `{block}`, then
  the `{block}` token is discarded.
- After this pull request:
  - Issue caddyserver#7518 noticed that in cases that `{block}` was not imported,
    a runtime error was raised due to the assumption that tokens were
    always added to `tokensCopy` on every iteration of `importedTokens`.
    This was fixed by pull request caddyserver#7543.
  - Issue caddyserver#7557 notices that {block} can be ignored when imported from a
    certain file. There, it's again an issue with how the import works.
    When `import snippets` is called, this import instruction doesn't
    contains any nested blocks. And when the argument replacer that is
    the `importedTokens` loop is called and finds `{block}`, it uses the
    block from the file's import (which in this case is nothing),
    `{block}` is erased, and unavailable when the import directive is
    called for the imported snippet.

The changed in this commit addresses the second issue by checking before
replacing `{block}` if we're currently in a snippet definition, and
appending the `{block}` token to `tokensCopy` if we are.

With this changes, when importing those snippets, the `{block}` token
will be available to be replaced by the nested blocks in `tokensToAdd`
if needed, or erased if there are no nested blocks and `tokensToAdd` is empty.

Tests added in pull requests caddyserver#7206 and caddyserver#7543 passes with this new
implementation, confirming that unused `{block}` are accepted if nothing
is passed to `import`, as well as the other usual tests.
A new test was also added based on issue caddyserver#7557 reporting, and also passes.

Signed-off-by: prettysunflower <me@prettysunflower.moe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index out of range error when using {block}

4 participants