[3.0] 2e0cd51 Return an error on duplicated Host headers

Tollef Fog Heen tfheen at varnish-cache.org
Wed Jun 12 10:19:23 CEST 2013


commit 2e0cd51d8bd1fab963bc8e57d9011fb3537674f1
Author: Tollef Fog Heen <tfheen at varnish-software.com>
Date:   Wed Jun 12 10:18:59 2013 +0200

    Return an error on duplicated Host headers

diff --git a/bin/varnishd/cache.h b/bin/varnishd/cache.h
index d7f6ab8..be319df 100644
--- a/bin/varnishd/cache.h
+++ b/bin/varnishd/cache.h
@@ -769,6 +769,7 @@ double http_GetHdrQ(const struct http *hp, const char *hdr, const char *field);
 uint16_t http_GetStatus(const struct http *hp);
 const char *http_GetReq(const struct http *hp);
 int http_HdrIs(const struct http *hp, const char *hdr, const char *val);
+int http_IsHdr(const txt *hh, const char *hdr);
 uint16_t http_DissectRequest(struct sess *sp);
 uint16_t http_DissectResponse(struct worker *w, const struct http_conn *htc,
     struct http *sp);
diff --git a/bin/varnishd/cache_http.c b/bin/varnishd/cache_http.c
index 76b3f86..8753acc 100644
--- a/bin/varnishd/cache_http.c
+++ b/bin/varnishd/cache_http.c
@@ -156,7 +156,7 @@ http_Setup(struct http *hp, struct ws *ws)
 
 /*--------------------------------------------------------------------*/
 
-static int
+int
 http_IsHdr(const txt *hh, const char *hdr)
 {
 	unsigned l;
@@ -638,6 +638,28 @@ http_splitline(struct worker *w, int fd, struct http *hp,
 
 /*--------------------------------------------------------------------*/
 
+static int
+htc_request_check_host_hdr(struct http *hp)
+{
+	int u;
+	int seen_host = 0;
+	for (u = HTTP_HDR_FIRST; u < hp->nhd; u++) {
+		if (hp->hd[u].b == NULL)
+			continue;
+		AN(hp->hd[u].b);
+		AN(hp->hd[u].e);
+		if (http_IsHdr(&hp->hd[u], H_Host)) {
+			if (seen_host) {
+				return (400);
+			}
+			seen_host = 1;
+		}
+	}
+	return (0);
+}
+
+/*--------------------------------------------------------------------*/
+
 static void
 http_ProtoVer(struct http *hp)
 {
@@ -675,6 +697,12 @@ http_DissectRequest(struct sess *sp)
 		return (retval);
 	}
 	http_ProtoVer(hp);
+
+	retval = htc_request_check_host_hdr(hp);
+	if (retval != 0) {
+		WSP(sp, SLT_Error, "Duplicated Host header");
+		return (retval);
+	}
 	return (retval);
 }
 
diff --git a/bin/varnishtest/tests/b00037.vtc b/bin/varnishtest/tests/b00037.vtc
new file mode 100644
index 0000000..42b23ab
--- /dev/null
+++ b/bin/varnishtest/tests/b00037.vtc
@@ -0,0 +1,19 @@
+varnishtest "Error on multiple Host headers"
+
+server s1 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {
+} -start
+
+client c1 {
+	txreq -hdr "Host: foo" -hdr "Host: bar"
+} -run
+
+varnish v1 -expect sess_closed == 1
+varnish v1 -expect client_req == 1
+varnish v1 -expect cache_hit == 0
+varnish v1 -expect cache_hitpass == 0
+varnish v1 -expect cache_miss == 0



More information about the varnish-commit mailing list