Skip to content

Commit 912b1e8

Browse files
committed
Added Sanitizing to ensure alphanum ID values
1 parent 336f502 commit 912b1e8

1 file changed

Lines changed: 41 additions & 35 deletions

File tree

src/backend/db.js

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function Generate_Checksum() {
132132
return Hash_SHA256(crypto.randomBytes(64).toString('hex'));
133133
}
134134

135+
function SanitizeAlphaNumeric(input) {
136+
return input.replace(/[^a-zA-Z0-9]/g, '');
137+
}
138+
135139
async function GenerateJWT(username, email) {
136140
username = SanitizeString(username);
137141
email = SanitizeString(email);
@@ -207,7 +211,7 @@ async function GenerateAdminJWT(username) {
207211
async function UpdateTeamCompletions(team_id) {
208212
if (team_id === "None" || !team_id) return;
209213
console.log("[*] Attempting to Update Team Completions. . .");
210-
let teamProfile = await TeamCollection.findOne({ _id: team_id });
214+
let teamProfile = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(team_id) });
211215
if (teamProfile) {
212216
const mergedCompletions = []; // Initialize as an array of objects
213217
const teamMembers = teamProfile.members;
@@ -220,7 +224,7 @@ async function UpdateTeamCompletions(team_id) {
220224
// remove entries within TeamCollections.completions that contain
221225
// memberIds that are not contained in the teamMembers Array
222226
await TeamCollection.updateOne(
223-
{ _id: team_id },
227+
{ _id: SanitizeAlphaNumeric(team_id) },
224228
{
225229
$pull: {
226230
completions: {
@@ -231,11 +235,11 @@ async function UpdateTeamCompletions(team_id) {
231235
);
232236

233237
// reference update after a modification
234-
teamProfile = await TeamCollection.findOne({ _id: team_id });
238+
teamProfile = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(team_id) });
235239
console.log("AFTER: ", teamProfile.completions);
236240

237241
for (const memberId of teamMembers) {
238-
const memberProfile = await UserCollection.findOne({ _id: memberId });
242+
const memberProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(memberId) });
239243

240244
if (!memberProfile || !memberProfile.completions) {
241245
continue;
@@ -246,7 +250,7 @@ async function UpdateTeamCompletions(team_id) {
246250
const [index, { id, time }] = data; // break down the entry
247251
// console.log("Completion Data -> ", { name, time });
248252

249-
const challengeProfile = await ChallengeCollection.findOne({ _id: id })
253+
const challengeProfile = await ChallengeCollection.findOne({ _id: SanitizeAlphaNumeric(id) })
250254
if (challengeProfile) {
251255
// Find if the challenge already exists in mergedCompletions
252256
const existingChallenge = mergedCompletions.find(completion => completion.id === id);
@@ -272,7 +276,7 @@ async function UpdateTeamCompletions(team_id) {
272276

273277
// Update the team completions as an array
274278
await TeamCollection.updateOne(
275-
{ _id: team_id },
279+
{ _id: SanitizeAlphaNumeric(team_id) },
276280
{ $set: { completions: mergedCompletions } }
277281
);
278282

@@ -342,7 +346,7 @@ async function GetLeaderboardData() {
342346
user.name = user.username;
343347

344348
for (const completion of user.completions) {
345-
const challengeProfile = await ChallengeCollection.findOne({ _id: completion.id });
349+
const challengeProfile = await ChallengeCollection.findOne({ _id: SanitizeAlphaNumeric(completion.id) });
346350
if (challengeProfile) {
347351
user.points += challengeProfile.points;
348352
}
@@ -561,7 +565,7 @@ async function GetUserProfile(username) {
561565
}
562566

563567
if (userRecord.team_id !== "None") {
564-
const teamRecord = await TeamCollection.findOne({ _id: userRecord.team_id });
568+
const teamRecord = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(userRecord.team_id) });
565569

566570
// check if the connection matches
567571
if (teamRecord) {
@@ -680,11 +684,11 @@ async function GetTeamInfo(username) {
680684
return null;
681685
}
682686

683-
const teamRecord = await TeamCollection.findOne({ _id: userProfile.team_id });
687+
const teamRecord = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(userProfile.team_id) });
684688

685689
// null | { ... }
686690
if (teamRecord) {
687-
const leader_record = await UserCollection.findOne({ _id: teamRecord.team_leader_id });
691+
const leader_record = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(teamRecord.team_leader_id) });
688692
const members_list = await FetchMemberNames(teamRecord.members);
689693

690694
if (leader_record) {
@@ -704,7 +708,7 @@ async function GetTeamInfo(username) {
704708
// need to ensure this Array population finishes before returning
705709
const join_requests = await Promise.all(
706710
requests.map(async (request) => {
707-
const sender_profile = await UserCollection.findOne({ _id: request.sender_id });
711+
const sender_profile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(request.sender_id) });
708712
if (sender_profile) {
709713
return {
710714
"_id": request._id,
@@ -786,7 +790,7 @@ async function CreateTeam(team_creator, team_name) {
786790

787791
// update leader_record to show theyre on a team
788792
const leaderUpdate = await UserCollection.updateOne(
789-
{ _id: leader_id },
793+
{ _id: SanitizeAlphaNumeric(leader_id) },
790794
{ $set: { team_id: addNewTeam._id } }
791795
);
792796

@@ -831,7 +835,7 @@ async function UpdateTeam(team_creator, new_team_name) {
831835
const leader_record = await UserCollection.findOne({ username: team_creator });
832836
if (leader_record) {
833837
// check if the team_exists
834-
const team_exists = await TeamCollection.findOne({ _id: leader_record.team_id })
838+
const team_exists = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(leader_record.team_id) })
835839
if (team_exists) {
836840
// check if the name matches
837841
if (new_team_name === team_exists.name) {
@@ -851,7 +855,7 @@ async function UpdateTeam(team_creator, new_team_name) {
851855

852856
// update the name attribute of the team entry
853857
const updateTeamName = await TeamCollection.updateOne(
854-
{ _id: leader_record.team_id },
858+
{ _id: SanitizeAlphaNumeric(leader_record.team_id) },
855859
{ $set: { name: new_team_name } }
856860
);
857861

@@ -986,13 +990,13 @@ async function AddMember(request_id, checksum) {
986990

987991
// find the request object that has matching attributes
988992
// to request_id and checksum
989-
const joinRequest = await TeamRequestCollection.findOne({ _id: request_id, checksum: checksum })
993+
const joinRequest = await TeamRequestCollection.findOne({ _id: SanitizeAlphaNumeric(request_id), checksum: checksum })
990994
if (joinRequest) {
991995
console.log("[+] Found Request Object!");
992996

993997
// if there are already 3 members we need to drop this
994998
// addMember request
995-
const teamProfile = await TeamCollection.findOne({ _id: joinRequest.team_id })
999+
const teamProfile = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(joinRequest.team_id) })
9961000
if (teamProfile) {
9971001
if (teamProfile.members.length === 3) {
9981002
console.log("[-] This Team has reached Maximum number of Members!");
@@ -1005,7 +1009,7 @@ async function AddMember(request_id, checksum) {
10051009

10061010
// add the sender_id into the team object where _id matches team_id
10071011
const insertNewMember = await TeamCollection.updateOne(
1008-
{ _id: joinRequest.team_id },
1012+
{ _id: SanitizeAlphaNumeric(joinRequest.team_id) },
10091013
{ $addToSet: { members: joinRequest.sender_id } }
10101014
);
10111015

@@ -1016,7 +1020,7 @@ async function AddMember(request_id, checksum) {
10161020

10171021
// update sender_id user object to show they are on the team
10181022
const updateMemberProfile = await UserCollection.updateOne(
1019-
{ _id: joinRequest.sender_id },
1023+
{ _id: SanitizeAlphaNumeric(joinRequest.sender_id) },
10201024
{ $set: { team_id: joinRequest.team_id } }
10211025
);
10221026

@@ -1026,7 +1030,7 @@ async function AddMember(request_id, checksum) {
10261030
}
10271031

10281032
// remove all join requests that match sender_id
1029-
const result = await TeamRequestCollection.deleteMany({ sender_id: joinRequest.sender_id });
1033+
const result = await TeamRequestCollection.deleteMany({ sender_id: SanitizeAlphaNumeric(joinRequest.sender_id) });
10301034
if (result) {
10311035
console.log(`[*] ${result.deletedCount} requests sent by "${joinRequest.sender_id}" were deleted`);
10321036
}
@@ -1082,7 +1086,7 @@ async function RemoveMember(member_username, jwt) {
10821086
// update the team profile and remove the member from the
10831087
// members Array
10841088
const removeMember = await TeamCollection.updateOne(
1085-
{ _id: memberProfile.team_id },
1089+
{ _id: SanitizeAlphaNumeric(memberProfile.team_id) },
10861090
{ $pull: { members: member_id } }
10871091
);
10881092

@@ -1094,7 +1098,7 @@ async function RemoveMember(member_username, jwt) {
10941098
// update the user profile of member_username and set their
10951099
// team attribute to None
10961100
const updateMemberProfile = await UserCollection.updateOne(
1097-
{ _id: memberProfile._id },
1101+
{ _id: SanitizeAlphaNumeric(memberProfile._id) },
10981102
{ $set: { team_id: "None" } }
10991103
);
11001104

@@ -1118,7 +1122,7 @@ async function SetNewLeader(teamProfile) {
11181122
let maxCompletions = -1;
11191123

11201124
for (const member_id of teamProfile.members) {
1121-
const memberProfile = await UserCollection.findOne({ _id: member_id });
1125+
const memberProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(member_id) });
11221126
if (memberProfile && Array.isArray(memberProfile.completions)) {
11231127
const numCompletions = memberProfile.completions.length;
11241128

@@ -1241,7 +1245,7 @@ async function ValidateFlag(challenge_id, flag_value, jwt) {
12411245
}
12421246

12431247
// find the challenge object based off the id
1244-
const chall = await ChallengeCollection.findOne({ _id: challenge_id })
1248+
const chall = await ChallengeCollection.findOne({ _id: SanitizeAlphaNumeric(challenge_id) })
12451249
if (chall) {
12461250
// check if the user has already claimed the flag:
12471251
// before doing an insert check if there is an object with
@@ -1314,7 +1318,8 @@ async function ConvertCompletions(userCompletions, teamCompletions) {
13141318
// create new attribute name based on id attribute
13151319
let readableUserCompletions = userCompletions;
13161320
for (let item of readableUserCompletions) {
1317-
const challengeProfile = await ChallengeCollection.findOne({ _id: item.id })
1321+
// sanitize the item (alphanum)
1322+
const challengeProfile = await ChallengeCollection.findOne({ _id: SanitizeAlphaNumeric(item.id) })
13181323
if (challengeProfile) {
13191324
item['name'] = challengeProfile.name;
13201325
}
@@ -1324,7 +1329,8 @@ async function ConvertCompletions(userCompletions, teamCompletions) {
13241329
if (teamCompletions && teamCompletions.length > 0) {
13251330
// Iterate through data and modify memberId using for...of to handle async correctly
13261331
for (let item of teamCompletions) {
1327-
const memberProfile = await UserCollection.findOne({ _id: item.memberId });
1332+
// sanitize the item (alphanum)
1333+
const memberProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(item.memberId) });
13281334
if (memberProfile) {
13291335
const memberUsername = memberProfile.username;
13301336
// Replace memberId with memberName
@@ -1333,7 +1339,7 @@ async function ConvertCompletions(userCompletions, teamCompletions) {
13331339
}
13341340

13351341
// create new attribute name based on id attribute
1336-
const challengeProfile = await ChallengeCollection.findOne({ _id: item.id })
1342+
const challengeProfile = await ChallengeCollection.findOne({ _id: SanitizeAlphaNumeric(item.id) })
13371343
if (challengeProfile) {
13381344
item['name'] = challengeProfile.name;
13391345
}
@@ -1453,7 +1459,7 @@ async function GetAllUsers() {
14531459
// resolve team_id to team name for readability
14541460
const team_id = user.team_id;
14551461
if (team_id !== "None") {
1456-
const teamProfile = await TeamCollection.findOne({ _id: team_id })
1462+
const teamProfile = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(team_id) })
14571463
if (teamProfile) {
14581464
// console.log(teamProfile.name);
14591465
user.team_id = teamProfile.name;
@@ -1475,7 +1481,7 @@ async function GetAllTeams() {
14751481
// resolve members _id to usernames for readability
14761482
let members = [];
14771483
for (let user_id of team.members) {
1478-
const userProfile = await UserCollection.findOne({ _id: user_id });
1484+
const userProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(user_id) });
14791485
if (userProfile) {
14801486
members.push(userProfile.username);
14811487
} else {
@@ -1484,7 +1490,7 @@ async function GetAllTeams() {
14841490
}
14851491

14861492
console.log("Team LeaderID: " + team.team_leader_id)
1487-
const leaderProfile = await UserCollection.findOne({ _id: team.team_leader_id });
1493+
const leaderProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(team.team_leader_id) });
14881494
if (leaderProfile) {
14891495
members.push(leaderProfile.username);
14901496
} else {
@@ -1506,7 +1512,7 @@ async function RemoveTeam(team_id) {
15061512
}
15071513

15081514
// change every member and leader team_id to None before deletion
1509-
const teamProfile = await TeamCollection.findOne({ _id: team_id.toString() })
1515+
const teamProfile = await TeamCollection.findOne({ _id: SanitizeAlphaNumeric(team_id.toString()) })
15101516
if (teamProfile) {
15111517
// remove all join requests to this team
15121518
await TeamRequestCollection.deleteMany({ team_id: teamProfile._id.toString() });
@@ -1545,7 +1551,7 @@ async function RemoveTeam(team_id) {
15451551

15461552
async function RemoveUser(user_id) {
15471553
// if the user is a team leader we need to update the team
1548-
const userProfile = await UserCollection.findOne({ _id: user_id })
1554+
const userProfile = await UserCollection.findOne({ _id: SanitizeAlphaNumeric(user_id) })
15491555
if (userProfile && userProfile.team_id !== "None") {
15501556
const teamProfile = await TeamCollection.findOne({ _id: userProfile.team_id })
15511557

@@ -1561,7 +1567,7 @@ async function RemoveUser(user_id) {
15611567
}
15621568
}
15631569

1564-
const action = await UserCollection.deleteOne({ _id: user_id });
1570+
const action = await UserCollection.deleteOne({ _id: SanitizeAlphaNumeric(user_id) });
15651571
if (action.deletedCount === 1) {
15661572
console.log("User Deleted!")
15671573
return { "acknowledge":true, "message":"User Deleted Successfully!" }
@@ -1585,9 +1591,9 @@ async function UpdateChallenge(data) {
15851591
*/
15861592

15871593
const action = await ChallengeCollection.updateOne(
1588-
{ _id: data.challenge_id },
1594+
{ _id: SanitizeAlphaNumeric(data.challenge_id) },
15891595
{ $set: {
1590-
"challenge_id": data.challenge_id,
1596+
"challenge_id": SanitizeAlphaNumeric(data.challenge_id),
15911597
"name": data.name,
15921598
"description": data.description,
15931599
"category": data.category,
@@ -1698,7 +1704,7 @@ async function DeleteChallenge(data, adminUsername) {
16981704
}
16991705

17001706
console.log("Attempting to Delete Challenge Entry from DB. . .")
1701-
const action = await ChallengeCollection.deleteOne({ _id: data.challenge_id })
1707+
const action = await ChallengeCollection.deleteOne({ _id: SanitizeAlphaNumeric(data.challenge_id) })
17021708

17031709
if (action.acknowledged && action.deletedCount !== 0) {
17041710
console.log("Deleted Challenge")

0 commit comments

Comments
 (0)