Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
355f21b
Passwords also refuse a distinctive word of an institute's name, not …
omerFixesBugs Nov 16, 2026
7016eff
Sprint 1 P1-J plan (docs/plans/sprint-1-p1j-password-hash.md): the ap…
omerFixesBugs Nov 16, 2026
25da8a3
Sprint 1 P2 plan (docs/plans/sprint-1-p2-files.md): institute profile…
omerFixesBugs Nov 16, 2026
995e61e
Test users get phone numbers no other test user of the same database …
omerFixesBugs Nov 16, 2026
356c448
The app login can no longer read users.password_hash (P1-J): SELECT o…
omerFixesBugs Nov 16, 2026
3ca69eb
Every Prisma client leaves users.password_hash out of its reads (P1-J…
omerFixesBugs Nov 16, 2026
30e85f6
Files and the institute profile in the database (P2 task 1): stored_f…
omerFixesBugs Nov 16, 2026
687a83b
CORS_ORIGINS is normalised in env.ts as browsers send Origin (lower-c…
omerFixesBugs Nov 16, 2026
ff8981c
Shared file caps, types and the institute profile schema (P2 task 2):…
omerFixesBugs Nov 16, 2026
917d5eb
The start-up login check also refuses a login with REPLICATION (a rep…
omerFixesBugs Nov 16, 2026
bc6644b
Password login without the hash (P1-J): the API hashes the typed pass…
omerFixesBugs Nov 16, 2026
b0e6315
GET /me/password reads hasPassword from auth_my_password_is_set() (P1…
omerFixesBugs Nov 16, 2026
47fb357
Docs for P1-J: database.md (password_hash not readable by the app, a …
omerFixesBugs Nov 16, 2026
20b29e9
Redis is pinned to one exact version, redis:7.4.11-alpine (the versio…
omerFixesBugs Nov 16, 2026
81bfc26
Docs for the follow-ups: decisions-pending gets the section "Small fo…
omerFixesBugs Nov 16, 2026
9cc9d54
Image processing for uploads (P2 task 3): sharp 0.35.5 (already in th…
omerFixesBugs Nov 16, 2026
b0e010b
FileStore and the per-institute cap (P2 task 4): PostgresFileStore is…
omerFixesBugs Nov 16, 2026
caec9f7
Photo and profile routes (P2 task 5): GET/PUT/DELETE /students/:id/ph…
omerFixesBugs Nov 16, 2026
57cf79f
Web (P2 task 6): Settings → Institute profile (names, EIIN, contact, …
omerFixesBugs Nov 16, 2026
4e4cbb2
Docs for P2 (task 7): docs/files.md, database.md (the three tables an…
omerFixesBugs Nov 16, 2026
724ffb0
The photo dialog's preview URL is made when a file is chosen, not in …
omerFixesBugs Nov 16, 2026
110857d
files.spec: the profile fields by id ("নাম (বাংলা)" is also part of t…
omerFixesBugs Nov 16, 2026
8c3a4f9
After the review (P1-J7): a dedicated login for the password login. j…
omerFixesBugs Nov 16, 2026
72b714b
The password login looks the hash up on its own database login (Login…
omerFixesBugs Nov 16, 2026
06f3236
Docs after the P1-J review: what an injection in an ordinary request …
omerFixesBugs Nov 16, 2026
82f6600
Test phones never wrap and their lock survives races (review of PR #1…
omerFixesBugs Nov 16, 2026
27403c3
The institute-name rule compares passwords and names like the phone r…
omerFixesBugs Nov 16, 2026
7aa31c5
An origin setting with a login, a path, a query or a fragment is no o…
omerFixesBugs Nov 16, 2026
fd6430e
Docs after the review of PR #17: P1-I's title line says it is built h…
omerFixesBugs Nov 16, 2026
9dbee45
P1-J migration after the second review: it first checks that jantrik_…
omerFixesBugs Nov 16, 2026
a980a60
The password login's database login must reach the same database, por…
omerFixesBugs Nov 16, 2026
ff673b8
Docs after the second P1-J review: the server upgrade creates the rol…
omerFixesBugs Nov 16, 2026
13deed2
A file reference is tied to its kind (review R6): UNIQUE (tenant_id, …
omerFixesBugs Nov 16, 2026
cb86722
Uploads hold no database connection while they arrive (review R1): th…
omerFixesBugs Nov 16, 2026
459bcf7
Web after the review: a chosen image is shrunk in the browser to 1600…
omerFixesBugs Nov 16, 2026
944e840
Docs after the review: auth.md (the request pipeline and @OwnTransact…
omerFixesBugs Nov 16, 2026
282c8b6
Review round 2, API: uploads in flight bounded before multer (16 per …
omerFixesBugs Nov 16, 2026
cfd4506
Review round 2, web: the browser asks for the EXIF orientation explic…
omerFixesBugs Nov 16, 2026
73eb83b
Docs for review round 2: auth.md (the upload routes' re-checks, Uploa…
omerFixesBugs Nov 16, 2026
e622e52
The interceptor opens its transaction through an unchecked entry (aro…
omerFixesBugs Nov 16, 2026
3d2c5d5
Review round 3: no upload slot leaks when the client left before Uplo…
omerFixesBugs Nov 16, 2026
de19108
The two new origin tests' development settings also name DATABASE_LOG…
omerFixesBugs Nov 16, 2026
aa0105b
Merge P1-J (#16) into the small follow-ups (#17): the ready resolutio…
omerFixesBugs Nov 16, 2026
b1efceb
Merge remote-tracking branch 'origin/feature/sprint-1-p1j-password-ha…
omerFixesBugs Nov 16, 2026
b5d3697
Merge remote-tracking branch 'origin/feature/sprint-1-p2-files' into …
omerFixesBugs Nov 16, 2026
c857102
Merge remote-tracking branch 'origin/feature/small-followups-after-p1…
omerFixesBugs Nov 16, 2026
9309fa8
Work in progress: a staff guide draft and draft legal texts (terms of…
omerFixesBugs Nov 19, 2026
ed5d56c
Docs only: a plain staff guide draft (signing in, setup, students, gu…
omerFixesBugs Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Docs after the second P1-J review: the server upgrade creates the rol…
…e after the checkout and before migrate, with the recovery (migrate resolve --rolled-back); an injection can still take over an account through set_config (auth.md, database.md; P1-J8, not new, recorded for the owner); the email lookup's missing case-insensitive index (P1-J9); the worker also holds the login URL

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
omerFixesBugs and claude committed Nov 16, 2026
commit ff673b8f2902493e7b0a7b0b1670a675e4e5194c
18 changes: 13 additions & 5 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,13 @@ alone has its own database login:
verifies with argon2 as before.
- `jantrik_login_app` may run that one function and nothing else (no table, no other function),
and `jantrik_app` may not run it. Only `AuthService` holds that client
(`login-isolation.spec.ts`). Its pool is small (4 connections).
(`login-isolation.spec.ts`). Its pool is small (4 connections). The queue worker builds the
same modules, so it gets `DATABASE_LOGIN_URL` and runs the start-up checks too, but never
looks a hash up; the receiver never gets it.
- At start-up the API refuses a `DATABASE_LOGIN_URL` that is a superuser, has BYPASSRLS or can
become such a role (fail closed, like the other logins), and one mixed up with `DATABASE_URL`.
become such a role (fail closed, like the other logins), one mixed up with `DATABASE_URL`, and
one that reaches another database or server than `DATABASE_URL` (database, port and the
server's start time compared).
- A stored hash above 64 MiB, 10 passes or 4 lanes, or one argon2 cannot read, counts as no
password (the dummy check runs): a hash someone wrote into their own row cannot make each login
attempt arbitrarily expensive.
Expand All @@ -247,12 +251,16 @@ alone has its own database login:

- It **cannot** read any password hash or salt: its own, a co-member's or a stranger's.
- It **cannot** run the password lookup, or anything as `jantrik_login_app`: that login's
password is only in the API's environment.
password is only in the API's (and the worker's) environment, never in the database.
- It **can** learn whether a given person has a password at all, yes or no:
`auth_my_password_is_set()` (for `GET /me/password`) answers for `app.user_id`, which any SQL can
set with `set_config`. It reveals nothing about the password itself.
- It can still set its own row's hash (the app login keeps UPDATE on the column, under RLS: its
own row only), as before.
- It **can still take over an account**, as before P1-J: the request's identity is
`app.user_id`, which any SQL can change with `set_config`. Set to someone else's id, the RLS
policies let it write that person's `password_hash` (then sign in with a password it chose) or
create a session for them. P1-J protects the password itself (people reuse passwords
elsewhere), not the account against an injection. Recorded for the owner: P1-J8 in
decisions-pending.

The platform admins' login is separate and unchanged ([platform.md](platform.md)).

Expand Down
7 changes: 5 additions & 2 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ references such as `sections.shift_id`. A row can only point at rows of its own
tenant A must not edit them.
- **Only some columns (sprint 1 P1-B):** `jantrik_app` may update `locale`, `color_scheme`,
`name_bn`, `password_hash`, `password_changed_at` and `updated_at`, and nothing else. The
login identifiers (`phone`, `email`) and the global `name` are out of the app's reach, so no
app code (or injection) can change how a person signs in. Tests that need another column set
login identifiers (`phone`, `email`) and the global `name` are out of the app's reach: no app
code (or injection) can change the phone or email a person signs in with. The password hash is
not: an injection can set `app.user_id` to anyone with `set_config` and then write their
`password_hash` (or create a session for them), which RLS allows for "your own row"
(P1-J8 in decisions-pending). Tests that need another column set
it through the owner role, as that user.
- **`password_changed_at`:** when the person last set their password themselves (null: never,
or only by the seed). Written only by `PasswordSettingsService`, with `password_hash`
Expand Down
8 changes: 6 additions & 2 deletions docs/decisions-pending.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ P1-I. **Open, for the owner: catch part of an institute's name too?** The rule r

- To change: `passwordProblems` in `packages/shared/src/password.ts` (the server and the form share it).

P1-J. **Built (day run 2026-11-16), for the owner's review with its PR: no ordinary request can read a password hash.** Plan: [plans/sprint-1-p1j-password-hash.md](plans/sprint-1-p1j-password-hash.md), written and built while the owner was away, as asked, then changed after the database-security review (P1-J7). `jantrik_app` lost SELECT on `users.password_hash` (granted column by column; UPDATE kept), its login lookup returns no password data, and the password login looks the hash up on its own database login (`jantrik_login_app`, `DATABASE_LOGIN_URL`) through one function only that login may run. Every Prisma client omits the hash. **Setup on your machine:** create the role once (docs/database.md → Database roles, the `CREATE ROLE jantrik_login_app …` line) and add `DATABASE_LOGIN_URL` to `.env` (from `.env.example`). Decisions taken alone, each the safest option (P1-J1 to P1-J7):
P1-J. **Built (day run 2026-11-16), for the owner's review with its PR: no ordinary request can read a password hash.** Plan: [plans/sprint-1-p1j-password-hash.md](plans/sprint-1-p1j-password-hash.md), written and built while the owner was away, as asked, then changed after the database-security review (P1-J7). `jantrik_app` lost SELECT on `users.password_hash` (granted column by column; UPDATE kept), its login lookup returns no password data, and the password login looks the hash up on its own database login (`jantrik_login_app`, `DATABASE_LOGIN_URL`) through one function only that login may run. Every Prisma client omits the hash. **Setup on your machine, in this order:** create the role once (docs/database.md → Database roles, the `CREATE ROLE jantrik_login_app …` line), add `DATABASE_LOGIN_URL` to `.env` (from `.env.example`), then `pnpm db:migrate`. Decisions taken alone, each the safest option (P1-J1 to P1-J7):

P1-J1. **The app's login lookup returned any user's hash, so it changed too.** `auth_find_user_for_login`, which `jantrik_app` may run, returned `password_hash` for any phone or email, whatever the institute: hiding the column alone would have closed nothing. It now returns id, name and locale only (the OTP login never needed more).

Expand All @@ -667,6 +667,10 @@ P1-J5. **(Dropped after the review.)** The first version accepted only argon2id

P1-J6. **Not changed: the platform admins' login** (`platform_find_admin_for_login` returns `platform_admins` hashes and TOTP secrets to `jantrik_platform_app`). It is a separate login with no tenant traffic; the same change can follow if you want it.

P1-J7. **Built after the review; you may drop these commits:** the first version kept the hash in the database and compared a digest there (`auth_my_password_matches`). The review showed that an injection could read any user's salt, set `app.user_id` to them and check 200,000 offline-computed guesses in one 6.4-second statement: close to reading the hash. Now a dedicated login, `jantrik_login_app`, is the only one that may run the hash lookup (`auth_find_user_for_password_login`); `jantrik_app` has neither the lookup nor any comparison function, and the API verifies with argon2 as before. It follows the platform and receiver logins: its own env URL (required, refused in production as the owner or superuser, never given to the receiver), its own small pool, the fail-closed privilege check and a mixed-up-URLs check at start-up, and the role in `01-roles.sh`, `create-roles.sh`, the production compose, CI, the restore check and the docs. **Upgrade gap:** between `migrate` and the restart, the old API fails whole-row reads of `users` (the refresh, `/auth/me`) and both logins; a failed refresh is retried and nobody is signed out (docs/deploy.md → Upgrades).
P1-J7. **Built after the review; you may drop these commits:** the first version kept the hash in the database and compared a digest there (`auth_my_password_matches`). The review showed that an injection could read any user's salt, set `app.user_id` to them and check 200,000 offline-computed guesses in one 6.4-second statement: close to reading the hash. Now a dedicated login, `jantrik_login_app`, is the only one that may run the hash lookup (`auth_find_user_for_password_login`); `jantrik_app` has neither the lookup nor any comparison function, and the API verifies with argon2 as before. It follows the platform and receiver logins: its own env URL (required, refused in production as the owner or superuser, never given to the receiver), its own small pool, the fail-closed privilege check and a mixed-up-URLs check at start-up, and the role in `01-roles.sh`, `create-roles.sh`, the production compose, CI, the restore check and the docs. **Upgrade:** the migration first checks that `jantrik_login_app` exists and refuses to run otherwise (Prisma does not run a migration in one transaction, so a failure half-way would leave the first statements applied); every statement can run again after `prisma migrate resolve --rolled-back`. On a server the role is created after the checkout and before `migrate` (docs/deploy.md → Upgrades). Between `migrate` and the restart, the old API fails whole-row reads of `users` (the refresh, `/auth/me`) and both logins; a failed refresh is retried and nobody is signed out.

P1-J8. **Follow-up for the owner (not built, and not new): an SQL injection can still take over an account.** The request's identity is `app.user_id` (and `app.tenant_id`), set with `set_config`, which any SQL in the same transaction can change. Set to a victim's id, RLS lets it update the victim's `password_hash` (then sign in with a password it chose) or insert a session for them. P1-J stops reading hashes (it protects the password, which people reuse elsewhere), not this. Options, each a larger change: a verified context (e.g. the API signs the context and a definer function checks the signature before anything trusts it), or writes to `users.password_hash` and `sessions` only through definer functions that check a server-side secret or the session itself. Main has the same today.

P1-J9. **Follow-up for the owner (not built): email login lookups are case-insensitive but not indexed.** Both login lookups compare `lower(u.email) = lower(p_email)`; `users_email_key` is case-sensitive, so that comparison scans the table (an unknown email takes longer as the table grows) and two emails differing only in case can coexist. The fix: check for such duplicates first, then a unique index on `lower(email)` (and drop the old one). Not in this PR, because existing data could hold duplicates.

P1-K. **Fixed from the review** (each with a test that failed first): a code is single use even when two requests race with it (login codes too); the phone rule ignores every punctuation mark and symbol between the digits; the other sessions are revoked in one statement; the common-password list is read at start-up; password login accepts up to 512 code points (a 128-character password typed decomposed); the wrong-code message asks to check the code before sending a new one. Kept as they are: the staff check after a correct password costs one extra query (it only confirms a password the caller already has); the login page's hint sits under the password tab, which guardians do not use.
42 changes: 30 additions & 12 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,36 @@ deploy/compose.sh ps
`ALTER ROLE jantrik_ingest_app SET statement_timeout = '5s';`
3. upgrade as above (`up -d` restarts Redis with the new user), then run the Redis check
(§ 10a).
- **A server set up before sprint 1's P1-J** (the password login's own database login):
1. add `DB_LOGIN_PASSWORD` (`openssl rand -hex 24`, different from every other password) to
the settings file (`DATABASE_LOGIN_URL` needs nothing: the compose file builds it);
2. create the role **before** the migration (the migration grants to it): recreate the
Postgres container so it gets the new password (`deploy/compose.sh up -d postgres`; the data
stays), then run the roles script again, which creates only what is missing
(`deploy/compose.sh exec -u postgres postgres sh /jantrik/postgres/create-roles.sh`);
3. upgrade as above. **Between `migrate` and `up -d`**, the old API still running fails: every
whole-row read of `users` (the session refresh, `/auth/me`) and both logins (its login
lookup asks for a column the function no longer returns), until it restarts. Nothing is
lost: a refresh that fails is retried by the browser, and nobody is signed out. Keep the
gap short (run the two commands together).
- **A server set up before sprint 1's P1-J** (the password login's own database login,
`jantrik_login_app`). Its migration refuses to run until the role exists, and the role script
that creates it is the **new** one, so the order matters:

```bash
cd /srv/jantrik
deploy/compose.sh run --rm backup # a backup first
git fetch && git checkout <new tag or commit> # the new create-roles.sh and compose file
nano deploy/.env.prod # add DB_LOGIN_PASSWORD (openssl rand -hex 24,
# unlike every other password)
deploy/compose.sh up -d postgres # Postgres gets the new password; data stays
deploy/compose.sh exec -u postgres postgres sh /jantrik/postgres/create-roles.sh
# creates only what is missing
deploy/compose.sh build
deploy/compose.sh run --rm migrate
deploy/compose.sh up -d
```

- `DATABASE_LOGIN_URL` needs nothing: the compose file builds it from `DB_LOGIN_PASSWORD`.
- **If `migrate` stopped with "create the role jantrik_login_app first"**, nothing was changed:
create the role as above, mark the migration rolled back, and run it again:
`deploy/compose.sh run --rm migrate node_modules/.bin/prisma migrate resolve --rolled-back 20261116000000_users_password_hash_hidden`,
then `deploy/compose.sh run --rm migrate`. (On a development machine:
`pnpm --filter @jantrik/db exec prisma migrate resolve --rolled-back 20261116000000_users_password_hash_hidden`,
then `pnpm db:deploy`.) Every statement of that migration can run again, so the same
recovery works after a failure half-way.
- **Between `migrate` and `up -d`** the old API still running fails every whole-row read of
`users` (the session refresh, `/auth/me`) and both logins (its lookup asks for a column the
function no longer returns), until it restarts. Nothing is lost: a refresh that fails is
retried by the browser, and nobody is signed out. Run the last two commands together.

## 12. Backups

Expand Down
6 changes: 6 additions & 0 deletions docs/plans/sprint-1-p1j-password-hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ instead, and what this plan's sections below describe only where they say so:
role once (database.md → Database roles) and add `DATABASE_LOGIN_URL` to `.env`. On an existing
server: deploy.md → Upgrades (create the role before the migration; the old API fails some reads
between migrate and restart).
- **Second review round:** the migration checks for the role before it changes anything and every
statement can run again (a DB test runs it twice); the role step on a server comes after the
checkout; a view guard; the login client must reach the same database and server as the app
client; stored hashes bounded in size; `create-roles.sh` run twice in CI; and two follow-ups
recorded for the owner: an injection can still take over an account through `set_config`
(P1-J8, not new), and the email lookup has no case-insensitive index (P1-J9).

**Goal:** an SQL injection in any institute request (which runs as `jantrik_app`) can no longer
read a staff member's argon2 password hash, neither of co-members nor of anyone else. Everything
Expand Down