mirror of
https://github.com/matank001/cursor-security-rules.git
synced 2025-12-12 20:35:42 +01:00
Add file path manipulations rule
This commit is contained in:
@@ -76,4 +76,31 @@ For file validation, tilize MIME Type Validation libraries, like `MimeDetective`
|
|||||||
PropertyNameCaseInsensitive = true
|
PropertyNameCaseInsensitive = true
|
||||||
};
|
};
|
||||||
return JsonSerializer.Deserialize<T>(json, options) // Type-safe
|
return JsonSerializer.Deserialize<T>(json, options) // Type-safe
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 7. Validate and Normalize File Paths
|
||||||
|
- **Rule:** To prevent file path manipulations, normalize and validate input file paths to prevent access to sensitive files.
|
||||||
|
|
||||||
|
- **Unsafe:**
|
||||||
|
```cs
|
||||||
|
string basePath = "/home/files/";
|
||||||
|
// Dangerous - filename can contain "../../etc/passwd"
|
||||||
|
string fullPath = Path.Combine(basePath, filename);
|
||||||
|
string content = System.IO.File.ReadAllText(fullPath);
|
||||||
|
```
|
||||||
|
- **Safe:**
|
||||||
|
```cs
|
||||||
|
string basePath = "/home/files/";
|
||||||
|
// Absolute path is resolved and normalized
|
||||||
|
string fullPath = Path.GetFullPath(Path.Combine(basePath, filename));
|
||||||
|
// Ensure the resolved path starts with the base path
|
||||||
|
if (!fullPath.StartsWith(basePath, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
return BadRequest("Invalid file path.");
|
||||||
|
}
|
||||||
|
if (!System.IO.File.Exists(fullPath))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
string content = System.IO.File.ReadAllText(fullPath);
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user