Skip to content

Commit 24b165a

Browse files
committed
style: sonarqube security
1 parent 33d9c64 commit 24b165a

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/image/image-repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ImageRepository {
2626

2727
return Image.create({
2828
...image,
29-
tag: _idTag ?? image.tag, // Use validated tag or fallback
29+
tag: _idTag ?? image.tag, // Use validated tag or fallback
3030
});
3131
}
3232

src/user/user-middleware.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,23 @@ const validateUser = async (
5353
next: NextFunction,
5454
): Promise<MiddlewareUser> => {
5555
try {
56-
const { username, password } = req.body as { username: string; password: string };
57-
const [sanitizedUsername, sanitizedPassword] = [username.toString(), password.toString()];
56+
const { username, password } = req.body as {
57+
username: string;
58+
password: string;
59+
};
60+
const [sanitizedUsername, sanitizedPassword] = [
61+
username.toString(),
62+
password.toString(),
63+
];
5864

59-
const user = await User.findOne({ $expr: { $eq: ['$username', sanitizedUsername] } });
65+
const user = await User.findOne({
66+
$expr: { $eq: ['$username', sanitizedUsername] },
67+
});
6068

61-
if (user?.password && (await bcrypt.compare(sanitizedPassword, user.password))) {
69+
if (
70+
user?.password &&
71+
(await bcrypt.compare(sanitizedPassword, user.password))
72+
) {
6273
return next();
6374
}
6475

@@ -82,7 +93,9 @@ const isAdmin = async (
8293
const sanitizedUsername = username.toString();
8394

8495
try {
85-
const user = await User.findOne({ $expr: { $eq: ['$username', sanitizedUsername] } });
96+
const user = await User.findOne({
97+
$expr: { $eq: ['$username', sanitizedUsername] },
98+
});
8699

87100
if (user?.isAdmin) {
88101
return next();
@@ -108,10 +121,10 @@ const validateToken = async (
108121
try {
109122
const { authorization } = req.headers;
110123
const token = authorization?.replace('Bearer ', '');
124+
if (!token) return res.status(401).json(boom.unauthorized());
111125
if (secret) {
112-
if (!token) return res.json(boom.unauthorized());
113126
const decoded = jwt.verify(token, secret);
114-
return decoded ? next() : res.json(boom.unauthorized());
127+
return decoded ? next() : res.status(401).json(boom.unauthorized());
115128
}
116129
} catch (error) {
117130
return res.status(401).json(boom.unauthorized());

src/user/user-repository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UserRepository {
3434
*/
3535
async findUser(id: string): Promise<IUser | null> {
3636
const sanitizedId = id.toString();
37-
return User.findOne({ $expr: { $eq: ["$_id", sanitizedId] } })
37+
return User.findOne({ $expr: { $eq: ['$_id', sanitizedId] } });
3838
}
3939

4040
/**
@@ -43,7 +43,7 @@ class UserRepository {
4343
*/
4444
async findUserByUsername(username: string): Promise<IUser | null> {
4545
const sanitizedUsername = username.toString();
46-
return User.findOne({ $expr: { $eq: ["$username", sanitizedUsername] } });
46+
return User.findOne({ $expr: { $eq: ['$username', sanitizedUsername] } });
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)