Skip to content

Context.Scheme accepts malformed forwarded scheme values used by host redirects #2952

@shblue21

Description

@shblue21

Issue Description

Context.Scheme() is documented as returning http or https, but X-Forwarded-Proto, X-Forwarded-Protocol, and X-Url-Scheme values are returned as-is.

This affects middleware.WWWRedirect() and middleware.NonWWWRedirect(), since both use c.Scheme() when building the Location header.

Example:

Host: app.example
X-Forwarded-Proto: //external.example

With middleware.WWWRedirect(), the response contains:

Location: //external.example://www.app.example/

Because the value starts with //, browsers resolve the Location as a network-path reference with host external.example.

This requires the redirect middleware to be enabled and the forwarded header to reach Echo. Context.Scheme() should ignore forwarded scheme values other than http and https, matching its documented return values.

Relevant code

In context.go:

if scheme := c.request.Header.Get(HeaderXForwardedProto); scheme != "" {
	return scheme
}
if scheme := c.request.Header.Get(HeaderXForwardedProtocol); scheme != "" {
	return scheme
}
if scheme := c.request.Header.Get(HeaderXUrlScheme); scheme != "" {
	return scheme
}

In middleware/redirect.go:

return true, scheme + "://www." + host + uri
return true, scheme + "://" + host[4:] + uri

Version/commit

This appears to affect both the current v5 branch and the supported v4 branch.

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