From d577d7e3b2dcc5c47751755e85be276c526d985d Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:40:49 +0100 Subject: [PATCH] fix(web): guard t() against prototype keys and check the dictionary in CI t() looked a key up with dict[key] ?? key, so t("constructor") returned Object and t("toString", {}) threw. Backend values are used as keys, so this was reachable. Use an own-property check for the dictionary and the vars, and commit the key-check script as npm run test:i18n in the web job so tr.json cannot drift. --- .github/workflows/ci.yml | 1 + web/package.json | 3 +- web/scripts/check-i18n.mjs | 64 ++++++++++++++++++++++++++++++++++++++ web/src/i18n/index.ts | 6 ++-- 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 web/scripts/check-i18n.mjs 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; 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) {