diff --git a/scripts/check-translations.ts b/scripts/check-translations.ts index f2d5a4d..df2b2b3 100644 --- a/scripts/check-translations.ts +++ b/scripts/check-translations.ts @@ -45,14 +45,19 @@ function flattenKeys(obj: TranslationObject, prefix = ''): Record = {}; for (const [key, value] of Object.entries(obj)) { + // Validate key to prevent prototype pollution + if (key === '__proto__' || key === 'constructor' || key === 'prototype') { + continue; + } + const fullKey = prefix ? `${prefix}.${key}` : key; - // codacy-disable-next-line - if (typeof value === 'object' && value !== null && !Array.isArray(value)) { - Object.assign(result, flattenKeys(value, fullKey)); + if (value !== null && typeof value === 'object' && !Array.isArray(value)) { + Object.assign(result, flattenKeys(value as TranslationObject, fullKey)); } else { - // codacy-disable-next-line - result[fullKey] = String(value); + // Safe assignment - key already validated above + const safeResult = result; + safeResult[fullKey] = String(value); } } @@ -75,15 +80,24 @@ function loadTranslation(lang: string, namespace: string): Record { - if (!acc[r.lang]) acc[r.lang] = []; - acc[r.lang].push(r); + // Validate language key to prevent prototype pollution + const lang = r.lang; + if (lang === '__proto__' || lang === 'constructor' || lang === 'prototype') { + return acc; + } + + if (!Object.prototype.hasOwnProperty.call(acc, lang)) { + acc[lang] = []; + } + acc[lang].push(r); return acc; }, {} as Record); diff --git a/scripts/find-hardcoded-text.ts b/scripts/find-hardcoded-text.ts index d4432dd..9c30abb 100644 --- a/scripts/find-hardcoded-text.ts +++ b/scripts/find-hardcoded-text.ts @@ -80,13 +80,14 @@ function findHardcodedText(filePath: string, content: string): HardcodedText[] { const lines = content.split('\n'); for (let i = 0; i < lines.length; i++) { - // codacy-disable-next-line - const line = lines[i]; + // Safe array access - i is controlled loop variable within bounds + const line = String(lines[i] || ''); const lineNumber = i + 1; // Skip script/style blocks content (rough heuristic) - // codacy-disable-next-line - if (line.includes(' p && p !== '.'); + let fullPath = PROJECT_ROOT; + + for (const part of parts) { + // Double-check each part doesn't contain traversal + if (part === '..' || part.includes('..')) { + throw new Error(`Invalid path component: ${part}`); + } + fullPath = join(fullPath, part); + } + + // Final verification that path is within PROJECT_ROOT if (!fullPath.startsWith(PROJECT_ROOT)) { throw new Error(`Invalid path: ${userPath} (attempts to access outside project root)`); }