- GET /subscribe/{address}: email field, an off-screen honeypot named website,
a Subscribe button; 404 (the usual page) for a private, closed or unknown list
- POST /subscribe/{address}: one plain ASCII address of at most 254 characters,
else the form again (400) with the escaped typed value; a filled honeypot gets
the success page and nothing happens; otherwise Router.RequestJoin and always
the same neutral page, so a member, a rate limited address or an unknown
one cannot be told apart; the body is limited to 4 KiB (413)
- at most 10 POSTs per hour per TCP peer address (X-Forwarded-For is ignored),
then 429 with Retry-After; idle addresses are swept; the clock is injectable
- a Subscribe link on the archive page of a list that qualifies, nothing else
- no-store on the subscribe pages; the CSP is unchanged
- tests for each of these, including escaping, enumeration, the limit and its expiry
81 lines
2 KiB
Go
81 lines
2 KiB
Go
package web
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// A person subscribes once; a script posting the form for other people's addresses is what this
|
|
// slows down: at most subscribeMax POSTs per subscribeWindow from one address. The window starts
|
|
// at the first request of an address and everything about it is forgotten when it ends.
|
|
const (
|
|
subscribeMax = 10
|
|
subscribeWindow = time.Hour
|
|
sweepEvery = time.Minute
|
|
)
|
|
|
|
type visits struct {
|
|
count int
|
|
first time.Time
|
|
}
|
|
|
|
type subscribeLimiter struct {
|
|
mu sync.Mutex
|
|
now func() time.Time
|
|
byIP map[string]*visits
|
|
lastSweep time.Time
|
|
}
|
|
|
|
func newSubscribeLimiter(now func() time.Time) *subscribeLimiter {
|
|
return &subscribeLimiter{now: now, byIP: map[string]*visits{}, lastSweep: now()}
|
|
}
|
|
|
|
// allow counts a request from ip. When the address is over its limit it returns false and how long
|
|
// until it may try again; a refused request is not counted.
|
|
func (l *subscribeLimiter) allow(ip string) (retryAfter time.Duration, ok bool) {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
now := l.now()
|
|
l.sweep(now)
|
|
v := l.byIP[ip]
|
|
if v == nil || now.Sub(v.first) >= subscribeWindow {
|
|
v = &visits{first: now}
|
|
l.byIP[ip] = v
|
|
}
|
|
if v.count >= subscribeMax {
|
|
return v.first.Add(subscribeWindow).Sub(now), false
|
|
}
|
|
v.count++
|
|
return 0, true
|
|
}
|
|
|
|
// sweep forgets addresses whose window is over, so the map cannot grow forever.
|
|
func (l *subscribeLimiter) sweep(now time.Time) {
|
|
if now.Sub(l.lastSweep) < sweepEvery {
|
|
return
|
|
}
|
|
l.lastSweep = now
|
|
for ip, v := range l.byIP {
|
|
if now.Sub(v.first) >= subscribeWindow {
|
|
delete(l.byIP, ip)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (l *subscribeLimiter) size() int {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
return len(l.byIP)
|
|
}
|
|
|
|
// clientIP is the address of the TCP peer. X-Forwarded-For is deliberately ignored: anyone can
|
|
// send it, so trusting it would let a script pick a fresh identity for every request.
|
|
func clientIP(r *http.Request) string {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
return r.RemoteAddr
|
|
}
|
|
return host
|
|
}
|