Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce 65k character limit when attempting to update setting values. #3162

Merged
merged 14 commits into from
Nov 12, 2021
16 changes: 15 additions & 1 deletion src/Api/Controller/SetSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Flarum\Settings\Event;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -33,10 +35,11 @@ class SetSettingsController implements RequestHandlerInterface
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher)
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $dispatcher, Factory $validatorFactory)
{
$this->settings = $settings;
$this->dispatcher = $dispatcher;
$this->validatorFactory = $validatorFactory;
}

/**
Expand All @@ -48,6 +51,17 @@ public function handle(ServerRequestInterface $request): ResponseInterface

$settings = $request->getParsedBody();

$validation = $this->validatorFactory->make(
$settings,
array_map(function ($value) {
return 'max:65000';
}, $settings),
);

if ($validation->fails()) {
throw new ValidationException($validation);
}

$this->dispatcher->dispatch(new Event\Saving($settings));

foreach ($settings as $k => $v) {
Expand Down