Skip to content

respond blanks literal braces in header values (unknown placeholders) #8013

Closed

Description

@hktitof

Which area

caddyhttp (respond)

What happened

the respond directive expands header field names and values with repl.ReplaceAll, which blanks any {...} the replacer does not recognize

a config like

respond {
  header X-Json {"key":"value"}
  header X-Lit value-{unknown}-kept
}

sends X-Json empty, and X-Lit arrives as value--kept, bcs the whole unrecognized brace span gets swallowed

the body on the same handler already uses repl.ReplaceKnown (that is why JSON bodies survive), and the header handler was switched to ReplaceKnown in #4880 after the same class of report in #4418, so respond headers look like the last site still on ReplaceAll

Repro (1 command, go test on master 425a338)

go test ./modules/caddyhttp/ -run TestStaticResponseHeadersKeepUnknownPlaceholders -count=1

raw output on untouched master:

--- FAIL: TestStaticResponseHeadersKeepUnknownPlaceholders (0.00s)
    staticresp_test.go:88: X-Json header = "", want "{\"key\":\"value\"}" (unknown placeholders in header values must not be blanked)
    staticresp_test.go:91: X-Lit header = "value--kept", want "value-{not-a-real-placeholder}-kept"
FAIL

expected: header values with unknown {...} survive, real placeholders still expand

Version

master 425a338 (post v2.10.2)

Assistance disclosure

AI used: yes, for drafting the test and formatting this report

Activity

  1. steffenbusch commented on Sep 13, 2026

    @steffenbusch
    Contributor

    Hi @steadytao, @hktitof

    the respond directive expands header field names and values with repl.ReplaceAll, which blanks any {...} the replacer does not recognize

    a config like

    respond {
    header X-Json {"key":"value"}
    header X-Lit value-{unknown}-kept
    }
    sends X-Json empty, and X-Lit arrives as value--kept, bcs the whole unrecognized brace span gets swallowed

    Just a note that the respond directive in Caddyfile does not even have header sub-directives - the JSON config has https://caddyserver.com/docs/json/apps/http/servers/routes/handle/static_response

    $ cat Caddyfile-test
    :8977 {
            respond {
                    header X-Json {"key":"value"}
                    header X-Lit value-{unknown}-kept
            }
    }
    
    $ ./caddy adapt -a caddyfile --config Caddyfile-test
    Error: parsing caddyfile tokens for 'respond': wrong argument count or unexpected line ending after 'respond', at Caddyfile-test:2
    

    I'm not sure if replacing repl.ReplaceAll with repl.ReplaceKnown is a good idea in more and more places (other PRs, in particular #8008 for request_body) as it could break existing configurations where placeholders derived from e.g. request headers are preferred to be empty rather than the placeholder configuration itself. I understand that for JSON it's more convenient, but the opening { can also be escaped with \{ if i remember @francislavoie correctly (caddyserver/website#468) to avoid replacing at all.

  2. hktitof commented on Sep 13, 2026

    @hktitof
    ContributorAuthor

    good catch on the caddyfile snippet @steffenbusch, you are right that respond has no header subdirective so that example only works in the JSON config where static_response.headers lives, the go test on master is the actual repro, my bad for the confusing snippet

    on ReplaceKnown vs ReplaceAll the direction was already set by mholt in #7021 and the maintainers applied it themselves, #4880 switched the header directive back in 2022 after #4418 reported the same blanking, and #8003 plus #8014 landed the same way for health and respond headers

    the blanking of unknown {...} is exactly the bug both reports are about, so expanding only known placeholders keeps literal text like json working without escapes while { still exists for the rare case where you want a known placeholder name as literal text

    the concern about configs that relied on the blanking is fair though, that call belongs to the maintainers, #8008 (request body) and #8017 (basic auth) are open for the remaining sites so let's see what they prefer there

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