Skip to content

Commit 97464c9

Browse files
committed
fix: avoid native i18n branch regex backtracking
1 parent e3f46d0 commit 97464c9

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎scripts/native-app-i18n.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,42 @@ const EXCLUDED_FILE_RE = /(?:Tests?|UITests?|Previews?|Testing)\.(?:swift|kt|kts
170170
const BUILD_SETTING_RE = /\$\([A-Za-z0-9_.-]+\)/gu;
171171
const NATIVE_I18N_LOCALE_SET = new Set<string>(NATIVE_I18N_LOCALES);
172172

173+
function isAsciiLowercaseLetter(character: string): boolean {
174+
return character >= "a" && character <= "z";
175+
}
176+
177+
function isAsciiUppercaseLetter(character: string): boolean {
178+
return character >= "A" && character <= "Z";
179+
}
180+
181+
function isAsciiAlphaNumeric(character: string): boolean {
182+
return (
183+
isAsciiLowercaseLetter(character) ||
184+
isAsciiUppercaseLetter(character) ||
185+
(character >= "0" && character <= "9")
186+
);
187+
}
188+
189+
export function isConditionalBranchIdentifier(source: string): boolean {
190+
let index = 0;
191+
while (index < source.length && isAsciiLowercaseLetter(source[index])) {
192+
index += 1;
193+
}
194+
195+
// Keep this scanner linear: PR-controlled native source passes through CI,
196+
// so a backtracking regex here can become a cheap native-i18n DoS trigger.
197+
if (index === 0 || index >= source.length || !isAsciiUppercaseLetter(source[index])) {
198+
return false;
199+
}
200+
201+
for (index += 1; index < source.length; index += 1) {
202+
if (!isAsciiAlphaNumeric(source[index])) {
203+
return false;
204+
}
205+
}
206+
return true;
207+
}
208+
173209
function isTranslatableCandidate(source: string, kind: string): boolean {
174210
if (BUILD_SETTING_RE.test(source)) {
175211
BUILD_SETTING_RE.lastIndex = 0;
@@ -180,7 +216,7 @@ function isTranslatableCandidate(source: string, kind: string): boolean {
180216
if (!isDirectUiText && (/^[a-z0-9_.:/$-]+$/u.test(source) || /^[A-Z0-9_.:/$-]+$/u.test(source))) {
181217
return false;
182218
}
183-
if (kind === "conditional-branch" && /^[a-z]+(?:[A-Z][A-Za-z0-9]*)+$/u.test(source)) {
219+
if (kind === "conditional-branch" && isConditionalBranchIdentifier(source)) {
184220
return false;
185221
}
186222
if (/[{}[\]]/u.test(source) && !/(?:\\\(|\$\{)/u.test(source)) {

‎test/scripts/native-app-i18n.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import { describe, expect, it } from "vitest";
44
import {
55
collectNativeI18nEntries,
6+
isConditionalBranchIdentifier,
67
NATIVE_I18N_LOCALES,
78
parseNativeI18nCommand,
89
syncNativeLocale,
@@ -11,6 +12,14 @@ import {
1112
import { cleanupTempDirs, makeTempDir } from "../helpers/temp-dir.js";
1213

1314
describe("native app i18n inventory", () => {
15+
it("detects conditional branch identifiers without regex backtracking", () => {
16+
expect(isConditionalBranchIdentifier("isEnabled")).toBe(true);
17+
expect(isConditionalBranchIdentifier("hasFA2Enabled")).toBe(true);
18+
expect(isConditionalBranchIdentifier("abc123A")).toBe(false);
19+
expect(isConditionalBranchIdentifier("already_lowercase")).toBe(false);
20+
expect(isConditionalBranchIdentifier(`a${"A".repeat(4_096)}!`)).toBe(false);
21+
});
22+
1423
it("collects stable Android and Apple UI entries", async () => {
1524
const entries = await collectNativeI18nEntries();
1625
const surfaces = new Set(entries.map((entry) => entry.surface));

0 commit comments

Comments
 (0)