Skip to content

Commit fc832d0

Browse files
cyril59310autofix-ci[bot]CommanderStorm
authored
feat: Added a translation key for “Password is too weak (#6614)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Frank Elsinga <frank@elsinga.de>
1 parent 82c6b36 commit fc832d0

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

server/server.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const gracefulShutdown = require("http-graceful-shutdown");
7575
log.debug("server", "Importing prometheus-api-metrics");
7676
const prometheusAPIMetrics = require("prometheus-api-metrics");
7777
const { passwordStrength } = require("check-password-strength");
78+
const TranslatableError = require("./translatable-error");
7879

7980
log.debug("server", "Importing 2FA Modules");
8081
const notp = require("notp");
@@ -673,7 +674,7 @@ let needSetup = false;
673674
socket.on("setup", async (username, password, callback) => {
674675
try {
675676
if (passwordStrength(password).value === "Too weak") {
676-
throw new Error("Password is too weak. It should contain alphabetic and numeric characters. It must be at least 6 characters in length.");
677+
throw new TranslatableError("passwordTooWeak");
677678
}
678679

679680
if ((await R.knex("user").count("id as count").first()).count !== 0) {
@@ -697,6 +698,7 @@ let needSetup = false;
697698
callback({
698699
ok: false,
699700
msg: e.message,
701+
msgi18n: !!e.msgi18n,
700702
});
701703
}
702704
});
@@ -1410,7 +1412,7 @@ let needSetup = false;
14101412
}
14111413

14121414
if (passwordStrength(password.newPassword).value === "Too weak") {
1413-
throw new Error("Password is too weak. It should contain alphabetic and numeric characters. It must be at least 6 characters in length.");
1415+
throw new TranslatableError("passwordTooWeak");
14141416
}
14151417

14161418
let user = await doubleCheckPassword(socket, password.currentPassword);
@@ -1429,6 +1431,7 @@ let needSetup = false;
14291431
callback({
14301432
ok: false,
14311433
msg: e.message,
1434+
msgi18n: !!e.msgi18n,
14321435
});
14331436
}
14341437
});

server/translatable-error.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class TranslatableError extends Error {
2+
/**
3+
* Error whose message is a translation key.
4+
* @augments Error
5+
*/
6+
/**
7+
* Create a TranslatableError.
8+
* @param {string} key - Translation key present in src/lang/en.json
9+
*/
10+
constructor(key) {
11+
super(key);
12+
this.msgi18n = true;
13+
this.key = key;
14+
Error.captureStackTrace(this, this.constructor);
15+
}
16+
}
17+
module.exports = TranslatableError;

src/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@
13041304
"Show this Maintenance Message on which Status Pages": "Show this Maintenance Message on which Status Pages",
13051305
"Endpoint": "Endpoint",
13061306
"Details": "Details",
1307+
"passwordTooWeak": "Password is too weak. It should contain alphabetic and numeric characters. It must be at least 6 characters in length.",
13071308
"TLS Alerts": "TLS Alerts",
13081309
"Expected TLS Alert": "Expected TLS Alert",
13091310
"None (Successful Connection)": "None (Successful Connection)",

test/backend-test/check-translations.test.js

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ function* walk(dir) {
2020
}
2121
}
2222

23+
/**
24+
* Fallback to get start/end indices of a key within a line.
25+
* @param {string} line - Line of text to search in.
26+
* @param {string} key - Key to find.
27+
* @returns {[number, number]} Array [start, end] representing the indices of the key in the line.
28+
*/
29+
function getStartEnd(line, key) {
30+
let start = line.indexOf(key);
31+
if (start === -1) {
32+
start = 0;
33+
}
34+
return [ start, start + key.length ];
35+
}
36+
2337
describe("Check Translations", () => {
2438
it("should not have missing translation keys", () => {
2539
const enTranslations = JSON.parse(fs.readFileSync("src/lang/en.json", "utf-8"));
@@ -28,28 +42,53 @@ describe("Check Translations", () => {
2842
/// this check is just to save on maintainer energy to explain this on every review ^^
2943
const translationRegex = /\$t\(['"](?<key1>.*?)['"]\s*[,)]|i18n-t[^>]*\s+keypath="(?<key2>[^"]+)"/gd;
3044

45+
// detect server-side TranslatableError usage: new TranslatableError("key")
46+
const translatableErrorRegex = /new\s+TranslatableError\(\s*['"](?<key3>[^'"]+)['"]\s*\)/g;
47+
3148
const missingKeys = [];
3249

33-
for (const filePath of walk("src")) {
34-
if (filePath.endsWith(".vue") || filePath.endsWith(".js")) {
35-
const lines = fs.readFileSync(filePath, "utf-8").split("\n");
36-
lines.forEach((line, lineNum) => {
37-
let match;
38-
while ((match = translationRegex.exec(line)) !== null) {
39-
const key = match.groups.key1 || match.groups.key2;
40-
if (key && !enTranslations[key]) {
41-
const [ start, end ] = match.groups.key1 ? match.indices.groups.key1 : match.indices.groups.key2;
42-
missingKeys.push({
43-
filePath,
44-
lineNum: lineNum + 1,
45-
key,
46-
line: line,
47-
start,
48-
end,
49-
});
50+
const roots = [ "src", "server" ];
51+
52+
for (const root of roots) {
53+
for (const filePath of walk(root)) {
54+
if (filePath.endsWith(".vue") || filePath.endsWith(".js")) {
55+
const lines = fs.readFileSync(filePath, "utf-8").split("\n");
56+
lines.forEach((line, lineNum) => {
57+
let match;
58+
// front-end style keys ($t / i18n-t)
59+
while ((match = translationRegex.exec(line)) !== null) {
60+
const key = match.groups.key1 || match.groups.key2;
61+
if (key && !enTranslations[key]) {
62+
const [ start, end ] = getStartEnd(line, key);
63+
missingKeys.push({
64+
filePath,
65+
lineNum: lineNum + 1,
66+
key,
67+
line: line,
68+
start,
69+
end,
70+
});
71+
}
72+
}
73+
74+
// server-side TranslatableError usage
75+
let m;
76+
while ((m = translatableErrorRegex.exec(line)) !== null) {
77+
const key3 = m.groups.key3;
78+
if (key3 && !enTranslations[key3]) {
79+
const [ start, end ] = getStartEnd(line, key3);
80+
missingKeys.push({
81+
filePath,
82+
lineNum: lineNum + 1,
83+
key: key3,
84+
line: line,
85+
start,
86+
end,
87+
});
88+
}
5089
}
51-
}
52-
});
90+
});
91+
}
5392
}
5493
}
5594

0 commit comments

Comments
 (0)