Skip to content

🛡️ Sentinel: [HIGH] Fix path traversal prefix matching vulnerability in Lua sandbox#170

Open
mleem97 wants to merge 1 commit into
mainfrom
sentinel-fix-lua-path-traversal-12043133702173173878
Open

🛡️ Sentinel: [HIGH] Fix path traversal prefix matching vulnerability in Lua sandbox#170
mleem97 wants to merge 1 commit into
mainfrom
sentinel-fix-lua-path-traversal-12043133702173173878

Conversation

@mleem97

@mleem97 mleem97 commented Jul 10, 2026

Copy link
Copy Markdown
Owner

🚨 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_secret starting 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 using String.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

@google-labs-jules

Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 HIGH RISK

The isolation logic in the require() implementation contains two critical issues:

  1. Accessing pair.Key.String directly will throw an InvalidCastException if any global variable has a non-string key (e.g., _G[1] = true).
  2. The greg table is explicitly excluded, which prevents required modules from accessing the sandboxed mod API (like greg.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).

See Complexity in Codacy

OnShutdown = script.Globals.Get("on_shutdown").Type == DataType.Function
? script.Globals.Get("on_shutdown").Function : null,
};
_pluginMap[modId] = entry;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ 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.

See Complexity in Codacy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant