Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
anubis (1.27.0-0+keel3) trixie; urgency=medium

* Close pass-challenge's open redirect without REDIRECT_DOMAINS
(debian/patches/redir-same-host.patch): with it empty, redir may only
be relative or name the request's own scheme, host and port, never
carry userinfo; upstream compared redir's host with itself. Every
name a site is reached by keeps working, which a fixed
REDIRECT_DOMAINS cannot promise.
* Refuse redir forms a browser resolves to another host
(debian/patches/redir-validate.patch, a backport of upstream's
validateRedirect): backslashes (/\evil is //evil to a browser),
control bytes, leading space, opaque URLs, a path starting with //,
checked again after percent-decoding (/%2F/evil, /%5Cevil).
* autopkgtest: pass-challenge behind nginx allows the same host by an
unlisted name, a relative redir and an IPv6 literal, and refuses
another host, //evil, userinfo and another IPv6 address, and /\evil,
/%2F/evil, /%5Cevil, %09//evil and " //evil" as invalid.

-- Marcos Méndez <mendez.foto@gmail.com> Fri, 02 Oct 2026 06:00:00 +0000

anubis (1.27.0-0+keel2) trixie; urgency=medium

* Serve the challenge to clients that do not accept gzip
Expand Down
154 changes: 154 additions & 0 deletions debian/patches/redir-same-host.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
Description: Redirect only to the request's own origin while REDIRECT_DOMAINS is empty
PassChallenge sends the client to redir once the challenge is solved.
With REDIRECT_DOMAINS empty it compared the host of redir with itself
(r.URL is redir, parsed again), so any host passed: an open redirect.
REDIRECT_DOMAINS closes it only for the names written in it, and Keel
Web's sites are reached by names and addresses the machine cannot list
ahead of time (a new DNS name, a site of handbook decision 0042, a
cloud public IP behind NAT).
.
redirectAllowed now decides: never a URL with userinfo; always a
relative one, as before; with REDIRECT_DOMAINS set, a host in it, as
before; with it empty, only the scheme, host and port of the request
itself (X-Forwarded-Proto or http, and the Host header, default ports
filled in), so every name the site is reached by works and no other
host can be reached through it. TestRedirectAllowed covers the same
host by name, by an unlisted name, by IPv4 and by an IPv6 literal, and
refuses another host, a scheme-relative //evil, userinfo, another port
and another scheme.
Author: Marcos Mendez <mendez.foto@gmail.com>
Bug: https://github.com/Keel-Linux/keel-web/pull/3
Forwarded: no
Last-Update: 2026-10-02
---
--- a/lib/anubis.go
+++ b/lib/anubis.go
@@ -496,6 +496,59 @@ func (s *Server) MakeChallenge(w http.Re
}
}

+// redirectAllowed says whether pass-challenge may send the client to
+// redir. A URL with userinfo never. A relative URL (no host, no scheme)
+// always, as before. An absolute one: when allowed (REDIRECT_DOMAINS) is
+// set, when its host is in it, as before; when it is empty, only when its
+// scheme, host and port are those of the request (X-Forwarded-Proto, or
+// http, and the Host header), so every name the site is reached by works
+// and no other host can be reached through it. Upstream compared the host
+// of redir with itself there, which let any host through.
+func redirectAllowed(r *http.Request, redir *url.URL, allowed []string) bool {
+ if redir.User != nil {
+ return false
+ }
+ if redir.Host == "" {
+ return redir.Scheme == ""
+ }
+ if len(allowed) != 0 {
+ return matchRedirectDomain(allowed, redir.Host)
+ }
+ scheme := requestScheme(r)
+ if redir.Scheme != "" && !strings.EqualFold(redir.Scheme, scheme) {
+ return false
+ }
+ return sameHostPort(redir.Host, r.Host, scheme)
+}
+
+// requestScheme is the scheme the client used: the X-Forwarded-Proto of
+// the proxy in front when it is http or https, else that of the
+// connection.
+func requestScheme(r *http.Request) string {
+ switch proto := strings.ToLower(r.Header.Get("X-Forwarded-Proto")); proto {
+ case "http", "https":
+ return proto
+ }
+ if r.TLS != nil {
+ return "https"
+ }
+ return "http"
+}
+
+// sameHostPort compares two URL hosts, case-insensitively, with the
+// scheme's default port filled in where none is written.
+func sameHostPort(a, b, scheme string) bool {
+ canonical := func(host string) string {
+ u := url.URL{Host: host}
+ port := u.Port()
+ if port == "" {
+ port = map[string]string{"http": "80", "https": "443"}[scheme]
+ }
+ return net.JoinHostPort(strings.ToLower(u.Hostname()), port)
+ }
+ return b != "" && canonical(a) == canonical(b)
+}
+
func (s *Server) PassChallenge(w http.ResponseWriter, r *http.Request) {
lg, r := s.getRequestLogger(r)
localizer := localization.GetLocalizer(r)
@@ -539,7 +592,7 @@ func (s *Server) PassChallenge(w http.Re
s.respondWithError(w, r, localizer.T("redirect_not_parseable"), makeCode(err))
return
}
- if (len(urlParsed.Host) > 0 && len(s.opts.RedirectDomains) != 0 && !matchRedirectDomain(s.opts.RedirectDomains, urlParsed.Host)) || urlParsed.Host != r.URL.Host {
+ if !redirectAllowed(r, urlParsed, s.opts.RedirectDomains) || urlParsed.Host != r.URL.Host {
lg.DebugContext(r.Context(), "domain not allowed", "domain", urlParsed.Host)
s.respondWithError(w, r, localizer.T("redirect_domain_not_allowed"), "")
return
--- /dev/null
+++ b/lib/redirect_same_host_test.go
@@ -0,0 +1,57 @@
+package lib
+
+import (
+ "net/http/httptest"
+ "net/url"
+ "testing"
+)
+
+// TestRedirectAllowed makes sure that, while REDIRECT_DOMAINS is empty,
+// pass-challenge redirects only to the origin the request came to
+// (scheme, host and port), or to a relative URL, so that every name the
+// site is reached by works and the open redirect stays closed; and that
+// a configured REDIRECT_DOMAINS still decides as before.
+func TestRedirectAllowed(t *testing.T) {
+ for _, tt := range []struct {
+ name string
+ host string
+ proto string
+ redir string
+ domains []string
+ want bool
+ }{
+ {name: "relative path", host: "web.example.org", proto: "https", redir: "/page?x=1", want: true},
+ {name: "same host", host: "web.example.org", proto: "https", redir: "https://web.example.org/page", want: true},
+ {name: "same host, other case", host: "Web.Example.org", proto: "https", redir: "https://web.example.ORG/", want: true},
+ {name: "same host, default port written", host: "web.example.org", proto: "https", redir: "https://web.example.org:443/", want: true},
+ {name: "a name not in /etc/hosts", host: "unlisted.keel.test", proto: "https", redir: "https://unlisted.keel.test/", want: true},
+ {name: "same IPv4", host: "10.0.3.86", proto: "https", redir: "https://10.0.3.86/", want: true},
+ {name: "same IPv6 literal", host: "[fd42:b2:0:1::6554]", proto: "https", redir: "https://[fd42:b2:0:1::6554]/x", want: true},
+ {name: "other IPv6 literal", host: "[fd42:b2:0:1::6554]", proto: "https", redir: "https://[2001:db8::1]/", want: false},
+ {name: "other host", host: "web.example.org", proto: "https", redir: "https://evil.example/", want: false},
+ {name: "scheme-relative other host", host: "web.example.org", proto: "https", redir: "//evil.example/", want: false},
+ {name: "userinfo naming the host", host: "web.example.org", proto: "https", redir: "https://web.example.org@evil.example/", want: false},
+ {name: "userinfo on the same host", host: "web.example.org", proto: "https", redir: "https://a@web.example.org/", want: false},
+ {name: "other port", host: "web.example.org", proto: "https", redir: "https://web.example.org:8443/", want: false},
+ {name: "other scheme", host: "web.example.org", proto: "https", redir: "http://web.example.org/", want: false},
+ {name: "no X-Forwarded-Proto is http", host: "web.example.org", proto: "", redir: "http://web.example.org/", want: true},
+ {name: "a scheme with no host", host: "web.example.org", proto: "https", redir: "https:/page", want: false},
+ {name: "REDIRECT_DOMAINS allows a listed host", host: "web.example.org", proto: "https", redir: "https://other.example/", domains: []string{"other.example"}, want: true},
+ {name: "REDIRECT_DOMAINS refuses the request's own host when unlisted", host: "web.example.org", proto: "https", redir: "https://web.example.org/", domains: []string{"other.example"}, want: false},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ req := httptest.NewRequest("GET", "/.within.website/x/cmd/anubis/api/pass-challenge", nil)
+ req.Host = tt.host
+ if tt.proto != "" {
+ req.Header.Set("X-Forwarded-Proto", tt.proto)
+ }
+ redir, err := url.Parse(tt.redir)
+ if err != nil {
+ t.Fatalf("cannot parse %q: %v", tt.redir, err)
+ }
+ if got := redirectAllowed(req, redir, tt.domains); got != tt.want {
+ t.Errorf("redirectAllowed(Host %q, %q) = %v, want %v", tt.host, tt.redir, got, tt.want)
+ }
+ })
+ }
+}
180 changes: 180 additions & 0 deletions debian/patches/redir-validate.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
Description: Refuse redir forms a browser resolves to another host
redir-same-host.patch allowed a redir with neither scheme nor host, and
Go parses `/\evil.example` that way, while browsers read `/\` as `//`:
http.Redirect sent it unchanged and the visitor landed on evil.example.
This backports the checks of upstream's validateRedirect (after
1.27.0): no backslash, no control byte, no leading space, no opaque URL,
no path starting with `//`, a relative redir must start with `/`, only
http and https. They run on redir as given and again once it is
percent-decoded, so `/%2F/evil` and `/%5Cevil` are refused too; a
refusal answers "Invalid redirect" with 400 before the challenge is
looked up. TestPassChallengeRefusesRedirectTricks drives PassChallenge
with those forms, whitespace and CRLF among them, and checks that a
relative and a same-origin redir still pass.
Origin: backport, https://github.com/TecharoHQ/anubis/blob/main/lib/http.go (validateRedirect)
Author: Marcos Mendez <mendez.foto@gmail.com>
Bug: https://github.com/Keel-Linux/anubis/pull/3
Forwarded: not-needed
Last-Update: 2026-10-02
---
--- a/lib/anubis.go
+++ b/lib/anubis.go
@@ -496,6 +496,61 @@ func (s *Server) MakeChallenge(w http.Re
}
}

+var errInvalidRedirectForm = errors.New("invalid redirect")
+
+// validateRedirectForm refuses a redir that a browser would resolve to
+// another host although Go parses it with neither scheme nor host, as
+// upstream's validateRedirect (after 1.27.0) does: a backslash (browsers
+// read `/\evil` as `//evil`), a control byte, leading whitespace, an
+// opaque URL, a path starting with `//`, a relative redir that does not
+// start with `/`, and anything else than http and https. The same checks
+// run again on the percent-decoded string, so `/%2F/evil` and `/%5Cevil`
+// are refused too.
+func validateRedirectForm(redir string) error {
+ if redir == "" {
+ return errInvalidRedirectForm
+ }
+ if err := redirectFormLooksSafe(redir); err != nil {
+ return err
+ }
+ decoded, err := url.PathUnescape(redir)
+ if err != nil {
+ return fmt.Errorf("%w: %w", errInvalidRedirectForm, err)
+ }
+ return redirectFormLooksSafe(decoded)
+}
+
+func redirectFormLooksSafe(redir string) error {
+ if strings.HasPrefix(redir, " ") || strings.ContainsAny(redir, "\\") {
+ return errInvalidRedirectForm
+ }
+ for _, c := range redir {
+ if c < 0x20 || c == 0x7f {
+ return errInvalidRedirectForm
+ }
+ }
+ u, err := url.Parse(redir)
+ if err != nil {
+ return fmt.Errorf("%w: %w", errInvalidRedirectForm, err)
+ }
+ if u.Opaque != "" {
+ return errInvalidRedirectForm
+ }
+ switch u.Scheme {
+ case "":
+ if u.Host != "" || !strings.HasPrefix(redir, "/") || strings.HasPrefix(u.Path, "//") {
+ return errInvalidRedirectForm
+ }
+ case "http", "https":
+ if u.Hostname() == "" {
+ return errInvalidRedirectForm
+ }
+ default:
+ return errInvalidRedirectForm
+ }
+ return nil
+}
+
// redirectAllowed says whether pass-challenge may send the client to
// redir. A URL with userinfo never. A relative URL (no host, no scheme)
// always, as before. An absolute one: when allowed (REDIRECT_DOMAINS) is
@@ -554,6 +609,11 @@ func (s *Server) PassChallenge(w http.Re
localizer := localization.GetLocalizer(r)

redir := r.FormValue("redir")
+ if err := validateRedirectForm(redir); err != nil {
+ lg.DebugContext(r.Context(), "invalid redirect", "err", err)
+ s.respondWithStatus(w, r, localizer.T("invalid_redirect"), "", http.StatusBadRequest)
+ return
+ }
redirURL, err := url.ParseRequestURI(redir)
if err != nil {
lg.ErrorContext(r.Context(), "invalid redirect", "err", err)
--- /dev/null
+++ b/lib/redirect_validate_test.go
@@ -0,0 +1,82 @@
+package lib
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+
+ "github.com/TecharoHQ/anubis"
+)
+
+// TestPassChallengeRefusesRedirectTricks drives PassChallenge itself with
+// redir values that browsers resolve to another host although they have
+// neither scheme nor host in Go's parse: a backslash (`/\evil` is `//evil`
+// to a browser), an encoded slash or backslash, and leading whitespace or
+// control bytes. None may come back as a redirect; each is refused as an
+// invalid redirect before the challenge is looked at. A same-origin redir
+// passes that check and fails later, on the made-up challenge.
+func TestPassChallengeRefusesRedirectTricks(t *testing.T) {
+ srv := spawnAnubis(t, Options{
+ Next: http.NewServeMux(),
+ Policy: loadPolicies(t, "testdata/aggressive_403.yaml", 0),
+ })
+
+ pass := func(redir string) *httptest.ResponseRecorder {
+ q := url.Values{
+ "id": {"00000000-0000-0000-0000-000000000000"},
+ "response": {"00"},
+ "nonce": {"0"},
+ "elapsedTime": {"1"},
+ "redir": {redir},
+ }
+ req := httptest.NewRequest("GET", "/.within.website/x/cmd/anubis/api/pass-challenge?"+q.Encode(), nil)
+ req.Host = "web.example.org"
+ req.Header.Set("X-Forwarded-Proto", "https")
+ req.Header.Set("X-Real-IP", "127.0.0.1")
+ req.Header.Set("User-Agent", "CHALLENGE")
+ req.AddCookie(&http.Cookie{Name: srv.cookieName(anubis.TestCookieName), Value: "x"})
+ w := httptest.NewRecorder()
+ srv.PassChallenge(w, req)
+ return w
+ }
+
+ for _, redir := range []string{
+ `/\evil.example`,
+ `/\/evil.example`,
+ `\\evil.example`,
+ `/%2F/evil.example`,
+ `/%2f%2fevil.example`,
+ `/%5Cevil.example`,
+ `/%5cevil.example`,
+ "\t//evil.example",
+ "%09//evil.example",
+ " //evil.example",
+ "//evil.example",
+ "/x\r\nLocation: https://evil.example/",
+ "evil.example",
+ "javascript:alert(1)",
+ "https:evil.example",
+ "",
+ } {
+ t.Run(redir, func(t *testing.T) {
+ w := pass(redir)
+ if loc := w.Header().Get("Location"); loc != "" {
+ t.Fatalf("redir %q: redirected to %q", redir, loc)
+ }
+ if !strings.Contains(w.Body.String(), "Invalid redirect") && !strings.Contains(w.Body.String(), "Redirect domain not allowed") {
+ t.Fatalf("redir %q: not refused as a redirect (status %d)", redir, w.Code)
+ }
+ })
+ }
+
+ for _, redir := range []string{"/page?x=1", "https://web.example.org/page"} {
+ t.Run("allowed "+redir, func(t *testing.T) {
+ body := pass(redir).Body.String()
+ if strings.Contains(body, "Invalid redirect") || strings.Contains(body, "Redirect domain not allowed") {
+ t.Fatalf("redir %q: refused as a redirect", redir)
+ }
+ })
+ }
+}
2 changes: 2 additions & 0 deletions debian/patches/series
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
challenge-without-gzip.patch
redirect-domains-ipv6.patch
redir-same-host.patch
redir-validate.patch
Loading
Loading