Avoid ambigous characters in IDs

The issue with ambigous characters is that they are hard to write
when only seeing them, as one could confuse 0 for O with certain
typefaces for example.
Ambiguity is lessened by removing `Oo0Iil1` from the possible charset.
In order to keep a big enough pool of IDs the ID length changed from 6
to 8 characters instead, which seems like an OK tradeoff.
This commit is contained in:
Wolvan 2021-12-29 14:13:35 +01:00
parent 9d80a009ca
commit 584d0092d8

View file

@ -17,7 +17,7 @@ type Poll = {
creationTime: Date, creationTime: Date,
}; };
function randomString(length = 10, charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") { function randomString(length = 10, charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789") {
let result = ""; let result = "";
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
result += charset.charAt(Math.floor(Math.random() * charset.length)); result += charset.charAt(Math.floor(Math.random() * charset.length));
@ -76,7 +76,7 @@ export default async function init(router: Router): Promise<void> {
const options = req.body.options; const options = req.body.options;
if (!Array.isArray(options) || options.filter(i => i).length < 2) if (!Array.isArray(options) || options.filter(i => i).length < 2)
return res.status(400).json({ error: "Options must be an array and have at least 2 entries" }); return res.status(400).json({ error: "Options must be an array and have at least 2 entries" });
let id = randomString(6); let id = randomString(8);
while (await polls.getItem(id)) id = randomString(6); while (await polls.getItem(id)) id = randomString(6);
await polls.setItem(id, {}); await polls.setItem(id, {});
const dupeCheckMode = ["none", "ip", "cookie"].includes(req.body.dupeCheckMode) ? req.body.dupeCheckMode : "ip"; const dupeCheckMode = ["none", "ip", "cookie"].includes(req.body.dupeCheckMode) ? req.body.dupeCheckMode : "ip";