Skip to content

Commit 90e5446

Browse files
committed
add privateID username check
- bump AGPL to package-lock
1 parent dc0bde0 commit 90e5446

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/routes/setUsername.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export async function setUsername(req: Request, res: Response): Promise<Response
3232
// eslint-disable-next-line no-control-regex
3333
userName = userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
3434

35+
// check privateID against publicID
36+
if (!await checkPrivateUsername(userName, userID)) {
37+
return res.sendStatus(400);
38+
}
39+
3540
if (adminUserIDInput != undefined) {
3641
//this is the admin controlling the other users account, don't hash the controling account's ID
3742
adminUserIDInput = await getHashCache(adminUserIDInput);
@@ -88,3 +93,13 @@ export async function setUsername(req: Request, res: Response): Promise<Response
8893
return res.sendStatus(500);
8994
}
9095
}
96+
97+
async function checkPrivateUsername(username: string, userID: string): Promise<boolean> {
98+
const userIDHash = await getHashCache(userID);
99+
const userNameHash = await getHashCache(username);
100+
if (userIDHash == userNameHash) return false;
101+
const sponsorTimeRow = await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ? LIMIT 1`, [userNameHash]);
102+
const userNameRow = await db.prepare("get", `SELECT "userID" FROM "userNames" WHERE "userID" = ? LIMIT 1`, [userNameHash]);
103+
if ((sponsorTimeRow || userNameRow)?.userID) return false;
104+
return true;
105+
}

test/cases/setUsername.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ const user07PrivateUserID = "setUsername_07";
2222
const username07 = "Username 07";
2323
const user08PrivateUserID = "setUsername_08";
2424

25+
// private = public cases
26+
// user09 - username === privateID
27+
const user09PrivateUserID = "setUsername_09";
28+
// user 10/11 - user 11 username === user 10 privateID
29+
const user10PrivateUserID = "setUsername_10_collision";
30+
const username10 = "setUsername_10";
31+
const user11PrivateUserID = "setUsername_11";
32+
const user12PrivateUserID = "setUsername_12";
33+
const username12 = "Username 12";
34+
2535
async function addUsername(userID: string, userName: string, locked = 0) {
2636
await db.prepare("run", 'INSERT INTO "userNames" ("userID", "userName", "locked") VALUES(?, ?, ?)', [userID, userName, locked]);
2737
await addLogUserNameChange(userID, userName);
2838
}
2939

30-
async function getUsernameInfo(userID: string): Promise<{ userName: string, locked: string }> {
40+
async function getUsernameInfo(userID: string): Promise<{ userName: string, locked: string}> {
3141
const row = await db.prepare("get", 'SELECT "userName", "locked" FROM "userNames" WHERE "userID" = ?', [userID]);
3242
if (!row) {
3343
return null;
@@ -88,6 +98,10 @@ describe("setUsername", () => {
8898
await addUsername(getHash(user05PrivateUserID), username05, 0);
8999
await addUsername(getHash(user06PrivateUserID), username06, 0);
90100
await addUsername(getHash(user07PrivateUserID), username07, 1);
101+
await addUsername(getHash(user07PrivateUserID), username07, 0);
102+
await addUsername(getHash(user10PrivateUserID), username10, 0);
103+
// user11 skipped
104+
await addUsername(getHash(user12PrivateUserID), username12, 0);
91105
});
92106

93107
it("Should be able to set username that has never been set", (done) => {
@@ -240,6 +254,44 @@ describe("setUsername", () => {
240254
const usernameInfo = await getUsernameInfo(getHash(user08PrivateUserID));
241255
assert.strictEqual(usernameInfo, null);
242256
done();
243-
});
257+
})
258+
.catch((err) => done(err));
259+
});
260+
261+
it("Should return error if trying to set username to privateID", (done) => {
262+
const privateID = user09PrivateUserID;
263+
postSetUserName(privateID, privateID)
264+
.then(async (res) => {
265+
assert.strictEqual(res.status, 400);
266+
const usernameInfo = await getUsernameInfo(getHash(privateID));
267+
assert.strictEqual(usernameInfo, null);
268+
done();
269+
})
270+
.catch((err) => done(err));
271+
});
272+
273+
it("Should return error if trying to set username to someone else's privateID", (done) => {
274+
const privateID = user11PrivateUserID;
275+
postSetUserName(privateID, user10PrivateUserID)
276+
.then(async (res) => {
277+
assert.strictEqual(res.status, 400);
278+
const usernameInfo = await getUsernameInfo(getHash(privateID)); // user 10's privateID
279+
assert.strictEqual(usernameInfo, null);
280+
done();
281+
})
282+
.catch((err) => done(err));
283+
});
284+
285+
it("Should not return error if trying to set username to someone else's publicID", (done) => {
286+
const privateID = user12PrivateUserID;
287+
const user10PublicID = getHash(user10PrivateUserID);
288+
postSetUserName(privateID, user10PublicID)
289+
.then(async (res) => {
290+
assert.strictEqual(res.status, 200);
291+
const usernameInfo = await getUsernameInfo(getHash(privateID)); // user 10's publicID
292+
assert.strictEqual(usernameInfo.userName, user10PublicID);
293+
done();
294+
})
295+
.catch((err) => done(err));
244296
});
245297
});

0 commit comments

Comments
 (0)