mirror of
https://github.com/henrygd/beszel.git
synced 2026-09-21 08:57:48 +02:00
feat(hub): add TRUSTED_PROXY_IPS allowlist for TRUSTED_AUTH_HEADER (#2327)
With TRUSTED_AUTH_HEADER set, the hub authenticates a request from the header alone, whichever address it comes from. That is right when every request passes through the reverse proxy, and not when the hub can also be reached directly: anyone who can reach it sets the header themselves. TRUSTED_PROXY_IPS takes a comma-separated list of IPs or CIDR ranges. When set, the header is only honored on requests whose peer address is in the list; other requests fall through to the normal authentication. When unset, nothing changes. The check uses the connection's RemoteAddr, not a forwarded header, so the list names the proxy itself. IPv4-mapped IPv6 entries are treated as IPv4. Entries that do not parse are skipped with a warning on the console; a list with no valid entry trusts nobody, so a typo narrows the allowlist instead of widening it.
This commit is contained in:
committed by
GitHub
parent
18f7a4bbc0
commit
4bf70700f2
@@ -2,7 +2,11 @@ package hub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -78,12 +82,81 @@ func (h *Hub) registerMiddlewares(se *core.ServeEvent) {
|
||||
}
|
||||
// authenticate with trusted header
|
||||
if trustedHeader, _ := utils.GetEnv("TRUSTED_AUTH_HEADER"); trustedHeader != "" {
|
||||
// only honor the header from these peers, if set
|
||||
trustedProxies, restricted := parseTrustedProxies()
|
||||
se.Router.BindFunc(func(e *core.RequestEvent) error {
|
||||
if restricted && !isTrustedProxy(trustedProxies, e.Request.RemoteAddr) {
|
||||
return e.Next()
|
||||
}
|
||||
return authorizeRequestWithEmail(e, e.Request.Header.Get(trustedHeader))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// parseTrustedProxies reads TRUSTED_PROXY_IPS (comma-separated IPs or CIDRs).
|
||||
// restricted is false when the variable is unset or empty, meaning the trusted
|
||||
// header is accepted from any peer. Invalid entries are skipped with a warning,
|
||||
// so a typo narrows the allowlist rather than widening it.
|
||||
func parseTrustedProxies() (prefixes []netip.Prefix, restricted bool) {
|
||||
value, _ := utils.GetEnv("TRUSTED_PROXY_IPS")
|
||||
if value == "" {
|
||||
return nil, false
|
||||
}
|
||||
for entry := range strings.SplitSeq(value, ",") {
|
||||
entry = strings.TrimSpace(entry)
|
||||
if entry == "" {
|
||||
continue
|
||||
}
|
||||
if prefix, err := parseProxyPrefix(entry); err == nil {
|
||||
prefixes = append(prefixes, prefix)
|
||||
} else {
|
||||
slog.Warn("Ignoring invalid TRUSTED_PROXY_IPS entry", "entry", entry)
|
||||
}
|
||||
}
|
||||
return prefixes, true
|
||||
}
|
||||
|
||||
// parseProxyPrefix parses an IP or CIDR into a masked prefix. IPv4-mapped IPv6
|
||||
// entries are converted to IPv4 so they match IPv4 peers.
|
||||
func parseProxyPrefix(entry string) (netip.Prefix, error) {
|
||||
prefix, err := netip.ParsePrefix(entry)
|
||||
if err != nil {
|
||||
addr, err := netip.ParseAddr(entry)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, err
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
return netip.PrefixFrom(addr, addr.BitLen()), nil
|
||||
}
|
||||
if prefix.Addr().Is4In6() {
|
||||
if prefix.Bits() < 96 {
|
||||
return netip.Prefix{}, fmt.Errorf("%s covers more than the IPv4-mapped range", entry)
|
||||
}
|
||||
prefix = netip.PrefixFrom(prefix.Addr().Unmap(), prefix.Bits()-96)
|
||||
}
|
||||
return prefix.Masked(), nil
|
||||
}
|
||||
|
||||
// isTrustedProxy reports whether the peer address of a request (host:port) is
|
||||
// within one of the prefixes.
|
||||
func isTrustedProxy(prefixes []netip.Prefix, remoteAddr string) bool {
|
||||
host, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
host = remoteAddr
|
||||
}
|
||||
addr, err := netip.ParseAddr(host)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
addr = addr.Unmap().WithZone("")
|
||||
for _, prefix := range prefixes {
|
||||
if prefix.Contains(addr) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// registerApiRoutes registers custom API routes
|
||||
func (h *Hub) registerApiRoutes(se *core.ServeEvent) error {
|
||||
// auth protected routes
|
||||
|
||||
Reference in New Issue
Block a user