Strengthens path validation against reserved names
Enhances path validation to prevent issues caused by reserved Windows names. The previous implementation only checked for reserved names when the entire path matched a reserved name. This update extends the check to ensure that no segment of the path, including the base name without extension, matches a reserved name. This prevents the use of reserved names, even with extensions or within directory structures, mitigating potential security risks.
This commit is contained in:
1 file changed
+40
-9
@@ -85,7 +85,8 @@ export class SecurityUtils {
|
|||||||
* - Null bytes and control characters
|
* - Null bytes and control characters
|
||||||
* - Double slashes and other suspicious patterns
|
* - Double slashes and other suspicious patterns
|
||||||
* - Invalid filename characters (<>:"|?*)
|
* - Invalid filename characters (<>:"|?*)
|
||||||
* - Windows reserved system names (CON, PRN, AUX, etc.)
|
* - Windows reserved system names in any path segment (CON, PRN, AUX, etc.)
|
||||||
|
* including when followed by extensions (e.g., CON.txt, AUX.log)
|
||||||
*
|
*
|
||||||
* The validation is intentionally strict - when in doubt, reject the path
|
* The validation is intentionally strict - when in doubt, reject the path
|
||||||
* rather than risk a security vulnerability. Use getPathValidationError()
|
* rather than risk a security vulnerability. Use getPathValidationError()
|
||||||
@@ -105,6 +106,8 @@ export class SecurityUtils {
|
|||||||
* SecurityUtils.validateOutputPath('../../../etc'); // false - traversal
|
* SecurityUtils.validateOutputPath('../../../etc'); // false - traversal
|
||||||
* SecurityUtils.validateOutputPath('/tmp/evil'); // false - absolute
|
* SecurityUtils.validateOutputPath('/tmp/evil'); // false - absolute
|
||||||
* SecurityUtils.validateOutputPath('CON'); // false - reserved name
|
* SecurityUtils.validateOutputPath('CON'); // false - reserved name
|
||||||
|
* SecurityUtils.validateOutputPath('reports/CON/file'); // false - reserved in segment
|
||||||
|
* SecurityUtils.validateOutputPath('AUX.txt'); // false - reserved with extension
|
||||||
* SecurityUtils.validateOutputPath('test<>file'); // false - invalid chars
|
* SecurityUtils.validateOutputPath('test<>file'); // false - invalid chars
|
||||||
* SecurityUtils.validateOutputPath('path//with//double'); // false - suspicious
|
* SecurityUtils.validateOutputPath('path//with//double'); // false - suspicious
|
||||||
* ```
|
* ```
|
||||||
@@ -129,11 +132,26 @@ export class SecurityUtils {
|
|||||||
const suspiciousPatterns = [
|
const suspiciousPatterns = [
|
||||||
/\.\./, // Directory traversal
|
/\.\./, // Directory traversal
|
||||||
/\/\//, // Double slashes
|
/\/\//, // Double slashes
|
||||||
/[<>:"|?*]/, // Invalid filename characters
|
/[<>:"|?*]/ // Invalid filename characters
|
||||||
/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i // Windows reserved names
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return !suspiciousPatterns.some(pattern => pattern.test(normalizedPath));
|
if (suspiciousPatterns.some(pattern => pattern.test(normalizedPath))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for Windows reserved names in any path segment
|
||||||
|
// Reserved names are invalid even with extensions (e.g., CON.txt, AUX.log)
|
||||||
|
const reservedNamePattern = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i;
|
||||||
|
const hasReservedName = normalizedPath
|
||||||
|
.split(/[\\/]+/)
|
||||||
|
.filter(segment => segment && segment !== '.')
|
||||||
|
.some(segment => {
|
||||||
|
// Strip extension and check base name
|
||||||
|
const baseName = segment.split('.')[0] ?? '';
|
||||||
|
return reservedNamePattern.test(baseName);
|
||||||
|
});
|
||||||
|
|
||||||
|
return !hasReservedName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,7 +263,9 @@ export class SecurityUtils {
|
|||||||
* const testPaths = [
|
* const testPaths = [
|
||||||
* '../../../etc', // "Path traversal attempts (..) are not allowed"
|
* '../../../etc', // "Path traversal attempts (..) are not allowed"
|
||||||
* '/absolute/path', // "Absolute paths are not allowed..."
|
* '/absolute/path', // "Absolute paths are not allowed..."
|
||||||
* 'CON', // "Reserved system names are not allowed..."
|
* 'CON', // "Reserved system names are not allowed in paths (found: CON)"
|
||||||
|
* 'reports/AUX/file', // "Reserved system names are not allowed in paths (found: AUX)"
|
||||||
|
* 'COM1.txt', // "Reserved system names are not allowed in paths (found: COM1.txt)"
|
||||||
* 'invalid<>chars' // "Invalid characters found in path"
|
* 'invalid<>chars' // "Invalid characters found in path"
|
||||||
* ];
|
* ];
|
||||||
*
|
*
|
||||||
@@ -278,10 +298,21 @@ export class SecurityUtils {
|
|||||||
if (/[<>:"|?*]/.test(normalizedPath)) {
|
if (/[<>:"|?*]/.test(normalizedPath)) {
|
||||||
return 'Invalid characters found in path';
|
return 'Invalid characters found in path';
|
||||||
}
|
}
|
||||||
if (/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i.test(normalizedPath)) {
|
|
||||||
return 'Reserved system names are not allowed as folder names';
|
// Check for Windows reserved names in any path segment
|
||||||
|
const reservedNamePattern = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i;
|
||||||
|
const reservedSegment = normalizedPath
|
||||||
|
.split(/[\\/]+/)
|
||||||
|
.filter(segment => segment && segment !== '.')
|
||||||
|
.find(segment => {
|
||||||
|
const baseName = segment.split('.')[0] ?? '';
|
||||||
|
return reservedNamePattern.test(baseName);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (reservedSegment) {
|
||||||
|
return `Reserved system names are not allowed in paths (found: ${reservedSegment})`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'Invalid path format';
|
return 'Invalid path format';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user