600 /api/ requests a minute per client address (--requests-per-minute), 429 with Retry-After; static UI files and the node's own machine are exempt. The real client behind a reverse proxy is read from X-Forwarded-For only when the connection comes from a --trusted-proxy, taking the last entry that is not itself a trusted proxy. k8s: TRUSTED_PROXY in config.env (the private ranges) is passed as --trusted-proxy. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package api
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// defaultRequestsPerMinute is how many API requests one client address may
|
|
// make per minute. The browser UI makes a handful per page and polls a mailbox
|
|
// every few seconds, so this is far above honest use and far below a flood.
|
|
const defaultRequestsPerMinute = 600
|
|
|
|
// ParseProxies reads trusted proxy addresses or networks ("10.0.0.0/8",
|
|
// "203.0.113.5") for Config.TrustedProxies.
|
|
func ParseProxies(in []string) ([]netip.Prefix, error) {
|
|
var out []netip.Prefix
|
|
for _, s := range in {
|
|
s = strings.TrimSpace(s)
|
|
if p, err := netip.ParsePrefix(s); err == nil {
|
|
out = append(out, p.Masked())
|
|
continue
|
|
}
|
|
a, err := netip.ParseAddr(s)
|
|
if err != nil {
|
|
return nil, &net.ParseError{Type: "proxy address or network", Text: s}
|
|
}
|
|
out = append(out, netip.PrefixFrom(a, a.BitLen()))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func inPrefixes(a netip.Addr, ps []netip.Prefix) bool {
|
|
for _, p := range ps {
|
|
if p.Contains(a) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// clientIP is the address a request really comes from. Without trusted proxies
|
|
// that is the peer address of the connection: forwarding headers are trivial to
|
|
// forge, so behind a reverse proxy every client then shares one allowance (the
|
|
// safe failure). When the connection comes from a trusted proxy, the client is
|
|
// the last address in X-Forwarded-For that is not itself a trusted proxy: each
|
|
// proxy appends the address it saw, so only the entries our own proxies added
|
|
// can be believed, and anything a client wrote sits to their left.
|
|
func (s *server) clientIP(r *http.Request) string {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
peer, err := netip.ParseAddr(host)
|
|
if err != nil || len(s.TrustedProxies) == 0 || !inPrefixes(peer.Unmap(), s.TrustedProxies) {
|
|
return host
|
|
}
|
|
var hops []string
|
|
for _, v := range r.Header.Values("X-Forwarded-For") {
|
|
hops = append(hops, strings.Split(v, ",")...)
|
|
}
|
|
for i := len(hops) - 1; i >= 0; i-- {
|
|
a, err := netip.ParseAddr(strings.TrimSpace(hops[i]))
|
|
if err != nil {
|
|
return host // a header we cannot read is not believed
|
|
}
|
|
if !inPrefixes(a.Unmap(), s.TrustedProxies) {
|
|
return a.Unmap().String()
|
|
}
|
|
}
|
|
return host
|
|
}
|
|
|
|
// rateLimited refuses API requests from an address that makes too many. The
|
|
// UI's static files are not counted; a node's own machine is not limited, so
|
|
// the owner is never locked out of their own node.
|
|
func (s *server) rateLimited(next http.Handler) http.Handler {
|
|
l := newLimiter(s.RequestsPerMinute, time.Minute, s.Clock)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
ip := s.clientIP(r)
|
|
// A request from this machine is exempt, unless a proxy on this
|
|
// machine passed it on (it then carries forwarding headers and
|
|
// really comes from elsewhere).
|
|
forwarded := r.Header.Get("X-Forwarded-For") != "" || r.Header.Get("Forwarded") != ""
|
|
if a, err := netip.ParseAddr(ip); err != nil || !a.IsLoopback() || forwarded {
|
|
if !l.allow(ip) {
|
|
w.Header().Set("Retry-After", "60")
|
|
fail(w, http.StatusTooManyRequests, "too many requests from this address; try again in a minute")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|