Skip to content

Commit 72a1a62

Browse files
committed
style: sonarqube security
1 parent 24b165a commit 72a1a62

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/image/image-repository.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ class ImageRepository {
1515
* @return { Promise<ImageProp> } - A new image created
1616
*/
1717
async create(image: ImageProp): Promise<ImageProp> {
18-
const sanitizedTagId = image.tag.toString().trim();
19-
if (!Types.ObjectId.isValid(sanitizedTagId)) {
20-
rollbar.error('Invalid tag id');
18+
19+
const sanitizedTagId = image.tag.tag_id?.toString();
20+
if (!Types.ObjectId.isValid(image.tag.tag_id)) {
2121
throw new Error('Invalid tag id');
2222
}
2323

24-
const tagExists = await Tag.findOne({ tag_id: sanitizedTagId });
25-
const _idTag = tagExists?._id;
24+
const tagExists = await Tag.findById({ tag_id: { $eq: sanitizedTagId } });
25+
26+
if (!tagExists) {
27+
rollbar.error('Tag not found');
28+
throw new Error('Tag not found');
29+
}
2630

2731
return Image.create({
2832
...image,
29-
tag: _idTag ?? image.tag, // Use validated tag or fallback
33+
tag: tagExists._id,
3034
});
3135
}
3236

0 commit comments

Comments
 (0)