Skip to content

Commit d382e2c

Browse files
committed
fix(settings): Throw JSON errors and return 400 status if JSON encode fails
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent b3bc012 commit d382e2c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

apps/settings/lib/Controller/AISettingsController.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCA\Settings\Settings\Admin\ArtificialIntelligence;
1212
use OCP\AppFramework\Controller;
13+
use OCP\AppFramework\Http;
1314
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
1415
use OCP\AppFramework\Http\DataResponse;
1516
use OCP\EventDispatcher\IEventDispatcher;
@@ -30,7 +31,7 @@ public function __construct(
3031
}
3132

3233
/**
33-
* Sets the email settings
34+
* Sets the AI settings
3435
*
3536
* @param array $settings
3637
* @return DataResponse
@@ -42,11 +43,16 @@ public function update($settings) {
4243
if (!isset($settings[$key])) {
4344
continue;
4445
}
45-
$changed = $this->appConfig->setValueString('core', $key, json_encode($settings[$key]), lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
46+
try {
47+
$value = json_encode($settings[$key], flags: \JSON_THROW_ON_ERROR);
48+
} catch (\JsonException) {
49+
return new DataResponse(['error' => "Setting value for '$key' must be JSON-compatible"], Http::STATUS_BAD_REQUEST);
50+
}
51+
$changed = $this->appConfig->setValueString('core', $key, $value, lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
4652
if ($changed) {
4753
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent(
4854
'AI configuration was changed by user %s: %s was set to %s',
49-
[$this->userId, $key, json_encode($settings[$key])]
55+
[$this->userId, $key, $value]
5056
));
5157
}
5258
}

0 commit comments

Comments
 (0)