From 5ce3752ef509d94ca0b0bbb4ef6da4fb0d817d04 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 16 Sep 2026 14:51:13 +0200 Subject: [PATCH 1/2] Fix: reject non-numeric referenceId values in AttReport bulk insert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /att_report/bulk returned HTTP 500 (MySQL 1366) when bulk payloads contained non-integer strings for klassReferenceId ('כל') or studentReferenceId ('not-a-number'). These columns had only @ValidateIf/@IsNotEmpty and no numeric validation, unlike year/klassId/ lessonId which got @NumberType + @IsNumber({maxDecimalPlaces:0}) in #186. Adds the same validators to studentReferenceId, klassReferenceId, teacherReferenceId and lessonReferenceId (all int columns), so invalid values now fail validation with 400 instead of reaching MySQL. --- server/src/db/entities/AttReport.entity.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/src/db/entities/AttReport.entity.ts b/server/src/db/entities/AttReport.entity.ts index e3f95074..51d0ca99 100644 --- a/server/src/db/entities/AttReport.entity.ts +++ b/server/src/db/entities/AttReport.entity.ts @@ -115,6 +115,8 @@ export class AttReport implements IHasUserId { always: true, }) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) + @NumberType + @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @Column({ nullable: true }) @Index('att_reports_student_reference_id_idx') studentReferenceId: number; @@ -129,6 +131,8 @@ export class AttReport implements IHasUserId { always: true, }) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) + @NumberType + @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @Column({ nullable: true }) @Index('att_reports_teacher_reference_id_idx') teacherReferenceId: number; @@ -145,6 +149,8 @@ export class AttReport implements IHasUserId { always: true, }) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) + @NumberType + @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @Column({ nullable: true }) @Index('att_reports_klass_reference_id_idx') klassReferenceId: number; @@ -159,6 +165,8 @@ export class AttReport implements IHasUserId { @ValidateIf((attReport: AttReport) => !Boolean(attReport.lessonId) && Boolean(attReport.lessonReferenceId), { always: true, }) + @NumberType + @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @Column({ nullable: true }) @Index('att_reports_lesson_reference_id_idx') lessonReferenceId: number; From db589626be2de868417192e9327966aff11b6940 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 16 Sep 2026 21:21:53 +0200 Subject: [PATCH 2/2] Fix: validate ReferenceId columns whenever present, closing tz/id-sibling bypass QA found a bypass in 5ce3752: the @ValidateIf predicates gated ALL validation on the tz/id sibling being empty, so a payload supplying BOTH studentTz AND studentReferenceId='garbage' skipped @IsNumber and hit MySQL 1366 -> HTTP 500 again. Predicates now run validation whenever the referenceId value is present (!== undefined && !== null), regardless of the sibling field. Adds regression spec proving 'garbage' is rejected even when the sibling tz/id is also supplied, plus tz/id-only payloads still pass. --- .../att-report-bulk-validation.spec.ts | 72 +++++++++++++++++++ server/src/db/entities/AttReport.entity.ts | 36 ++++++---- 2 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 server/src/__tests__/att-report-bulk-validation.spec.ts diff --git a/server/src/__tests__/att-report-bulk-validation.spec.ts b/server/src/__tests__/att-report-bulk-validation.spec.ts new file mode 100644 index 00000000..b90fbc23 --- /dev/null +++ b/server/src/__tests__/att-report-bulk-validation.spec.ts @@ -0,0 +1,72 @@ +import { validateBulk } from '@shared/base-entity/base-entity.util'; +import { AttReport } from 'src/db/entities/AttReport.entity'; + +describe('AttReport bulk validation', () => { + const REPORT_DATE = '2026-09-16'; + + it('rejects non-numeric studentReferenceId even when studentTz is also supplied', async () => { + await expect( + validateBulk([{ studentTz: '1234567892', studentReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport), + ).rejects.toThrow(); + }); + + it('rejects non-numeric teacherReferenceId even when teacherId is also supplied', async () => { + await expect( + validateBulk([{ teacherId: '1234567892', teacherReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport), + ).rejects.toThrow(); + }); + + it('rejects non-numeric klassReferenceId even when klassId is also supplied', async () => { + await expect( + validateBulk([{ klassId: 7, klassReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport), + ).rejects.toThrow(); + }); + + it('rejects non-numeric lessonReferenceId even when lessonId is also supplied', async () => { + await expect( + validateBulk([{ lessonId: 7, lessonReferenceId: 'garbage', reportDate: REPORT_DATE }], AttReport), + ).rejects.toThrow(); + }); + + it('rejects non-numeric referenceIds when supplied without the tz/id sibling', async () => { + await expect( + validateBulk( + [ + { studentReferenceId: 'כל', reportDate: REPORT_DATE }, + { klassReferenceId: 'not-a-number', reportDate: REPORT_DATE }, + ], + AttReport, + ), + ).rejects.toThrow(); + }); + + it('accepts valid numeric referenceIds, even alongside tz/id siblings', async () => { + await expect( + validateBulk( + [ + { + studentReferenceId: 123, + teacherReferenceId: 456, + klassReferenceId: 45, + lessonReferenceId: 78, + studentTz: '1234567892', + teacherId: '1234567892', + klassId: 7, + lessonId: 8, + reportDate: REPORT_DATE, + }, + ], + AttReport, + ), + ).resolves.toBeUndefined(); + }); + + it('accepts tz/id-based payload without referenceIds', async () => { + await expect( + validateBulk( + [{ studentTz: '1234567892', teacherId: '1234567892', klassId: 7, lessonId: 8, reportDate: REPORT_DATE }], + AttReport, + ), + ).resolves.toBeUndefined(); + }); +}); diff --git a/server/src/db/entities/AttReport.entity.ts b/server/src/db/entities/AttReport.entity.ts index 51d0ca99..d7afd286 100644 --- a/server/src/db/entities/AttReport.entity.ts +++ b/server/src/db/entities/AttReport.entity.ts @@ -111,9 +111,12 @@ export class AttReport implements IHasUserId { @Column('varchar', { name: 'student_tz', length: 10, nullable: true }) studentTz: string; - @ValidateIf((attReport: AttReport) => !Boolean(attReport.studentTz) && Boolean(attReport.studentReferenceId), { - always: true, - }) + @ValidateIf( + (attReport: AttReport) => attReport.studentReferenceId !== undefined && attReport.studentReferenceId !== null, + { + always: true, + }, + ) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) @NumberType @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @@ -127,9 +130,12 @@ export class AttReport implements IHasUserId { @Column('varchar', { name: 'teacher_id', length: 10, nullable: true }) teacherId: string; - @ValidateIf((attReport: AttReport) => !Boolean(attReport.teacherId) && Boolean(attReport.teacherReferenceId), { - always: true, - }) + @ValidateIf( + (attReport: AttReport) => attReport.teacherReferenceId !== undefined && attReport.teacherReferenceId !== null, + { + always: true, + }, + ) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) @NumberType @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @@ -145,9 +151,12 @@ export class AttReport implements IHasUserId { @Column('int', { name: 'klass_id', nullable: true }) klassId: number | null; - @ValidateIf((attReport: AttReport) => !Boolean(attReport.klassId) && Boolean(attReport.klassReferenceId), { - always: true, - }) + @ValidateIf( + (attReport: AttReport) => attReport.klassReferenceId !== undefined && attReport.klassReferenceId !== null, + { + always: true, + }, + ) @IsNotEmpty({ groups: [CrudValidationGroups.CREATE] }) @NumberType @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @@ -162,9 +171,12 @@ export class AttReport implements IHasUserId { @Column('int', { name: 'lesson_id', nullable: true }) lessonId: number; - @ValidateIf((attReport: AttReport) => !Boolean(attReport.lessonId) && Boolean(attReport.lessonReferenceId), { - always: true, - }) + @ValidateIf( + (attReport: AttReport) => attReport.lessonReferenceId !== undefined && attReport.lessonReferenceId !== null, + { + always: true, + }, + ) @NumberType @IsNumber({ maxDecimalPlaces: 0 }, { always: true }) @Column({ nullable: true })