caddy icon indicating copy to clipboard operation
caddy copied to clipboard

Design: Stricter Host validation and lower-level HTTP error handling

Open steadytao opened this issue 7 months ago • 4 comments

Issue Details

The recent discussion on #7551, including Matt’s note that related design questions have also been discussed in the dev Slack makes me think this needs a focused public design discussion before the implementation there can really be evaluated on its own.

Relevant context:

  • #7459 raised the immediate bug report: Caddy currently accepts a number of syntactically invalid Host values, especially around IP-literals and malformed ports and returns 200 rather than 400
  • #7551 proposes implementing Host validation in line with RFC 3986 authority/host syntax rules
  • #4841 previously explored a broader “strict HTTP mode” for malformed requests more generally, including malformed query strings, paths and headers

From #4841, the important backdrop seems to be:

  • there are malformed requests that can create surprising behavior or awkward interactions with upstream apps and filesystems even when they are not direct vulnerabilities in Caddy itself
  • stricter request validation has some upside in terms of spec compliance and reducing ambiguous/broken traffic
  • but it also risks breaking legitimate use cases, adds complexity in the hot path and raises questions about where that behavior should live
  • the earlier conclusion there seemed to drift toward “if this exists, it may be better as opt-in / strict mode / plugin territory rather than default core behavior”

From the current #7551 discussion, two design questions seem especially important:

  1. Should Caddy enforce stricter Host syntax than the Go standard library by default, particularly where RFC 3986 defines the grammar for host / authority, RFC 9110 adopts that URI grammar for HTTP semantics, and RFC 9112 gives HTTP/1.1 requirements around malformed requests and Host handling?
  2. How should lower-level HTTP validation/parsing errors be surfaced so users can actually act on them especially given the current limitations around handle_errors and similar routing-level handling?

This also does not seem unique to Caddy. Similar discussions are happening in other projects as well. For example, h2o issue h2o/h2o#3564 raises malformed Host / authority handling and lands on many of the same questions: compatibility risk, unclear security impact and whether spec invalidity alone is enough reason to reject by default. One useful point from that discussion is that while RFC 9110 clearly imports RFC 3986 URI grammar, it is not always equally clear what behavior implementations should adopt by default when input falls outside that grammar especially across HTTP/1.1, HTTP/2, and HTTP/3.

My current opinion:

  • I generally lean toward stricter/spec-compliant handling where it gives clear operational or ecosystem benefit
  • but in this specific case I do not think #7551 should be decided purely as a standalone parsing patch
  • it seems adjacent to the broader strict-HTTP question from #4841 and to the unresolved question of how lower-level request-validation failures should fit into Caddy’s error-handling model
  • so I think the architecture and product direction should be settled first then #7551 can be evaluated against that agreed model

Questions that may be worth settling here:

  1. Should malformed Host handling be a default behavior, an opt-in strict mode or something better suited to a plugin/module boundary?
  2. What concrete benefits would stricter Host validation provide in practice for Caddy users beyond spec compliance?
  3. How should lower-level request validation errors be surfaced so they are observable and, where appropriate, user-actionable?
  4. Should Host validation be treated as a one-off special case or as part of a broader strategy for stricter malformed-request handling?

Assistance Disclosure

AI not used

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

No response

steadytao avatar Apr 04 '26 06:04 steadytao

@mholt, I wanted to add a clearer summary of the reasoning I am putting behind this issue.

My primary concern is not whether malformed Host values are invalid under the relevant RFCs. They are. The harder question is whether stricter handling should be default Caddy behaviour, opt-in strict mode or part of a broader malformed-request model and how lower-level validation failures should surface in practice.

That is why I think the architectural and product questions matter more than the narrow parsing question in isolation:

  • what concrete benefit stricter default handling provides to Caddy users beyond spec compliance alone
  • whether this should be treated as a one-off fix or as part of a broader malformed-request strategy
  • where this behaviour belongs in Caddy’s boundaries
  • how these failures should be surfaced to users and operators

I wrote a longer piece expanding on that reasoning here, if useful: https://steadytao.com/writing/rfc-compliance-isnt-an-implementation-strategy/

steadytao avatar Apr 10 '26 18:04 steadytao

Thanks; these are good questions.

In my opinion:

  • I would prefer to make Host header validation the default behavior, if it does not impact performance. Only add a way to turn it off if there's real-world valid use cases for it.
  • Users should be able to control the error handling using their error routes.

In the broader picture, I'd prefer that users can control more errors from the error routes, but the net/http package still responds in some error cases without ever invoking our handlers. Maybe someday there'll be an upstream patch that gives us an API to hook into those errors. (Maybe it could be a proposal for the Go team.)

mholt avatar Apr 10 '26 20:04 mholt

That makes sense to me. I think that gives us a reasonably clear default direction: validate Host by default if the performance cost is negligible, make opt-out contingent on real-world valid compatibility cases and let users handle these failures through error routes wherever the lower layers allow it.

To me, the main remaining questions are then mostly boundary questions rather than parsing questions:

  • what exactly should be validated at Caddy’s layer versus what remains constrained by net/http
  • which malformed-request cases can realistically be surfaced into error routes and which cannot
  • whether this should stay as a narrow Host decision or be treated as one piece of a broader malformed-request model over time

If the real uncertainty is compatibility rather than spec validity, it may also be worth gathering examples of legitimate traffic patterns that stricter default Host validation would break. A short thread on the forum could be useful for that if we think the potential real-world use cases are not yet clear.

I do think this helps frame #7551 more concretely though so I am happy to leave it here for others to weigh in with their opinions on the matter.

steadytao avatar Apr 11 '26 00:04 steadytao

I tested this with a minimal repro Caddyfile containing one normal handler, one handle_errors route and obvious logging then sent raw HTTP requests over a socket rather than using curl. A forced Caddy error hit handle_errors as expected so the boundary check was working. From there, the split was fairly clear.

Rejected by Go/net/http before Caddy handlers ran with no access log entry and no handle_errors involvement:

  • missing Host
  • duplicate Host
  • invalid header name
  • invalid header value containing a control character
  • malformed request line
  • unsupported HTTP version

Did reach Caddy handlers in the repro and returned the normal handler response:

  • Host: []
  • Host: [12345]
  • Host: [123g::1]
  • Host: example.com:abc

So I think the boundary is now a bit clearer:

  • some malformed-request cases are lower-layer net/http rejections and cannot currently be surfaced through Caddy error routes as expected
  • the malformed Host cases from #7459 do appear to survive into Caddy-space so those are still meaningfully a Caddy-layer policy/validation question rather than purely a lower-layer parsing question

That makes me think stricter default Host validation can be discussed on its own merits while lower-level malformed-request surfacing remains mostly separate.

steadytao avatar Apr 11 '26 13:04 steadytao