Skip to content

Commit 24e9cea

Browse files
Merge pull request #99 from jd-apprentice/development
Improvements from sonarqube suggestions
2 parents 48e81d1 + 24b165a commit 24e9cea

4 files changed

Lines changed: 60 additions & 16 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: 27 additions & 9 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,23 @@ 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 {
57+
username: string;
58+
password: string;
59+
};
60+
const [sanitizedUsername, sanitizedPassword] = [
61+
username.toString(),
62+
password.toString(),
63+
];
5764

58-
if (user?.password && (await bcrypt.compare(password, user.password))) {
65+
const user = await User.findOne({
66+
$expr: { $eq: ['$username', sanitizedUsername] },
67+
});
68+
69+
if (
70+
user?.password &&
71+
(await bcrypt.compare(sanitizedPassword, user.password))
72+
) {
5973
return next();
6074
}
6175

@@ -75,10 +89,13 @@ const isAdmin = async (
7589
res: Response,
7690
next: NextFunction,
7791
): Promise<MiddlewareUser> => {
78-
const { username } = req.body;
92+
const { username } = req.body as { username: string };
93+
const sanitizedUsername = username.toString();
7994

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

83100
if (user?.isAdmin) {
84101
return next();
@@ -104,9 +121,10 @@ const validateToken = async (
104121
try {
105122
const { authorization } = req.headers;
106123
const token = authorization?.replace('Bearer ', '');
124+
if (!token) return res.status(401).json(boom.unauthorized());
107125
if (secret) {
108-
const decoded = jwt.verify(token as string, secret);
109-
return decoded ? next() : res.json(boom.unauthorized());
126+
const decoded = jwt.verify(token, secret);
127+
return decoded ? next() : res.status(401).json(boom.unauthorized());
110128
}
111129
} catch (error) {
112130
return res.status(401).json(boom.unauthorized());

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)