Skip to content

A self-hosted media server and file management system

fix: storage paths have no sanitation

aminnausinbd7183fc207c
diff --git a/app/Http/Controllers/Api/V1/Server/ServerConfigController.php b/app/Http/Controllers/Api/V1/Server/ServerConfigController.php
index e6259eed..7a752abc 100644
--- a/app/Http/Controllers/Api/V1/Server/ServerConfigController.php
+++ b/app/Http/Controllers/Api/V1/Server/ServerConfigController.php
@@ -13,16 +13,15 @@ use App\Services\Server\ServerConfigService;
use Illuminate\Http\JsonResponse;
class ServerConfigController extends Controller {
- public function __construct(private ServerConfigService $config) {
- }
+ public function __construct(private ServerConfigService $config) {}
public function index(): JsonResponse {
$configs = ServerConfig::all()->groupBy('group');
return response()->json(
[
- 'values' => $configs->map(fn($group) => $group->pluck('value', 'key')),
- 'defaults' => $configs->map(fn($group) => $group->pluck('default_value', 'key')),
+ 'values' => $configs->map(fn ($group) => $group->pluck('value', 'key')),
+ 'defaults' => $configs->map(fn ($group) => $group->pluck('default_value', 'key')),
]
);
}
diff --git a/app/Http/Requests/Server/UpdateStorageConfigRequest.php b/app/Http/Requests/Server/UpdateStorageConfigRequest.php
index 84f9d951..017d7d98 100644
--- a/app/Http/Requests/Server/UpdateStorageConfigRequest.php
+++ b/app/Http/Requests/Server/UpdateStorageConfigRequest.php
@@ -4,6 +4,8 @@ namespace App\Http\Requests\Server;
use Closure;
use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Str;
class UpdateStorageConfigRequest extends FormRequest {
/**
@@ -13,6 +15,16 @@ class UpdateStorageConfigRequest extends FormRequest {
return $this->user()?->isAdmin();
}
+ protected function prepareForValidation() {
+ if ($this->has('cache_path')) {
+ $this->merge(['cache_path' => rtrim($this->cache_path, '/')]);
+ }
+
+ if ($this->has('metadata_path')) {
+ $this->merge(['metadata_path' => rtrim($this->metadata_path, '/')]);
+ }
+ }
+
/**
* Get the validation rules that apply to the request.
* Gets category and potentially folder id from query strings
@@ -36,8 +48,22 @@ class UpdateStorageConfigRequest extends FormRequest {
* @return bool
*/
private function validatePath(string $path, Closure $fail) {
- if ($path && ! is_dir($path)) {
- $fail("The path '$path' does not exist on the server.");
+ if (! $path) {
+ return;
+ }
+
+ if (str_contains($path, '..') || str_contains($path, "\0")) {
+ $fail("The path '$path' is invalid.");
+
+ return;
+ }
+
+ $exists = str_starts_with($path, 'storage/')
+ ? Storage::disk('local')->exists(Str::after($path, 'storage/'))
+ : is_dir($path);
+
+ if (! $exists) {
+ $fail("The path '$path' does not exist.");
}
}
}