Skip to content

Commit

Permalink
Add flood control
Browse files Browse the repository at this point in the history
closes #271
  • Loading branch information
tobyzerner committed Oct 22, 2015
1 parent c0364cb commit 415b68f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
4 changes: 4 additions & 0 deletions js/lib/App.js
Expand Up @@ -263,6 +263,10 @@ export default class App {
children = app.translator.trans('core.lib.error.not_found_message');
break;

case 429:
children = app.translator.trans('core.lib.error.rate_limit_exceeded_message');
break;

default:
children = app.translator.trans('core.lib.error.generic_message');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Command/StartDiscussionHandler.php
Expand Up @@ -60,7 +60,7 @@ public function handle(StartDiscussion $command)
$this->assertCan($actor, 'startDiscussion');

// Create a new Discussion entity, persist it, and dispatch domain
// events. Before persistance, though, fire an event to give plugins
// events. Before persistence, though, fire an event to give plugins
// an opportunity to alter the discussion entity based on data in the
// command they may have passed through in the controller.
$discussion = Discussion::start(
Expand Down
1 change: 1 addition & 0 deletions src/Core/CoreServiceProvider.php
Expand Up @@ -91,6 +91,7 @@ public function boot()
$events->subscribe('Flarum\Core\Listener\UserMetadataUpdater');
$events->subscribe('Flarum\Core\Listener\EmailConfirmationMailer');
$events->subscribe('Flarum\Core\Listener\DiscussionRenamedNotifier');
$events->subscribe('Flarum\Core\Listener\FloodController');

$events->subscribe('Flarum\Core\Access\DiscussionPolicy');
$events->subscribe('Flarum\Core\Access\GroupPolicy');
Expand Down
33 changes: 33 additions & 0 deletions src/Core/Exception/FloodingException.php
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Core\Exception;

use Exception;
use Tobscure\JsonApi\Exception\JsonApiSerializableInterface;

class FloodingException extends Exception implements JsonApiSerializableInterface
{
/**
* {@inheritdoc}
*/
public function getStatusCode()
{
return 429;
}

/**
* {@inheritdoc}
*/
public function getErrors()
{
return [];
}
}
75 changes: 75 additions & 0 deletions src/Core/Listener/FloodController.php
@@ -0,0 +1,75 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Core\Listener;

use DateTime;
use Flarum\Core\Exception\FloodingException;
use Flarum\Core\Post;
use Flarum\Core\User;
use Flarum\Event\DiscussionWillBeSaved;
use Flarum\Event\PostWillBeSaved;
use Illuminate\Contracts\Events\Dispatcher;

class FloodController
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(DiscussionWillBeSaved::class, [$this, 'whenDiscussionWillBeSaved']);
$events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']);
}

/**
* @param DiscussionWillBeSaved $event
*/
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
{
if ($event->discussion->exists) {
return;
}

$this->assertNotFlooding($event->actor);
}

/**
* @param PostWillBeSaved $event
*/
public function whenPostWillBeSaved(PostWillBeSaved $event)
{
if ($event->post->exists) {
return;
}

$this->assertNotFlooding($event->actor);
}

/**
* @param User $actor
* @throws FloodingException
*/
protected function assertNotFlooding(User $actor)
{
if ($this->isFlooding($actor)) {
throw new FloodingException;
}
}

/**
* @param User $actor
* @return bool
*/
protected function isFlooding(User $actor)
{
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();

This comment has been minimized.

Copy link
@franzliedke

franzliedke Oct 22, 2015

Contributor

Is ten seconds enough? Should that be configurable?

This comment has been minimized.

Copy link
@tobyzerner

tobyzerner Oct 22, 2015

Author Contributor

Probably should be configurable, yes

}
}

0 comments on commit 415b68f

Please sign in to comment.