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
Review round 2, API: uploads in flight bounded before multer (16 per …
…process, 2 per person, 503 IMAGE_BUSY at once, the slot given back when the response closes, a body not received in 60 s dropped); RequestTransaction.run refuses to nest; the store's transaction re-checks the session and the route's roles; the target is checked before decoding (no decode or quota for a wrong, deactivated or suspended target); sharp's 10 s timeout as 422 IMAGE_TIMEOUT; a replacement no bigger than the old file is never refused; a profile save takes the file lock first. Each with a test that failed first; plus tests for a suspended institute and a disabled member, the slow body on both routes, the ON CONFLICT path and the signature column's kind

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
omerFixesBugs and claude committed Nov 16, 2026
commit 282c8b6b35f4842bea8c171659f7c6b4158080f4
6 changes: 4 additions & 2 deletions apps/api/src/files/file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export interface FileStore {
lock(): Promise<void>;
/**
* Stores a file; 413 STORAGE_FULL when the institute's total would pass its cap. `replaces`:
* the file this one replaces (the caller deletes it in the same transaction), so a same-size
* replacement is never refused near the cap.
* the file this one replaces, counted as freed, so a replacement that does not grow the total
* is allowed near the cap, and one no bigger than the old file even over it. The caller MUST
* then delete that file in the same transaction (after pointing the row at the new one), or
* the total ends above the cap. Take `lock()` before reading which file is replaced.
*/
put(file: PutFile, opts?: { replaces?: string | null }): Promise<StoredFileMeta>;
/** The file with its bytes, or null (unknown here). */
Expand Down
17 changes: 14 additions & 3 deletions apps/api/src/files/files.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Inject,
Injectable,
Module,
UseGuards,
UseInterceptors,
applyDecorators,
} from '@nestjs/common';
Expand All @@ -15,6 +16,7 @@ import { RateLimiter } from '../redis/rate-limiter.service.js';
import { FILE_STORE, type FileStore, type StoredFileMeta } from './file-store.js';
import { ImageService, type ProcessedImage } from './image.service.js';
import { PostgresFileStore } from './postgres-file-store.js';
import { UploadGuard, UploadLimits } from './upload-limits.js';

/** What multer hands over for the `file` field (memory storage). */
export interface UploadedImage {
Expand All @@ -26,10 +28,13 @@ export const UPLOADS_PER_USER = 60;

/**
* A multipart route with one image in the field `file`: at most UPLOAD_MAX_BYTES (bigger: 413),
* one file and no other fields. The type and name the browser sends are never trusted.
* one file and no other fields. The type and name the browser sends are never trusted. UploadGuard
* runs first: a bounded number of uploads in flight per process and per person, a slot given
* back when the response closes, and a body that has not arrived in 60 s dropped.
*/
export const ImageUpload = () =>
applyDecorators(
UseGuards(UploadGuard),
UseInterceptors(
FileInterceptor('file', { limits: { fileSize: UPLOAD_MAX_BYTES, files: 1, fields: 0 } }),
),
Expand Down Expand Up @@ -96,7 +101,13 @@ export async function sendStoredImage(

@Global()
@Module({
providers: [ImageService, ImageUploads, { provide: FILE_STORE, useClass: PostgresFileStore }],
exports: [ImageService, ImageUploads, FILE_STORE],
providers: [
ImageService,
ImageUploads,
UploadLimits,
UploadGuard,
{ provide: FILE_STORE, useClass: PostgresFileStore },
],
exports: [ImageService, ImageUploads, UploadLimits, UploadGuard, FILE_STORE],
})
export class FilesModule {}
14 changes: 13 additions & 1 deletion apps/api/src/files/image.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomBytes } from 'node:crypto';
import { crc32, deflateSync } from 'node:zlib';
import sharp from 'sharp';
import { ImageService, Slots, processImage, sniffImage } from './image.service.js';
import { ImageService, Slots, imageFailure, processImage, sniffImage } from './image.service.js';

// docs/files.md: what an upload becomes before it is stored. Test images are made with sharp.

Expand Down Expand Up @@ -157,6 +157,18 @@ describe('processImage', () => {
});
});

describe('imageFailure (review round 2)', () => {
it("maps sharp's timeout to 422 IMAGE_TIMEOUT and anything else to IMAGE_INVALID", () => {
const timeout = imageFailure(new Error('timeout: 10s exceeded'));
expect([timeout.getStatus(), (timeout.getResponse() as { code: string }).code]).toEqual([
422,
'IMAGE_TIMEOUT',
]);
const other = imageFailure(new Error('Input buffer contains unsupported image format'));
expect((other.getResponse() as { code: string }).code).toBe('IMAGE_INVALID');
});
});

describe('PNG metadata', () => {
it('strips EXIF from a PNG too (the eXIf chunk), whatever it becomes', async () => {
const input = await noise(300, 300)
Expand Down
27 changes: 23 additions & 4 deletions apps/api/src/files/image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ export function sniffImage(input: Buffer): Sniffed | null {
const invalid = () =>
apiError(422, 'IMAGE_INVALID', 'The file is not a JPEG, PNG or WebP image, or it is damaged');

/** Seconds a decode or an encode may take (sharp's own timer), well inside the queue's turn. */
const SHARP_TIMEOUT = { seconds: 10 };

/** sharp's error as the API's: its timeout → 422 IMAGE_TIMEOUT, anything else IMAGE_INVALID. */
export function imageFailure(error: unknown) {
return /timeout/i.test((error as Error)?.message ?? '')
? apiError(422, 'IMAGE_TIMEOUT', 'The image took too long to process; try a simpler one')
: invalid();
}

/** sharp's types are an `export =` namespace; these are the shapes used here. */
type Sharp = ReturnType<typeof sharp>;
type Metadata = Awaited<ReturnType<Sharp['metadata']>>;
Expand Down Expand Up @@ -184,9 +194,9 @@ export async function processImage(kind: StoredFileKind, input: Buffer): Promise
.rotate()
.resize(box.width, box.height, { fit: 'inside', withoutEnlargement: true });
if (flatten) pipeline = pipeline.flatten({ background: '#ffffff' });
raw = await pipeline.raw().toBuffer({ resolveWithObject: true });
} catch {
throw invalid();
raw = await pipeline.raw().timeout(SHARP_TIMEOUT).toBuffer({ resolveWithObject: true });
} catch (error) {
throw imageFailure(error);
}

const fromRaw = () =>
Expand All @@ -204,7 +214,16 @@ export async function processImage(kind: StoredFileKind, input: Buffer): Promise
const height = Math.max(1, Math.round(raw.info.height * scale));
for (const encoder of encoders) {
const sized = scale === 1 ? fromRaw() : fromRaw().resize(width, height, { fit: 'fill' });
const { data, info } = await encoder.apply(sized).toBuffer({ resolveWithObject: true });
let encoded: RawImage;
try {
encoded = await encoder
.apply(sized)
.timeout(SHARP_TIMEOUT)
.toBuffer({ resolveWithObject: true });
} catch (error) {
throw imageFailure(error);
}
const { data, info } = encoded;
if (data.length <= limit.maxBytes) {
return { bytes: data, mime: encoder.mime, width: info.width, height: info.height };
}
Expand Down
4 changes: 3 additions & 1 deletion apps/api/src/files/postgres-file-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export class PostgresFileStore implements FileStore {
})
)?.sizeBytes ?? 0)
: 0;
if (bytesUsed - replaced + size > capBytes) {
// A replacement no bigger than the file it replaces is never refused, even when the total is
// over a lowered cap: it cannot make it worse (the caller deletes the old file).
if (size > replaced && bytesUsed - replaced + size > capBytes) {
throw apiError(413, 'STORAGE_FULL', "The institute's storage for images is full", {
bytesUsed,
capBytes,
Expand Down
86 changes: 86 additions & 0 deletions apps/api/src/files/upload-limits.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { EventEmitter } from 'node:events';
import type { Request, Response } from 'express';
import {
UPLOAD_BODY_TIMEOUT_MS,
UPLOADS_IN_FLIGHT,
UPLOADS_IN_FLIGHT_PER_USER,
UploadLimits,
} from './upload-limits.js';

// Review round 2: uploads in flight are capped per process and per person before multer reads a
// byte, and every slot is given back when the response closes (finished, aborted or failed).

function exchange() {
const req = Object.assign(new EventEmitter(), { destroy: vi.fn() });
const res = new EventEmitter();
return { req, res, asReq: req as unknown as Request, asRes: res as unknown as Response };
}

const code = (fn: () => void) => {
try {
fn();
} catch (e) {
const err = e as { getStatus(): number; getResponse(): { code: string } };
return `${err.getStatus()} ${err.getResponse().code}`;
}
return 'ok';
};

describe('UploadLimits', () => {
afterEach(() => vi.useRealTimers());

it(`lets a person have ${UPLOADS_IN_FLIGHT_PER_USER} uploads in flight; the next gets 503 IMAGE_BUSY`, () => {
const limits = new UploadLimits();
const a = exchange();
const b = exchange();
limits.enter('u1', a.asReq, a.asRes);
limits.enter('u1', b.asReq, b.asRes);
const c = exchange();
expect(code(() => limits.enter('u1', c.asReq, c.asRes))).toBe('503 IMAGE_BUSY');
// Someone else is not affected.
expect(code(() => limits.enter('u2', c.asReq, c.asRes))).toBe('ok');
expect(limits.inFlight).toBe(3);
});

it(`caps the process at ${UPLOADS_IN_FLIGHT} uploads in flight`, () => {
const limits = new UploadLimits();
for (let i = 0; i < UPLOADS_IN_FLIGHT; i++) {
const x = exchange();
limits.enter(`user-${i}`, x.asReq, x.asRes);
}
const x = exchange();
expect(code(() => limits.enter('one-more', x.asReq, x.asRes))).toBe('503 IMAGE_BUSY');
});

it('gives the slot back when the response closes, once, whatever ended it', () => {
const limits = new UploadLimits();
const a = exchange();
limits.enter('u1', a.asReq, a.asRes);
const b = exchange();
limits.enter('u1', b.asReq, b.asRes);
a.res.emit('close'); // finished
b.res.emit('close'); // aborted by the client: the same event
a.res.emit('close'); // a second close changes nothing
expect(limits.inFlight).toBe(0);
const c = exchange();
limits.enter('u1', c.asReq, c.asRes);
limits.enter('u1', exchange().asReq, exchange().asRes);
expect(limits.inFlight).toBe(2);
});

it(`drops a body that has not arrived within ${UPLOAD_BODY_TIMEOUT_MS / 1000} s; one that has is left alone`, () => {
vi.useFakeTimers();
const limits = new UploadLimits();
const slow = exchange();
const quick = exchange();
limits.enter('u1', slow.asReq, slow.asRes);
limits.enter('u2', quick.asReq, quick.asRes);
quick.req.emit('end'); // its body arrived
vi.advanceTimersByTime(UPLOAD_BODY_TIMEOUT_MS);
expect(slow.req.destroy).toHaveBeenCalledTimes(1);
expect(quick.req.destroy).not.toHaveBeenCalled();
slow.res.emit('close');
quick.res.emit('close');
expect(limits.inFlight).toBe(0);
});
});
69 changes: 69 additions & 0 deletions apps/api/src/files/upload-limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Injectable, type CanActivate, type ExecutionContext } from '@nestjs/common';
import type { Request, Response } from 'express';
import { apiError } from '../common/api-error.js';
import { RequestContext } from '../common/request-context.js';

/** Uploads whose body may be in memory at once, per API process (5 MB each at most). */
export const UPLOADS_IN_FLIGHT = 16;
/** Per person: an admin's second tab is fine; a script holding many bodies is not. */
export const UPLOADS_IN_FLIGHT_PER_USER = 2;
/** A body not fully received by then is dropped (5 MB at 0.75 Mbps takes about 55 s). */
export const UPLOAD_BODY_TIMEOUT_MS = 60_000;

const busy = () => apiError(503, 'IMAGE_BUSY', 'Too many uploads at once; try again in a moment');

/**
* Bounds what uploads can hold in memory (review round 2). The upload routes read their body
* with no request transaction (@OwnTransaction), into memory (multer), so nothing else limited
* how many bodies could be in flight. A slot is taken before multer reads a byte, refused at once
* with 503 IMAGE_BUSY when the process or the person has too many, and given back when the
* response closes, however it ends (sent, failed, or the client went away). A body that has not
* arrived within UPLOAD_BODY_TIMEOUT_MS is dropped, which closes the response too.
*/
@Injectable()
export class UploadLimits {
private total = 0;
private readonly perUser = new Map<string, number>();

get inFlight(): number {
return this.total;
}

enter(userId: string, req: Request, res: Response): void {
const mine = this.perUser.get(userId) ?? 0;
if (this.total >= UPLOADS_IN_FLIGHT || mine >= UPLOADS_IN_FLIGHT_PER_USER) throw busy();
this.total++;
this.perUser.set(userId, mine + 1);
const timer = setTimeout(() => req.destroy(), UPLOAD_BODY_TIMEOUT_MS);
req.once('end', () => clearTimeout(timer));
let released = false;
res.once('close', () => {
if (released) return;
released = true;
clearTimeout(timer);
this.total--;
const left = (this.perUser.get(userId) ?? 1) - 1;
if (left > 0) this.perUser.set(userId, left);
else this.perUser.delete(userId);
});
}
}

/** The guard of the upload routes: runs after the token and roles guards, before multer. */
@Injectable()
export class UploadGuard implements CanActivate {
constructor(
private readonly limits: UploadLimits,
private readonly context: RequestContext,
) {}

canActivate(ctx: ExecutionContext): boolean {
const http = ctx.switchToHttp();
this.limits.enter(
this.context.requirePrincipal().userId,
http.getRequest<Request>(),
http.getResponse<Response>(),
);
return true;
}
}
51 changes: 31 additions & 20 deletions apps/api/src/institute/institute-profile.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const FIELDS = [

const BANGLA = /[ঀ-৿]/;

/** Who may change the profile (the routes' @Roles), checked again when an image is stored. */
const EDITORS = ['OWNER', 'ADMIN'] as const;

/**
* The institute's own profile (sprint 1 P2, docs/files.md): names, EIIN, contact, the head of the
* institute, and three images. One row per institute, created on the first save or image (no
Expand Down Expand Up @@ -112,6 +115,9 @@ export class InstituteProfileService {

async update(input: InstituteProfileFields): Promise<InstituteProfile> {
const tenantId = this.context.requireTenantId();
// The file lock (an image upload holds it from before it creates the row to its commit), so a
// first image racing this first save is seen here and the save is audited as an UPDATE.
await this.files.lock();
const before = await this.row();
const data = Object.fromEntries(
FIELDS.map((f) => [f, input[f]]),
Expand Down Expand Up @@ -143,27 +149,32 @@ export class InstituteProfileService {
* institute's file lock comes before reading the old id, and the cap counts the file replaced.
*/
async setImage(slot: InstituteImageSlot, upload: UploadedImage | undefined) {
// A suspended institute or a lost role costs no decoding and no quota.
await this.transaction.run(async () => undefined, { roles: EDITORS });
const image = await this.uploads.prepare(INSTITUTE_IMAGE_KIND[slot], upload);
return this.transaction.run(async () => {
await this.files.lock();
const profile = await this.ensureRow();
const oldId = profile[COLUMN[slot]];
const stored = await this.uploads.store(image, oldId);
await this.db.tx.instituteProfile.update({
where: { id: profile.id },
data: { [COLUMN[slot]]: stored.id },
select: { id: true },
});
if (oldId) await this.files.delete(oldId);
await this.audit.record({
entity: 'institute_profile',
entityId: profile.id,
action: 'UPDATE',
oldValues: { image: slot, fileId: oldId },
newValues: { image: slot, fileId: stored.id },
});
return this.get();
});
return this.transaction.run(
async () => {
await this.files.lock();
const profile = await this.ensureRow();
const oldId = profile[COLUMN[slot]];
const stored = await this.uploads.store(image, oldId);
await this.db.tx.instituteProfile.update({
where: { id: profile.id },
data: { [COLUMN[slot]]: stored.id },
select: { id: true },
});
if (oldId) await this.files.delete(oldId);
await this.audit.record({
entity: 'institute_profile',
entityId: profile.id,
action: 'UPDATE',
oldValues: { image: slot, fileId: oldId },
newValues: { image: slot, fileId: stored.id },
});
return this.get();
},
{ roles: EDITORS },
);
}

async removeImage(slot: InstituteImageSlot): Promise<InstituteProfile> {
Expand Down
Loading