Files
selfpost/internal/web/handlers/handlers_account.go
T
mixandClaude Opus 5.5 f09b60aaa2
test / test (push) Canceled after 0s
panel-notifications: findings of the technical and security reviews
Neither review (read-only agents, Opus and Fable, over 972a9f8..400aa76) has
a Critical finding. Both found the same serious one, and more:

- The notification about refused mail was built on what a rejected row says
  of itself. Its domain is the one in MAIL FROM, which the client typed and
  Postfix has not yet held against the login when the milter refuses: an
  application over its limit could have had the panel write a message per
  made-up domain, to another tenant's users among others. And the same
  status is written when an application is refused for its client address,
  which is no limit. Refusals are now counted by the domain the login is
  registered under, only for logins that exist and only while a limit is set
  on the login or its domain; one subject per sending domain. They are read
  through a partial index of their own (migration 0005): the query walked
  the whole send log every minute.
- Account and user e-mails were checked with the DMARC report validator,
  which refuses Gmail and the like - the owner's own kind of address. A new
  validate.AccountEmail accepts them and refuses a local part beginning "-".
- "Said" now means sent: what arises while nobody can be told, or while
  Postfix does not take the message, is told at a later check. Its ending
  with nobody left to tell is simply over.
- Two DNS checks must be 45 minutes apart as well as two: the last check
  before a restart and the first after it were minutes apart.
- The notifier's own mail waiting in the queue, and its bounce coming back
  to postmaster@ this server, are not the queue's subject. A listing cut at
  its size limit or with a line too long to read is no listing; postqueue -j
  gets 30 seconds.
- The tables must name the selector the panel shows, not only have the line.
- An a/mx lookup that did not answer inside the SPF check made the record
  "not authorising"; it is "cannot tell" now, for the pages too.
- A ceiling of 60 messages an hour; a pass stops between domains when the
  process is asked to stop; sendmail gets a WaitDelay; days are rounded as a
  person counts; a failed read of the switch shows it on, not off.

Recorded as accepted in security.md, with a roadmap candidate
(from-header-binding): an application can have a message signed as the
server's own name - or, as always, as another registered domain - by writing
it into the From: header, which nothing holds against the login.

Left as found: a recipient whose copy failed while another's went out is not
tried again; a certificate that lives under 14 days is reported at each
renewal.

On the test server, for this tree: go vet clean, go test ./... 23 packages
ok, make e2e ok (326 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:00:01 +03:00

182 lines
5.9 KiB
Go

package handlers
import (
"errors"
"net/http"
"strings"
"github.com/mixeme/selfpost/internal/store"
"github.com/mixeme/selfpost/internal/web/validate"
"github.com/mixeme/selfpost/internal/web/view"
"golang.org/x/crypto/bcrypt"
)
// Account is the signed-in user's own half of what used to be one Settings
// page: who they are to the panel (username, e-mail) and their password. Every
// role has it. Each part is its own form and its own POST, so changing one
// never asks for the others.
//
// The e-mail is the user's — what the panel writes to. A domain's DMARC report
// address is set in the domain's settings, where a button fills it with this
// e-mail; nothing here refers to a domain.
// accountForm is what the page shows back: the stored values, or the ones just
// submitted when a form is re-rendered with an error.
type accountForm struct {
Err string
Username string
Email string
Notify bool
}
func (h *Handlers) accountUser(w http.ResponseWriter, r *http.Request) (store.User, bool) {
p, ok := h.principal(r)
if !ok {
http.NotFound(w, r)
return store.User{}, false
}
u, err := h.store.GetUser(p.ID)
if err != nil {
logf("panel: account: get user %d: %v", p.ID, err)
http.Error(w, "internal error", http.StatusInternalServerError)
return store.User{}, false
}
return u, true
}
func formOf(u store.User) accountForm {
return accountForm{Username: u.Username, Email: u.Email, Notify: u.Notify}
}
// HandleAccount shows the Account page.
func (h *Handlers) HandleAccount(w http.ResponseWriter, r *http.Request) {
u, ok := h.accountUser(w, r)
if !ok {
return
}
h.renderAccount(w, r, http.StatusOK, u, formOf(u))
}
func (h *Handlers) renderAccount(w http.ResponseWriter, r *http.Request, status int, u store.User, f accountForm) {
page := view.NewAccount(h.shellMeta(r), string(u.Role))
page.Username, page.Email, page.Notify = f.Username, f.Email, f.Notify
page.WithResult(accountFlash(r), f.Err)
h.view.Render(w, status, "account", page)
}
func accountFlash(r *http.Request) string {
switch r.URL.Query().Get("done") {
case "profile":
return "Profile saved."
case "password":
return "Password changed. Any other signed-in sessions were signed out."
}
return ""
}
// HandleAccountProfile saves the username and the account e-mail.
func (h *Handlers) HandleAccountProfile(w http.ResponseWriter, r *http.Request) {
u, ok := h.accountUser(w, r)
if !ok {
return
}
f := formOf(u)
fail := func(status int, msg string) {
f.Err = msg
h.renderAccount(w, r, status, u, f)
}
if err := r.ParseForm(); err != nil {
fail(http.StatusBadRequest, "Invalid form submission.")
return
}
f.Username = strings.TrimSpace(r.PostFormValue("username"))
f.Email = strings.TrimSpace(r.PostFormValue("email"))
// An unticked checkbox is not sent at all: absent is off.
f.Notify = r.PostFormValue("notify") != ""
if f.Username == "" {
f.Username = u.Username
}
if f.Username != u.Username {
if err := validate.Username(f.Username); err != nil {
fail(http.StatusBadRequest, err.Error())
return
}
}
if err := validate.AccountEmail(f.Email); err != nil {
fail(http.StatusBadRequest, err.Error())
return
}
if err := h.store.UpdateUser(u.ID, f.Username, u.PasswordHash, f.Email); err != nil {
if errors.Is(err, store.ErrUserExists) {
fail(http.StatusConflict, "That username is already in use.")
return
}
logf("panel: account: update user %d: %v", u.ID, err)
fail(http.StatusInternalServerError, "Could not save the profile. Please check the logs and try again.")
return
}
if f.Notify != u.Notify {
if err := h.store.SetUserNotify(u.ID, f.Notify); err != nil {
logf("panel: account: set notifications of user %d: %v", u.ID, err)
fail(http.StatusInternalServerError, "Could not save the profile. Please check the logs and try again.")
return
}
}
logf("panel: user %d profile updated (username: %t, e-mail: %t, notifications: %t)", u.ID, f.Username != u.Username, f.Email != u.Email, f.Notify)
http.Redirect(w, r, "/account?done=profile", http.StatusSeeOther)
}
// HandleAccountPassword changes the password. It asks for the current one and
// is rate-limited like a sign-in: the form is a place to guess a password from
// an unattended session.
func (h *Handlers) HandleAccountPassword(w http.ResponseWriter, r *http.Request) {
u, ok := h.accountUser(w, r)
if !ok {
return
}
f := formOf(u)
fail := func(status int, msg string) {
f.Err = msg
h.renderAccount(w, r, status, u, f)
}
if !h.auth.AllowLoginAttempt(r) {
fail(http.StatusTooManyRequests, "Too many attempts. Please wait and try again.")
return
}
if err := r.ParseForm(); err != nil {
fail(http.StatusBadRequest, "Invalid form submission.")
return
}
current := r.PostFormValue("current_password")
password := r.PostFormValue("new_password")
confirm := r.PostFormValue("new_password_confirm")
if err := bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(current)); err != nil {
fail(http.StatusUnauthorized, "Current password is incorrect.")
return
}
if password != confirm {
fail(http.StatusBadRequest, "New passwords do not match.")
return
}
if err := validate.AdminPassword(password); err != nil {
fail(http.StatusBadRequest, err.Error())
return
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {
logf("panel: account: hashing password failed: %v", err)
fail(http.StatusInternalServerError, "Internal error. Please try again.")
return
}
if err := h.store.UpdateUser(u.ID, u.Username, string(hash), u.Email); err != nil {
logf("panel: account: update password of user %d: %v", u.ID, err)
fail(http.StatusInternalServerError, "Could not save the password. Please check the logs and try again.")
return
}
if token, ok := h.auth.SessionToken(r); ok {
h.auth.DestroyOtherSessions(u.ID, token)
}
logf("panel: user %d changed their password", u.ID)
http.Redirect(w, r, "/account?done=password", http.StatusSeeOther)
}