Recognise a duplicate sign-up on PostgreSQL, and say what to do

A taken address on PostgreSQL fell through to a generic error, because
the store looked for SQLite's "UNIQUE". It checks code 23505 too. The
done page tells someone whose address had an account to sign in or ask
for a reset, without saying which case it was; other failures give a
logged reference.

Refs #2 (item 6).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-01 12:15:37 +02:00
1 parent 2ef4220643
commit 8957db30a2
6 files changed
+138 -3

No files matched your search

+15 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/base32"
"encoding/hex"
"errors"
"log/slog"
"net/http"
@@ -169,8 +170,12 @@ func (p *AccountPages) signup(w http.ResponseWriter, r *http.Request) {
case errors.Is(err, account.ErrWeakPassword), errors.Is(err, account.ErrInvalidEmail):
d.Error = err.Error()
default:
p.log.Error("signup failed", "err", err)
d.Error = "something went wrong; try again"
// Nothing of the database's words on the page; a reference that finds
// them in the log for whoever runs the gateway.
ref := errorRef()
p.log.Error("signup failed", "ref", ref, "err", err)
d.Error = "something went wrong on our side, and your account was not created. " +
"Try again later; if it keeps happening, give an operator this reference: " + ref
}
render(w, "signup", d)
}
@@ -638,3 +643,11 @@ func (p *AccountPages) renderWithCSRF(w http.ResponseWriter, r *http.Request,
// SetLlamaCppVersion tells the pages which llama.cpp build the nodes run.
func (p *AccountPages) SetLlamaCppVersion(v string) { p.llamaVersion = v }
// errorRef is a short reference for an error a page does not explain, logged
// beside the error itself.
func errorRef() string {
var b [4]byte
_, _ = rand.Read(b[:])
return "err-" + hex.EncodeToString(b[:])
}
+54
View File
@@ -0,0 +1,54 @@
package web
import (
"context"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// signupAccounts is an account service whose sign-up answers with err.
type signupAccounts struct {
fakeAccounts
err error
}
func (s signupAccounts) SignUp(context.Context, string, string, string) error { return s.err }
func (signupAccounts) SignupOpen() bool { return true }
func signupBody(t *testing.T, err error) string {
t.Helper()
mux := http.NewServeMux()
NewAccountPages(signupAccounts{err: err}, nil, nil, "v0", slog.New(slog.DiscardHandler)).Register(mux)
form := url.Values{"email": {"a@b.c"}, "name": {"A"}, "password": {"long enough password"}}
req := httptest.NewRequest(http.MethodPost, "/signup", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
return rec.Body.String()
}
func TestASignupThatFailsSaysSoWithAReferenceAndNoDatabaseWords(t *testing.T) {
body := signupBody(t, errors.New(`duplicate key value violates unique constraint "users_email_key"`))
if strings.Contains(body, "users_email_key") || strings.Contains(body, "constraint") {
t.Errorf("the database's own words reached the page: %s", body)
}
if !strings.Contains(body, "was not created") || !strings.Contains(body, "reference: err-") {
t.Errorf("no plain failure with a reference to give an operator: %s", body)
}
}
func TestASignupSaysWhatToDoIfTheAddressHadAnAccount(t *testing.T) {
// The service answers a taken address as it does a new one, so nobody
// can find out from this page who has an account; what the page can do
// is say what to do in either case.
body := signupBody(t, nil)
if !strings.Contains(body, "already had an account") || !strings.Contains(body, `href="/login"`) ||
!strings.Contains(body, "reset it") {
t.Errorf("the done page leaves someone who already had an account stuck: %s", body)
}
}
+5
View File
@@ -11,6 +11,11 @@
<p class="note">You will not get an email from this gateway — it does not send any.
Tell whoever you asked for access that you have signed up.</p>
<p><a href="/login">Sign in</a> once you have been approved.</p>
{{/* The same page whether or not the address already had an account, so
it says what to do in either case without saying which this was. */}}
<p class="note">If this address already had an account, nothing new was made:
<a href="/login">sign in</a> with it, or, if you have forgotten its password, ask
an operator to reset it.</p>
{{else}}
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
<form method="post" autocomplete="on">