Index | Thread | Search

From:
Rafael Sadowski <rafael@sizeofvoid.org>
Subject:
httpd: accept absolute-form request targets
To:
tech@openbsd.org
Date:
Mon, 16 Nov 2026 21:10:05 +0100

Download raw body.

Thread
  • Rafael Sadowski:

    httpd: accept absolute-form request targets

I found "XXX the Host can also appear in the URL path." in the
server_response and knew that we had something in relayd that would allow
us to solve the "problem" relatively easily.

This diff copy parse_url form relayd and use the host from the URL
instead of the Host header, as required by RFC 9112 3.2.2. While here,
reject multiple Host headers.

As I didn't have to implement parse_url, I think this diff is self-explanatory.

Feedback, OK?

diff --git a/httpd.c b/httpd.c
index 92f98ce..42ea617 100644
--- a/httpd.c
+++ b/httpd.c
@@ -1297,3 +1297,49 @@ header_rule_dup(const struct header_rule *src)
 
 	return (r);
 }
+
+int
+parse_url(const char *url, char **protoptr, char **hostptr, char **pathptr)
+{
+	char	*p, *proto = NULL, *host = NULL, *path = NULL;
+
+	/* return error if it is not a URL */
+	if ((p = strstr(url, ":/")) == NULL ||
+	    (strcspn(url, ":/") != (size_t)(p - url)))
+		return (-1);
+
+	/* get protocol */
+	if ((proto = strdup(url)) == NULL)
+		goto fail;
+	p = proto + (p - url);
+
+	/* get host */
+	p += strspn(p, ":/");
+	if (*p == '\0' || (host = strdup(p)) == NULL)
+		goto fail;
+	*p = '\0';
+
+	/* find and copy path or default to "/" */
+	if ((p = strchr(host, '/')) == NULL)
+		p = "/";
+	if ((path = strdup(p)) == NULL)
+		goto fail;
+
+	/* strip path after host */
+	host[strcspn(host, "/")] = '\0';
+
+	log_debug("%s: %s proto %s, host %s, path %s", __func__,
+	    url, proto, host, path);
+
+	*protoptr = proto;
+	*hostptr = host;
+	*pathptr = path;
+
+	return (0);
+
+ fail:
+	free(proto);
+	free(host);
+	free(path);
+	return (-1);
+}
diff --git a/httpd.h b/httpd.h
index f0076f6..14ab85b 100644
--- a/httpd.h
+++ b/httpd.h
@@ -789,6 +789,7 @@ void			 print_custom_header(const char *,
 int			 header_exists(struct server_config *, const char *);
 struct custom_header	*header_dup(const struct custom_header *);
 struct header_rule	*header_rule_dup(const struct header_rule *);
+int			 parse_url(const char *, char **, char **, char **);
 
 extern struct httpd *httpd_env;
 
diff --git a/server_http.c b/server_http.c
index 851c2fc..8cf54d6 100644
--- a/server_http.c
+++ b/server_http.c
@@ -1425,6 +1425,8 @@ server_response(struct httpd *httpd, struct client *clt)
 {
 	char			 path[PATH_MAX];
 	char			 hostname[HOST_NAME_MAX + 1];
+	char			*urlproto = NULL, *urlhost = NULL;
+	char			*urlpath = NULL;
 	struct http_descriptor	*desc = clt->clt_descreq;
 	struct http_descriptor	*resp = clt->clt_descresp;
 	struct server		*srv = clt->clt_srv;
@@ -1432,10 +1434,56 @@ server_response(struct httpd *httpd, struct client *clt)
 	struct kv		*kv, key, *host;
 	char			*query;
 	struct header_rule	*mrule = NULL;
+	int			 http11;
+
+	if (desc->http_path == NULL)
+		goto fail;
+
+	/* Multiple Host headers are not allowed (RFC 9112 3.2) */
+	key.kv_key = "Host";
+	if ((host = kv_find(&desc->http_headers, &key)) != NULL &&
+	    !TAILQ_EMPTY(&host->kv_children))
+		goto fail;
+
+	http11 = (strcmp(desc->http_version, "HTTP/1.1") == 0);
+
+	/* A client MUST send a Host header field in all HTTP/1.1 */
+	if (host == NULL && http11)
+		goto fail;
+
+	/*
+	 * Clients may send the full URL (GET http://host/path),
+	 * Use the path as request path. The host from the URL wins
+	 * over the Host header, which origin servers must ignore in
+	 * this case (RFC 9112 3.2.2).
+	 */
+	if (*desc->http_path != '/') {
+		if (parse_url(desc->http_path, &urlproto, &urlhost,
+		    &urlpath) == -1)
+			goto fail;
+		if ((strcasecmp(urlproto, "http://") != 0 &&
+		    strcasecmp(urlproto, "https://") != 0) ||
+		    strchr(urlhost, '@') != NULL)
+			goto fail;
+
+		free(desc->http_path);
+		desc->http_path = urlpath;
+		urlpath = NULL;
+
+		if (host != NULL) {
+			if (kv_set(host, "%s", urlhost) == -1)
+				goto fail;
+		} else if (kv_add(&desc->http_headers, "Host",
+		    urlhost) == NULL)
+			goto fail;
+
+		free(urlproto);
+		free(urlhost);
+		urlproto = urlhost = NULL;
+	}
 
 	/* Preserve original path */
-	if (desc->http_path == NULL ||
-	    (desc->http_path_orig = strdup(desc->http_path)) == NULL)
+	if ((desc->http_path_orig = strdup(desc->http_path)) == NULL)
 		goto fail;
 
 	/* Decode the URL */
@@ -1449,9 +1497,7 @@ server_response(struct httpd *httpd, struct client *clt)
 	if ((desc->http_path = strdup(path)) == NULL)
 		goto fail;
 
-	key.kv_key = "Host";
-	if ((host = kv_find(&desc->http_headers, &key)) != NULL &&
-	    host->kv_value != NULL) {
+	if (host != NULL && host->kv_value != NULL) {
 		if (strlcpy(hostname, host->kv_value, sizeof(hostname)) >=
 		    sizeof(hostname))
 			goto fail;
@@ -1464,11 +1510,7 @@ server_response(struct httpd *httpd, struct client *clt)
 			goto fail;
 	}
 
-	if (strcmp(desc->http_version, "HTTP/1.1") == 0) {
-		/* Host header is mandatory */
-		if (host == NULL)
-			goto fail;
-
+	if (http11) {
 		/* Is the connection persistent? */
 		key.kv_key = "Connection";
 		if ((kv = kv_find(&desc->http_headers, &key)) != NULL &&
@@ -1486,10 +1528,7 @@ server_response(struct httpd *httpd, struct client *clt)
 			clt->clt_persist = 0;
 	}
 
-	/*
-	 * Do we have a Host header and matching configuration?
-	 * XXX the Host can also appear in the URL path.
-	 */
+	 /* Do we have a Host header and matching configuration? */
 	if (host != NULL) {
 		if ((srv_conf = server_by_host_header(clt, hostname)) == NULL)
 			goto fail;
@@ -1600,6 +1639,9 @@ server_response(struct httpd *httpd, struct client *clt)
 	} else
 		return (server_file(httpd, clt));
  fail:
+	free(urlproto);
+	free(urlhost);
+	free(urlpath);
 	server_abort_http(clt, 400, "bad request");
 	return (-1);
 }