---
id: "CVE-2026-118657"
kind: "vulnerability"
aliases: ["CVE-2026-118657", "GHSA-2ffq-g2xg-c22p"]
title: "Echo: HTTPS Redirect and HSTS Bypass via Unconditional Trust of Forwarded Scheme Headers"
severity: "medium"
cvss: 5.9
epss: null
exploited: false
status: "open"
category: "package"
products: [{"name": "github.com/labstack/echo/v5", "fixed": ["5.4.0"], "affected": "< 5.4.0", "ecosystem": "go"}, {"name": "github.com/labstack/echo/v4", "fixed": ["4.16.0"], "affected": "< 4.16.0", "ecosystem": "go"}]
ecosystems: ["go"]
tags: ["cwe-346", "echo", "go", "package", "self-hosted"]
not_affected_if: ["managed-hosting", "static-export"]
published_at: "2026-10-28T21:04:52+00:00"
first_seen_at: "2026-10-28T21:07:44+00:00"
updated_at: "2026-10-28T21:09:30+00:00"
sources: [{"source": "ghsa", "url": "https://github.com/advisories/GHSA-2ffq-g2xg-c22p", "first_seen_at": "2026-10-28T21:07:44+00:00"}, {"source": "nvd", "url": "https://nvd.nist.gov/vuln/detail/CVE-2026-118657", "first_seen_at": "2026-10-28T21:35:45+00:00"}]
---

# Echo: HTTPS Redirect and HSTS Bypass via Unconditional Trust of Forwarded Scheme Headers

Echo's Context.Scheme() trusts four client-sent forwarded headers without configuration, so a request can report itself as HTTPS. An attacker who reaches the app directly can skip the HTTPS redirect and suppress HSTS headers, and redirect URLs can carry an arbitrary scheme.

_Summary, check and fix written by a model from the sources; labelled as such._

## Affected

- go:github.com/labstack/echo/v5 affected < 5.4.0 fixed 5.4.0
- go:github.com/labstack/echo/v4 affected < 4.16.0 fixed 4.16.0

## How to check

- Check the Echo version in go.mod: `github.com/labstack/echo/v5` below `5.4.0` or `github.com/labstack/echo/v4` below `4.16.0`
- Look for `middleware.HTTPSRedirect`, `HTTPSWWWRedirect`, `HTTPSNonWWWRedirect`, `WWWRedirect` or `NonWWWRedirect` in the code
- Check whether the app is reachable directly, bypassing any proxy that overwrites `X-Forwarded-Proto`

## Fix

- Upgrade `github.com/labstack/echo/v5` to `5.4.0` or later
- Upgrade `github.com/labstack/echo/v4` to `4.16.0` or later
- Until upgraded, make sure a reverse proxy overwrites `X-Forwarded-Proto` and the app is not reachable directly

## Timeline

- 2026-10-28T21:07:44+00:00 ghsa https://github.com/advisories/GHSA-2ffq-g2xg-c22p
- 2026-10-28T21:35:45+00:00 nvd https://nvd.nist.gov/vuln/detail/CVE-2026-118657

## From the sources

## Description

`Context.Scheme()` unconditionally trusts four client-controlled HTTP headers to determine the request scheme, with no mechanism to configure trusted sources. This allows a remote unauthenticated attacker to bypass HTTPS redirect enforcement and suppress HSTS headers.

The trusted headers (checked in this order of precedence):
1. `X-Forwarded-Proto`
2. `X-Forwarded-Protocol`
3. `X-Forwarded-Ssl`
4. `X-Url-Scheme`

### Threat model

**Scenario A — Directly exposed application (vulnerable by default):**
The Echo application listens on HTTP and is reachable directly by clients (no L7 proxy in front), or an attacker can bypass the proxy and reach the backend directly. In this case, the attacker has full control over all request headers. Sending `X-Forwarded-Proto: https` defeats the `HTTPSRedirect` middleware entirely — traffic stays on plaintext HTTP, enabling man-in-the-middle attacks.

This is the primary scenario. The application is vulnerable by default with no misconfiguration required — simply using `e.Pre(middleware.HTTPSRedirect())` as documented is sufficient.

**Scenario B — Behind L7 proxy with header sanitization (reduced risk):**
If the application is strictly behind a correctly configured L7 proxy that always overwrites `X-Forwarded-Proto` and there is no direct access to the backend, the practical risk is lower. However, this relies on perimeter controls external to the framework, not on the framework itself. As Echo's own IP-extraction documentation states: *"you must not trust HTTP headers unconditionally."*

### Root cause

`context.go`, lines 161–181:

```go
func (c *Context) Scheme() string {
    if c.IsTLS() {
        return "https"
    }
    if scheme := c.request.Header.Get(HeaderXForwardedProto); scheme != "" {
        return scheme
    }
    if scheme := c.request.Header.Get(HeaderXForwardedProtocol); scheme != "" {
        return scheme
    }
    if ssl := c.request.Header.Get(HeaderXForwardedSsl); ssl == "on" {
        return "https"
    }
    if scheme := c.request.Header.Get(HeaderXUrlScheme); scheme != "" {
        return scheme
    }
    return "http"
}
```

Two additional issues with this implementation:

1. **No value validation.** `Scheme()` returns any arbitrary string from the header, not just `"http"` or `"https"`. This breaks the documented contract ("returns the HTTP protocol scheme, `http` or `https`") and causes unexpected behavior in middleware that uses the return value to construct URLs (e.g., `WWWRedirect` builds `scheme + "://www." + host + uri`).

2. **Consumed by security-critical middleware.** The return value is used directly by redirect middleware (`middleware/redirect.go:136`) and secure middleware (`middleware/secure.go:125`) for security enforcement decisions.

### Affected components

| Component | File | Line(s) | Impact |
|-----------|------|---------|--------|
| `Context.Scheme()` | `context.go` | 161–181 | Returns attacker-controlled scheme |
| `HTTPSRedirect` | `middleware/redirect.go` | 147–151 | HTTPS enforcement bypassed |
| `HTTPSWWWRedirect` | `middleware/redirect.go` | 154–161 | HTTPS enforcement bypassed |
| `HTTPSNonWWWRedirect` | `middleware/redirect.go` | 163–170 | HTTPS enforcement bypassed |
| `WWWRedirect` | `middleware/redirect.go` | 172–177 | URL constructed with arbitrary scheme |
| `NonWWWRedirect` | `middleware/redirect.go` | 179–184 | URL constructed with arbitrary scheme |
| Secure middleware (HSTS) | `middleware/secure.go` | 125 | HSTS header suppressed |

### This is an unsafe-by-default API surface, not just misconfiguration

This behavior is not accidental — it is codified in the project's own test suite. For example, `middleware/redirect_test.go` (lines 29–34) explicitly asserts that `X-Forwarded-Proto: https` on an HTTP request results in 200 OK (no redirect):

```go
{
    whenHost:         "labstack.com",
    whenHeader:       map[string][]string{echo.HeaderXForwardedProto: {"https"}},
    expectLocation:   "",
    expectStatusCode: http.StatusOK,
},
```

Similarly, `middleware/secure_test.go` (lines 42–53) and `context_test.go` (lines 1104–1111) assert that scheme is derived from the header.

The problem is not that the framework *can* use forwarded headers — that is legitimate behind a trusted proxy. The problem is that **there is no mechanism to control whether this trust is appropriate**, unlike the existing IP header trust model. `Scheme()` always trusts the headers, making any middleware that depends on it unsafe-by-default.

### Inconsistency with existing trust model

Echo already recognizes this class of header-trust vulnerability for IP addresses. The `IPExtractor` mechanism with `TrustOption` (`TrustLoopback`, `TrustPrivateNet`, `TrustLinkLocal`, `TrustIPRange`) was introduced specifically because:

> *"you must not trust HTTP headers unconditionally. Otherwise, you might give someone a chance of deceiving you. **A security risk!**"*
> — `ip.go`, lines 20–21

> *"If you put no proxy (e.g., directly facing the internet), all you need to (and have to) see is IP address from network layer. Any HTTP header is untrustable because the clients have full control what headers to be set."*
> — `ip.go`, lines 36–38

No equivalent mechanism exists for scheme detection. `Scheme()` unconditionally trusts all four forwarded headers with no configuration, opt-in, or documentation warning.

### Note on proxies and the "3 additional headers"

A correctly configured nginx `proxy_set_header X-Forwarded-Proto $scheme;` overwrites `X-Forwarded-Proto` with the actual incoming scheme. When a proxy does this, `X-Forwarded-Proto` is trustworthy and the three fallback headers (`X-Forwarded-Protocol`, `X-Forwarded-Ssl`, `X-Url-Scheme`) are never reached because `X-Forwarded-Proto` takes precedence in `Scheme()`.

The three additional headers become real attack vectors specifically when:
- The proxy does **not** set `X-Forwarded-Proto` at all (common in default proxy configs), or
- The application is directly exposed (Scenario A above).

In such cases, an attacker can bypass via any of the four headers. The main vulnerability (HTTPS redirect bypass via `X-Forwarded-Proto` on a directly exposed app) does not depend on the additional headers.

## Impact

**HTTPS downgrade / MITM:** An attacker who can reach the Echo HTTP endpoint directly can prevent HTTPS redirection, keeping client traffic on plaintext HTTP and enabling man-in-the-middle attacks.

**HSTS suppression:** Behind a TLS-terminating proxy, spoofing `X-Forwarded-Proto: http` prevents the `Strict-Transport-Security` header from being sent. When the response ultimately reaches the client over HTTPS (via the proxy), the missing HSTS header weakens browser-side HTTPS enforcement. Note: browsers ignore HSTS received over plain HTTP, so the practical risk is specifically in the suppression scenario behind a TLS-terminating proxy, not in injecting HSTS on HTTP.

**URL construction with arbitrary scheme:** `WWWRedirect` and `NonWWWRedirect` use `c.Scheme()` to build redirect URLs (`scheme + "://www." + host + uri`). An attacker can inject arbitrary strings (e.g., `javascript:`, `data:`) as the scheme component of redirect URLs.

## Proof of Concept

Tested on Echo **v5.0.4** (commit `fa5311b`, 2026-02-15).

### PoC 1: HTTPS Redirect Bypass (Go test)

```go
package echo_test

import (
    "net/http"
    "net/http/httptest"
    "testing"

    "github.com/labstack/echo/v5"
    "github.com/labstack/echo/v5/middleware"
)

func TestHTTPSRedirectBypass(t *testing.T) {
    e := echo.New()
    e.Pre(middleware.HTTPSRedirect())
    e.GET("/", func(c *echo.Context) error {
        return c.String(http.StatusOK, "served over: "+c.Scheme())
    })

    // Normal HTTP → should redirect
    req := httptest.NewRequest(http.MethodGet, "/", nil)
    rec := httptest.NewRecorder()
    e.ServeHTTP(rec, req)
    if rec.Code != http.StatusMovedPermanently {
        t.Fatalf("expected 301, got %d", rec.Code)
    }
    t.Logf("Normal HTTP →

---

_Free to read at a human pace. An API with keys and an MCP server are in preparation; early access: hello@infrastack.ai_
