Skip to content

DefaultHTTPErrorHandler keeps stale Content-Encoding and Content-Length on error responses #3242

Open

Description

@vishr

Problem

A handler may set Content-Encoding or Content-Length for the response it plans to send, for example a precompressed file served with Content-Encoding: br, and then fail before writing anything. DefaultHTTPErrorHandler writes its own JSON (or empty HEAD) body but leaves those headers in place:

e.GET("/", func(c *echo.Context) error {
	c.Response().Header().Set(echo.HeaderContentEncoding, "br")
	c.Response().Header().Set(echo.HeaderContentLength, "5000")
	return echo.ErrNotFound
})

The client gets a plain JSON body labeled br with the wrong length, so decoding fails or the connection is cut short.

Expected

Before writing its body, the error handler should remove Content-Length and, when no middleware is wrapping the response writer, Content-Encoding. A wrapping middleware such as a compressor may encode the error body itself, so in that case Content-Encoding is left alone.

Found while reviewing #3239.

Activity

  1. vishr commented on Nov 20, 2026

    @vishr
    MemberAuthor

    Scope update for the fix in #3243, which differs from the "Expected" section above in two ways:

    • DefaultHTTPErrorHandler and ProblemDetailsHTTPErrorHandler remove Content-Disposition too, so an error after c.Attachment no longer downloads the error JSON as the attachment.
    • Content-Encoding is removed even when a middleware wraps the response. Keeping it for wrapped writers let a stale br survive under Gzip + RequestLogger (HandleError: true), and Gzip then passed the plain JSON through labeled br. Echo's Gzip sets its own Content-Encoding only when it starts compressing, so it is not affected. A third-party compressor that sets the header before writing would need to set it again.

    Content-Type, Content-Range, ETag and Last-Modified are kept, because they can be meant for the error response (a vendor media type, a 416, a 304).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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