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

+14
View File
@@ -47,6 +47,20 @@ as nvidia-smi does, by PCI bus, and CUDA's own order is fastest first — on a
node with two kinds of GPU, a shard could get a different card from the one it
was given.
### Signing up with an address that has an account
On PostgreSQL, signing up with an address already registered showed
"something went wrong; try again": the store recognised a duplicate by the
word "UNIQUE", which is SQLite's, and PostgreSQL says it in lower case under
code 23505. It now knows both, and the address is answered as on SQLite —
with the same page as a new sign-up, so the page cannot be used to find out
who has an account. That page now also says what to do if the address had
one: sign in, or ask an operator to reset the password.
An error the page does not explain says the account was not created and
gives a reference (`err-…`), logged beside the error, for an operator to find
it; the database's own words stay in the log.
### An adapter no fabric uses does not keep a node out
`llmgw doctor` failed a node for a RoCE port with no RoCE v2 GID entry, or for
+30
View File
@@ -0,0 +1,30 @@
package store
import (
"errors"
"fmt"
"testing"
"github.com/jackc/pgx/v5/pgconn"
)
func TestADuplicateIsRecognisedOnEitherDatabase(t *testing.T) {
for _, c := range []struct {
name string
err error
want bool
}{
{"sqlite", errors.New("constraint failed: UNIQUE constraint failed: users.email (2067)"), true},
// What pgx returns, wrapped as database/sql passes it on: "unique"
// in lower case, which a check for "UNIQUE" missed.
{"postgres", fmt.Errorf("exec: %w", &pgconn.PgError{Code: "23505",
Message: `duplicate key value violates unique constraint "users_email_key"`}), true},
{"postgres, as text", errors.New(`ERROR: duplicate key value violates unique constraint "users_email_key" (SQLSTATE 23505)`), true},
{"another postgres error", &pgconn.PgError{Code: "23502", Message: "null value in column"}, false},
{"nothing", nil, false},
} {
if got := isUniqueViolation(c.err); got != c.want {
t.Errorf("%s: %v, want %v", c.name, got, c.want)
}
}
}
+20 -1
View File
@@ -6,6 +6,8 @@ import (
"errors"
"strings"
"time"
"github.com/jackc/pgx/v5/pgconn"
)
// UserState is where an account is in its life.
@@ -66,12 +68,29 @@ func (s *DB) CreateUser(ctx context.Context, u User) error {
VALUES (?, ?, ?, ?, ?, ?, ?)`,
u.ID, u.Email, u.Name, u.PasswordHash, string(u.State),
nullString(u.ProjectID), millis(u.CreatedAt))
if err != nil && strings.Contains(err.Error(), "UNIQUE") {
if isUniqueViolation(err) {
return ErrEmailTaken
}
return err
}
// isUniqueViolation reports whether err is a row refused for a value another
// row already has. Each database says it its own way: SQLite in words
// ("UNIQUE constraint failed"), PostgreSQL by code, 23505, in a message with
// "unique" in lower case — which a check for the upper-case word missed, so a
// second sign-up with an address on PostgreSQL was "something went wrong".
func isUniqueViolation(err error) bool {
if err == nil {
return false
}
var pe *pgconn.PgError
if errors.As(err, &pe) {
return pe.Code == "23505"
}
m := err.Error()
return strings.Contains(m, "UNIQUE constraint failed") || strings.Contains(m, "SQLSTATE 23505")
}
// UserByEmail finds an account by its address.
func (s *DB) UserByEmail(ctx context.Context, email string) (User, error) {
return s.scanUser(s.db.QueryRowContext(ctx,
+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">