Skip to content

Backticks do not correctly escape brackets #7021

Closed

Description

@bayardar

Context

When trying to use the health_request_body directive, the use of backticks for a json body does not work.
Ex :

  • health_request_body {"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]} --> This works
Host:               192.168.XXX.XXX:XXXX
User-Agent:         Go-http-client/1.1
Content-Length:     63
Accept-Encoding:    gzip
Content-Type:       application/json
Via:                1.1 Caddy
X-Forwarded-For:    192.168.XXX.XXX
X-Forwarded-Host:   blablabla.xyz
X-Forwarded-Proto:  http
JSON
{
    "id": 1,
    "jsonrpc": "2.0",
    "method": "eth_blockNumber",
    "params": []
}
  • health_request_body `{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}` --> This does not works it send an empty body
Host:               192.168.XXX.XXX:XXXX
User-Agent:         Go-http-client/1.1
Content-Length:     0
Accept-Encoding:    gzip
Content-Type:       application/json
Via:                1.1 Caddy
X-Forwarded-For:   192.168.XXX.XXX
X-Forwarded-Host:   blablabla.xyz
X-Forwarded-Proto:  http
No request content

Might be interesting to write that in the doc or even adapt the code to take it into account properly!
This is not working as expected here https://caddyserver.com/docs/caddyfile/concepts#tokens-and-quotes in the backticks section!

How to repoduce

  • OS : debian 12
sudo apt install mitmproxy
mitmproxy --mode reverse:http://<SERVER1_URL>>:<SERVER1_PORT> -p 8080

And here is a sample Caddyfile

http://<YOUR_DESTINATION_URL>:<YOUR_DESTINATION_PORT> {
        log
        reverse_proxy http://<SERVER1_URL>:<SERVER1_PORT> {
                lb_policy first
                health_status 2xx
                health_uri /
                health_method POST
                health_headers {
                        Content-Type application/json
                }
                health_request_body "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_blockNumber\",\"params\":[]}"

                header_up Host {upstream_hostport}
        }
}

http:/<SERVER1_URL>:<SERVER1_PORT> {
        log
        reverse_proxy http://localhost:8080 {
                header_up Host {upstream_hostport}
                header_down X-Acting-Upstream "primary"
        }
}

Now you will see the request handled by mitmproxy, you can inspect the request content.

Activity

  1. transferred this issue fromcaddyserver/websiteon May 13, 2025
  2. francislavoie commented on May 13, 2025

    @francislavoie
    Member

    Did you try escaping the opening { so Caddy doesn't read it as a placeholder?

    `\{"jsonrpc":...
    
  3. ngotchac commented on May 13, 2025

    @ngotchac

    This indeed works, I think it's more of a documentation issue, as it wasn't obvious that health_request_body was using placeholders. This will be an issue for anyone trying to send JSON requests.

    I also think there should be a WARN log in caddy when trying to use unknown/invalid placeholders, such as {"json": ; WDYT?

  4. mholt commented on May 13, 2025

    @mholt
    Member

    We could probably switch to replacing only known placeholders.

  5. bayardar commented on May 14, 2025

    @bayardar
    Author

    @francislavoie Yep escaping { works but as @ngotchac said it is not clear with what is written in the doc since we have an example like that

    directive `{"foo": "bar"}`
    

    @mholt sounds like a great idea!

  6. francislavoie commented on May 14, 2025

    @francislavoie
    Member

    It's dependent on each directive's code. Some do ReplaceAll and some do ReplaceKnown, which is the difference in this case.

  7. IslamElsayed commented on Sep 8, 2026

    @IslamElsayed
    Contributor

    I'd like to take this, if nobody else is on it.

    Following @mholt's suggestion of replacing only known placeholders: in doActiveHealthCheck (modules/caddyhttp/reverseproxy/healthchecks.go) the body uses repl.ReplaceAll at line 434, so any {...} the global replacer doesn't recognise is blanked and a JSON health_request_body goes out empty.

    The same function already does it the other way three lines apart — header values use repl.ReplaceKnown (line 458), but Host on line 454 still uses ReplaceAll, which looks unintentional rather than deliberate. I'd switch both the body and Host to ReplaceKnown and add coverage.

    I'd leave the header name on line 452 as-is unless you'd rather it changed too — a placeholder in a header name feels like a different question from one in a value, and I'd rather not widen the diff on a guess.

    Disclosure per CONTRIBUTING: I'm using AI assistance (Claude) on this. I'll only submit code I can explain line by line and that's covered by tests.

  8. IslamElsayed commented on Sep 8, 2026

    @IslamElsayed
    Contributor

    Correcting myself on the Host line before I open the PR — I said I'd switch it too, and testing says I shouldn't.

    Go's HTTP server blanks a Host header containing braces. Sending Host: {not.a.placeholder} from a plain http.Client to an httptest server, the handler sees r.Host == "". That's stdlib behaviour, nothing to do with Caddy.

    So for line 454 the two options are:

    • ReplaceAll (today): an unknown {...} is blanked, req.Host ends up empty, and Go falls back to the URL host — the request still works.
    • ReplaceKnown: the literal {...} survives into req.Host, Go sends an invalid Host, and the upstream discards it.

    Changing it would make that case worse, not better, and there's no way to write an end-to-end test that asserts anything useful. So the PR touches only the body on line 434, which is the reported bug.

    One thing worth noting from the tests: ReplaceAll doesn't just break bodies with no placeholders. A body that does contain a real one is mangled too — {"token":"{env.SOME_VAR}"} comes out as "} today, because the opening {"token":" is consumed as an unrecognised placeholder before the real one is reached. So this affects anyone sending JSON, whether or not they use placeholders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions