mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-12-01 09:22:25 +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');
|
||||
|
||||
Reference in new issue
Block a user