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
Test users get phone numbers no other test user of the same database …
…has, in this run or an earlier one (decisions-pending P0-8, the appearance flake). Random numbers repeated now and then (a full run makes thousands of users and the test database keeps them all), and a repeated phone joined an existing user, who then had two memberships (a new owner got 403 on /me/context). Now each database has one counter in a file under the OS temp folder, shared by every test process and run; a process takes 200 numbers at a time under a lock, and the counter never falls behind the clock, so a lost file does not restart at old numbers. The API tests draw from +88013 and the db package's from +88014, 50 000 000 upward: the old random phones used 017/018 and the seeds' fixed numbers sit lower

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
omerFixesBugs and claude committed Nov 16, 2026
commit 995e61e56e81c13c9da87aecf45a3105d63da4fc
9 changes: 8 additions & 1 deletion apps/api/test/support/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { SMS_PROVIDER, type MemorySmsProvider } from '../../src/sms/sms.provider
import { TenantAccessService } from '../../src/tenancy/tenant-access.service.js';
import { Clock } from '../../src/common/clock.js';
import { TestController } from './test.controller.js';
import { phoneSource } from './test-phones.js';

export interface Harness {
app: INestApplication;
Expand Down Expand Up @@ -76,8 +77,14 @@ export async function createHarness(opts: { clock?: Clock } = {}): Promise<Harne
};
}

let phones: (() => string) | undefined;
/**
* A valid Bangladeshi mobile number (E.164) that no other test user of this database has, in this
* run or an earlier one (`test-phones.ts`; the db package's tests draw from +88014, these from
* +88013).
*/
export const randomPhone = (): string =>
`+88018${String(Math.floor(Math.random() * 1e8)).padStart(8, '0')}`;
(phones ??= phoneSource('+88013', process.env['DATABASE_URL']!))();
/**
* A distinct client IP per call (sent as X-Forwarded-For) so per-IP limits don't collide. Fully
* random: test files run in parallel workers and share Redis, so a per-worker counter repeats.
Expand Down
104 changes: 104 additions & 0 deletions apps/api/test/support/test-phones.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

/**
* Phone numbers for test users that no other test user of the same database has (decisions-pending
* P0-8). Random numbers repeated now and then: a full run makes thousands of users, the test
* database keeps every run's users, and a repeated phone joins an existing user, which then has
* two memberships (a new owner got 403 on /me/context once).
*
* Each database has one counter, in a file under the OS temp folder, shared by every test process
* and every run against it. A process takes a block of numbers under a lock, then hands them out
* one by one. The counter never goes back behind the clock (`floorAt`), so a lost counter file
* does not restart at numbers earlier runs gave out. The numbers sit where nothing else is:
* `<prefix>` + 50 000 000–98 999 999 (the seeds' fixed numbers are below; the old random phones
* used other operators).
*
* Kept identical in packages/db/test/test-phones.ts and apps/api/test/support/test-phones.ts (a
* test of the db copy pins how it behaves).
*/

/** Numbers per block a process takes at once. */
const BLOCK = 200;
const LOW = 50_000_000;
const SPAN = 49_000_000;
/** The counter's floor grows 20 a second from this day, so it is always ahead of older runs. */
const EPOCH_MS = Date.UTC(2026, 10, 1);
const PER_SECOND = 20;

export const floorAt = (nowMs: number): number =>
Math.max(0, Math.floor(((nowMs - EPOCH_MS) / 1000) * PER_SECOND));

const pause = (ms: number): void => {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
};

/** Runs `fn` while holding a lock directory; a lock older than 10 s is left over and taken. */
function withLock<T>(lockDir: string, fn: () => T): T {
for (let tries = 0; ; tries++) {
try {
fs.mkdirSync(lockDir);
break;
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
try {
if (Date.now() - fs.statSync(lockDir).mtimeMs > 10_000) {
fs.rmSync(lockDir, { recursive: true, force: true });
continue;
}
} catch {
continue; // released meanwhile
}
if (tries > 3_000) throw new Error(`test-phones: lock ${lockDir} stays taken`);
pause(10);
}
}
try {
return fn();
} finally {
fs.rmSync(lockDir, { recursive: true, force: true });
}
}

/**
* A source of phones `<prefix>NNNNNNNN` (prefix e.g. `+88013`) unique among all users of the
* database `databaseUrl` names (server, port and name; never the login).
*/
export function phoneSource(
prefix: string,
databaseUrl: string,
options: { dir?: string; now?: () => number } = {},
): () => string {
const url = new URL(databaseUrl);
const key = `${url.hostname}_${url.port || '5432'}_${url.pathname.slice(1)}_${prefix}`.replace(
/[^\w.-]/g,
'_',
);
const dir = options.dir ?? path.join(os.tmpdir(), 'jantrik-test-phones');
const now = options.now ?? Date.now;
const counterFile = path.join(dir, `${key}.txt`);
let next = 0;
let end = 0;

const takeBlock = (): void => {
fs.mkdirSync(dir, { recursive: true });
withLock(`${counterFile}.lock`, () => {
let stored = 0;
try {
stored = Number.parseInt(fs.readFileSync(counterFile, 'utf8'), 10) || 0;
} catch {
// No counter yet: start from the clock.
}
next = Math.max(stored, floorAt(now()));
end = next + BLOCK;
fs.writeFileSync(counterFile, String(end));
});
};

return () => {
if (next >= end) takeBlock();
const n = next++;
return `${prefix}${LOW + (n % SPAN)}`;
};
}
9 changes: 6 additions & 3 deletions packages/db/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import {
type Tenant,
} from '../src/index.js';
import { APP_URL, INGEST_URL, OWNER_URL, PLATFORM_URL } from './env.js';
import { phoneSource } from './test-phones.js';

export const appClient = (): PrismaClient => createPrismaClient(APP_URL);

/** A random valid Bangladeshi mobile number (E.164). */
export const randomPhone = (): string =>
`+88017${String(Math.floor(Math.random() * 1e8)).padStart(8, '0')}`;
/**
* A valid Bangladeshi mobile number (E.164) that no other test user of this database has, in this
* run or an earlier one (`test-phones.ts`; the API tests draw from +88013, these from +88014).
*/
export const randomPhone: () => string = phoneSource('+88014', APP_URL);
export const ownerClient = (): PrismaClient => createPrismaClient(OWNER_URL);
/** The API platform module's role: the only one that may run the platform_* functions. */
export const platformClient = (): PrismaClient => createPrismaClient(PLATFORM_URL);
Expand Down
77 changes: 77 additions & 0 deletions packages/db/test/test-phones.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { BD_PHONE_E164 } from '@jantrik/shared';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { floorAt, phoneSource } from './test-phones.js';

describe('test phones (decisions-pending P0-8)', () => {
let dir: string;
const db = 'postgresql://u:p@localhost:5432/jantrik_x_test';
const t0 = Date.UTC(2026, 10, 16, 9);

beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-phones-'));
});
afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));

it('never repeats across processes sharing a database (two sources, interleaved)', () => {
const a = phoneSource('+88013', db, { dir, now: () => t0 });
const b = phoneSource('+88013', db, { dir, now: () => t0 });
const seen = new Set<string>();
for (let i = 0; i < 1_000; i++) {
for (const phone of [a(), b()]) {
expect(seen.has(phone)).toBe(false);
seen.add(phone);
}
}
expect(seen.size).toBe(2_000);
});

it('never repeats across runs: a later run continues after the earlier one', () => {
const first = phoneSource('+88013', db, { dir, now: () => t0 });
const earlier = new Set(Array.from({ length: 500 }, () => first()));
// The next run starts a second later: its clock floor is behind the counter.
const second = phoneSource('+88013', db, { dir, now: () => t0 + 1_000 });
for (let i = 0; i < 500; i++) expect(earlier.has(second())).toBe(false);
});

it('starts from the clock when the counter file is lost, ahead of what earlier runs used', () => {
const first = phoneSource('+88013', db, { dir, now: () => t0 });
const earlier = new Set(Array.from({ length: 300 }, () => first()));
fs.rmSync(dir, { recursive: true, force: true });
const later = phoneSource('+88013', db, { dir, now: () => t0 + 60_000 });
for (let i = 0; i < 300; i++) expect(earlier.has(later())).toBe(false);
expect(floorAt(t0 + 60_000) - floorAt(t0)).toBeGreaterThan(300);
});

it('keeps a counter per database: another database starts on its own', () => {
const one = phoneSource('+88013', db, { dir, now: () => t0 });
const other = phoneSource('+88013', db.replace('jantrik_x_test', 'jantrik_y_test'), {
dir,
now: () => t0,
});
expect(one()).toBe(other());
});

it('gives valid Bangladeshi mobile numbers, clear of the seeds and the old random ranges', () => {
const next = phoneSource('+88013', db, { dir, now: () => t0 });
for (let i = 0; i < 50; i++) {
const phone = next();
expect(phone).toMatch(BD_PHONE_E164);
const n = Number(phone.slice(6));
expect(n).toBeGreaterThanOrEqual(50_000_000);
expect(n).toBeLessThan(99_000_000);
}
});

it('takes over a lock left behind by a process that died', () => {
const counter = path.join(dir, 'localhost_5432_jantrik_x_test__88013.txt');
fs.mkdirSync(`${counter}.lock`);
const old = new Date(Date.now() - 60_000);
fs.utimesSync(`${counter}.lock`, old, old);
const next = phoneSource('+88013', db, { dir, now: () => t0 });
expect(next()).toMatch(BD_PHONE_E164);
expect(fs.existsSync(`${counter}.lock`)).toBe(false);
});
});
104 changes: 104 additions & 0 deletions packages/db/test/test-phones.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

/**
* Phone numbers for test users that no other test user of the same database has (decisions-pending
* P0-8). Random numbers repeated now and then: a full run makes thousands of users, the test
* database keeps every run's users, and a repeated phone joins an existing user, which then has
* two memberships (a new owner got 403 on /me/context once).
*
* Each database has one counter, in a file under the OS temp folder, shared by every test process
* and every run against it. A process takes a block of numbers under a lock, then hands them out
* one by one. The counter never goes back behind the clock (`floorAt`), so a lost counter file
* does not restart at numbers earlier runs gave out. The numbers sit where nothing else is:
* `<prefix>` + 50 000 000–98 999 999 (the seeds' fixed numbers are below; the old random phones
* used other operators).
*
* Kept identical in packages/db/test/test-phones.ts and apps/api/test/support/test-phones.ts (a
* test of the db copy pins how it behaves).
*/

/** Numbers per block a process takes at once. */
const BLOCK = 200;
const LOW = 50_000_000;
const SPAN = 49_000_000;
/** The counter's floor grows 20 a second from this day, so it is always ahead of older runs. */
const EPOCH_MS = Date.UTC(2026, 10, 1);
const PER_SECOND = 20;

export const floorAt = (nowMs: number): number =>
Math.max(0, Math.floor(((nowMs - EPOCH_MS) / 1000) * PER_SECOND));

const pause = (ms: number): void => {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
};

/** Runs `fn` while holding a lock directory; a lock older than 10 s is left over and taken. */
function withLock<T>(lockDir: string, fn: () => T): T {
for (let tries = 0; ; tries++) {
try {
fs.mkdirSync(lockDir);
break;
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
try {
if (Date.now() - fs.statSync(lockDir).mtimeMs > 10_000) {
fs.rmSync(lockDir, { recursive: true, force: true });
continue;
}
} catch {
continue; // released meanwhile
}
if (tries > 3_000) throw new Error(`test-phones: lock ${lockDir} stays taken`);
pause(10);
}
}
try {
return fn();
} finally {
fs.rmSync(lockDir, { recursive: true, force: true });
}
}

/**
* A source of phones `<prefix>NNNNNNNN` (prefix e.g. `+88013`) unique among all users of the
* database `databaseUrl` names (server, port and name; never the login).
*/
export function phoneSource(
prefix: string,
databaseUrl: string,
options: { dir?: string; now?: () => number } = {},
): () => string {
const url = new URL(databaseUrl);
const key = `${url.hostname}_${url.port || '5432'}_${url.pathname.slice(1)}_${prefix}`.replace(
/[^\w.-]/g,
'_',
);
const dir = options.dir ?? path.join(os.tmpdir(), 'jantrik-test-phones');
const now = options.now ?? Date.now;
const counterFile = path.join(dir, `${key}.txt`);
let next = 0;
let end = 0;

const takeBlock = (): void => {
fs.mkdirSync(dir, { recursive: true });
withLock(`${counterFile}.lock`, () => {
let stored = 0;
try {
stored = Number.parseInt(fs.readFileSync(counterFile, 'utf8'), 10) || 0;
} catch {
// No counter yet: start from the clock.
}
next = Math.max(stored, floorAt(now()));
end = next + BLOCK;
fs.writeFileSync(counterFile, String(end));
});
};

return () => {
if (next >= end) takeBlock();
const n = next++;
return `${prefix}${LOW + (n % SPAN)}`;
};
}