caddy: `reverse_proxy` leads to duplicate `Server` headers

When using Caddy’s reverse_proxy in front of a server that sets a Server header, Caddy prepends its own Server header rather than replacing it, leading to duplicate Server headers. This violates RFC 9110 §5.3:

This means that, aside from the well-known exception noted below, a sender MUST NOT generate multiple field lines with the same name in a message (whether in the headers or trailers) or append a field line when a field line of the same name already exists in the message, unless that field’s definition allows multiple field line values to be recombined as a comma-separated list (i.e., at least one alternative of the field’s definition allows a comma-separated list, such as an ABNF rule of #(values) defined in Section 5.6.1).

as Server is not defined as comma-separated.

Caddyfile for self-contained reproduction:

http://localhost:1234 {
	reverse_proxy http://localhost:1235
}

http://localhost:1235 {
	reverse_proxy http://localhost:1236
}

http://localhost:1236 {
	header Server "MyServer"
	respond "Hello"
}
# caddy version
v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=
# caddy run
2024/04/26 20:13:00.317	INFO	using adjacent Caddyfile
2024/04/26 20:13:00.318	INFO	admin	admin endpoint started	{"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2024/04/26 20:13:00.318	INFO	tls.cache.maintenance	started background certificate maintenance	{"cache": "0xc0000b6000"}
2024/04/26 20:13:00.319	INFO	http.log	server running	{"name": "srv0", "protocols": ["h1", "h2", "h3"]}
2024/04/26 20:13:00.319	INFO	http.log	server running	{"name": "srv1", "protocols": ["h1", "h2", "h3"]}
2024/04/26 20:13:00.319	INFO	http.log	server running	{"name": "srv2", "protocols": ["h1", "h2", "h3"]}
2024/04/26 20:13:00.319	INFO	autosaved config (load with --resume flag)	{"file": "/root/.config/caddy/autosave.json"}
2024/04/26 20:13:00.319	INFO	serving initial configuration
2024/04/26 20:13:00.365	WARN	tls	storage cleaning happened too recently; skipping for now	{"storage": "FileStorage:/root/.local/share/caddy", "instance": "29a71809-9042-40ac-854b-3b22247b7194", "try_again": "2024/04/27 20:13:00.365", "try_again_in": 86399.999998755}
2024/04/26 20:13:00.365	INFO	tls	finished cleaning storage units
^Z
[1]+  Stopped                 caddy run
# bg
[1]+ caddy run &
# curl -i http://localhost:1234
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain; charset=utf-8
Date: Fri, 26 Apr 2024 20:13:10 GMT
Server: Caddy
Server: Caddy
Server: MyServer

Hello

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 29 (14 by maintainers)

Most upvoted comments

FWIW – sending multiple Server headers is indeed violating the HTTP specification, for the reasons cited. If caddy is acting as an intermediary, the appropriate place to carry this information is the Via header field. When it acts as a reverse proxy (aka Surrogate Origin Server, Gateway), it would be appropriate to drop the existing Server header field (or move it to a different field) and insert its own single server value.

There’s no impossibility. Possible reconciliations include

  1. Discard the upstream Server header, and set our own Server.
  2. Pass through the upstream Server header unchanged, and don’t set our own Server.
  3. Rename the upstream Server header to (say) X-Upstream-Server, which we define to be comma-separated to allow multiple values, and set our own Server.
  4. Modify the upstream Server header, such as by adding a prefix or suffix.

As an origin server, we have the freedom to choose any of these.

Now, I’m one to be quick to want to comply with the standard. So is Francis. So Francis and I discussed this in Slack, and I actually find his arguments quite compelling.

  • We are already conforming with the de-facto standard behaviour. This is true, nginx has done this for years and years, and is what a lot of the Web expects, even though the latest standard potentially says otherwise. (It does leave some room for interpretation, read on.)

  • A reverse proxy is an origin. The same RFC 9110 says in Section 3.7:

    A “gateway” (a.k.a. “reverse proxy”) is an intermediary that acts as an origin server for the outbound connection but translates received requests and forwards them inbound to another server or servers.

    I was under the impression that a proxy is just a relay/intermediate, but the spec actually says reverse proxies act as origins, thus it follows that they should be allowed to set a Server header. (I can see how a forward proxy would NOT be an origin. But a reverse proxy? It is an authority to respond for its domain.)

  • What is the actual problem? We are not sure what problem this is causing (as it is not new behavior, by a long shot) and what changing this would really fix. But we do know that changing this would actually cause us some problems… next point:

  • The current behaviour is the most useful, and can be opted-out very easily. As Francis said to me, “The amount of times I’ve been able to successfully debug things on the forums because of Server: Caddy appearing twice… very very useful. If we hid it by default when proxying then we have no way to prove that the request did get handled by Caddy, especially when people have their DNS wrong and they think they’re hitting Caddy but they’re not.” Having this current behaviour be the default is highly advantageous to our helpers.

  • The RFC strictness on multiple header fields is intended for other use cases. I believe, IIRC, that our Server header is actually ordered appropriately, for one thing. So that part is still in compliance with the RFC regarding order mattering. While it does seem to suggest that only list-formatted fields can appear multiple times, the Server field is kind of a list, it’s just space-separated instead of comma-separated, purely for compatibility reasons I suppose. But the semantics are similar.

While I definitely don’t want to cause breakage, I can see both sides of the argument here. The RFC does seem to say that there should only be 1 Server header, but distinguishing comma-separated lists from space-separated lists seems silly in this context. It also seems that a reverse proxy is, in itself, an origin, even before considering the de-facto standard behavior today. The presence of our Server header is also extremely useful in helping people.

Is the current behavior causing some sort of problem?

@mnot Via looks like it will do. 👍

The “server” field takes a single entry. Sending several fields with the same name can lead to:

  • components ignoring the first one
  • components ignoring the last one
  • components combining them with “,”, causing a broken field value
  • components ignoring the field values as invalid
  • components dropping the complete message as invalid

So adding a new Server response field to a response that already has one is an extremely bad idea, and recipients can rightfully reject them. Actually, they should.

@andersk I’m afraid none of those solutions are acceptable.

  1. Discard the upstream Server header, and set our own Server.
  2. Rename the upstream Server header to (say) X-Upstream-Server, which we define to be comma-separated to allow multiple values, and set our own Server.
  3. Modify the upstream Server header, such as by adding a prefix or suffix.

These result in a broken HTTP proxy, so they’re out.

  1. Pass through the upstream Server header unchanged, and don’t set our own Server.

This results in confusion when troubleshooting and inconsistent behavior in production, sometimes the Server header is from one instance, sometimes it’s from another.

@Dreamsorcerer

Welcome!

HTTP request smuggling attacks.

Interesting, can you demonstrate a security exploit by smuggling the Server header?

That creates an impossible condition for a correct HTTP reverse proxy, then, because there would be no way to reconcile the upstream’s Server header versus the proxy’s Server header. Again, I don’t think that’s in spirit of the RFC nor what the standards authors intended.

This was not an overnight change. The same requirement was present in HTTP 1.0 and 1.1 (RFC 2068 §4.2 and RFC 2616 §4.2):

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one “field-name: field-value” pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.

I also think the argument being made is not in spirit with what the RFC intended. If so, it introduced a huge breaking change across millions of servers that have been deployed for decades, contrary to the guidance in previous RFCs. In practice, many responses on the Internet still have multiple Server headers, including from nginx.

I wonder why multiple headers breaks aiohttp (other than acting like the police and deliberately producing an error). Like what actual technical problem is it creating with aiohttp?

In any case, I’m not convinced that we have anything to fix here, and I’m not convinced that the spec authors intended to break millions of deployed servers overnight.

The MUST in the RFC is explicitly clear that only fields that can be recombined as a comma-separated list are permitted. “Kind of a list” is not good enough. (I understand you think this is a silly distinction, but it is nonetheless what the RFC mandates.)

I already explained above why Caddy is not in violation. When acting as a reverse proxy it acts as an origin server and is thus allowed to set a Server header. The Server header is a list of values, and list-valued header fields can be split out until multiple.