Files
selfpost/internal/store/sessions_test.go
T
mixandClaude Opus 5.5 b2fbe91de9 panel-redesign: security review of 2.0 - sessions belong to a user by id
Stage 3, step 2. Reviewed by Fable (read-only), fixed by Sonnet with a
failing test before each fix, checked by Opus. No Critical or High
finding: route authorization, tenant isolation by id, the /server/
subtree guard, output encoding and the origin check were found sound.

Fixed
  - M1: a session was keyed by user name and had no link to the user. A
    deleted or renamed user's cookie stayed alive (renewed on every hit,
    before the user was looked up) and became a session of whoever was
    later created under that name, with that account's role. The
    baseline now has sessions.user_id REFERENCES users(id) ON DELETE
    CASCADE; the principal is resolved by id and a session is renewed
    only after its user was found. RenameSession is gone.
  - M2: any user's password change deleted every other user's sessions.
    It ends that user's other sessions only.
  - L1: a password set on the user form ends that user's sessions (an
    administrator editing their own account keeps the current one).
  - L2: store, saslpasswd2 and Postfix reload errors were shown verbatim
    on the application form, the inbound upstream / recipients forms and
    the limit recalculation - pages a domain administrator reaches.
    Validation errors (app.ValidationError, inbound.ValidationError,
    store.ErrNotAutoRateLimit) keep their text; anything else is logged
    and shown as "Could not save ... Check the logs and try again."
  - L3: store.ListDMARCReports treated an empty domain list as "all". It
    takes a scope now (AllDomains or a list; an empty list is no rows),
    and the DMARC hub is open to the domain role as the plan says: the
    reports of the assigned domains, without the instance-wide ingest
    box. The menu entry was already shown to them and led to a 404.
  - I1: /static/ answered with a listing of the embedded assets. A path
    that is not a known asset is a 404.

Consequences
  - The global DMARC hub lists reports of domains that were deleted
    since, too.
  - dev/migrate1x (not in the tree) no longer carries sessions over:
    everyone signs in again after the transfer.
  - docs/schema-migrations.md: the sessions row.

Recorded, not changed - for the owner:
  - L4: the report-address select names the followed user and their
    address to every administrator of the domain, by default the global
    administrator's login name. The plan asks for it.
  - L5: a delegated inbound upstream may be any host and port, loopback
    and private ranges included - within the trusted-operator model; to
    be listed among the accepted risks in security.md.
  - L6: Account's profile is saved without the password; becomes a
    requirement once password-reset mails to users.email.
  - Still echoing an error's text: backup and domain import (global
    only), the domain form's validation messages.

Not checked by the review: anything needing the container (e2e, real
Postfix / DNS), CSP behaviour in a live browser.

  go test -count=1 ./internal/web/... ./internal/store/... ./internal/app/...
    ./internal/inbound/... ./internal/dmarc/... ./cmd/panel
      ok  (all eleven packages; dev/migrate1x also ok)

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

128 lines
4.1 KiB
Go

package store
import (
"testing"
"time"
)
func sessionExpiry() time.Time { return time.Now().Add(time.Hour) }
// A session belongs to a person, not to a name: once the user is deleted the
// cookie must be dead for good, including when somebody else is later created
// under the same username.
func TestSessionDoesNotOutliveItsUserOrPassToANewOneWithTheSameName(t *testing.T) {
st := openTestStore(t)
if _, err := st.CreateUser("admin", "h", RoleGlobal, Reach{}); err != nil {
t.Fatal(err)
}
a, err := st.CreateUser("alice", "h", RoleGlobal, Reach{})
if err != nil {
t.Fatal(err)
}
if err := st.CreateSession("old-token", a, sessionExpiry()); err != nil {
t.Fatalf("CreateSession: %v", err)
}
if err := st.DeleteUser(a); err != nil {
t.Fatalf("DeleteUser: %v", err)
}
if _, err := st.CreateUser("alice", "h", RoleGlobal, Reach{}); err != nil {
t.Fatal(err)
}
if _, found, err := st.LookupSession("old-token"); err != nil || found {
t.Fatalf("LookupSession after the user was deleted and the name reused = found %t, err %v; want no session", found, err)
}
}
// Renaming a user must not detach the sessions they already have.
func TestSessionsFollowTheUserThroughARename(t *testing.T) {
st := openTestStore(t)
id, err := st.CreateUser("alice", "h", RoleGlobal, Reach{})
if err != nil {
t.Fatal(err)
}
if err := st.CreateSession("one", id, sessionExpiry()); err != nil {
t.Fatal(err)
}
if err := st.CreateSession("two", id, sessionExpiry()); err != nil {
t.Fatal(err)
}
if err := st.UpdateUser(id, "alicia", "h", ""); err != nil {
t.Fatalf("UpdateUser: %v", err)
}
for _, token := range []string{"one", "two"} {
row, found, err := st.LookupSession(token)
if err != nil || !found {
t.Fatalf("session %q after the rename = found %t, err %v; want it kept", token, found, err)
}
if row.UserID != id {
t.Errorf("session %q belongs to user %d after the rename, want %d", token, row.UserID, id)
}
if u, err := st.GetUser(row.UserID); err != nil || u.Username != "alicia" {
t.Errorf("session %q resolves to %q (%v) after the rename, want alicia", token, u.Username, err)
}
}
}
// A password change signs out the other sessions of the person who changed it,
// not of anyone else.
func TestDeleteOtherSessionsTouchesOnlyThatUsersSessions(t *testing.T) {
st := openTestStore(t)
ids := map[string]int64{}
for _, name := range []string{"alice", "bob"} {
id, err := st.CreateUser(name, "h", RoleGlobal, Reach{})
if err != nil {
t.Fatal(err)
}
ids[name] = id
}
for token, name := range map[string]string{"alice-1": "alice", "bob-1": "bob", "bob-2": "bob"} {
if err := st.CreateSession(token, ids[name], sessionExpiry()); err != nil {
t.Fatal(err)
}
}
if err := st.DeleteOtherSessions(ids["bob"], "bob-1"); err != nil {
t.Fatalf("DeleteOtherSessions: %v", err)
}
for token, want := range map[string]bool{"alice-1": true, "bob-1": true, "bob-2": false} {
if _, found, _ := st.LookupSession(token); found != want {
t.Errorf("session %q present = %t, want %t", token, found, want)
}
}
}
// Setting somebody's password from the user form ends all of their sessions.
func TestDeleteUserSessionsTouchesOnlyThatUser(t *testing.T) {
st := openTestStore(t)
alice, _ := st.CreateUser("alice", "h", RoleGlobal, Reach{})
bob, _ := st.CreateUser("bob", "h", RoleGlobal, Reach{})
for token, id := range map[string]int64{"alice-1": alice, "bob-1": bob, "bob-2": bob} {
if err := st.CreateSession(token, id, sessionExpiry()); err != nil {
t.Fatal(err)
}
}
if err := st.DeleteUserSessions(bob); err != nil {
t.Fatalf("DeleteUserSessions: %v", err)
}
for token, want := range map[string]bool{"alice-1": true, "bob-1": false, "bob-2": false} {
if _, found, _ := st.LookupSession(token); found != want {
t.Errorf("session %q present = %t, want %t", token, found, want)
}
}
}
// A session cannot be written for a user that does not exist.
func TestCreateSessionRefusesAMissingUser(t *testing.T) {
st := openTestStore(t)
if err := st.CreateSession("orphan", 42, sessionExpiry()); err == nil {
t.Fatal("CreateSession for a missing user succeeded")
}
}