caddy icon indicating copy to clipboard operation
caddy copied to clipboard

headers, rewrite: replace with an empty search inserts the replacement between every character

Open wankhede04 opened this issue 1 month ago • 0 comments

Issue Details

Header replace operations (header, request_header, header_up, header_down), uri query replace, and uri replace misbehave when the search string is empty: the replacement is inserted between every character of the value. For example a header value "abc" with replace "-" becomes "-a-b-c-", and uri replace turns "/abc?x=yz" into "-/-a-b-c-?-x-=-y-z-".

How an empty search happens in practice:

  • Caddyfile: header X-Foo "" bar. The Caddyfile adapter always builds Replacement{SearchRegexp: value}, so an empty argument, or an empty environment variable such as {$FOO}, gives an empty search_regexp.
  • JSON: a replacement with neither search nor search_regexp set. Only setting both is rejected today (HeaderOps.validate).
  • At runtime: a search or search_regexp that is non-empty in the config but expands to "" (for example a placeholder for a header that is not present). The expanded value is used directly, so an empty regexp is compiled per request, with the same result.
  • uri replace: substrReplacer.do already returns early when the configured find is empty, but it checks the raw config value, not the value after placeholders are expanded.

Where in the code (master at 32bba15):

  • modules/caddyhttp/headers/headers.go: HeaderOps.validate and the replace loop in HeaderOps.ApplyTo (strings.ReplaceAll with search == "").
  • modules/caddyhttp/rewrite/rewrite.go: queryOpsReplacement.Provision, queryOps.do (two strings.ReplaceAll calls), substrReplacer.do.
  • Precedent: rewrite.go Provision already rejects an empty path_regexp find ("find cannot be empty").

Proposal (I have a small patch with tests ready):

  1. At provision time, return an error when both search and search_regexp are empty, for header replace and for query replace ("search or search_regexp must be set").
  2. At request time, skip the replacement when the search is empty AFTER placeholder expansion, in header replace (substring and regexp), query replace and uri replace.

Design tradeoff I would like a decision on: error when the search is literally empty in the config, but skip silently when it is only empty after placeholder expansion (a literally empty search is a config mistake that should be visible; an expanded-empty value is data-dependent and failing the request would be worse than leaving the value unchanged). One consequence: header X-Foo "{$UNSET_VAR}" bar now fails to load, because environment variables are substituted when the Caddyfile is parsed. If you would rather skip silently in every case, or only skip at runtime and never error, that is a smaller change and I can adjust the patch.

I searched existing issues and PRs ("header replace empty", "empty search", "replace empty regexp", "query replace") and found nothing about this.

Assistance Disclosure

AI used

If AI was used, describe the extent to which it was used.

An AI coding assistant (Claude) helped read the code, draft the change and write the tests. I reviewed the code, ran the tests myself, and can explain every change.

wankhede04 avatar Oct 29 '26 13:10 wankhede04