Skip to content
This repository was archived by the owner on Mar 2, 2025. It is now read-only.

Commit 578d45d

Browse files
committed
fix critical things + validation
1 parent 8c7df0b commit 578d45d

File tree

5 files changed

+43
-57
lines changed

5 files changed

+43
-57
lines changed

routes/api.js

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@ const database = require("../database/sqlite");
33
const logger = require("../utilities/logger");
44
const {discordLinkTemplate, clientId, redirectUri, use_caching, cache_size, use_given_table, checkGuild, guildId} = require("../configuration/config");
55
const dHelper = require("../utilities/discordhelper");
6-
const {checkAccess} = require("../utilities/tokenutils");
7-
const path = require("path");
6+
const {validateToken} = require("../utilities/tokenutils");
87
const CacheManager = require("../utilities/cache_managing");
98
const {setGivenTo, getGivenBySS14Id, setGivenToZeroAll, setGivenDiscordTo, getGivenByDiscordId, getUserByDiscordId,
109
getUserBySS14Id } = require('../database/sqlite');
1110
const {checkInGuild} = require("../utilities/discordhelper");
1211

1312
const userCache = new CacheManager(cache_size);
1413

15-
router.get("/check", async (req, res) => {
16-
if (!req.query.api_token)
17-
return res.status(401).send("Unauthorized")
18-
19-
if (!checkAccess(req.query.api_token))
20-
return res.status(401).send("Unauthorized")
14+
router.use(validateToken);
2115

22-
if (!req.query.userid) {
16+
router.get("/check", async (req, res) => {
17+
if (!req.query.userid)
2318
return res.status(400).json({ error: "No user id provided" });
24-
}
2519

2620
if (use_caching) {
2721
const user = userCache.get(req.query.userid);
@@ -56,12 +50,6 @@ router.get("/check", async (req, res) => {
5650

5751
// generate auth link
5852
router.get('/link', async (req, res) => {
59-
if (!req.query.api_token)
60-
return res.status(401).send("Unauthorized")
61-
62-
if (!checkAccess(req.query.api_token))
63-
return res.status(401).send("Unauthorized")
64-
6553
if (!req.query.userid) {
6654
return res.status(400).json({ error: "No user ID provided" });
6755
}
@@ -76,15 +64,8 @@ router.get('/link', async (req, res) => {
7664
});
7765

7866
router.get('/roles', async (req, res) => {
79-
if (!req.query.api_token)
80-
return res.status(401).send("Unauthorized")
81-
82-
if (!checkAccess(req.query.api_token))
83-
return res.status(401).send("Unauthorized")
84-
85-
if (!req.query.userid) {
67+
if (!req.query.userid)
8668
return res.status(400).json({ error: "No user ID provided" });
87-
}
8869

8970
if (!req.query.guildid) {
9071
return res.status(400).json({error: 'No guild ID provided'});
@@ -111,20 +92,13 @@ router.get('/roles', async (req, res) => {
11192
})
11293

11394
router.get('/user', async (req, res) => {
114-
if (!req.query.api_token)
115-
return res.status(401).send("Unauthorized")
116-
117-
if (!checkAccess(req.query.api_token))
118-
return res.status(401).send("Unauthorized")
119-
120-
if (!req.query.method) {
121-
return res.status(400).json({error: "Method is not provided"})
122-
}
123-
95+
if (!req.query.method)
96+
return res.status(400).json({error: "Method is not provided"});
12497

12598
if (!req.query.id) {
12699
return res.status(400).json({ error: "No user ID provided" });
127100
}
101+
128102
const uid = req.query.id;
129103

130104
if (use_caching && req.query.method === 'ss14') {
@@ -154,7 +128,7 @@ router.get('/user', async (req, res) => {
154128
}
155129

156130
if (!user) {
157-
return res.status(400).json({error: "Not Found"});
131+
return res.status(404).json({ error: 'User not found' });
158132
}
159133

160134
const {id, access_token, refresh_token, ...newUser} = user;
@@ -163,12 +137,6 @@ router.get('/user', async (req, res) => {
163137
})
164138

165139
router.post('/given', async (req, res) => {
166-
if (!req.query.api_token)
167-
return res.status(401).send("Unauthorized");
168-
169-
if (!checkAccess(req.query.api_token))
170-
return res.status(401).send("Unauthorized");
171-
172140
if (!use_given_table)
173141
return res.status(405).send("Given table is turned off")
174142

@@ -204,12 +172,6 @@ router.post('/given', async (req, res) => {
204172
})
205173

206174
router.get('/is_given', async (req, res) => {
207-
if (!req.query.api_token)
208-
return res.status(401).send("Unauthorized")
209-
210-
if (!checkAccess(req.query.api_token))
211-
return res.status(401).send("Unauthorized")
212-
213175
if (!use_given_table)
214176
return res.status(405).send("Given table is turned off")
215177

@@ -250,12 +212,6 @@ router.get('/is_given', async (req, res) => {
250212
})
251213

252214
router.post('/wipe_given', async (req, res) => {
253-
if (!req.query.api_token)
254-
return res.status(401).send("Unauthorized")
255-
256-
if (!checkAccess(req.query.api_token))
257-
return res.status(401).send("Unauthorized")
258-
259215
if (!use_given_table)
260216
return res.status(405).send("Given table is turned off")
261217

routes/auth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path');
33

44
const logger = require('../utilities/logger.js');
55
const {insertUser, insertGivenUser, getUserByDiscordId} = require('../database/sqlite.js');
6-
const {use_given} = require('../configuration/config')
6+
const {use_given_table} = require('../configuration/config')
77
const {exchangeCode, getDiscordIdentifyScopeUnsafe} = require("../utilities/discordhelper");
88

99
router.get('/callback', async (req, res) => {
@@ -56,11 +56,11 @@ router.get('/callback', async (req, res) => {
5656
new Date().toISOString()); // current date time
5757
logger.info(`Added new user with userid - ${userid}`);
5858

59-
if (use_given) {
59+
if (use_given_table) {
6060
result1 = await insertGivenUser(userObject.user.id, userid, 0);
6161
}
6262

63-
if (result && (!use_given || result1)) {
63+
if (result && (!use_given_table || result1)) {
6464
res.status(200).sendFile(path.join(__dirname, '..', 'public', 'html', 'success.html'));
6565
return;
6666
}

utilities/cache_managing.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const {cache_update_timeout} = require('../configuration/config')
22
const logger = require('./logger')
33

4+
const getUser = (cache, key) => {
5+
const user = cache.get(key);
6+
7+
}
8+
49
class CacheManager {
510
constructor(maxSize = 5) {
611
this.cacheMap = {};

utilities/tokenutils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ const checkAccess = (api_token) => {
44
return api_key === api_token;
55
}
66

7-
module.exports = {checkAccess}
7+
const validateToken = (req, res, next) => {
8+
if (!req.query.api_token)
9+
return res.status(401).json({ error: "Unauthorized" })
10+
11+
if (!checkAccess(req.query.api_token))
12+
return res.status(401).json({ error: "Unauthorized" })
13+
14+
next();
15+
}
16+
17+
module.exports = {checkAccess, validateToken}

utilities/validation.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const guidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
2+
const discordIdRegex = /^\d{17,19}$/;
3+
4+
const validateGuid = async (guidStr) => {
5+
return guidRegex.test(guidStr);
6+
}
7+
8+
const validateDiscordId = async (discordId) => {
9+
return discordIdRegex.test(discordId);
10+
}
11+
12+
module.exports = {
13+
validateGuid,
14+
validateDiscordId,
15+
};

0 commit comments

Comments
 (0)