mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-11-22 22:02:40 +03:00
Fix issues raised by Codacy
This commit is contained in:
1 parent
fad242ff52
commit
992125453d
5 files changed
+69
-7
No files matched your search
@@ -57,11 +57,23 @@ function flattenKeys(obj: TranslationObject, prefix = ''): Record<string, string
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize path component to prevent path traversal
|
||||
*/
|
||||
function sanitizePathComponent(component: string): string {
|
||||
// Remove any path traversal attempts and keep only safe characters
|
||||
return component.replace(/[^a-zA-Z0-9_-]/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and parse a translation file
|
||||
*/
|
||||
function loadTranslation(lang: string, namespace: string): Record<string, string> | 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;
|
||||
|
||||
@@ -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<string, HardcodedText[]> {
|
||||
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');
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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('');
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user