Skip to content

Commit

Permalink
Merge pull request #4 from lukeraymonddowning/disabling
Browse files Browse the repository at this point in the history
Allows disabling of Honey and environment detection.
  • Loading branch information
lukeraymonddowning committed Nov 27, 2020
2 parents 71eb2fa + c5681b2 commit 871bb57
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":5728:{a:2:{s:7:"defects";a:22:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:4;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:4;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:4;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:4;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:4;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:5;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;}s:5:"times";a:27:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.061;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.049;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.014;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.01;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":6522:{a:2:{s:7:"defects";a:26:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;}s:5:"times";a:31:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.007;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.007;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.051;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.044;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.01;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.002;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.009;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.002;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.043;}}}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ If spammer IP tracking is enabled, Honey can go one step further. By default, it
will block any IP address in the `spammers` table that has hit the `maximum_attempts` defined further down in the
config file. If you would like more granular control or wish to remove this functionality entirely, simply disable the feature.

#### Environments
You likely don't want to run Honey in every environment. Honey will only run in the environments specified in this array.
You can also manually disable Honey by calling the `disable` method.

```php
Honey::disable();
```

#### Checks
Each time the `honey` middleware is run or `Honey::check()` is called, Honey runs through an array of checks to determine
if the request is spam. You can tailor which checks are to be run by adding or removing items in the `checks` array.
Expand Down
16 changes: 15 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use Lukeraymonddowning\Honey\Checks\AlpineInputFilledCheck;
use Lukeraymonddowning\Honey\Checks\MinimumTimePassedCheck;
use Lukeraymonddowning\Honey\Checks\PresentButEmptyCheck;
use Lukeraymonddowning\Honey\Checks\UserIsBlockedSpammerCheck;
Expand All @@ -24,6 +23,21 @@
// Features::neverGonnaGiveYouUp(),
],

/**
* --------------------------------------------------------------------------
* Environments
* --------------------------------------------------------------------------
*
* You probably don't want to run Honey all the time, especially in tests and such.
* Here you can select the specific environments that Honey should run in. We've
* enabled `local` for you to test, but you might want to comment it out for
* day to day use.
*/
'environments' => [
'production',
'local'
],

/**
* --------------------------------------------------------------------------
* Checks
Expand Down
26 changes: 25 additions & 1 deletion src/Honey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Lukeraymonddowning\Honey\Models\Spammer;

class Honey
{
protected static Collection $checks;
protected static $failUsing;
protected static $config;
protected $isEnabled = false;
protected $hooks = [
'beforeFailing' => []
];

public function __construct(Collection $checks, callable $failUsing)
public function __construct(Collection $checks, callable $failUsing, $config)
{
static::$checks = $checks;
static::$failUsing = $failUsing;
static::$config = $config;

if (Features::spammerIpTrackingIsEnabled()) {
$this->registerSpammerTracking();
}

$this->isEnabled = in_array(config('app.env'), $config['environments']);
}

public function isEnabled()
{
return $this->isEnabled;
}

public function disable()
{
$this->isEnabled = false;
}

protected function registerSpammerTracking()
Expand All @@ -33,6 +49,10 @@ protected function registerSpammerTracking()

public function check($data)
{
if (!$this->isEnabled()) {
return true;
}

return static::$checks->map->passes($data)->filter()->count() === static::$checks->count();
}

Expand All @@ -48,6 +68,10 @@ public function beforeFailing(callable $hook)

public function fail()
{
if (!$this->isEnabled()) {
return;
}

$this->runHooks('beforeFailing');
return app()->call(static::$failUsing);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Providers/HoneyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
use Lukeraymonddowning\Honey\Http\Middleware\PreventSpam;
use Lukeraymonddowning\Honey\InputNameSelectors\InputNameSelector;
use Lukeraymonddowning\Honey\InputNameSelectors\StaticInputNameSelector;
use Lukeraymonddowning\Honey\Views\Honey as HoneyComponent;
use Lukeraymonddowning\Honey\Recaptcha;
use Lukeraymonddowning\Honey\Views\Honey as HoneyComponent;
use Lukeraymonddowning\Honey\Views\Recaptcha as RecaptchaComponent;

class HoneyServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('honey', fn() => new Honey(static::getChecks(), self::defaultMethodOfFailing()));
$this->app->singleton(
'honey',
fn() => new Honey(static::getChecks(), self::defaultMethodOfFailing(), config('honey'))
);
$this->app->singleton('honey-recaptcha', fn() => app(Recaptcha::class));
$this->app->singleton(InputNameSelector::class, fn() => app(static::getInputNameSelectorClass()));
$this->app->singleton(
Expand All @@ -40,7 +43,7 @@ protected static function getChecks()

protected static function defaultMethodOfFailing()
{
return function() {
return function () {
if (Features::rickrollingEnabled()) {
throw new HttpResponseException(redirect('https://youtu.be/dQw4w9WgXcQ'));
}
Expand Down
31 changes: 31 additions & 0 deletions tests/EnvironmentDisableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Lukeraymonddowning\Honey\Tests;


use Lukeraymonddowning\Honey\Facades\Honey;

class EnvironmentDisableTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set('app.env', 'local');
}

/** @test */
public function it_is_only_enabled_in_specified_environments()
{
// If Honey is not disabled, it will throw an exception here
Honey::fail();
$this->expectNotToPerformAssertions();
}

/** @test */
public function check_will_always_return_true_when_disabled()
{
$this->assertTrue(Honey::check([]));
}

}
1 change: 1 addition & 0 deletions tests/Feature/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function setUp(): void

protected function getEnvironmentSetUp($app)
{
$app['config']->set('honey.environments', ['testing']);
$app['config']->set('database.default', 'testing');
}
}
20 changes: 20 additions & 0 deletions tests/ManualDisableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace Lukeraymonddowning\Honey\Tests;


use Lukeraymonddowning\Honey\Facades\Honey;

class ManualDisableTest extends TestCase
{

/** @test */
public function it_can_be_disabled_manually()
{
$this->assertTrue(Honey::isEnabled());
Honey::disable();
$this->assertFalse(Honey::isEnabled());
}

}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);
$app['config']->set('honey.features', []);
$app['config']->set('honey.environments', ['testing']);
$app['config']->set('app.key', 'base64:F0OdoHVLC2wHKO2YsAA9EVvilD0O2i5B9TnoBYeg7qc=');
}
}

0 comments on commit 871bb57

Please sign in to comment.