mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
fix(scripts): make extract-tools-desc quote matching linear
`(?:[^']+|(?<=\\)')+` nests quantifiers and backtracks exponentially on long
unterminated values. Use one `'(?:[^'\\]|\\.)*'` pattern for both extracting
and replacing. Extraction used `(.*?)`, which stopped at the first escaped
quote (`'It\'s'` became `It\`); it now reads the whole value and decodes
JS string escapes (\n, \xHH, \uHHHH, \u{...}, \') before writing the
locale file.
Fixes CodeQL js/redos alerts in scripts/extract-tools-desc.mjs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1b8d4879ac
commit
a891be1a13
1 file changed
+17
-9
@@ -11,29 +11,37 @@ if (fs.existsSync(localesFile)) {
|
||||
localesData = yaml.parse(fs.readFileSync(localesFile, 'utf-8')) || {};
|
||||
}
|
||||
|
||||
// Single-quoted JS string that may contain escaped quotes. `[^'\\]|\\.` cannot overlap, so it cannot backtrack exponentially.
|
||||
const quotedValue = (key) => new RegExp(`\\b${key}: '((?:[^'\\\\]|\\\\.)*)'`);
|
||||
// Decode JS string escapes (\n, \x27, \u00e9, \u{1F600}, \') the way the JS parser would
|
||||
const JS_ESCAPES = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', v: '\v', 0: '\0' };
|
||||
const unescapeValue = (value) =>
|
||||
value.replace(/\\(?:u\{([\da-fA-F]+)\}|u([\da-fA-F]{4})|x([\da-fA-F]{2})|(.))/g, (_, codePoint, u4, x2, char) =>
|
||||
char === undefined ? String.fromCodePoint(Number.parseInt(codePoint ?? u4 ?? x2, 16)) : (JS_ESCAPES[char] ?? char));
|
||||
|
||||
function processFile(filePath) {
|
||||
const parentDir = path.basename(path.dirname(filePath)); // Get parent directory name
|
||||
const originalContent = fs.readFileSync(filePath, 'utf-8');
|
||||
|
||||
// Extract existing name and description values
|
||||
const nameMatch = originalContent.match(/name: '(.*?)'/);
|
||||
const descriptionMatch = originalContent.match(/description: '(.*?)'/);
|
||||
const externAccessDescriptionMatch = originalContent.match(/externAccessDescription: '(.*?)'/);
|
||||
const nameMatch = originalContent.match(quotedValue('name'));
|
||||
const descriptionMatch = originalContent.match(quotedValue('description'));
|
||||
const externAccessDescriptionMatch = originalContent.match(quotedValue('externAccessDescription'));
|
||||
|
||||
if (!nameMatch || !descriptionMatch) {
|
||||
console.warn(`Skipping ${filePath}: Unable to extract name or description.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const nameValue = nameMatch[1];
|
||||
const descriptionValue = descriptionMatch[1];
|
||||
const externAccessDescriptionValue = externAccessDescriptionMatch ? externAccessDescriptionMatch[1] : null;
|
||||
const nameValue = unescapeValue(nameMatch[1]);
|
||||
const descriptionValue = unescapeValue(descriptionMatch[1]);
|
||||
const externAccessDescriptionValue = externAccessDescriptionMatch ? unescapeValue(externAccessDescriptionMatch[1]) : null;
|
||||
|
||||
// Modify name and description to use the translate function
|
||||
const transformedContent = originalContent
|
||||
.replace(/name: '((?:[^']+|(?<=\\)')+)'/, `name: t('tools.${parentDir}.title')`)
|
||||
.replace(/description: '((?:[^']+|(?<=\\)')+)'/, `description: t('tools.${parentDir}.description')`)
|
||||
.replace(/externAccessDescription: '((?:[^']+|(?<=\\)')+)'/, `externAccessDescription: t('tools.${parentDir}.externalAccess')`);
|
||||
.replace(quotedValue('name'), `name: t('tools.${parentDir}.title')`)
|
||||
.replace(quotedValue('description'), `description: t('tools.${parentDir}.description')`)
|
||||
.replace(quotedValue('externAccessDescription'), `externAccessDescription: t('tools.${parentDir}.externalAccess')`);
|
||||
|
||||
fs.writeFileSync(filePath, `import { translate as t } from '@/plugins/i18n.plugin';\n${transformedContent}`, 'utf-8');
|
||||
console.log(`Transformed: ${filePath}`);
|
||||
|
||||
Reference in new issue
Block a user