From a17e5b27c03230cc65ebedc6dc28dbfce5181c79 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Wed, 16 Apr 2025 15:47:42 +0200 Subject: [PATCH] BUG/MEDIUM: h3: trim whitespaces when parsing headers value Remove any leading and trailing whitespace from header field values prior to inserting a new HTX header block. This is done when parsing a HEADERS frame, both as headers and trailers. This must be backported up to 2.6. --- src/h3.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/h3.c b/src/h3.c index f624ddcbb..9b0c8ad4d 100644 --- a/src/h3.c +++ b/src/h3.c @@ -529,6 +529,7 @@ static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf, unsigned int flags = HTX_SL_F_NONE; struct ist meth = IST_NULL, path = IST_NULL; struct ist scheme = IST_NULL, authority = IST_NULL; + struct ist v; int hdr_idx, ret; int cookie = -1, last_cookie = -1, i; const char *ctl; @@ -869,7 +870,15 @@ static ssize_t h3_headers_to_htx(struct qcs *qcs, const struct buffer *buf, goto out; } - if (!htx_add_header(htx, list[hdr_idx].n, list[hdr_idx].v)) { + /* trim leading/trailing LWS */ + for (v = list[hdr_idx].v; v.len; v.len--) { + if (unlikely(HTTP_IS_LWS(*v.ptr))) + v.ptr++; + else if (!unlikely(HTTP_IS_LWS(v.ptr[v.len - 1]))) + break; + } + + if (!htx_add_header(htx, list[hdr_idx].n, v)) { len = -1; goto out; } @@ -972,6 +981,7 @@ static ssize_t h3_trailers_to_htx(struct qcs *qcs, const struct buffer *buf, int hdr_idx, ret; const char *ctl; int qpack_err; + struct ist v; int i; TRACE_ENTER(H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); @@ -1077,7 +1087,15 @@ static ssize_t h3_trailers_to_htx(struct qcs *qcs, const struct buffer *buf, goto out; } - if (!htx_add_trailer(htx, list[hdr_idx].n, list[hdr_idx].v)) { + /* trim leading/trailing LWS */ + for (v = list[hdr_idx].v; v.len; v.len--) { + if (unlikely(HTTP_IS_LWS(*v.ptr))) + v.ptr++; + else if (!unlikely(HTTP_IS_LWS(v.ptr[v.len - 1]))) + break; + } + + if (!htx_add_trailer(htx, list[hdr_idx].n, v)) { TRACE_ERROR("cannot add trailer", H3_EV_RX_FRAME|H3_EV_RX_HDR, qcs->qcc->conn, qcs); len = -1; goto out;