Mitigation Strategy: Strict Directory Whitelisting (in()
Method)
-
Description:
- Identify Allowed Directories: Determine the absolute minimum set of directories that the application needs to access via Symfony Finder.
- Create a Mapping: Create a hardcoded associative array (or a configuration file not editable by users) that maps user-friendly keys to the absolute paths of these allowed directories. Example:
$allowedDirectories = [ 'user_uploads' => '/var/www/app/data/uploads/', 'product_images' => '/var/www/app/public/images/products/', 'temp_files' => '/tmp/app_temp/', // Outside webroot ];
- User Input as Key: If users select a directory, only allow them to select from the keys of this mapping (e.g., 'user_uploads'). Never accept a raw path.
- Validate User Input: Verify that the user-provided key exists in the
$allowedDirectories
array. - Use the Mapped Path: If the key is valid, use the corresponding value (the absolute path) from the
$allowedDirectories
array in theFinder->in()
method. - Handle Invalid Input: If the key is not valid, handle the error. Do not use a default path that might be broader. Display a generic error message and log the attempt.
-
Threats Mitigated:
- Arbitrary File Read (High Severity): Prevents attackers from specifying arbitrary paths to read sensitive files.
- Information Disclosure (Medium Severity): Limits the attacker's ability to probe the filesystem.
- Denial of Service (DoS) (Medium Severity): Reduces risk of DoS by specifying a very large directory.
- Trigger Unexpected Behavior (High Severity): Prevents access to files that could lead to code execution.
-
Impact:
- Arbitrary File Read: Risk significantly reduced (almost eliminated).
- Information Disclosure: Risk significantly reduced.
- Denial of Service (DoS): Risk reduced.
- Trigger Unexpected Behavior: Risk significantly reduced.
-
Currently Implemented:
src/Controller/ImageController.php
: Whitelist for image directories.src/Service/ReportGenerator.php
: Whitelist for temp file directories.
-
Missing Implementation:
src/Controller/LegacyDataController.php
: Uses user-provided paths directly inFinder->in()
.src/Command/CleanupCommand.php
: Uses a config file path that is not validated.
Mitigation Strategy: Safe Pattern Construction and Validation (name()
, path()
, contains()
, filter()
, etc.)
-
Description:
- Avoid Direct User Input in Patterns: Never allow users to directly input regular expressions or wildcard patterns for
Finder
methods. - Predefined Patterns: Use only predefined, hardcoded patterns that you have thoroughly tested.
- Indirect Input with Sanitization: If you must use user input to construct a pattern:
- Character Whitelisting: Restrict allowed characters to the absolute minimum. Use
preg_replace()
to remove disallowed characters. - Escaping Special Characters: Escape characters with special meaning in regular expressions. Use
preg_quote()
. - Length Limits: Enforce strict length limits on user input.
- Pattern Construction: Construct the final pattern programmatically, incorporating the sanitized input into a predefined structure. Example:
$sanitizedInput = preg_replace('/[^a-zA-Z0-9_-]/', '', $userInput); $escapedInput = preg_quote($sanitizedInput, '/'); $pattern = '*' . $escapedInput . '*'; // Files *containing* input $finder->name($pattern);
- Character Whitelisting: Restrict allowed characters to the absolute minimum. Use
fnmatch()
for Simple Wildcards: If you only need simple wildcard matching (e.g.,*.txt
), usefnmatch()
within afilter()
instead of regular expressions.- Regex Timeouts (Advanced): Implement timeouts on regular expression execution. Use
symfony/process
to run regex matching in a separate process with a timeout.
- Avoid Direct User Input in Patterns: Never allow users to directly input regular expressions or wildcard patterns for
-
Threats Mitigated:
- Regular Expression Denial of Service (ReDoS) (High Severity): Prevents crafted regex causing excessive CPU usage.
- Arbitrary File Read (High Severity): Reduces risk of using patterns to match unintended files.
- Information Disclosure (Medium Severity): Limits using patterns to infer filesystem information.
- Trigger Unexpected Behavior (High Severity): Prevents access to files that could lead to code execution.
-
Impact:
- ReDoS: Risk significantly reduced (especially with timeouts).
- Arbitrary File Read: Risk reduced (relies on sanitization).
- Information Disclosure: Risk reduced.
- Trigger Unexpected Behavior: Risk significantly reduced.
-
Currently Implemented:
src/Controller/SearchController.php
: Sanitization and escaping for search terms.src/Service/FileIndexer.php
: Usesfnmatch()
for wildcards.
-
Missing Implementation:
src/Controller/ReportController.php
: Regex based on user input, no sanitization/timeouts.- No global regex timeout mechanism.
Mitigation Strategy: Careful Use of exclude()
-
Description:
- Prioritize
in()
Restrictions: Focus primarily on tightly controlling thein()
path (using whitelisting). exclude()
for Convenience, Not Primary Security: Useexclude()
to simplify code by excluding files/directories within the already-securely-definedin()
path. Do not rely on it as the primary access control.- Avoid User Input: Do not allow user to control what is excluded.
- Prioritize
-
Threats Mitigated:
- Arbitrary File Read (Low Severity): Provides a minor additional layer of defense if
in()
is already well-controlled. It does not reliably prevent access ifin()
is vulnerable.
- Arbitrary File Read (Low Severity): Provides a minor additional layer of defense if
-
Impact:
- Arbitrary File Read: Minimal impact on its own. Only useful as a secondary measure.
-
Currently Implemented:
src/Service/BackupService.php
usesexclude()
to omit temporary files during backups.
-
Missing Implementation:
- None. The existing usage is appropriate; the key is not to rely on
exclude()
for primary security.
- None. The existing usage is appropriate; the key is not to rely on