Decided by the owner on 2026-10-13 (in place of preflight), taken up on 2026-10-16; mockupsb2dcefc,cc33890,0feaa55(his: the greeting on a row of its own, the name check a tag beside Names). - The certificate: health.Certificate keeps its names (DNS names, or the CN without any) and ForHost holds it to SELFPOST_HOSTNAME as a TLS client does - VerifyHostname over the DNS names, the CN only without any, as OpenSSL's X509_check_host and so a sending Postfix still take it (src/tls/tls_client.c:439, 513). A certificate for another name fails; the tag beside Names says matches / not for the hostname. - The greeting: postconf -x -h smtpd_banner, read once at the panel's start like the served certificate's path (main.cf is not rewritten after), is what smtpd writes after "220 " (src/smtpd/smtpd.c:5979). A first word other than the hostname warns, with what to set. A fact may now take the whole row (Fact.Wide, sp-wide) and carry a tag beside its label (Fact.LabelTag); both on the kit page. - The proxy: auth.Module.Seen says the address the panel counts sign-ins against, the peer, and whether X-Forwarded-For was read or left unread for an untrusted peer; the Configuration card shows "Your address" and, in the second case, warns that every visitor shares one sign-in limit and names the /32 (or /128) to put in TRUSTED_PROXY_CIDR. Tests: TestCertificateForHost (CN-only, DNS names, a wildcard, the CN ignored beside DNS names), TestSeenAddress, TestHealthSaysWhereItSeesTheViewer, TestHealthGreetingAndCertificateName; e2e reads the stand's right state on Health (matches, "220 mail.e2e.test ESMTP", a direct client). go vet + go test (26 packages) and make e2e on the test server for this tree. Docs: help (word for word with the mockup), guide § Overview and Health, CHANGELOG [Unreleased] § Added, roadmap row done. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
4.9 KiB
Go
162 lines
4.9 KiB
Go
// Package auth implements the panel's login sessions, one-time setup flow,
|
|
// and authentication middleware.
|
|
package auth
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mixeme/selfpost/internal/store"
|
|
"github.com/mixeme/selfpost/internal/web/view"
|
|
)
|
|
|
|
// Config holds auth-specific panel configuration.
|
|
type Config struct {
|
|
CookieSecure bool
|
|
Hostname string
|
|
SessionIdleDays int
|
|
TrustedProxyCIDRs []*net.IPNet
|
|
}
|
|
|
|
// Module handles login, logout, setup, and session middleware.
|
|
type Module struct {
|
|
store *store.Store
|
|
cfg Config
|
|
view *view.Engine
|
|
sessions *sessionStore
|
|
setup *setupManager
|
|
|
|
loginLimiter *rateLimiter
|
|
setupLimiter *rateLimiter
|
|
passwordLimiter *rateLimiter
|
|
|
|
trustedProxies []*net.IPNet
|
|
}
|
|
|
|
// New builds the auth module. setupTokenPath is where the current setup token
|
|
// is mirrored on disk (security.md).
|
|
func New(st *store.Store, cfg Config, v *view.Engine, setupTokenPath string) *Module {
|
|
idleDays := cfg.SessionIdleDays
|
|
if idleDays <= 0 {
|
|
idleDays = 7
|
|
}
|
|
m := &Module{
|
|
store: st,
|
|
cfg: cfg,
|
|
view: v,
|
|
sessions: newSessionStore(st, time.Duration(idleDays)*24*time.Hour),
|
|
setupLimiter: newRateLimiter(10, time.Minute),
|
|
loginLimiter: newRateLimiter(10, 15*time.Minute),
|
|
// The Account form's current-password check has a bucket of its
|
|
// own, per user: ten wrong guesses there used to lock the address
|
|
// out of signing in (audit 2026-10-12, L15), and the one guessing
|
|
// is whoever holds that user's session, wherever from.
|
|
passwordLimiter: newRateLimiter(10, 15*time.Minute),
|
|
trustedProxies: cfg.TrustedProxyCIDRs,
|
|
}
|
|
m.setupLimiter.startSweeper()
|
|
m.loginLimiter.startSweeper()
|
|
m.passwordLimiter.startSweeper()
|
|
// Made now rather than at the first unknown name, which would otherwise
|
|
// pay for two bcrypt runs (review of audit stage 4).
|
|
go noSuchUserHash()
|
|
m.setup = newSetupManager(st, cfg.Hostname, setupTokenPath)
|
|
return m
|
|
}
|
|
|
|
// Bootstrap runs once at startup. If setup is not complete it mints and
|
|
// announces the first setup token (security.md).
|
|
func (m *Module) Bootstrap() error {
|
|
return m.setup.bootstrap()
|
|
}
|
|
|
|
// AllowPasswordChange reports whether another current-password check on the
|
|
// Account form is within the user's rate limit (security.md).
|
|
func (m *Module) AllowPasswordChange(userID int64) bool {
|
|
return m.passwordLimiter.Allow("user:" + strconv.FormatInt(userID, 10))
|
|
}
|
|
|
|
// SessionToken returns the session token the request carries, if exactly one
|
|
// cookie of that name is present.
|
|
func (m *Module) SessionToken(r *http.Request) (string, bool) {
|
|
return m.sessionToken(r)
|
|
}
|
|
|
|
// DestroyOtherSessions invalidates every session of the user except keep.
|
|
func (m *Module) DestroyOtherSessions(userID int64, keep string) {
|
|
m.sessions.DestroyOthers(userID, keep)
|
|
}
|
|
|
|
// DestroyUserSessions invalidates every session of the user.
|
|
func (m *Module) DestroyUserSessions(userID int64) {
|
|
m.sessions.DestroyAll(userID)
|
|
}
|
|
|
|
func logf(format string, args ...any) {
|
|
log.Printf(format, args...)
|
|
}
|
|
|
|
// clientIP extracts the peer IP for rate-limiting. By default it is the
|
|
// transport peer (RemoteAddr), which cannot be spoofed. If RemoteAddr matches
|
|
// one of trustedProxies, the last entry of X-Forwarded-For is used instead —
|
|
// that is the address the trusted proxy itself appended, so a client can't
|
|
// forge it by sending its own XFF header.
|
|
func clientIP(r *http.Request, trustedProxies []*net.IPNet) string {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
|
|
if len(trustedProxies) > 0 {
|
|
if peer := net.ParseIP(host); peer != nil && ipInAny(peer, trustedProxies) {
|
|
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
|
parts := strings.Split(xff, ",")
|
|
if ip := net.ParseIP(strings.TrimSpace(parts[len(parts)-1])); ip != nil {
|
|
return ip.String()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return host
|
|
}
|
|
|
|
// SeenAddress is how the panel sees a request's address, for Health: the
|
|
// address it counts sign-ins against (Client), the transport peer (Peer),
|
|
// and whether Client came from X-Forwarded-For (FromHeader) or the header
|
|
// was there and left unread because the peer is not a trusted proxy
|
|
// (HeaderIgnored) - the case where every visitor shares the proxy's address
|
|
// and one sign-in limit.
|
|
type SeenAddress struct {
|
|
Client string
|
|
Peer string
|
|
FromHeader bool
|
|
HeaderIgnored bool
|
|
}
|
|
|
|
// Seen reads a request's address as clientIP does.
|
|
func (m *Module) Seen(r *http.Request) SeenAddress {
|
|
peer, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
peer = r.RemoteAddr
|
|
}
|
|
s := SeenAddress{Client: clientIP(r, m.trustedProxies), Peer: peer}
|
|
hasHeader := r.Header.Get("X-Forwarded-For") != ""
|
|
s.FromHeader = hasHeader && s.Client != peer
|
|
s.HeaderIgnored = hasHeader && !s.FromHeader
|
|
return s
|
|
}
|
|
|
|
func ipInAny(ip net.IP, nets []*net.IPNet) bool {
|
|
for _, n := range nets {
|
|
if n.Contains(ip) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|