Report a rejected superuser at sign-in as a server error
CI / go (Media) (push) Successful in 17m6s
CI / go (Server) (push) Failing after 14m0s
CI / go (Web-App) (push) Successful in 10m3s
CI / frontend (push) Failing after 9m40s
CI / desktop (ubuntu-latest) (push) Successful in 52m25s
CI / mobile (push) Failing after 5m0s
CI / docker (push) Failing after 14m55s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s

The two-step sign-in check reads the user with docwell-server's own
PocketBase superuser. When those credentials were wrong, PocketBase's
401/403 was mapped to "unauthorized", so a correct password showed
"Your session has expired" and nothing was logged. totpRecord and
ResetTOTP now pass superuser failures through (logged, answered 500);
a missing user on reset is "not found".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-11-26 19:21:22 +01:00
1 parent f7a2ded680
commit e50fc1fe2d
2 files changed
+27 -2

No files matched your search

+17
View File
@@ -25,6 +25,9 @@ type fakePB struct {
passwords map[string]string
verifyCalls atomic.Int32
mfaFor string // email that requires MFA
// rejectSuperuser makes superuser reads of a user fail as PocketBase does
// when docwell-server's own credentials are wrong.
rejectSuperuser bool
// resetSubject is the users collection's reset subject now; sentWith
// collects it at each password-reset request.
resetSubject string
@@ -91,6 +94,10 @@ func newFakePB(t *testing.T) (*fakePB, *pocketbase.Client) {
mux.HandleFunc("GET /api/collections/users/records/{id}", func(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
if f.rejectSuperuser {
reply(w, 403, map[string]any{"status": 403, "message": "Only superusers can perform this action."})
return
}
if u := byID(r.PathValue("id")); u != nil {
reply(w, 200, u)
return
@@ -286,6 +293,16 @@ func TestLogin(t *testing.T) {
if _, err := s.Login(ctx, "ann@docwell.test", "password123", "mfa123"); err != nil {
t.Errorf("mfa second step err = %v", err)
}
f.mfaFor = ""
// Right password, but the server's own PocketBase superuser is rejected:
// that is a server fault, not the person's expired session.
f.rejectSuperuser = true
_, err := s.Login(ctx, "ann@docwell.test", "password123", "")
var pbErr *pocketbase.Error
if err == nil || errors.Is(err, ErrUnauthorized) || !errors.As(err, &pbErr) || pbErr.Status != 403 {
t.Errorf("superuser rejected err = %v, want the PocketBase 403 passed through", err)
}
}
func TestVerifyCachesAndRejectsDisabled(t *testing.T) {
+10 -2
View File
@@ -20,6 +20,7 @@ import (
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"docwell/server/internal/apperr"
"docwell/server/internal/store/pocketbase"
)
@@ -75,8 +76,12 @@ type totpRecord struct {
}
func (s *Service) totpRecord(ctx context.Context, userID string) (totpRecord, error) {
// This read runs as the server's superuser, not as the person signing in:
// a 401/403 here means docwell-server's own PocketBase credentials are
// wrong. Passing it through (not as ErrUnauthorized) gets it logged and
// answered as a server error instead of "your session has expired".
rec, err := pocketbase.Get[totpRecord](ctx, s.pb, "users", userID)
return rec, tokenError(err)
return rec, err
}
func randomToken(n int) string {
@@ -300,5 +305,8 @@ func (s *Service) ResetTOTP(ctx context.Context, userID string) error {
"totp_secret": "", "totp_pending": "", "totp_enabled": false, "recovery_codes": []string{},
})
s.Forget(userID)
return tokenError(err)
if pocketbase.IsNotFound(err) {
return apperr.NotFound("user not found")
}
return err // a superuser call, like totpRecord
}