Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(web): guard t() against prototype keys and check the dictionary in CI

BaaRaid577d7e3b2dc
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f949165..f58189f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -149,6 +149,7 @@ jobs:
- run: npm run test:polling
- run: npm run test:replay
- run: npm run test:upload
+ - run: npm run test:i18n
- run: npm run build:static
python:
diff --git a/web/package.json b/web/package.json
index 4c823af..f5265be 100644
--- a/web/package.json
+++ b/web/package.json
@@ -15,7 +15,8 @@
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts",
"test:polling": "node --test scripts/polling.test.mjs",
"test:replay": "node --test scripts/replay.test.mjs",
- "test:upload": "node --test scripts/upload.test.mjs"
+ "test:upload": "node --test scripts/upload.test.mjs",
+ "test:i18n": "node scripts/check-i18n.mjs"
},
"lint-staged": {
"*.{js,ts,jsx,tsx}": [
diff --git a/web/scripts/check-i18n.mjs b/web/scripts/check-i18n.mjs
new file mode 100644
index 0000000..149308b
--- /dev/null
+++ b/web/scripts/check-i18n.mjs
@@ -0,0 +1,64 @@
+import { readFileSync, readdirSync, statSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+// Every literal t("...") / t('...') key must exist in the locale dictionary
+// with the same {placeholders}. Dynamic keys (variables, template literals
+// with ${}) are skipped: there is nothing to compare them against.
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const dict = JSON.parse(readFileSync(join(root, "src/i18n/locales/tr.json"), "utf8"));
+
+const files = [];
+function walk(dir) {
+ for (const name of readdirSync(dir)) {
+ const p = join(dir, name);
+ const st = statSync(p);
+ if (st.isDirectory()) walk(p);
+ else if (/\.(ts|tsx)$/.test(name)) files.push(p);
+ }
+}
+walk(join(root, "src"));
+
+const calls = new Map(); // key -> first file that uses it
+const literalCall = /(?:^|[^\w$.])t\(\s*(?:"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)')/gm;
+for (const file of files) {
+ const src = readFileSync(file, "utf8");
+ for (const m of src.matchAll(literalCall)) {
+ const key =
+ m[1] !== undefined ? JSON.parse(`"${m[1]}"`) : m[2].replace(/\\(['\\])/g, "$1");
+ if (!calls.has(key)) calls.set(key, file.slice(root.length + 1));
+ }
+}
+
+function placeholders(s) {
+ return [...s.matchAll(/\{(\w+)\}/g)]
+ .map((m) => m[1])
+ .sort()
+ .join(",");
+}
+
+const missing = [];
+const mismatch = [];
+for (const [key, file] of calls) {
+ if (!Object.prototype.hasOwnProperty.call(dict, key)) {
+ missing.push(`${file}: ${JSON.stringify(key)}`);
+ continue;
+ }
+ if (placeholders(dict[key]) !== placeholders(key)) {
+ mismatch.push(`${JSON.stringify(key)}: source {${placeholders(key)}} dict {${placeholders(dict[key])}}`);
+ }
+}
+
+if (missing.length || mismatch.length) {
+ if (missing.length) {
+ console.error(`Missing tr.json entries (${missing.length}):\n${missing.join("\n")}`);
+ }
+ if (mismatch.length) {
+ console.error(`Placeholder mismatches (${mismatch.length}):\n${mismatch.join("\n")}`);
+ }
+ process.exit(1);
+}
+
+console.log(
+ `i18n: ${calls.size} literal keys checked against ${Object.keys(dict).length} tr.json entries`,
+);
diff --git a/web/src/i18n/index.ts b/web/src/i18n/index.ts
index d5b68d8..41aee64 100644
--- a/web/src/i18n/index.ts
+++ b/web/src/i18n/index.ts
@@ -59,9 +59,11 @@ const dict = DICTIONARIES[locale];
export type TVars = Record<string, string | number>;
export function t(key: string, vars?: TVars): string {
- const s = dict[key] ?? key;
+ const s = Object.prototype.hasOwnProperty.call(dict, key) ? dict[key] : key;
if (!vars) return s;
- return s.replace(/\{(\w+)\}/g, (m, name: string) => (name in vars ? String(vars[name]) : m));
+ return s.replace(/\{(\w+)\}/g, (m, name: string) =>
+ Object.prototype.hasOwnProperty.call(vars, name) ? String(vars[name]) : m,
+ );
}
export function setLocale(next: Locale) {