Skip to content

Commit e8eaa0d

Browse files
committed
fix(api): prevent stack overflow during base64 file parsing
1 parent 043c70a commit e8eaa0d

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

apps/api/src/evidence-forms/evidence-forms.service.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,17 @@ export class EvidenceFormsService {
196196
);
197197
}
198198

199-
const base64Pattern = /^[A-Za-z0-9+/]+={0,2}$/;
200-
if (!base64Pattern.test(normalized)) {
201-
throw new BadRequestException(
202-
'Invalid file data. Expected base64 string.',
203-
);
204-
}
199+
try {
200+
const fileBuffer = Buffer.from(normalized, 'base64');
205201

206-
const fileBuffer = Buffer.from(normalized, 'base64');
207-
if (!fileBuffer.length) {
208-
throw new BadRequestException('File cannot be empty');
209-
}
202+
if (!fileBuffer.length) {
203+
throw new BadRequestException('File cannot be empty.');
204+
}
210205

211-
return fileBuffer;
206+
return fileBuffer;
207+
} catch {
208+
throw new BadRequestException('Invalid file data. Expected base64 string.');
209+
}
212210
}
213211

214212
/**

0 commit comments

Comments
 (0)