Skip to content

Commit 3475a80

Browse files
codergautamclaude
andcommitted
fix: Add type validation for username in eloRank and leaderboard APIs
- eloRank.js: Add typeof check for username parameter - leaderboard.js: Add typeof check for myUsername query param Prevents NoSQL injection via ?username[$ne]=foo query strings. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ec56163 commit 3475a80

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

api/eloRank.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default async function handler(req, res) {
3333
try {
3434

3535
let user;
36-
if(username) {
36+
if(username && typeof username === 'string') {
37+
// Prevent NoSQL injection - username must be a string
3738
user = await User.findOne({ username: username }).collation(USERNAME_COLLATION).cache(120);
3839
} else if(secret && typeof secret === 'string') {
3940
// Prevent NoSQL injection - secret must be a string

api/leaderboard.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ export default async function handler(req, res) {
113113
const isXp = req.query.mode === 'xp';
114114
console.log(`[API] leaderboard: mode=${isXp ? 'xp' : 'elo'}, pastDay=${pastDay}, user=${myUsername || 'none'}`);
115115

116+
// Prevent NoSQL injection - username must be a string if provided
117+
if (myUsername && typeof myUsername !== 'string') {
118+
return res.status(400).json({ message: 'Invalid username' });
119+
}
120+
116121
if (req.method !== 'GET') {
117122
return res.status(405).json({ message: 'Method not allowed' });
118123
}

0 commit comments

Comments
 (0)