Skip to content

Should auth.scanUser tolerate NULL email/password_hash, or should the columns be NOT NULL? #640

Closed

Description

@jasonthagerty

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:

  1. It is a production change driven by a fixture artifact. Every production path writes both columns — UserRepository.Create (repository.go:153-157) is the only INSERT INTO users outside tests, and it unconditionally binds NormalizeEmail(input.Email) and a bcrypt hash. The plugin auth provider synthesizes <username>@plugin-<installation>.local when 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.
  2. COALESCE(email, '') fixes one column and stops there. password_hash fails identically one column later, then role, then enabled.
  3. Doing it properly means choosing a value for enabled. Coalescing a NULL enabled to true silently 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-Create data is still out there, harden the read path deliberately: scan through nullable targets, map NULL to the zero value for email/password_hash/role, and treat a NULL enabled as disabled rather than enabled. An empty password_hash already fails closed — bcrypt.CompareHashAndPassword rejects it — so that part is safe.
  • If not, add the NOT NULL constraints 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

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 users in .go and .sql and found Create is 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-Create rows; that is the fact I am missing and the reason this is a question rather than a patch.

Activity

  1. added
    needs-review
    on Sep 20, 2026
  2. blurbery commented on Nov 19, 2026

    @blurbery
    Contributor

    This was settled in code, so closing it.

    • fix(auth): read accounts that have no email, username, or password hash #3392 (merged 2 November) reads email, username and password_hash as nullable everywhere scanUser and scanUsers read a user (userLogin in internal/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 empties ListAll.
    • Migration 20260912231132_users_role_enabled_not_null.sql made role and enabled NOT NULL, which covers the NULL role/enabled concern.

    Neither change linked this issue, which is why it stayed open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions