Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
**Learning:** When dealing with structured text files where line breaks determine the structure (like `.env`, `ini`, or CSV files), user input MUST be explicitly stripped of all newline characters (`\r` and `\n`) before interpolation. Relying only on `trim()` is insufficient as it only removes whitespace at the boundaries, not within the payload.

**Prevention:** Sanitize all user input destined for line-based configuration files by explicitly stripping `\r` and `\n` characters (e.g., using `str_replace(["\r", "\n"], '', $value)` in PHP) to ensure the input remains strictly on a single line.

## 2024-05-25 - Path Validation Bypass (Directory Traversal)
**Vulnerability:** In `validatePath` and `isValidProjectPath` across multiple API files (`files.php`, `terminal.php`, `projects.php`, `src/Utils/Security.php`), the code used `strpos($realPath, $allowedReal) === 0` to check if a user-supplied path was within an allowed directory.
**Learning:** This is a classic path traversal bypass in PHP. Because it only checks for a string prefix, an allowed path like `/var/www` will successfully match a malicious sibling directory like `/var/www_backup` or `/var/www-secret`.
**Prevention:** Always append a trailing directory separator (`/`) to the allowed path prefix before checking with `strpos`, or perform an exact match if the paths are identical. For example: `strpos($realPath, rtrim($allowedReal, '/') . '/') === 0`.
16 changes: 13 additions & 3 deletions public/api/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,19 @@ function validatePath($path, $allowedPaths) {
$isValid = false;
foreach ($allowedPaths as $allowed) {
$allowedReal = realpath($allowed);
if ($allowedReal && strpos($realPath, $allowedReal) === 0) {
$isValid = true;
break;
if ($allowedReal) {
if ($realPath === $allowedReal) {
$isValid = true;
break;
}

$normalizedReal = str_replace('\\', '/', $realPath);
$normalizedAllowed = rtrim(str_replace('\\', '/', $allowedReal), '/') . '/';

if (strpos($normalizedReal, $normalizedAllowed) === 0) {
$isValid = true;
break;
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions public/api/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,17 @@ function isValidProjectPath($path, $workspaces) {

foreach ($workspaces as $ws) {
$wsPath = realpath($ws['path']);
if ($wsPath && strpos($realPath, $wsPath) === 0) {
return true;
if ($wsPath) {
if ($realPath === $wsPath) {
return true;
}

$normalizedReal = str_replace('\\', '/', $realPath);
$normalizedAllowed = rtrim(str_replace('\\', '/', $wsPath), '/') . '/';

if (strpos($normalizedReal, $normalizedAllowed) === 0) {
return true;
}
}
}

Expand Down
16 changes: 13 additions & 3 deletions public/api/terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,19 @@ function validatePath($path, $allowedPaths) {
$isValid = false;
foreach ($allowedPaths as $allowed) {
$allowedReal = realpath($allowed);
if ($allowedReal && strpos($realPath, $allowedReal) === 0) {
$isValid = true;
break;
if ($allowedReal) {
if ($realPath === $allowedReal) {
$isValid = true;
break;
}

$normalizedReal = str_replace('\\', '/', $realPath);
$normalizedAllowed = rtrim(str_replace('\\', '/', $allowedReal), '/') . '/';

if (strpos($normalizedReal, $normalizedAllowed) === 0) {
$isValid = true;
break;
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/Utils/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ public static function validateFilePath(string $path, array $allowedPaths): ?str

foreach ($allowedPaths as $allowedPath) {
$realAllowedPath = realpath($allowedPath);
if ($realAllowedPath && strpos($realPath, $realAllowedPath) === 0) {
return $realPath;
if ($realAllowedPath) {
if ($realPath === $realAllowedPath) {
return $realPath;
}

$normalizedReal = str_replace('\\', '/', $realPath);
$normalizedAllowed = rtrim(str_replace('\\', '/', $realAllowedPath), '/') . '/';

if (strpos($normalizedReal, $normalizedAllowed) === 0) {
return $realPath;
}
}
}

Expand Down