Skip to content

reverseproxy: replace only known placeholders in health check body - #8003

Merged
mholt merged 1 commit into
caddyserver:masterfrom
IslamElsayed:fix-health-check-body-placeholders
Sep 9, 2026
Merged

mholt merged 1 commit into
caddyserver:masterfrom
IslamElsayed:fix-health-check-body-placeholders

Conversation

@IslamElsayed

Copy link
Copy Markdown
Contributor

Fixes #7021. Implements @mholt's suggestion in that thread ("We could probably switch to replacing only known placeholders"), scoped to the body.

Problem

The active health check body is expanded with repl.ReplaceAll, which blanks any {...} the replacer doesn't recognise. Only globals are available here, so a JSON health_request_body is sent as an empty body:

reverse_proxy 10.0.0.2:80 {
	health_request_body `{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}`
}

The header values three lines below already use ReplaceKnown, so the two halves of the same request were built with different rules.

This isn't limited to bodies without placeholders. A body that does use one is mangled too — {"token":"{env.SOME_VAR}"} goes out as "}, because the opening {"token":" is consumed as an unrecognised placeholder before the real one is reached. Both are in the tests.

Fix

One line: ReplaceAll → ReplaceKnown for the body. Unrecognised braces are left intact; real placeholders still expand.

Scope: why the Host header is deliberately left alone

I said on the issue that I'd change Host on line 454 too, since it uses ReplaceAll while the header values below it use ReplaceKnown. Testing showed that would make it worse, so I didn't.

Go's HTTP server blanks a Host containing braces — sending Host: {not.a.placeholder} to an httptest server, the handler sees r.Host == "". That's stdlib behaviour. So today ReplaceAll blanks an unknown placeholder and Go falls back to the URL host and the request works; ReplaceKnown would leave the literal {...} in req.Host and the upstream would discard it. Happy to revisit if you read it differently.

Tests

Two cases in active_health_test.go, built on the existing newActiveHandler/runActiveHealthCheck harness:

  • a JSON body arrives intact
  • a body containing {env.…} still has the placeholder expanded

Both fail on master and pass with the change:

--- FAIL: TestActiveHealthCheckBodyKeepsUnknownPlaceholders
    health check body = "", want "{\"jsonrpc\":\"2.0\",...}"
--- FAIL: TestActiveHealthCheckBodyStillReplacesKnownPlaceholders
    health check body = "\"}", want "{\"token\":\"s3cret\"}"

go test ./modules/caddyhttp/..., go vet and gofmt are clean.

Disclosure

Per CONTRIBUTING: this was written with AI assistance (Claude). I can explain every line, and the behaviour of both the fix and the Host finding above was verified by running the tests, not inferred.

The active health check body is user-supplied and commonly JSON, but it
was expanded with ReplaceAll, which blanks any `{...}` the replacer does
not recognize. Only globals are available here, so a JSON
health_request_body was sent as an empty body.

This is not limited to bodies without placeholders. A body that does use
one is mangled too: `{"token":"{env.SOME_VAR}"}` is sent as `"}`, since
the opening `{"token":"` is consumed as an unrecognized placeholder
before the real one is reached.

Use ReplaceKnown, matching the header values a few lines below, so
unrecognized braces are left intact and real placeholders still expand.

Only the body is changed. The Host header on the same path keeps
ReplaceAll deliberately: Go's HTTP server blanks a Host containing
braces, so leaving `{...}` intact there would send a Host the upstream
discards, where blanking it lets Go fall back to the URL host.

@steadytao steadytao 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.

Follows the same known-placeholder policy as health-check headers. LGTM.

@CLAassistant

CLAassistant commented Sep 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@steadytao

Copy link
Copy Markdown
Member

CLA assistant and its delays. Rechecked the PR so you can sign now.

@caddyserver caddyserver deleted a comment from CLAassistant Sep 9, 2026
@mholt
mholt merged commit 56ae39b into caddyserver:master Sep 9, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backticks do not correctly escape brackets

4 participants