mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 11:04:38 +03:00
replace http basic auth for web interfaces with session cookie & csrf-based auth
the http basic auth we had was very simple to reason about, and to implement. but it has a major downside: there is no way to logout, browsers keep sending credentials. ideally, browsers themselves would show a button to stop sending credentials. a related downside: the http auth mechanism doesn't indicate for which server paths the credentials are. another downside: the original password is sent to the server with each request. though sending original passwords to web servers seems to be considered normal. our new approach uses session cookies, along with csrf values when we can. the sessions are server-side managed, automatically extended on each use. this makes it easy to invalidate sessions and keeps the frontend simpler (than with long- vs short-term sessions and refreshing). the cookies are httponly, samesite=strict, scoped to the path of the web interface. cookies are set "secure" when set over https. the cookie is set by a successful call to Login. a call to Logout invalidates a session. changing a password invalidates all sessions for a user, but keeps the session with which the password was changed alive. the csrf value is also random, and associated with the session cookie. the csrf must be sent as header for api calls, or as parameter for direct form posts (where we cannot set a custom header). rest-like calls made directly by the browser, e.g. for images, don't have a csrf protection. the csrf value is returned by the Login api call and stored in localstorage. api calls without credentials return code "user:noAuth", and with bad credentials return "user:badAuth". the api client recognizes this and triggers a login. after a login, all auth-failed api calls are automatically retried. only for "user:badAuth" is an error message displayed in the login form (e.g. session expired). in an ideal world, browsers would take care of most session management. a server would indicate authentication is needed (like http basic auth), and the browsers uses trusted ui to request credentials for the server & path. the browser could use safer mechanism than sending original passwords to the server, such as scram, along with a standard way to create sessions. for now, web developers have to do authentication themselves: from showing the login prompt, ensuring the right session/csrf cookies/localstorage/headers/etc are sent with each request. webauthn is a newer way to do authentication, perhaps we'll implement it in the future. though hardware tokens aren't an attractive option for many users, and it may be overkill as long as we still do old-fashioned authentication in smtp & imap where passwords can be sent to the server. for issue #58
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
"context"
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
cryptorand "crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
@ -32,7 +33,6 @@ import (
|
||||
|
||||
_ "embed"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"golang.org/x/exp/maps"
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
@ -63,6 +63,7 @@ import (
|
||||
"github.com/mjl-/mox/store"
|
||||
"github.com/mjl-/mox/tlsrpt"
|
||||
"github.com/mjl-/mox/tlsrptdb"
|
||||
"github.com/mjl-/mox/webauth"
|
||||
)
|
||||
|
||||
var pkglog = mlog.New("webadmin", nil)
|
||||
@ -85,8 +86,6 @@ var webadminFile = &mox.WebappFile{
|
||||
|
||||
var adminDoc = mustParseAPI("admin", adminapiJSON)
|
||||
|
||||
var adminSherpaHandler http.Handler
|
||||
|
||||
func mustParseAPI(api string, buf []byte) (doc sherpadoc.Section) {
|
||||
err := json.Unmarshal(buf, &doc)
|
||||
if err != nil {
|
||||
@ -95,139 +94,98 @@ func mustParseAPI(api string, buf []byte) (doc sherpadoc.Section) {
|
||||
return doc
|
||||
}
|
||||
|
||||
var sherpaHandlerOpts *sherpa.HandlerOpts
|
||||
|
||||
func makeSherpaHandler(cookiePath string, isForwarded bool) (http.Handler, error) {
|
||||
return sherpa.NewHandler("/api/", moxvar.Version, Admin{cookiePath, isForwarded}, &adminDoc, sherpaHandlerOpts)
|
||||
}
|
||||
|
||||
func init() {
|
||||
collector, err := sherpaprom.NewCollector("moxadmin", nil)
|
||||
if err != nil {
|
||||
pkglog.Fatalx("creating sherpa prometheus collector", err)
|
||||
}
|
||||
|
||||
adminSherpaHandler, err = sherpa.NewHandler("/api/", moxvar.Version, Admin{}, &adminDoc, &sherpa.HandlerOpts{Collector: collector, AdjustFunctionNames: "none"})
|
||||
sherpaHandlerOpts = &sherpa.HandlerOpts{Collector: collector, AdjustFunctionNames: "none", NoCORS: true}
|
||||
// Just to validate.
|
||||
_, err = makeSherpaHandler("", false)
|
||||
if err != nil {
|
||||
pkglog.Fatalx("sherpa handler", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler returns a handler for the webadmin endpoints, customized for the
|
||||
// cookiePath.
|
||||
func Handler(cookiePath string, isForwarded bool) func(w http.ResponseWriter, r *http.Request) {
|
||||
sh, err := makeSherpaHandler(cookiePath, isForwarded)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
http.Error(w, "500 - internal server error - cannot handle requests", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
handle(sh, isForwarded, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// Admin exports web API functions for the admin web interface. All its methods are
|
||||
// exported under api/. Function calls require valid HTTP Authentication
|
||||
// credentials of a user.
|
||||
type Admin struct{}
|
||||
|
||||
// We keep a cache for authentication so we don't bcrypt for each incoming HTTP request with HTTP basic auth.
|
||||
// We keep track of the last successful password hash and Authorization header.
|
||||
// The cache is cleared periodically, see below.
|
||||
var authCache struct {
|
||||
sync.Mutex
|
||||
lastSuccessHash, lastSuccessAuth string
|
||||
type Admin struct {
|
||||
cookiePath string // From listener, for setting authentication cookies.
|
||||
isForwarded bool // From listener, whether we look at X-Forwarded-* headers.
|
||||
}
|
||||
|
||||
// started when we start serving. not at package init time, because we don't want
|
||||
// to make goroutines that early.
|
||||
func ManageAuthCache() {
|
||||
for {
|
||||
authCache.Lock()
|
||||
authCache.lastSuccessHash = ""
|
||||
authCache.lastSuccessAuth = ""
|
||||
authCache.Unlock()
|
||||
time.Sleep(15 * time.Minute)
|
||||
}
|
||||
type ctxKey string
|
||||
|
||||
var requestInfoCtxKey ctxKey = "requestInfo"
|
||||
|
||||
type requestInfo struct {
|
||||
SessionToken store.SessionToken
|
||||
Response http.ResponseWriter
|
||||
Request *http.Request // For Proto and TLS connection state during message submit.
|
||||
}
|
||||
|
||||
// check whether authentication from the config (passwordfile with bcrypt hash)
|
||||
// matches the authorization header "authHdr". we don't care about any username.
|
||||
// on (auth) failure, a http response is sent and false returned.
|
||||
func checkAdminAuth(ctx context.Context, passwordfile string, w http.ResponseWriter, r *http.Request) bool {
|
||||
log := pkglog.WithContext(ctx)
|
||||
|
||||
respondAuthFail := func() bool {
|
||||
// note: browsers don't display the realm to prevent users getting confused by malicious realm messages.
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="mox admin - login with empty username and admin password"`)
|
||||
http.Error(w, "http 401 - unauthorized - mox admin - login with empty username and admin password", http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
|
||||
authResult := "error"
|
||||
start := time.Now()
|
||||
var addr *net.TCPAddr
|
||||
defer func() {
|
||||
metrics.AuthenticationInc("webadmin", "httpbasic", authResult)
|
||||
if authResult == "ok" && addr != nil {
|
||||
mox.LimiterFailedAuth.Reset(addr.IP, start)
|
||||
}
|
||||
}()
|
||||
|
||||
var err error
|
||||
var remoteIP net.IP
|
||||
addr, err = net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
if err != nil {
|
||||
log.Errorx("parsing remote address", err, slog.Any("addr", r.RemoteAddr))
|
||||
} else if addr != nil {
|
||||
remoteIP = addr.IP
|
||||
}
|
||||
if remoteIP != nil && !mox.LimiterFailedAuth.Add(remoteIP, start, 1) {
|
||||
metrics.AuthenticationRatelimitedInc("webadmin")
|
||||
http.Error(w, "429 - too many auth attempts", http.StatusTooManyRequests)
|
||||
return false
|
||||
}
|
||||
|
||||
authHdr := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(authHdr, "Basic ") || passwordfile == "" {
|
||||
return respondAuthFail()
|
||||
}
|
||||
buf, err := os.ReadFile(passwordfile)
|
||||
if err != nil {
|
||||
log.Errorx("reading admin password file", err, slog.String("path", passwordfile))
|
||||
return respondAuthFail()
|
||||
}
|
||||
passwordhash := strings.TrimSpace(string(buf))
|
||||
authCache.Lock()
|
||||
defer authCache.Unlock()
|
||||
if passwordhash != "" && passwordhash == authCache.lastSuccessHash && authHdr != "" && authCache.lastSuccessAuth == authHdr {
|
||||
authResult = "ok"
|
||||
return true
|
||||
}
|
||||
auth, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(authHdr, "Basic "))
|
||||
if err != nil {
|
||||
return respondAuthFail()
|
||||
}
|
||||
t := strings.SplitN(string(auth), ":", 2)
|
||||
if len(t) != 2 || len(t[1]) < 8 {
|
||||
log.Info("failed authentication attempt", slog.String("username", "admin"), slog.Any("remote", remoteIP))
|
||||
return respondAuthFail()
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(passwordhash), []byte(t[1])); err != nil {
|
||||
authResult = "badcreds"
|
||||
log.Info("failed authentication attempt", slog.String("username", "admin"), slog.Any("remote", remoteIP))
|
||||
return respondAuthFail()
|
||||
}
|
||||
authCache.lastSuccessHash = passwordhash
|
||||
authCache.lastSuccessAuth = authHdr
|
||||
authResult = "ok"
|
||||
return true
|
||||
}
|
||||
|
||||
func Handle(w http.ResponseWriter, r *http.Request) {
|
||||
func handle(apiHandler http.Handler, isForwarded bool, w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), mlog.CidKey, mox.Cid())
|
||||
if !checkAdminAuth(ctx, mox.ConfigDirPath(mox.Conf.Static.AdminPasswordFile), w, r) {
|
||||
// Response already sent.
|
||||
return
|
||||
}
|
||||
|
||||
if lw, ok := w.(interface{ AddAttr(a slog.Attr) }); ok {
|
||||
lw.AddAttr(slog.Bool("authadmin", true))
|
||||
}
|
||||
log := pkglog.WithContext(ctx).With(slog.String("adminauth", ""))
|
||||
|
||||
// HTML/JS can be retrieved without authentication.
|
||||
if r.URL.Path == "/" {
|
||||
switch r.Method {
|
||||
case "GET", "HEAD":
|
||||
webadminFile.Serve(ctx, log, w, r)
|
||||
default:
|
||||
http.Error(w, "405 - method not allowed - use get", http.StatusMethodNotAllowed)
|
||||
return
|
||||
case "GET", "HEAD":
|
||||
}
|
||||
|
||||
webadminFile.Serve(ctx, pkglog.WithContext(ctx), w, r)
|
||||
return
|
||||
}
|
||||
adminSherpaHandler.ServeHTTP(w, r.WithContext(ctx))
|
||||
|
||||
isAPI := strings.HasPrefix(r.URL.Path, "/api/")
|
||||
// Only allow POST for calls, they will not work cross-domain without CORS.
|
||||
if isAPI && r.URL.Path != "/api/" && r.Method != "POST" {
|
||||
http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// All other URLs, except the login endpoint require some authentication.
|
||||
var sessionToken store.SessionToken
|
||||
if r.URL.Path != "/api/LoginPrep" && r.URL.Path != "/api/Login" {
|
||||
var ok bool
|
||||
_, sessionToken, _, ok = webauth.Check(ctx, log, webauth.Admin, "webadmin", isForwarded, w, r, isAPI, isAPI, false)
|
||||
if !ok {
|
||||
// Response has been written already.
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if isAPI {
|
||||
reqInfo := requestInfo{sessionToken, w, r}
|
||||
ctx = context.WithValue(ctx, requestInfoCtxKey, reqInfo)
|
||||
apiHandler.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
||||
func xcheckf(ctx context.Context, err error, format string, args ...any) {
|
||||
@ -254,6 +212,45 @@ func xcheckuserf(ctx context.Context, err error, format string, args ...any) {
|
||||
panic(&sherpa.Error{Code: "user:error", Message: errmsg})
|
||||
}
|
||||
|
||||
// LoginPrep returns a login token, and also sets it as cookie. Both must be
|
||||
// present in the call to Login.
|
||||
func (w Admin) LoginPrep(ctx context.Context) string {
|
||||
log := pkglog.WithContext(ctx)
|
||||
reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
|
||||
|
||||
var data [8]byte
|
||||
_, err := cryptorand.Read(data[:])
|
||||
xcheckf(ctx, err, "generate token")
|
||||
loginToken := base64.RawURLEncoding.EncodeToString(data[:])
|
||||
|
||||
webauth.LoginPrep(ctx, log, "webadmin", w.cookiePath, w.isForwarded, reqInfo.Response, reqInfo.Request, loginToken)
|
||||
|
||||
return loginToken
|
||||
}
|
||||
|
||||
// Login returns a session token for the credentials, or fails with error code
|
||||
// "user:badLogin". Call LoginPrep to get a loginToken.
|
||||
func (w Admin) Login(ctx context.Context, loginToken, password string) store.CSRFToken {
|
||||
log := pkglog.WithContext(ctx)
|
||||
reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
|
||||
|
||||
csrfToken, err := webauth.Login(ctx, log, webauth.Admin, "webadmin", w.cookiePath, w.isForwarded, reqInfo.Response, reqInfo.Request, loginToken, "", password)
|
||||
if _, ok := err.(*sherpa.Error); ok {
|
||||
panic(err)
|
||||
}
|
||||
xcheckf(ctx, err, "login")
|
||||
return csrfToken
|
||||
}
|
||||
|
||||
// Logout invalidates the session token.
|
||||
func (w Admin) Logout(ctx context.Context) {
|
||||
log := pkglog.WithContext(ctx)
|
||||
reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
|
||||
|
||||
err := webauth.Logout(ctx, log, webauth.Admin, "webadmin", w.cookiePath, w.isForwarded, reqInfo.Response, reqInfo.Request, "", reqInfo.SessionToken)
|
||||
xcheckf(ctx, err, "logout")
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Errors []string
|
||||
Warnings []string
|
||||
|
Reference in New Issue
Block a user