Skip to content

root: introduce down-propagating Helper.BlockState for other directives/plugins to use - #7594

Merged
francislavoie merged 7 commits into
caddyserver:masterfrom
henderkes:master
Mar 28, 2026
Merged

francislavoie merged 7 commits into
caddyserver:masterfrom
henderkes:master

Conversation

@henderkes

@henderkes henderkes commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Assistance Disclosure

No AI was used.

I feel like it may be useful to also generate warnings in case a root statement overwrites an existing one, e.g.

localhost

root /path
root * /path2

# or

root * /path1
root * /path2

But that may be a separate discussion.

return nil, h.ArgErr()
}
// store the unmatched root in state so other directives can access it
if userMatcherSet == nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread caddyconfig/httpcaddyfile/builtins.go Outdated
return nil, h.ArgErr()
}
// store the unmatched root in state so other directives can access it
h.State["root"] = h.Val()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have both accesses with strings and with constants (e.g. namedRoute), so I wasn't sure if I should add one here

@francislavoie

Copy link
Copy Markdown
Member

Overwriting root has many legitimate usecases, so let's not add a warning for that. For example you'd have a subpath like handle /uploads/* which changes the root to somewhere else, or intercept which changes the root to allow X-Accel-Redirect to work.

@francislavoie francislavoie changed the title add 'root' key to Helper.State for access in frankenphp's php_server directive root: Set Helper.State for other directives/plugins to use Mar 26, 2026

@francislavoie francislavoie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@francislavoie francislavoie added the feature ⚙️ New feature or request label Mar 26, 2026
@francislavoie francislavoie added this to the v2.11.3 milestone Mar 26, 2026
@francislavoie

francislavoie commented Mar 26, 2026 •

Copy link
Copy Markdown
Member

Worth noting though, this could misbehave:

example.com {
	root /foo

	handle_path /bar* {
		root /bar
		file_server
	}

	php_server # oops, root is /bar now
}

I'm not 100% sure but needs checking.

@henderkes

Copy link
Copy Markdown
Contributor Author

Worth noting though, this could misbehave:

example.com {
	root /foo

	handle_path /bar* {
		root /bar
		file_server
	}

	php_server # oops, root is /bar now
}

I'm not 100% sure but needs checking.

You're right, that breaks it, it overwrites the root to /bar.

@henderkes

henderkes commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor Author
:80 {
	root * public # * optional
	handle_path /bar* {
		root bar # gets initial copy of State, but later changes to parent may not be picked up?
		php_server { # sibling to php_server, sharing State
			worker index.php
		}
	}

	php_server { # sibling to root directive, sharing State
		env APP_ENV test
		worker index.php
	}
}

This works for our purposes now, but I am not sure if the lack of later propagation of parent -> child could cause an issue. You're more familiar with the ways the Caddyfile is parsed and what may need access to later-updated values in the State.

Although it kind of feels weird to me that this was the behaviour in the first place.

Edit: also, since this is a shallow copy, point values in the existing map would still be shared. Should I change this to a deep copy instead?

@henderkes
henderkes requested a review from francislavoie March 26, 2026 16:28
@francislavoie

Copy link
Copy Markdown
Member

I didn't even remember we had State tbh 😂 so I don't remember what it gets used for these days.

A quick scan of the existing usages suggest it's only used for server-level config stuff like tls, logs, and invoke.

I think it could break invoke if it doesn't write back into the parent state because those get pulled out later at the end of server parsing, so if it was hidden within the block scope it would change how that behaves

if state[namedRouteKey] != nil {
for name := range state[namedRouteKey].(map[string]struct{}) {
result := ConfigValue{Class: namedRouteKey, Value: name}
sb.pile[result.Class] = append(sb.pile[result.Class], result)
}
state[namedRouteKey] = nil

@francislavoie

Copy link
Copy Markdown
Member

Maybe it would be better to introduce a new BlockState map which is the same as State except it's scoped like you set up.

@henderkes

Copy link
Copy Markdown
Contributor Author

Thank you for the suggestion, but I think we'd all want to avoid duplicating that map if there's any helping it. It's too late tonight, but I'll try to figure out how this could potentially cause issues (or first verify whether invoke does). Can't hurt to get a bit more familiar with Caddy.

@henderkes

henderkes commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor Author
  • ["root"] is only used by this PR, shouldn't propagate back
  • ["tlsCertTags"] inner reference map, changes will still propagate back to the parent if it had set them. only if parent tlsCertTags wasn't set, it wouldn't see changes by children
    • never seems to check for it, as far as I can see, so no problem
  • ["logCounter"] would need to propagate back... but I don't think it can actually be increased in child directives of a site block?

The real question is what plugins do, outside of Caddy itself. I don't know if any of the would use State in a way where propagation back to the parent matters, but I assume it might. Unfortunately, creating a BlockState for things that should not carry back up seems to be the only way to prevent breakage for sure.

The question is whether it wouldn't be easier to just expose the parentBlock to child handlers instead. That would also satisfy our needs in frankenphp and we could iterate up through parent blocks to find the correct root. Though the BlockState would make it easier for us, of course.

Comment thread caddyconfig/httpcaddyfile/directives.go Resolved
@francislavoie

Copy link
Copy Markdown
Member

Thanks, I'm good with this, we can merge, if you could run gofmt -w on the file :)

@henderkes

Copy link
Copy Markdown
Contributor Author

Thanks, I'm good with this, we can merge, if you could run gofmt -w on the file :)

I made the grave error of upgrading GoLand and been suffering of git and formatting misbehaving since. Getting mighty tired of WSL support getting worse and worse. :/

@henderkes henderkes changed the title root: Set Helper.State for other directives/plugins to use root: introduce down-propagating Helper.BlockState for other directives/plugins to use Mar 28, 2026
@francislavoie

Copy link
Copy Markdown
Member

Error: caddyconfig/httpcaddyfile/directives.go:422:2: unnecessary trailing newline (whitespace)

@henderkes

Copy link
Copy Markdown
Contributor Author

Sorry, fixed!

@francislavoie
francislavoie enabled auto-merge (squash) March 28, 2026 17:39
@francislavoie
francislavoie merged commit 62e9c05 into caddyserver:master Mar 28, 2026
24 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
caddy 2.11.3

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.

- [ ] `resource` blocks have been checked for updates.
<details>
  <summary>release notes</summary>
  <pre>This release improves several aspects of Caddy with minor features, bug fixes, and security patches. Thank you to everyone and their bots who contributed to help make this release the best one yet!

**Security patches:**

- fastcgi: Carrying over a patch from FrankenPHP for a bug that could allow non-PHP files to be executed; collaborated on by @dunglas, @KC1zs4, and @chenjj.
- vars: A more thorough fix for GHSA-m2w3-8f23-hxxf, collaborated by @everping and @vnxme.
- admin: Array index normalization to prevent remote admin socket auth bypass, by @Amemoyoi and bot.
- admin: More rigorous path prefix matching to prevent remote admin socket auth bypass, by @Amemoyoi and bot.

We've also merged a couple PRs that fix upstream security bugs in other projects like quic-go and CertMagic. Thank you to @marten-seemann for maintaining quic-go so diligently!

## What's Changed
* caddyhttp: Sync placeholder expansion in `vars` and `vars_regexp` by @vnxme in caddyserver/caddy#7573
* caddytls: Avoid ACME fallback for implicit Tailscale *.ts.net policies by @steadytao in caddyserver/caddy#7577
* chore: Resolve recent CI failures by @mholt in caddyserver/caddy#7593
* caddytls: Consolidate empty APs more smartly by @mholt in caddyserver/caddy#7567
* rewrite: skip query rename when source key is absent by @steadytao in caddyserver/caddy#7599
* root: introduce down-propagating Helper.BlockState for other directives/plugins to use by @henderkes in caddyserver/caddy#7594
* http: make zstd checksum configurable by @ottenhoff in caddyserver/caddy#7586
* notify: Always send "READY=1" even after an error by @francislavoie in caddyserver/caddy#7597
* reverseproxy: Fix check for `header_up Host {upstream_hostport}` redundancy by @yubiuser in caddyserver/caddy#7564
* caddytls: Expand placeholders in dns_challenge override_domain tls parameter by @pberkel in caddyserver/caddy#7609
* tls: add system and combined CA pool modules by @HarshPatel5940 in caddyserver/caddy#7406
* vars: Don't expand placeholders in values by @vnxme in caddyserver/caddy#7629
* build(deps): bump go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp from 1.42.0 to 1.43.0 by @dependabot[bot] in caddyserver/caddy#7637
* build(deps): bump the all-updates group across 1 directory with 11 updates by @dependabot[bot] in caddyserver/caddy#7641
* reverseproxy: make stream copy buffer size configurable by @steadytao in caddyserver/caddy#7627
* vars: Add matcher placeholder handling tests by @steadytao in caddyserver/caddy#7640
* build(deps): bump github.com/go-jose/go-jose/v4 from 4.1.3 to 4.1.4 by @dependabot[bot] in caddyserver/caddy#7621
* logging: Add journald encoder wrapper by @steadytao in caddyserver/caddy#7623
* caddyfile: Improve import/global options UX for imports before global options by @steadytao in caddyserver/caddy#7642
* chore: replace `interface{}` with `any` for modernization by @tsinglua in caddyserver/caddy#7571
* chore: bump timberjack to v1.4.1 by @DeRuina in caddyserver/caddy#7618
* logging: Preserve ts for journald-wrapped JSON logs by @steadytao in caddyserver/caddy#7644
* fileserver: show symlink targets verbatim (#7476) by @maxtruxa in caddyserver/caddy#7579
* fix(caddyfile): {block} in snippet by @prettysunflower in caddyserver/caddy#7558
* caddyhttp: Document missing placeholders for escaped URI and prefixed query by @steffenbusch in caddyserver/caddy#7659
* chore: add `AGENTS.md` by @mohammed90 in caddyserver/caddy#7652
* build(deps): bump github.com/jackc/pgx/v5 from 5.8.0 to 5.9.0 by @dependabot[bot] in caddyserver/caddy#7655
* admin: Redact sensitive request headers in API logs by @steadytao in caddyserver/caddy#7578
* reverseproxy: add `lb_retry_match` condition on response status by @seroperson in caddyserver/caddy#7569
* caddyhttp: prefer port 443 in auto-HTTPS and add tests by @mholt in caddyserver/caddy#7666
* fix: Propagate ECH keys to the QUIC listener by @steadytao in caddyserver/caddy#7670
* chore: Use atomics where appropriate by @francislavoie in caddyserver/caddy#7648
* metrics: Implement pushing via OLTP by @dunglas in caddyserver/caddy#7664
* logging: Add regression coverage for rotated file mode by @steadytao in caddyserver/caddy#7620
* httpcaddyfile: Inherit global ACME issuer settings in tls shortcuts by @steadytao in caddyserver/caddy#7617
* build(deps): bump github.com/jackc/pgx/v5 from 5.9.0 to 5.9.2 by @dependabot[bot] in caddyserver/caddy#7668
* admin: require path segment boundary in remote access control by @Amemoyoi in caddyserver/caddy#7673
* reverseproxy: Add ability to clear dynamic upstreams cache during retries by @mholt in caddyserver/caddy#7662
* listeners: clean up stale Unix socket files on Windows by @mfrischknecht in caddyserver/caddy#7676
* admin: reject non-canonical config array indices by @Amemoyoi in caddyserver/caddy#7592
* caddytls: Expand ACME credentials by @tribut in caddyserver/caddy#7554
* caddyauth: set user placeholders before auth rejection by @cyphercodes in caddyserver/caddy#7685
* caddyauth: revert user placeholders on auth rejection by @steadytao in caddyserver/caddy#7688
* chore: Fix golangci-lint 2.12.1 findings by @steadytao in caddyserver/caddy#7690
* httpcaddyfile: accept duration strings for log sampling interval by @tomholford in caddyserver/caddy#7694
* tls: Add alpn to managed HTTPS records by @steadytao in caddyserver/caddy#7653
* caddytls: avoid duplicate automation for wildcard-covered hosts by @Rijul-A in caddyserver/caddy#7697
* docs: add documentation for fileExists and fileStat template functions by @steffenbusch in caddyserver/caddy#7700
* rewrite: escape file matcher paths before rewriting by @cyphercodes in caddyserver/caddy#7683
* metrics: Add nil check for metricsHandler in AdminMetrics.serveHTTP by @Br1an67 in caddyserver/caddy#7553

## New Contributors
* @steadytao made their first contribution in caddyserver/caddy#7577
* @henderkes made their first contribution in caddyserver/caddy#7594
* @yubiuser made their first contribution in caddyserver/caddy#7564
* @pberkel made their first contribution in caddyserver/caddy#7609
* @HarshPatel5940 made their first contribution in caddyserver/caddy#7406
* @tsinglua made their first contribution in caddyserver/caddy#7571
* @maxtruxa made their first contribution in caddyserver/caddy#7579
* @seroperson made their first contribution in caddyserver/caddy#7569
* @Amemoyoi made their first contribution in caddyserver/caddy#7673
* @mfrischknecht made their first contribution in caddyserver/caddy#7676
* @tribut made their first contribution in caddyserver/caddy#7554
* @cyphercodes made their first contribution in caddyserver/caddy#7685
* @tomholford made their first contribution in caddyserver/caddy#7694
* @Rijul-A made their first contribution in caddyserver/caddy#7697
* @Br1an67 made their first contribution in caddyserver/caddy#7553

**Full Changelog**: https://github.com/caddyserver/caddy/compare/v2.11.2...v2.11.3</pre>
  <p>View the full release notes at <a href="https://github.com/caddyserver/caddy/releases/tag/v2.11.3">https://github.com/caddyserver/caddy/releases/tag/v2.11.3</a>.</p>
</details>
<hr>



See merge request: Harmonybrew/homebrew-core!842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature ⚙️ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants