Skip to content

Commit 33d9c64

Browse files
committed
style: sonarqube security
1 parent 636e239 commit 33d9c64

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
22
"editor.formatOnSave": true,
3+
"sonarlint.connectedMode.project": {
4+
"connectionId": "dyallab",
5+
"projectKey": "jd-apprentice_waifuland-api"
6+
},
37
}

src/image/image-repository.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// External Modules
2+
import { Types } from 'mongoose';
3+
14
// Internal Modules
25
import Image from './schema/image-schema';
3-
import Tag from '../tag/schema/tag-schema';
4-
import { hasTag } from '../common/utils/ref';
6+
import Tag from 'src/tag/schema/tag-schema';
7+
import { hasTag } from 'src/common/utils/ref';
58
import { ImageProp } from './interfaces/image-interface';
9+
import { rollbar } from 'src/app/config/rollbar';
610

711
class ImageRepository {
812
/**
@@ -11,11 +15,18 @@ class ImageRepository {
1115
* @return { Promise<ImageProp> } - A new image created
1216
*/
1317
async create(image: ImageProp): Promise<ImageProp> {
14-
const tagExists = await Tag.findOne({ tag_id: { $eq: image.tag } });
18+
const sanitizedTagId = image.tag.toString().trim();
19+
if (!Types.ObjectId.isValid(sanitizedTagId)) {
20+
rollbar.error('Invalid tag id');
21+
throw new Error('Invalid tag id');
22+
}
23+
24+
const tagExists = await Tag.findOne({ tag_id: sanitizedTagId });
1525
const _idTag = tagExists?._id;
26+
1627
return Image.create({
1728
...image,
18-
tag: _idTag ?? image.tag,
29+
tag: _idTag ?? image.tag, // Use validated tag or fallback
1930
});
2031
}
2132

src/user/user-middleware.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ const userExists = async (
2424
res: Response,
2525
next: NextFunction,
2626
): Promise<MiddlewareUser> => {
27-
const { username } = req.body;
27+
const { username } = req.body as { username: string };
28+
const sanitizedUsername = username.toString();
2829

2930
try {
30-
const user = await User.findOne({ username: { $eq: username } });
31+
const user = await User.findOne({ username: { $eq: sanitizedUsername } });
3132

3233
if (user) {
3334
return res.status(409).json({ error: 'User already exists' });
@@ -52,10 +53,12 @@ const validateUser = async (
5253
next: NextFunction,
5354
): Promise<MiddlewareUser> => {
5455
try {
55-
const { username, password } = req.body;
56-
const user = await User.findOne({ username: { $eq: username } });
56+
const { username, password } = req.body as { username: string; password: string };
57+
const [sanitizedUsername, sanitizedPassword] = [username.toString(), password.toString()];
5758

58-
if (user?.password && (await bcrypt.compare(password, user.password))) {
59+
const user = await User.findOne({ $expr: { $eq: ['$username', sanitizedUsername] } });
60+
61+
if (user?.password && (await bcrypt.compare(sanitizedPassword, user.password))) {
5962
return next();
6063
}
6164

@@ -75,10 +78,11 @@ const isAdmin = async (
7578
res: Response,
7679
next: NextFunction,
7780
): Promise<MiddlewareUser> => {
78-
const { username } = req.body;
81+
const { username } = req.body as { username: string };
82+
const sanitizedUsername = username.toString();
7983

8084
try {
81-
const user = await User.findOne({ username: { $eq: username } });
85+
const user = await User.findOne({ $expr: { $eq: ['$username', sanitizedUsername] } });
8286

8387
if (user?.isAdmin) {
8488
return next();
@@ -105,7 +109,8 @@ const validateToken = async (
105109
const { authorization } = req.headers;
106110
const token = authorization?.replace('Bearer ', '');
107111
if (secret) {
108-
const decoded = jwt.verify(token as string, secret);
112+
if (!token) return res.json(boom.unauthorized());
113+
const decoded = jwt.verify(token, secret);
109114
return decoded ? next() : res.json(boom.unauthorized());
110115
}
111116
} catch (error) {

src/user/user-repository.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Internal Modules
2-
import User from '../user/schema/user-schema';
2+
import { rollbar } from 'src/app/config/rollbar';
3+
import User from 'src/user/schema/user-schema';
34
import { IUser, UserPicture } from './interfaces/user-interface';
45

56
class UserRepository {
@@ -8,6 +9,14 @@ class UserRepository {
89
* @param {Iuser} user - user to be created
910
*/
1011
async create(user: IUser) {
12+
const sanitizedUsername = user.username.toString();
13+
const userExists = await this.findUserByUsername(sanitizedUsername);
14+
15+
if (userExists) {
16+
rollbar.error('User already exists');
17+
throw new Error('User already exists');
18+
}
19+
1120
return User.create(user);
1221
}
1322

@@ -24,15 +33,17 @@ class UserRepository {
2433
* @param {string} id - id of the user
2534
*/
2635
async findUser(id: string): Promise<IUser | null> {
27-
return User.findOne({ _id: { $eq: id } });
36+
const sanitizedId = id.toString();
37+
return User.findOne({ $expr: { $eq: ["$_id", sanitizedId] } })
2838
}
2939

3040
/**
3141
* @description Find a user by username
3242
* @param {string} username - username of the user
3343
*/
3444
async findUserByUsername(username: string): Promise<IUser | null> {
35-
return User.findOne({ username: { $eq: username } });
45+
const sanitizedUsername = username.toString();
46+
return User.findOne({ $expr: { $eq: ["$username", sanitizedUsername] } });
3647
}
3748

3849
/**

0 commit comments

Comments
 (0)