Practice/Microsoft/Design a path validation system
Object Oriented DesignMust
Design and implement a secure path validation system that can verify whether file system paths or URL paths meet specific security and format requirements. Your system should protect against common security vulnerabilities like path traversal attacks while enforcing configurable rules about path depth, file extensions, and other criteria.
The validator should accept a path string and a set of validation rules, then return whether the path is considered valid according to those rules.
.. sequences) when enabledmax_depth, allowed_extensions, and block_traversalExample 1:
Input: path = "/home/user/documents/report.pdf" rules = \{"max_depth": 5, "allowed_extensions": [".pdf", ".docx"], "block_traversal": true\} Output: true Explanation: The path has 4 levels (home, user, documents, report.pdf), which is within the limit of 5. The extension .pdf is in the allowed list, and there are no traversal patterns.
Example 2:
Input: path = "/var/www/../../../etc/shadow" rules = \{"max_depth": 10, "allowed_extensions": [], "block_traversal": true\} Output: false Explanation: The path contains ".." sequences indicating a path traversal attempt. Since block_traversal is true, this path is invalid.
Example 3:
Input: path = "https://api.example.com/v1/users" rules = \{"max_depth": 3, "allowed_extensions": [], "block_traversal": false\} Output: false Explanation: The URL path has 4 segments (v1/users plus the domain and protocol), exceeding the max_depth of 3.
Example 4:
Input: path = "/data/output.csv" rules = \{"max_depth": 5, "allowed_extensions": [".txt", ".json"], "block_traversal": true\} Output: false Explanation: The file extension .csv is not in the allowed extensions list.