fix(accounts): a display name may not read as another account's
A member kept renaming themselves to a colleague's display name, plus "(you)", so the CPU/RAM card and every owner list showed two of that person. A display name is now refused (409 ENAMETAKEN) when, with marks, case, spaces, punctuation, bracketed parts and look-alike Cyrillic/Greek letters ignored, it equals another account's display name or username — in Settings → Account and on the Users page alike. Keeping the name one already has stays allowed, so an older clash does not block an admin's other edits.
This commit is contained in:
+45
-1
@@ -20,7 +20,8 @@
|
||||
* admin on the Users page (`server/routes/users.js`), where every change
|
||||
* needs the admin's own password again (`confirmPassword`, throttled).
|
||||
* An account also carries an optional display name and email
|
||||
* (`validateProfile`), which its owner edits in Settings → Account without
|
||||
* (`validateProfile`; no two accounts may read as one name — `assertNameFree`,
|
||||
* ENAMETAKEN), which its owner edits in Settings → Account without
|
||||
* that step-up; changing their OWN password there needs the old one and
|
||||
* keeps the login it was changed from (`setPassword`'s `keepToken`).
|
||||
* - Two password bars: an admin may GIVE any non-empty password (a create or a
|
||||
@@ -247,6 +248,47 @@ function validateProfile(body) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Latin look-alikes from other scripts, so "Vĩ" in Cyrillic still collides.
|
||||
const CONFUSABLE = {
|
||||
а: "a", в: "b", е: "e", к: "k", м: "m", н: "h", о: "o", р: "p", с: "c", т: "t",
|
||||
у: "y", х: "x", і: "i", ј: "j", ԁ: "d", ѕ: "s", ӏ: "l", α: "a", β: "b", ε: "e",
|
||||
η: "n", ι: "i", κ: "k", ν: "v", ο: "o", ρ: "p", τ: "t", υ: "u", χ: "x", "ı": "i",
|
||||
};
|
||||
|
||||
/**
|
||||
* The name two accounts may not share: no marks (Vĩ = Vi), đ = d, any case,
|
||||
* look-alike letters folded, and only letters and digits outside brackets —
|
||||
* so "Nguyễn Ngọc Trí Vĩ (you)" and "nguyen.ngoc-tri vi" are both one name.
|
||||
*/
|
||||
function nameKey(s) {
|
||||
return String(s || "")
|
||||
.normalize("NFKD")
|
||||
.replace(/\p{M}/gu, "")
|
||||
.toLowerCase()
|
||||
.replace(/\([^)]*\)|\[[^\]]*\]/g, "")
|
||||
.replace(/[đð]/g, "d")
|
||||
.replace(/./gu, (c) => CONFUSABLE[c] ?? c)
|
||||
.replace(/[^\p{L}\p{N}]/gu, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuse (ENAMETAKEN) a display name that reads as another account's display
|
||||
* name or username, so no one can pose as a colleague. Keeping the name one
|
||||
* already has is always allowed — a clash from before this rule is no reason
|
||||
* to block an admin's unrelated edit.
|
||||
*/
|
||||
function assertNameFree(userId, fields) {
|
||||
if (!("display_name" in fields) || !fields.display_name) return;
|
||||
const key = nameKey(fields.display_name);
|
||||
if (!key) return;
|
||||
const own = sql("SELECT display_name FROM users WHERE id = ?").get(userId);
|
||||
if (own && own.display_name === fields.display_name) return;
|
||||
const rows = sql("SELECT id, username, display_name FROM users WHERE id != ?").all(userId);
|
||||
if (rows.some((u) => nameKey(u.username) === key || nameKey(u.display_name) === key)) {
|
||||
throw err("ENAMETAKEN", "displayName: another account already uses that name");
|
||||
}
|
||||
}
|
||||
|
||||
/** Write `validateProfile`'s result for one account. */
|
||||
function setProfile(userId, fields) {
|
||||
for (const [col, value] of Object.entries(fields)) {
|
||||
@@ -812,6 +854,8 @@ module.exports = {
|
||||
unassignWorkspace,
|
||||
ownedWorkspaces,
|
||||
validateProfile,
|
||||
nameKey,
|
||||
assertNameFree,
|
||||
setProfile,
|
||||
validatePassword,
|
||||
validateOwnPassword,
|
||||
|
||||
Reference in New Issue
Block a user