|
8 | 8 |
|
9 | 9 | namespace OCA\Photos\Controller; |
10 | 10 |
|
| 11 | +use JsonException; |
11 | 12 | use OCA\Photos\AppInfo\Application; |
12 | | -use OCA\Photos\Service\UserConfigService; |
13 | 13 | use OCP\AppFramework\Controller; |
14 | 14 | use OCP\AppFramework\Http; |
15 | 15 | use OCP\AppFramework\Http\ContentSecurityPolicy; |
@@ -50,14 +50,58 @@ public function setUserConfig(string $key, string $value): JSONResponse { |
50 | 50 | return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); |
51 | 51 | } |
52 | 52 |
|
53 | | - if (!in_array($key, array_keys(UserConfigService::DEFAULT_CONFIGS))) { |
54 | | - return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED); |
| 53 | + switch ($key) { |
| 54 | + case 'croppedLayout': |
| 55 | + if ($value !== 'true' && $value !== 'false') { |
| 56 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
| 57 | + } |
| 58 | + break; |
| 59 | + case 'photosLocation': |
| 60 | + if (!$this->validatePath($value)) { |
| 61 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
| 62 | + } |
| 63 | + break; |
| 64 | + case 'photosSourceFolders': |
| 65 | + try { |
| 66 | + $paths = json_decode($value, true, 512, JSON_THROW_ON_ERROR); |
| 67 | + } catch (JsonException) { |
| 68 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
| 69 | + } |
| 70 | + |
| 71 | + if (!array_is_list($paths)) { |
| 72 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($paths as $path) { |
| 76 | + if (!$this->validatePath($path)) { |
| 77 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
| 78 | + } |
| 79 | + } |
| 80 | + break; |
| 81 | + default: |
| 82 | + return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
55 | 83 | } |
56 | 84 |
|
57 | 85 | $this->config->setUserValue($user->getUid(), Application::APP_ID, $key, $value); |
58 | 86 | return new JSONResponse([], Http::STATUS_OK); |
59 | 87 | } |
60 | 88 |
|
| 89 | + private function validatePath(mixed $path): bool { |
| 90 | + if (!is_string($path)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + if (!str_starts_with($path, '/')) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + if (str_contains($path, '..')) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
61 | 105 | /** |
62 | 106 | * @NoAdminRequired |
63 | 107 | * @NoCSRFRequired |
|
0 commit comments