Files
ilkerandClaude Opus 5 3bdf242609 hpcod: the session now reaches the clusters it is supposed to authorise
Clicking "Open portal" landed on the login page. The session cookie was
host-only for hpc.example.org, so a browser never sent it to
c001.hpc.example.org - and since the cluster portal gave up its own
password and relies on the control plane's session, no cookie meant no
entry.

My own check had said it worked, because it passed the cookie with a
hand-set header. Curl does what it is told; a browser does what the
cookie says. Repeating it with a cookie jar reproduced the failure
immediately, and the tests now assert the domain rather than the
behaviour of a header I wrote myself.

The cookie names the cluster domain, so it covers the control plane and
its cluster subdomains - every one of which is this same process, since
the proxy serves them all. Nothing else was loosened: still HttpOnly,
still Secure, still SameSite=Strict. Signing out names the same domain,
or the browser keeps the original and the logout does nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 13:33:15 +02:00

553 lines
17 KiB
Go

package httpapi
// Authentication for the HPC-on-Demand portal.
//
// The portal holds read credentials for every backing cloud, so a stranger who
// gets in learns the shape and capacity of the whole estate. Once it is
// reachable from the internet the only thing in the way is this file, so it is
// deliberately conservative:
//
// - bcrypt password verification against an htpasswd-style file
// - opaque 256-bit session tokens held server-side, never a signed cookie
// - SameSite=Strict cookies, which also removes the CSRF class of bug
// - per-username and per-IP lockout with exponential backoff
// - constant-time comparisons, and the same error text for every failure
//
// Sessions live in memory: a restart logs everyone out, which is the safe
// default for a service like this.
import (
"context"
"crypto/rand"
"crypto/subtle"
"encoding/hex"
"errors"
"html"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/ilkermanap/hpcod/internal/store"
"golang.org/x/crypto/bcrypt"
)
const (
sessionCookie = "hpcod_session"
maxFailures = 5
baseLockout = 30 * time.Second
maxLockout = 30 * time.Minute
)
type session struct {
user string
admin bool
created time.Time
lastSeen time.Time
ip string
}
type failure struct {
count int
until time.Time
}
// accountSource is the database, when there is one. An interface so the
// auth tests do not need a PostgreSQL to check who may sign in.
type accountSource interface {
AccountWithHash(ctx context.Context, username string) (*store.Account, string, error)
}
type authStore struct {
accounts accountSource
// cookieDomain widens the session beyond the control plane's own
// hostname to its cluster subdomains.
//
// Without it the cookie is host-only, so a browser sends it to
// hpc.example and never to c001.hpc.example - and since the cluster
// portal now relies on the control plane's session, clicking through
// to a cluster lands on the login page. Curl with a hand-set header
// does not reproduce that, which is how it got missed.
//
// Every host it widens to is this same process: the proxy serves the
// control plane and every cluster subdomain under that name.
cookieDomain string
mu sync.Mutex
users map[string]string // username -> bcrypt hash
sessions map[string]*session
fails map[string]*failure // keyed by username and by IP
idleTTL time.Duration
absTTL time.Duration
}
func newAuthStore(userFile string, idleTTL, absTTL time.Duration) (*authStore, error) {
users, err := loadUsers(userFile)
if err != nil {
return nil, err
}
a := &authStore{
users: users,
sessions: map[string]*session{},
fails: map[string]*failure{},
idleTTL: idleTTL,
absTTL: absTTL,
}
go a.reap()
return a, nil
}
// loadUsers reads "username:$2y$..." lines, the format htpasswd -B writes.
func loadUsers(path string) (map[string]string, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
users := map[string]string{}
for _, line := range strings.Split(string(raw), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
name, hash, ok := strings.Cut(line, ":")
if !ok || name == "" || !strings.HasPrefix(hash, "$2") {
continue
}
users[name] = hash
}
if len(users) == 0 {
return nil, os.ErrInvalid
}
return users, nil
}
func (a *authStore) lockedOut(key string) (bool, time.Duration) {
f := a.fails[key]
if f == nil || f.count < maxFailures {
return false, 0
}
if d := time.Until(f.until); d > 0 {
return true, d
}
return false, 0
}
func (a *authStore) noteFailure(key string) {
f := a.fails[key]
if f == nil {
f = &failure{}
a.fails[key] = f
}
f.count++
if f.count >= maxFailures {
// Exponential backoff, capped.
d := baseLockout << (f.count - maxFailures)
if d > maxLockout || d <= 0 {
d = maxLockout
}
f.until = time.Now().Add(d)
}
}
// authResult is what a sign-in attempt amounted to. Reason is safe to show:
// it is only ever more specific than "no" once the password has been
// verified, so it cannot be used to find out which usernames exist.
type authResult struct {
OK bool
Admin bool
RetryIn time.Duration
Reason string
}
// authenticate verifies a username/password pair. It always runs a bcrypt
// comparison, even for unknown users, so response time does not reveal whether
// an account exists.
//
// Accounts come from the database when there is one, and from the users file
// otherwise. A file account is an administrator: it is how the portal is
// bootstrapped, and someone with write access to that file has the machine
// anyway.
func (a *authStore) authenticate(ctx context.Context, user, pass, ip string) authResult {
a.mu.Lock()
for _, key := range []string{"u:" + user, "i:" + ip} {
if locked, d := a.lockedOut(key); locked {
a.mu.Unlock()
return authResult{RetryIn: d, Reason: "too many attempts"}
}
}
fileHash, inFile := a.users[user]
a.mu.Unlock()
// Resolved outside the lock: this reaches the database, and holding a
// mutex across a network call would let one slow query stall every
// other sign-in.
hash, known, state, admin := fileHash, inFile, "active", inFile
if a.accounts != nil {
if acct, h, err := a.accounts.AccountWithHash(ctx, user); err == nil {
hash, known, state, admin = h, true, acct.State, acct.IsAdmin
} else if !errors.Is(err, store.ErrNoAccount) {
log.Printf("auth: looking up %q: %v", user, err)
return authResult{Reason: "the account store is unavailable"}
}
}
if !known {
// NOT A CREDENTIAL. This is a fixed, publicly known bcrypt digest used
// only as a comparison target for unknown usernames, so that a failed
// login costs the same time whether or not the account exists. Without
// it, response timing would let an attacker enumerate valid usernames.
hash = "$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy"
}
passOK := bcrypt.CompareHashAndPassword([]byte(hash), []byte(pass)) == nil
a.mu.Lock()
defer a.mu.Unlock()
if !passOK || !known {
a.noteFailure("u:" + user)
a.noteFailure("i:" + ip)
log.Printf("auth: failed login for %q from %s", user, ip)
return authResult{}
}
// The password was right, so saying why the account still cannot be used
// tells its owner something they are entitled to know and tells an
// attacker nothing they could not already confirm.
switch state {
case "pending":
log.Printf("auth: %q signed in correctly but is awaiting approval", user)
return authResult{Reason: "this account is waiting for an administrator to approve it"}
case "disabled":
log.Printf("auth: %q signed in correctly but is disabled", user)
return authResult{Reason: "this account has been disabled"}
}
delete(a.fails, "u:"+user)
delete(a.fails, "i:"+ip)
return authResult{OK: true, Admin: admin}
}
// stillAllowed re-checks an account behind a live session. It reports
// whether they are an administrator now, and whether they may still be
// here at all.
//
// An account only in the users file has no row and stays valid: that file
// is the bootstrap, and a portal whose database is briefly unreachable
// should not lock its operator out of the machine that would fix it.
func (a *authStore) stillAllowed(ctx context.Context, user string) (admin, ok bool) {
a.mu.Lock()
_, inFile := a.users[user]
a.mu.Unlock()
if a.accounts == nil {
return inFile, true
}
acct, _, err := a.accounts.AccountWithHash(ctx, user)
switch {
case errors.Is(err, store.ErrNoAccount):
return inFile, inFile
case err != nil:
// The database is unreachable. Keeping the session is the lesser
// evil: signing everyone out on a blip is its own outage, and
// nothing they can reach changes an account.
log.Printf("auth: re-checking %q: %v", user, err)
return inFile, true
case !acct.Active():
log.Printf("auth: session for %q ended: the account is %s", user, acct.State)
return false, false
}
return acct.IsAdmin, true
}
func (a *authStore) newSession(user string, admin bool, ip string) string {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
panic(err) // a CSPRNG failure is not something to paper over
}
tok := hex.EncodeToString(b)
now := time.Now()
a.mu.Lock()
defer a.mu.Unlock()
a.sessions[tok] = &session{user: user, admin: admin, created: now, lastSeen: now, ip: ip}
log.Printf("auth: session opened for %q (admin=%v) from %s", user, admin, ip)
return tok
}
func (a *authStore) lookup(tok string) *session {
if tok == "" {
return nil
}
a.mu.Lock()
defer a.mu.Unlock()
// Constant-time lookup so token comparison does not leak by timing.
var found *session
for k, s := range a.sessions {
if subtle.ConstantTimeCompare([]byte(k), []byte(tok)) == 1 {
found = s
break
}
}
if found == nil {
return nil
}
now := time.Now()
if now.Sub(found.lastSeen) > a.idleTTL || now.Sub(found.created) > a.absTTL {
delete(a.sessions, tok)
return nil
}
found.lastSeen = now
return found
}
func (a *authStore) destroy(tok string) {
a.mu.Lock()
defer a.mu.Unlock()
if s := a.sessions[tok]; s != nil {
log.Printf("auth: session closed for %q", s.user)
}
delete(a.sessions, tok)
}
func (a *authStore) reap() {
for range time.Tick(5 * time.Minute) {
now := time.Now()
a.mu.Lock()
for tok, s := range a.sessions {
if now.Sub(s.lastSeen) > a.idleTTL || now.Sub(s.created) > a.absTTL {
delete(a.sessions, tok)
}
}
for k, f := range a.fails {
if f.count < maxFailures && now.Sub(f.until) > time.Hour {
delete(a.fails, k)
}
}
a.mu.Unlock()
}
}
// clientIP returns the caller's address, honouring X-Forwarded-For only when
// the immediate peer is the configured reverse proxy. Trusting that header
// from anyone would make the per-IP lockout trivially bypassable.
func clientIP(r *http.Request, trustedProxy string) string {
peer, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
peer = r.RemoteAddr
}
if trustedProxy != "" && peer == trustedProxy {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
first := strings.TrimSpace(strings.Split(xff, ",")[0])
if first != "" {
return first
}
}
}
return peer
}
// isHTTPS reports whether the browser's connection is TLS, which decides
// whether the session cookie may carry the Secure flag. Behind Caddy the
// backend hop is plain HTTP, so this comes from X-Forwarded-Proto.
func isHTTPS(r *http.Request, trustedProxy string) bool {
if r.TLS != nil {
return true
}
peer, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
peer = r.RemoteAddr
}
if trustedProxy != "" && peer == trustedProxy {
return strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https")
}
return false
}
// middleware gates everything except /healthz and the login endpoints.
func (a *authStore) middleware(next http.Handler, loginPage []byte, trustedProxy string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/healthz", "/signup":
// Public: the front door's sign-up form has to be reachable by
// someone who has no account yet, which is the whole point of
// it. It creates nothing usable - see store.CreateAccount.
next.ServeHTTP(w, r)
return
case "/login":
a.handleLogin(w, r, loginPage, trustedProxy)
return
case "/logout":
if c, err := r.Cookie(sessionCookie); err == nil {
a.destroy(c.Value)
}
clearCookie(w, isHTTPS(r, trustedProxy), a.cookieDomain)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
c, err := r.Cookie(sessionCookie)
sess := (*session)(nil)
if err == nil {
sess = a.lookup(c.Value)
}
// Rights are re-read on every request, not taken from what they
// were at sign-in. Otherwise disabling an account or revoking
// administrator rights would not take effect until the session
// expired, which can be twelve hours - and "disabled" has to mean
// now, or it does not mean much.
admin := false
if sess != nil {
var ok bool
if admin, ok = a.stillAllowed(r.Context(), sess.user); !ok {
a.destroy(c.Value)
clearCookie(w, isHTTPS(r, trustedProxy), a.cookieDomain)
sess = nil
}
}
// "/" is the front door and is public: someone arriving at the
// hostname should be told what this is, not handed a password
// prompt for an account they do not have. The page behind it
// carries nothing about the estate, and the session is still looked
// up first so that a signed-in operator gets their own overview
// there instead of the visitor's page.
//
// Nothing else is public. An unknown path still redirects rather
// than 404s, which keeps the set of routes from being enumerable by
// someone who cannot sign in.
if r.URL.Path == "/" {
ctx := r.Context()
if sess != nil {
ctx = withUser(ctx, sess.user, admin)
}
next.ServeHTTP(w, r.WithContext(ctx))
return
}
if sess == nil {
// An API client must get a clean 401 it can act on; a browser
// navigation gets the login form. Redirecting a curl or a script
// to an HTML page is the kind of thing that wastes an afternoon.
if strings.HasPrefix(r.URL.Path, "/v1/") ||
strings.Contains(r.Header.Get("Accept"), "application/json") {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"error":"session expired - reload and sign in again"}`))
return
}
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// Carry who this is, so an action that creates something lasting can
// record who asked for it.
next.ServeHTTP(w, r.WithContext(withUser(r.Context(), sess.user, admin)))
})
}
type userKey struct{}
type adminKey struct{}
func withUser(ctx context.Context, user string, admin bool) context.Context {
return context.WithValue(context.WithValue(ctx, userKey{}, user), adminKey{}, admin)
}
// userFromContext names the signed-in operator, or "" when authentication is
// disabled. Handlers must treat the empty case as "nobody said", not as a
// user called "".
func userFromContext(ctx context.Context) string {
u, _ := ctx.Value(userKey{}).(string)
return u
}
// isAdmin reports whether this request may approve accounts. It reads the
// session, not the request: a client cannot claim it.
func isAdmin(ctx context.Context) bool {
v, _ := ctx.Value(adminKey{}).(bool)
return v
}
func (a *authStore) handleLogin(w http.ResponseWriter, r *http.Request, page []byte, trustedProxy string) {
secure := isHTTPS(r, trustedProxy)
if r.Method == http.MethodGet {
if c, err := r.Cookie(sessionCookie); err == nil && a.lookup(c.Value) != nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
servePage(w, page, "")
return
}
if r.Method != http.MethodPost {
w.Header().Set("Allow", "GET, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
servePage(w, page, "Malformed request.")
return
}
ip := clientIP(r, trustedProxy)
user := strings.TrimSpace(r.PostFormValue("username"))
pass := r.PostFormValue("password")
res := a.authenticate(r.Context(), user, pass, ip)
if !res.OK {
switch {
case res.RetryIn > 0:
servePage(w, page, "Too many failed attempts. Try again in "+
res.RetryIn.Round(time.Second).String()+".")
case res.Reason != "":
// Only ever reached once the password has been verified, so
// this says nothing an attacker could not already confirm.
servePage(w, page, html.EscapeString(res.Reason)+".")
default:
// One message for every other failure mode: no account enumeration.
servePage(w, page, "Incorrect username or password.")
}
return
}
tok := a.newSession(user, res.Admin, ip)
http.SetCookie(w, &http.Cookie{
Name: sessionCookie,
Value: tok,
Path: "/",
Domain: a.cookieDomain,
HttpOnly: true,
Secure: secure,
SameSite: http.SameSiteStrictMode,
MaxAge: int(a.absTTL.Seconds()),
})
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// clearCookie must name the same domain the cookie was set with, or the
// browser keeps the original and signing out does nothing.
func clearCookie(w http.ResponseWriter, secure bool, domain string) {
http.SetCookie(w, &http.Cookie{
Name: sessionCookie, Value: "", Path: "/", Domain: domain,
HttpOnly: true, Secure: secure, SameSite: http.SameSiteStrictMode, MaxAge: -1,
})
}
func servePage(w http.ResponseWriter, page []byte, errMsg string) {
body := string(page)
if errMsg != "" {
body = strings.Replace(body, "<!--ERROR-->",
`<div class="err">`+html.EscapeString(errMsg)+`</div>`, 1)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
if errMsg != "" {
w.WriteHeader(http.StatusUnauthorized)
}
_, _ = w.Write([]byte(body))
}