Skip to content

Commit e927fd3

Browse files
Merge pull request #50 from beginwebdev2002/alert-autofix-3
Potential fix for code scanning alert no. 3: Database query built from user-controlled sources
2 parents 57ec02e + 0276a71 commit e927fd3

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎backend/src/modules/inventory/infrastructure/repositories/inventory.repository.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ export class InventoryRepository {
3939
id: string,
4040
updateData: Partial<Inventory>,
4141
): Promise<Inventory | null> {
42+
if (!id || !id.match(/^[0-9a-fA-F]{24}$/)) {
43+
return null;
44+
}
45+
46+
const sanitizedUpdate: Partial<
47+
Pick<Inventory, 'itemName' | 'quantity' | 'location'>
48+
> = {};
49+
50+
if (typeof updateData.itemName === 'string') {
51+
sanitizedUpdate.itemName = updateData.itemName;
52+
}
53+
if (typeof updateData.quantity === 'number') {
54+
sanitizedUpdate.quantity = updateData.quantity;
55+
}
56+
if (typeof updateData.location === 'string') {
57+
sanitizedUpdate.location = updateData.location;
58+
}
59+
4260
const doc = await this.inventoryModel
43-
.findByIdAndUpdate(id, { $set: updateData }, { new: true })
61+
.findByIdAndUpdate(id, { $set: sanitizedUpdate }, { new: true })
4462
.exec();
4563
return doc ? this.toDomain(doc) : null;
4664
}

0 commit comments

Comments
 (0)