Files
selfpost/internal/web/handlers/handlers_errors_test.go
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

209 lines
8.8 KiB
Go

package handlers
import (
"database/sql"
"errors"
"net/http"
"net/url"
"path/filepath"
"strings"
"testing"
"github.com/mixeme/selfpost/internal/app"
"github.com/mixeme/selfpost/internal/inbound"
"github.com/mixeme/selfpost/internal/postfix"
"github.com/mixeme/selfpost/internal/store"
)
// A page that a domain administrator sees says what is wrong with what they
// typed. It never says what went wrong inside the server: paths, SQL, the
// output of a command. Those go to the log, and the page gets a fixed line.
// leak is a stand-in for internal detail that must never reach a page.
const leak = "secret-internal-detail"
const savedElsewhere = "Check the logs and try again."
type failingSenderMaps struct{}
func (failingSenderMaps) RebuildSenderLoginMaps([]postfix.Binding) error {
return errors.New("write /srv/" + leak + "/sender_login_maps: permission denied")
}
type failingInboundMaps struct{}
func (failingInboundMaps) RebuildInboundMaps([]postfix.InboundRoute) error {
return errors.New("postmap /srv/" + leak + "/transport_maps: exit status 1")
}
// breakDatabase makes the statements fail inside SQLite with an error that
// carries the leak, as a real storage failure would carry its own detail.
func breakDatabase(t *testing.T, dbPath string, triggers ...string) {
t.Helper()
db, err := sql.Open("sqlite", "file:"+dbPath+"?_pragma=busy_timeout(5000)")
if err != nil {
t.Fatalf("open database directly: %v", err)
}
defer db.Close()
for _, stmt := range triggers {
if _, err := db.Exec(stmt); err != nil {
t.Fatalf("%s: %v", stmt, err)
}
}
}
// refusal checks a refused request: the status it has today, none of the
// internal detail, and the fixed line in its place.
func refusal(t *testing.T, what, body string, wantCode, code int) {
t.Helper()
shown := noticeOf(body)
if code != wantCode {
t.Errorf("%s = %d, want %d; page says %q", what, code, wantCode, shown)
}
if strings.Contains(body, leak) {
t.Errorf("%s shows internal detail; page says %q", what, shown)
}
if !strings.Contains(body, savedElsewhere) {
t.Errorf("%s does not give the fixed line %q; page says %q", what, savedElsewhere, shown)
}
}
// noticeOf is the text of the page's refusal notice, for failure messages.
func noticeOf(body string) string {
for _, line := range strings.Split(body, "\n") {
if strings.Contains(line, "ti-alert-circle") {
return strings.TrimSpace(line)
}
}
return "(no notice)"
}
func TestApplicationFormDoesNotEchoInternalErrors(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.db")
s := newAppStandAt(t, path)
working := s.h.apps
a, _, err := working.CreateWithSettings(s.d.ID, "prod-server", app.Settings{Mode: store.AddressModeWildcard}, nil)
if err != nil {
t.Fatal(err)
}
paths := map[string]string{"id": idStr(s.d.ID), "aid": idStr(a.ID)}
// The reload of the Postfix maps fails.
sasl := app.NewSASLDB("/data/sasl/sasldb2", "mail.example.org").WithRunner(func([]string, []byte) error { return nil })
s.h.apps = app.NewService(s.h.store, sasl, failingSenderMaps{})
rec := postFormAs(s.h.HandleApplicationCreate, globalPrincipal, "/outbound/domains/1/applications/new", s.paths,
url.Values{"login": {"new-server"}, "mode": {"wildcard"}})
refusal(t, "create when the reload fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
rec = postFormAs(s.h.HandleApplicationSave, globalPrincipal, "/outbound/domains/1/applications/1", paths,
url.Values{"mode": {"wildcard"}})
refusal(t, "save when the reload fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
// The database fails.
s.h.apps = working
breakDatabase(t, path, `CREATE TRIGGER boom BEFORE UPDATE ON applications BEGIN SELECT RAISE(ABORT, '`+leak+`'); END`)
rec = postFormAs(s.h.HandleApplicationSave, globalPrincipal, "/outbound/domains/1/applications/1", paths,
url.Values{"mode": {"wildcard"}})
refusal(t, "save when the database fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
}
// What the administrator typed wrong is still said in full.
func TestApplicationFormStillShowsValidationMessages(t *testing.T) {
s := newAppStand(t)
a, _, err := s.h.apps.CreateWithSettings(s.d.ID, "prod-server", app.Settings{Mode: store.AddressModeWildcard}, nil)
if err != nil {
t.Fatal(err)
}
paths := map[string]string{"id": idStr(s.d.ID), "aid": idStr(a.ID)}
for name, tc := range map[string]struct {
url.Values
want string
}{
"a bad login": {url.Values{"login": {"has space"}, "mode": {"wildcard"}}, "login may contain only letters, digits"},
"another domain's one": {url.Values{"login": {"other-app"}, "mode": {"list"}, "addresses": {"a@evil.example"}}, "does not belong to domain example.org"},
"a taken login": {url.Values{"login": {"prod-server"}, "mode": {"wildcard"}}, "That login is already in use"},
} {
rec := postFormAs(s.h.HandleApplicationCreate, globalPrincipal, "/outbound/domains/1/applications/new", s.paths, tc.Values)
if !strings.Contains(rec.Body.String(), tc.want) {
t.Errorf("create with %s does not say %q:\n%s", name, tc.want, rec.Body.String())
}
}
rec := postFormAs(s.h.HandleApplicationSave, globalPrincipal, "/outbound/domains/1/applications/1", paths,
url.Values{"mode": {"list"}, "addresses": {"a@evil.example"}})
if !strings.Contains(rec.Body.String(), "does not belong to domain example.org") {
t.Errorf("save with another domain's address does not say so:\n%s", rec.Body.String())
}
}
func TestInboundFormsDoNotEchoInternalErrors(t *testing.T) {
h, st := inboundHandlers(t)
d, err := st.AddInboundDomain("lists.example.com")
if err != nil {
t.Fatal(err)
}
h.inbound = inbound.NewService(st, failingInboundMaps{})
id := itoa(d.ID)
rec := inboundCall(h, http.MethodPost, "/inbound/domains/"+id+"/upstream",
url.Values{"host": {"10.0.0.8"}, "port": {"25"}, "tls_mode": {"encrypt"}}, globalPrincipal)
refusal(t, "upstream when the reload fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
rec = inboundCall(h, http.MethodPost, "/inbound/domains/"+id+"/recipients",
url.Values{"recipient_mode": {"any"}}, globalPrincipal)
refusal(t, "recipients when the reload fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
}
func TestInboundFormsStillShowValidationMessages(t *testing.T) {
h, st := inboundHandlers(t)
d, err := st.AddInboundDomain("lists.example.com")
if err != nil {
t.Fatal(err)
}
id := itoa(d.ID)
for name, tc := range map[string]struct {
target string
form url.Values
want string
}{
"a bad host": {"/upstream", url.Values{"host": {"bad host!"}, "port": {"25"}, "tls_mode": {"may"}}, "host is invalid"},
"a bad port": {"/upstream", url.Values{"host": {"10.0.0.8"}, "port": {"99999"}, "tls_mode": {"may"}}, "port must be between 1 and 65535"},
"a bad TLS mode": {"/upstream", url.Values{"host": {"10.0.0.8"}, "port": {"25"}, "tls_mode": {"sometimes"}}, "invalid TLS mode"},
"another domain": {"/recipients", url.Values{"recipient_mode": {"list"}, "addresses": {"abuse@other.com"}}, "does not belong to domain lists.example.com"},
"an empty list": {"/recipients", url.Values{"recipient_mode": {"list"}}, "requires at least one address"},
"an unknown mode": {"/recipients", url.Values{"recipient_mode": {"some"}}, "invalid recipient mode"},
} {
rec := inboundCall(h, http.MethodPost, "/inbound/domains/"+id+tc.target, tc.form, globalPrincipal)
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), tc.want) {
t.Errorf("%s = %d, does not say %q:\n%s", name, rec.Code, tc.want, rec.Body.String())
}
}
}
func TestRecalculationDoesNotEchoInternalErrors(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.db")
s := newAppStandAt(t, path)
a, _, err := s.h.apps.CreateWithSettings(s.d.ID, "prod-server", app.Settings{Mode: store.AddressModeWildcard}, nil)
if err != nil {
t.Fatal(err)
}
for scope, ref := range map[string]int64{store.RateLimitScopeDomain: s.d.ID, store.RateLimitScopeApp: a.ID} {
rl := store.RateLimit{Scope: scope, RefID: ref, Mode: store.RateLimitModeAuto, AutoMultiplier: store.DefaultAutoMultiplier, WindowSeconds: 3600}
if err := s.h.store.SetRateLimit(rl); err != nil {
t.Fatal(err)
}
}
breakDatabase(t, path,
`CREATE TRIGGER boom_insert BEFORE INSERT ON rate_limits BEGIN SELECT RAISE(ABORT, '`+leak+`'); END`,
`CREATE TRIGGER boom_update BEFORE UPDATE ON rate_limits BEGIN SELECT RAISE(ABORT, '`+leak+`'); END`)
rec := postFormAs(s.h.HandleDomainRateLimitRecalc, globalPrincipal, "/outbound/domains/1/settings/ratelimit/recalc", s.paths, url.Values{})
refusal(t, "domain recalculation when the database fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
paths := map[string]string{"id": idStr(s.d.ID), "aid": idStr(a.ID)}
rec = postFormAs(s.h.HandleAppRateLimitRecalc, globalPrincipal, "/outbound/domains/1/applications/1/ratelimit/recalc", paths, url.Values{})
refusal(t, "application recalculation when the database fails", rec.Body.String(), http.StatusBadRequest, rec.Code)
}