From 992125453d64bb5dcea0f33e4deb4a92a422a559 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sun, 26 Oct 2025 18:38:40 +0000 Subject: [PATCH] Fix issues raised by Codacy --- scripts/check-translations.ts | 14 +++++++++++++- scripts/find-hardcoded-text.ts | 24 +++++++++++++++++++++--- src/lib/i18n/index.ts | 8 +++++++- tests/unit/lib/i18n/index.test.ts | 26 ++++++++++++++++++++++++++ tests/unit/lib/stores/language.test.ts | 4 ++-- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/scripts/check-translations.ts b/scripts/check-translations.ts index 2de497b..3a19dcb 100644 --- a/scripts/check-translations.ts +++ b/scripts/check-translations.ts @@ -57,11 +57,23 @@ function flattenKeys(obj: TranslationObject, prefix = ''): Record | null { - const filePath = join(TRANSLATIONS_DIR, lang, `${namespace}.json`); + // Sanitize inputs to prevent path traversal + const safeLang = sanitizePathComponent(lang); + const safeNamespace = sanitizePathComponent(namespace); + + const filePath = join(TRANSLATIONS_DIR, safeLang, `${safeNamespace}.json`); if (!existsSync(filePath)) { return null; diff --git a/scripts/find-hardcoded-text.ts b/scripts/find-hardcoded-text.ts index a84b8e2..fda18be 100644 --- a/scripts/find-hardcoded-text.ts +++ b/scripts/find-hardcoded-text.ts @@ -12,7 +12,7 @@ */ import { readFileSync, readdirSync, statSync } from 'fs'; -import { join, relative, dirname } from 'path'; +import { join, relative, dirname, resolve, normalize } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); @@ -164,7 +164,10 @@ function groupByFile(results: HardcodedText[]): Map { if (!grouped.has(result.file)) { grouped.set(result.file, []); } - grouped.get(result.file)!.push(result); + const fileGroup = grouped.get(result.file); + if (fileGroup) { + fileGroup.push(result); + } } return grouped; @@ -211,6 +214,21 @@ function formatResults(results: HardcodedText[], minLength: number): string { return output; } +/** + * Validate and sanitize scan path to prevent directory traversal + */ +function validateScanPath(userPath: string): string { + // Resolve the full path and normalize it + const fullPath = resolve(PROJECT_ROOT, normalize(userPath)); + + // Ensure the path is within PROJECT_ROOT + if (!fullPath.startsWith(PROJECT_ROOT)) { + throw new Error(`Invalid path: ${userPath} (attempts to access outside project root)`); + } + + return fullPath; +} + /** * Main function */ @@ -219,7 +237,7 @@ function main() { const pathArg = args.find(arg => arg.startsWith('--path='))?.split('=')[1]; const minLengthArg = args.find(arg => arg.startsWith('--min-length='))?.split('=')[1]; - const scanPath = pathArg ? join(PROJECT_ROOT, pathArg) : DEFAULT_SCAN_PATH; + const scanPath = pathArg ? validateScanPath(pathArg) : DEFAULT_SCAN_PATH; const minLength = minLengthArg ? parseInt(minLengthArg, 10) : 3; console.log('🔍 Scanning for hardcoded text...\n'); diff --git a/src/lib/i18n/index.ts b/src/lib/i18n/index.ts index 4ef5ea8..8b09bf4 100644 --- a/src/lib/i18n/index.ts +++ b/src/lib/i18n/index.ts @@ -23,13 +23,19 @@ function interpolate(template: string, params: InterpolationParams): string { /** * Deep get value from nested object by dot-notation key * get({ foo: { bar: 'baz' } }, 'foo.bar') → 'baz' + * Protected against prototype pollution */ function getNestedValue(obj: TranslationObject, key: string): any { const keys = key.split('.'); let result: any = obj; for (const k of keys) { - if (result && typeof result === 'object' && k in result) { + // Prevent prototype pollution + if (k === '__proto__' || k === 'constructor' || k === 'prototype') { + return undefined; + } + + if (result && typeof result === 'object' && Object.prototype.hasOwnProperty.call(result, k)) { result = result[k]; } else { return undefined; diff --git a/tests/unit/lib/i18n/index.test.ts b/tests/unit/lib/i18n/index.test.ts index c6a0435..fc59a2f 100644 --- a/tests/unit/lib/i18n/index.test.ts +++ b/tests/unit/lib/i18n/index.test.ts @@ -301,6 +301,32 @@ describe('I18n Core', () => { }); }); + describe('security', () => { + it('prevents prototype pollution via __proto__', () => { + i18n.addTranslations('en', { + safe: { value: 'Safe value' }, + }); + + // Attempt prototype pollution + expect(i18n.t('__proto__.polluted')).toBe('__proto__.polluted'); + expect(i18n.t('safe.__proto__')).toBe('safe.__proto__'); + }); + + it('prevents prototype pollution via constructor', () => { + i18n.addTranslations('en', { test: { value: 'Test' } }); + + expect(i18n.t('constructor.prototype.polluted')).toBe('constructor.prototype.polluted'); + expect(i18n.t('test.constructor')).toBe('test.constructor'); + }); + + it('prevents prototype pollution via prototype', () => { + i18n.addTranslations('en', { test: { value: 'Test' } }); + + expect(i18n.t('prototype.polluted')).toBe('prototype.polluted'); + expect(i18n.t('test.prototype')).toBe('test.prototype'); + }); + }); + describe('edge cases', () => { it('handles empty string keys gracefully', () => { expect(i18n.t('')).toBe(''); diff --git a/tests/unit/lib/stores/language.test.ts b/tests/unit/lib/stores/language.test.ts index 799dead..807ea73 100644 --- a/tests/unit/lib/stores/language.test.ts +++ b/tests/unit/lib/stores/language.test.ts @@ -283,9 +283,9 @@ describe('language store', () => { locale.set('fr'); mockBuildLocalizedPath.mockReturnValue('/fr/settings'); - const path = localizedNavigate('/settings'); + const result = localizedNavigate('/settings'); - expect(path).toBe('/fr/settings'); + expect(result).toBe('/fr/settings'); }); });