Skip to content

Commit 32f5aaa

Browse files
authored
Merge pull request #4 from byRespect/alert-autofix-5
Potential fix for code scanning alert no. 5: Database query built from user-controlled sources
2 parents 20601db + 18fbe37 commit 32f5aaa

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/backend/src/sessions/sessions.service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,28 @@ export class SessionsService {
368368
};
369369
}
370370

371+
// Helper function to pick only allowed fields
372+
private pick(obj: Record<string, any>, allowed: string[]): Record<string, any> {
373+
return Object.keys(obj)
374+
.filter(key => allowed.includes(key))
375+
.reduce((acc, key) => {
376+
acc[key] = obj[key];
377+
return acc;
378+
}, {} as Record<string, any>);
379+
}
380+
371381
async update(
372382
id: string,
373383
updateSessionDto: UpdateSessionDto,
374384
): Promise<Session> {
385+
// Define the list of fields that are safe to update
386+
const allowedFields = [
387+
// TODO: Replace with the actual allowed UpdateSessionDto fields, such as:
388+
"field1", "field2", "field3"
389+
];
390+
const safeUpdate = this.pick(updateSessionDto, allowedFields);
375391
const updated = await this.sessionModel
376-
.findByIdAndUpdate(id, updateSessionDto, { new: true })
392+
.findByIdAndUpdate(id, safeUpdate, { new: true })
377393
.exec();
378394
if (!updated) {
379395
throw new NotFoundException(`Session with id ${id} not found`);

0 commit comments

Comments
 (0)