🛡️ Sentinel: CRITICAL Fix prefix path traversal in Lua IO sandbox#174
🛡️ Sentinel: CRITICAL Fix prefix path traversal in Lua IO sandbox#174mleem97 wants to merge 1 commit into
Conversation
Fixed a prefix-matching path traversal vulnerability in `GregIoLuaModule.cs`. Appended `Path.DirectorySeparatorChar` to the base directory before using `StartsWith` for sandbox confinement, and explicitly allowed exact matches to the base directory.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
While this PR addresses the critical prefix-matching vulnerability in the Lua IO sandbox, several secondary security and stability issues remain in GregIoLuaModule.cs. The most significant is a remaining path traversal vulnerability in the list_files pattern parameter and a potential Denial of Service vector due to unrestricted directory recursion.
Furthermore, despite this being a critical security fix in a complex and high-risk file, no new test files or updated unit tests were included in the PR. Although Codacy indicates the project is 'up to standards', the lack of verification for these specific logic changes is a significant gap. These issues should be addressed to ensure the sandbox is truly robust against exploitation.
About this PR
- This PR modifies critical security logic but does not include any new test cases or modifications to existing tests. For a fix regarding path traversal, it is essential to have unit tests that verify both the fix and edge cases (e.g., sibling directories, exact matches).
Test suggestions
- Found recommended test scenario: Accessing a file within the allowed data directory (e.g., data/config.txt)
- Found recommended test scenario: Accessing the base data directory path exactly
- Found recommended test scenario: Attempting to access a sibling directory with a matching prefix (e.g., ../modA_secret/file.txt)
- Found recommended test scenario: Attempting to access a file outside the sandbox via standard traversal (e.g., ../../outside.txt)
- Missing recommended test scenario: Accessing files outside the sandbox using the traversal sequences in the list_files pattern parameter
- Missing recommended test scenario: Verification of parent directory creation in append_file
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Found recommended test scenario: Accessing a file within the allowed data directory (e.g., data/config.txt)
2. Found recommended test scenario: Accessing the base data directory path exactly
3. Found recommended test scenario: Attempting to access a sibling directory with a matching prefix (e.g., ../modA_secret/file.txt)
4. Found recommended test scenario: Attempting to access a file outside the sandbox via standard traversal (e.g., ../../outside.txt)
5. Missing recommended test scenario: Accessing files outside the sandbox using the traversal sequences in the list_files pattern parameter
6. Missing recommended test scenario: Verification of parent directory creation in append_file
TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback
| { | ||
| try | ||
| { | ||
| var files = Directory.GetFiles(dataDir, pattern ?? "*.*", SearchOption.AllDirectories) |
There was a problem hiding this comment.
🔴 HIGH RISK
The list_files implementation contains multiple risks:
- Path Traversal: The
patternargument is not sanitized, allowing an attacker to use sequences like../../to list files outside the sandbox. - Denial of Service: Using
SearchOption.AllDirectorieswithout a depth limit can be exploited to cause a DoS by crawling deep directory structures. - Cross-Platform Compatibility: The default
*.*pattern will fail to match files without extensions on Linux/macOS.
Sanitize the pattern variable to block directory separators and '..', and consider restricting search depth or using SearchOption.TopDirectoryOnly.
| try | ||
| { | ||
| string fullPath = ResolveSafe(dataDir, path); | ||
| File.AppendAllText(fullPath, content); |
There was a problem hiding this comment.
🟡 MEDIUM RISK
Suggestion: Unlike write_file, this implementation does not create the parent directory if it is missing, which will cause a DirectoryNotFoundException if the provided path includes a new subdirectory. Update the function to call Directory.CreateDirectory before File.AppendAllText.
| }); | ||
|
|
||
| // greg.io.data_dir → string (read-only) | ||
| ioTable["data_dir"] = dataDir.Replace('\\', '/'); |
There was a problem hiding this comment.
⚪ LOW RISK
Exposing the absolute host path via greg.io.data_dir provides unnecessary information about the host system (CWE-200), such as the username and directory structure. Consider exposing a virtualized path like 'data/' instead.
| string dataDirFull = Path.GetFullPath(dataDir); | ||
|
|
||
| string dataDirWithSlash = dataDirFull; | ||
| if (!dataDirWithSlash.EndsWith(Path.DirectorySeparatorChar.ToString())) | ||
| dataDirWithSlash += Path.DirectorySeparatorChar; |
There was a problem hiding this comment.
⚪ LOW RISK
Nitpick: Path.GetFullPath(dataDir) is called on every sandbox validation. Consider pre-calculating this value and the version with a trailing slash during the Register method to avoid redundant filesystem calls.
🚨 Severity: CRITICAL
💡 Vulnerability: Prefix-matching path traversal in
GregIoLuaModule.cswhereString.StartsWithwas used to validate path confines. This allowed a mod in/mods/modAto bypass the sandbox by accessing/mods/modA_secretusing a path like../modA_secret/file.txt.🎯 Impact: Malicious mods could escape their restricted
datadirectory and write or delete files in sibling directories with matching prefixes, leading to data destruction or arbitrary file manipulation.🔧 Fix: Appended
Path.DirectorySeparatorCharto the base directory before evaluatingStartsWithand explicitly allowed exact matches to the base directory itself.✅ Verification: Ran
dotnet buildanddotnet testto confirm compilation and verify test stability.PR created automatically by Jules for task 14143233738429214598 started by @mleem97