Skip to content

What harm is there to trim header values in HttpServletResponse setHeader() or addHeader() calls? #926

@joakime

Description

@joakime

With HTTP/2, the Header Value cannot start or end with whitespace.

Code like this ...

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
    resp.addHeader("x-example", "foo "); // note the extra space at the end
}

Would fail at the HPACK level on HTTP/2 because of the extra space at the end of the Header value.
Note: this would also fail at the QPACK level on HTTP/3.

This would be trivial to fix at the servlet level to just recommend trimming the incoming value parameter.
But the question is, would this kind of trim of the value cause problems or be against the Servlet spec?

For HTTP/2 we are already lowercasing the header name, as required by the HTTP/2 spec.

See the spec for HTTP/2 ...

RFC9113 Section 8.2.1

A field value MUST NOT start or end with an ASCII whitespace character (ASCII SP or HTAB, 0x20 or 0x09).

A request or response that contains a field that violates any of these conditions MUST be treated as malformed (Section 8.1.1).

RFC9113 Section 8.1.1

Malformed requests or responses that are detected MUST be treated as a stream error (Section 5.4.2) of type PROTOCOL_ERROR.

For malformed requests, a server MAY send an HTTP response prior to closing or resetting the stream. Clients MUST NOT accept a malformed response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions