Repository navigation
Description
This is a question, not a bug report. I could not reach it from any production path, so filing it under the bug template would misrepresent it. It is a decision about how much the user-read path should tolerate, and it deserves an answer before someone else trips over it the way I did.
The mismatch
users.email and users.password_hash are nullable, and have been since 001_schema.sql:
migrations/sql/001_schema.sql:1069: email text,No migration in the ~300 since has made either NOT NULL. But auth.scanUser reads both into plain string (internal/auth/repository.go:58-90), so a row with either column NULL cannot be read at all:
scanning user: can't scan into dest[1] (col: email): cannot scan NULL into *string
That is not hypothetical output — it is what go test ./internal/auth prints today against a migrated database on main, 25 runs out of 25. The row in question comes from a test fixture, and PR #633 fixes the fixture. The question this issue asks is separate: should the repository have been able to read that row in the first place?
Why I did not just fix it in #633
I tried, and reverted it. Three reasons, in case they are useful:
- It is a production change driven by a fixture artifact. Every production path writes both columns —
UserRepository.Create(repository.go:153-157) is the onlyINSERT INTO usersoutside tests, and it unconditionally bindsNormalizeEmail(input.Email)and a bcrypt hash. The plugin auth provider synthesizes<username>@plugin-<installation>.localwhen the upstream sends no address (plugin_provider.go:248-251), so even SSO cannot produce a NULL. So the failing row was not one production creates. COALESCE(email, '')fixes one column and stops there.password_hashfails identically one column later, thenrole, thenenabled.- Doing it properly means choosing a value for
enabled. Coalescing a NULLenabledtotruesilently grants access to a row nobody deliberately enabled. That is a security-relevant default and not mine to pick.
The actual question
Can a row with NULL email or password_hash exist in a real deployment, and if so what should happen?
If yes — legacy rows predating the current code, an operator's manual SQL, a future writer that skips Create — then today a single such row is more damaging than it looks. It is not just that one user cannot log in: ListAll (repository.go:409) scans every user through the same path, so one bad row empties the entire admin user list.
If no, then the schema is lying about its own invariants and the honest fix is the other direction: NOT NULL constraints with a backfill, so the database enforces what the code already assumes.
There is one wrinkle pushing against the NOT NULL direction. users_email_key is a plain UNIQUE constraint, so NULL is the only value that lets more than one account have no email — '' can only ever be stored once. If email-less accounts are ever legitimate, NULL is the representation the schema already implies.
What I would suggest
Not a strong recommendation, because it depends on facts about real deployments that I do not have:
- If any pre-
Createdata is still out there, harden the read path deliberately: scan through nullable targets, map NULL to the zero value foremail/password_hash/role, and treat a NULLenabledas disabled rather than enabled. An emptypassword_hashalready fails closed —bcrypt.CompareHashAndPasswordrejects it — so that part is safe. - If not, add the
NOT NULLconstraints so the schema states the invariant, and this stops being a question.
Either way it is worth deciding rather than leaving the read path one legacy row away from an empty admin user list.
Happy to implement whichever you prefer.
Notes
models.User.Emailand.PasswordHasharestring, and both are serialized in/api/v1responses, so making them*stringis out under the additive-only rule ("never ... change a field's type"). Any hardening has to keep the wire shape.- Worth knowing: the repo's own test corpus routinely creates these rows —
INSERT INTO users (username, role)with no email appears in at least 8 files acrossdownloads,metadata,access,userstore/pgstoreandplayback/planstore. They pass because they read through their own queries rather thanscanUser. Onlyinternal/authround-trips through it, which is why only that package noticed. - Found while working [bug] CI runs no PostgreSQL, so 171 DB-backed tests silently skip — 7 are broken on main and 2 packages are flaky #599. Related: [bug] internal/auth and internal/access fight over the access_groups.is_default singleton — 25/25 concurrent runs fail #639, the other follow-up from that work.
AI Disclosure
- Tool(s): Claude Code
- Model(s): claude-opus-5
- Involvement: AI-assisted
- Adversarial review: My first take was that this was a bug and that the repository should coalesce NULLs. Checking the write paths refuted that — I grepped every
INSERT INTO usersin.goand.sqland foundCreateis the only production writer, and it always binds both columns, so no current path produces the row. That turned a "fix" into a question, and the COALESCE change was reverted from test: repair and isolate the DB-backed tests CI never runs #633. I have not verified whether any real deployment carries pre-Createrows; that is the fact I am missing and the reason this is a question rather than a patch.
Activity
blurbery commentedon Nov 19, 2026
This was settled in code, so closing it.
email,usernameandpassword_hashas nullable everywherescanUserandscanUsersread a user (userLoginininternal/auth/repository.go). An account can legitimately have only an email or only a username, so the columns stay nullable, and a NULL password hash reads as empty, which no password matches. One odd row no longer emptiesListAll.20260912231132_users_role_enabled_not_null.sqlmaderoleandenabledNOT NULL, which covers the NULLrole/enabledconcern.Neither change linked this issue, which is why it stayed open.