🛡️ Sentinel: [HIGH] Fix path traversal prefix matching vulnerability in Lua sandbox#170
🛡️ Sentinel: [HIGH] Fix path traversal prefix matching vulnerability in Lua sandbox#170mleem97 wants to merge 1 commit into
Conversation
|
👋 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 | 4 |
| 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
This PR addresses a high-severity path traversal vulnerability in the Lua sandbox by refining prefix matching for directory boundaries. While the implementation of trailing-separator validation aligns with the security requirements, there are significant concerns regarding the lack of automated regression tests for this fix.
Critical logic issues were identified in the LuaModuleLoader that could lead to runtime exceptions or functional regressions in required modules. Additionally, concurrency issues in the hot-reload mechanism pose a risk to system stability during plugin updates. Although Codacy results are up to standards, these logic and thread-safety findings should be addressed before merging to ensure the stability of the sandbox.
About this PR
- No new or updated unit tests were included in the PR to verify the prefix-matching bypass fix. Given the high-severity nature of path traversal vulnerabilities, automated test scenarios covering direct access, exact directory matches, and sibling directory rejection are essential to prevent future regressions.
Test suggestions
- Missing recommended test scenario: Verify that a file located directly inside an allowed sandbox directory is permitted.
- Missing recommended test scenario: Verify that the sandbox directory itself (without a trailing slash) is permitted as an exact match.
- Missing recommended test scenario: Verify that a sibling directory sharing a name prefix (e.g., '/mods/modA_secret' when the sandbox is '/mods/modA') is correctly rejected.
- Missing recommended test scenario: Verify that path traversal attempts using '..' are resolved and correctly rejected if they exit the sandbox.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Missing recommended test scenario: Verify that a file located directly inside an allowed sandbox directory is permitted.
2. Missing recommended test scenario: Verify that the sandbox directory itself (without a trailing slash) is permitted as an exact match.
3. Missing recommended test scenario: Verify that a sibling directory sharing a name prefix (e.g., '/mods/modA_secret' when the sandbox is '/mods/modA') is correctly rejected.
4. Missing recommended test scenario: Verify that path traversal attempts using '..' are resolved and correctly rejected if they exit the sandbox.
TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback
| // Copy allowed globals (but not greg table or original require to isolate modules) | ||
| foreach (var pair in _script.Globals.Pairs) | ||
| { | ||
| if (pair.Key.String != "greg" && pair.Key.String != "require") |
There was a problem hiding this comment.
🔴 HIGH RISK
The isolation logic in the require() implementation contains two critical issues:
- Accessing
pair.Key.Stringdirectly will throw anInvalidCastExceptionif any global variable has a non-string key (e.g.,_G[1] = true). - The
gregtable is explicitly excluded, which prevents required modules from accessing the sandboxed mod API (likegreg.io).
To fix both: Update the loop to verify pair.Key.Type == DataType.String before access, and include the greg table so modules can use the API (while still excluding require to maintain isolation).
| OnShutdown = script.Globals.Get("on_shutdown").Type == DataType.Function | ||
| ? script.Globals.Get("on_shutdown").Function : null, | ||
| }; | ||
| _pluginMap[modId] = entry; |
There was a problem hiding this comment.
🟡 MEDIUM RISK
The _pluginMap dictionary is accessed from both the registration thread and the timer-based reload thread, but Dictionary<TKey, TValue> is not thread-safe. Replace _pluginMap with a ConcurrentDictionary or wrap its accesses in a lock to ensure thread safety.
|
|
||
| private string? FindModForFile(string filePath) | ||
| { | ||
| string fullPath = Path.GetFullPath(filePath); |
There was a problem hiding this comment.
⚪ LOW RISK
Nitpick: Performance can be improved by caching the normalized root path (e.g., Path.GetFullPath(_watchRoot)) and its version with a trailing slash as private fields in the constructor. This avoids repeated I/O and string operations during high-frequency file watch events.
🚨 Severity: HIGH
💡 Vulnerability: A directory traversal vulnerability in the Lua module sandbox allowed accessing files outside the isolated directory by exploiting prefix matching on folder names (e.g.,
../modA_secretstarting with/mods/modA).🎯 Impact: A malicious script could read or write sensitive files in other sandbox directories that share a prefix with the current executing sandbox directory.
🔧 Fix: Appended a trailing slash (
Path.DirectorySeparatorChar) to the base directory before usingString.StartsWith, while retaining exact-match checks for the base directory.✅ Verification: Successfully ran the unit test suite and verified via script that path prefix matching now accurately respects directory boundaries.
PR created automatically by Jules for task 12043133702173173878 started by @mleem97