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
Merged
12 changes: 12 additions & 0 deletions src/Settings/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Flarum\Settings;

use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Settings\Event\Saving;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -41,4 +43,14 @@ public function register()

$this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings');
}

public function boot(Dispatcher $events, SettingsValidator $settingsValidator)
{
$events->listen(
Saving::class,
function (Saving $event) use ($settingsValidator) {
$settingsValidator->assertValid($event->settings);
}
);
}
}
61 changes: 61 additions & 0 deletions src/Settings/SettingsValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Settings;

use Flarum\Foundation\AbstractValidator;

class SettingsValidator extends AbstractValidator
{
/**
* @var array
*/
protected $rules = [];

/**
* These rules apply to all attributes.
*
* Entries in the default DB settings table are limited to 65,000
* characters. We validate against this to avoid confusing errors.
*
* @var array
*/
protected $globalRules = [
'max:65000',
];

/**
* Make a new validator instance for this model.
*
* @param array $attributes
* @return \Illuminate\Validation\Validator
*/
protected function makeValidator(array $attributes)
{
// Apply global rules first.
$rules = array_map(function () {
return $this->globalRules;
}, $attributes);

// Apply attribute specific rules.
foreach ($rules as $key => $value) {
if (array_key_exists($key, $this->rules)) {
$rules[$key] = array_merge($rules[$key], $this->rules[$key]);
}
}

$validator = $this->validator->make($attributes, $rules, $this->getMessages());

foreach ($this->configuration as $callable) {
$callable($this, $validator);
}

return $validator;
}
}