Add progressive throttling of subscriptions from the same IP address [MAILPOET-1128]
This commit is contained in:
@ -10,7 +10,7 @@ use MailPoet\Form\Util\FieldNameObfuscator;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\Subscription\Throttling as SubscriptionThrottling;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -98,16 +98,10 @@ class Subscribers extends APIEndpoint {
|
||||
$data = array_intersect_key($data, array_flip($form_fields));
|
||||
|
||||
// make sure we don't allow too many subscriptions with the same ip address
|
||||
$subscription_count = Subscriber::where(
|
||||
'subscribed_ip',
|
||||
Helpers::getIP()
|
||||
)->whereRaw(
|
||||
'(TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) < ? OR TIME_TO_SEC(TIMEDIFF(NOW(), updated_at)) < ?)',
|
||||
array(self::SUBSCRIPTION_LIMIT_COOLDOWN, self::SUBSCRIPTION_LIMIT_COOLDOWN)
|
||||
)->count();
|
||||
$timeout = SubscriptionThrottling::throttle();
|
||||
|
||||
if($subscription_count > 0) {
|
||||
throw new \Exception(__('You need to wait before subscribing again.', 'mailpoet'));
|
||||
if($timeout > 0) {
|
||||
throw new \Exception(sprintf(__('You need to wait %d seconds before subscribing again.', 'mailpoet'), $timeout));
|
||||
}
|
||||
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
|
@ -68,6 +68,7 @@ class Database {
|
||||
$subscribers = Env::$db_prefix . 'subscribers';
|
||||
$subscriber_segment = Env::$db_prefix . 'subscriber_segment';
|
||||
$subscriber_custom_field = Env::$db_prefix . 'subscriber_custom_field';
|
||||
$subscriber_ips = Env::$db_prefix . 'subscriber_ips';
|
||||
$newsletter_segment = Env::$db_prefix . 'newsletter_segment';
|
||||
$scheduled_tasks = Env::$db_prefix . 'scheduled_tasks';
|
||||
$scheduled_task_subscribers = Env::$db_prefix . 'scheduled_task_subscribers';
|
||||
@ -92,6 +93,7 @@ class Database {
|
||||
define('MP_SUBSCRIBERS_TABLE', $subscribers);
|
||||
define('MP_SUBSCRIBER_SEGMENT_TABLE', $subscriber_segment);
|
||||
define('MP_SUBSCRIBER_CUSTOM_FIELD_TABLE', $subscriber_custom_field);
|
||||
define('MP_SUBSCRIBER_IPS_TABLE', $subscriber_ips);
|
||||
define('MP_SCHEDULED_TASKS_TABLE', $scheduled_tasks);
|
||||
define('MP_SCHEDULED_TASK_SUBSCRIBERS_TABLE', $scheduled_task_subscribers);
|
||||
define('MP_SENDING_QUEUES_TABLE', $sending_queues);
|
||||
|
@ -23,6 +23,7 @@ class Migrator {
|
||||
'subscribers',
|
||||
'subscriber_segment',
|
||||
'subscriber_custom_field',
|
||||
'subscriber_ips',
|
||||
'newsletters',
|
||||
'newsletter_templates',
|
||||
'newsletter_option_fields',
|
||||
@ -207,6 +208,16 @@ class Migrator {
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function subscriberIps() {
|
||||
$attributes = array(
|
||||
'ip varchar(45) NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,',
|
||||
'PRIMARY KEY (created_at, ip),',
|
||||
'KEY ip (ip)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function newsletters() {
|
||||
$attributes = array(
|
||||
'id int(11) unsigned NOT NULL AUTO_INCREMENT,',
|
||||
|
8
lib/Models/SubscriberIP.php
Normal file
8
lib/Models/SubscriberIP.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class SubscriberIP extends Model {
|
||||
public static $_table = MP_SUBSCRIBER_IPS_TABLE;
|
||||
}
|
55
lib/Subscription/Throttling.php
Normal file
55
lib/Subscription/Throttling.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace MailPoet\Subscription;
|
||||
|
||||
use MailPoet\Models\SubscriberIP;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
class Throttling {
|
||||
static function throttle() {
|
||||
$subscription_limit_enabled = Hooks::applyFilters('mailpoet_subscription_limit_enabled', true);
|
||||
|
||||
$subscription_limit_window = Hooks::applyFilters('mailpoet_subscription_limit_window', DAY_IN_SECONDS);
|
||||
$subscription_limit_base = Hooks::applyFilters('mailpoet_subscription_limit_base', MINUTE_IN_SECONDS);
|
||||
|
||||
$subscriber_ip = Helpers::getIP();
|
||||
|
||||
if($subscription_limit_enabled && !is_user_logged_in()) {
|
||||
if(!empty($subscriber_ip)) {
|
||||
$subscription_count = SubscriberIP::where('ip', $subscriber_ip)
|
||||
->whereRaw(
|
||||
'(`created_at` >= NOW() - INTERVAL ? SECOND)',
|
||||
array((int)$subscription_limit_window)
|
||||
)->count();
|
||||
|
||||
if($subscription_count > 0) {
|
||||
$timeout = $subscription_limit_base * pow(2, $subscription_count - 1);
|
||||
$existing_user = SubscriberIP::where('ip', $subscriber_ip)
|
||||
->whereRaw(
|
||||
'(`created_at` >= NOW() - INTERVAL ? SECOND)',
|
||||
array((int)$timeout)
|
||||
)->findOne();
|
||||
|
||||
if(!empty($existing_user)) {
|
||||
return $timeout;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ip = SubscriberIP::create();
|
||||
$ip->ip = $subscriber_ip;
|
||||
$ip->save();
|
||||
|
||||
self::purge($subscription_limit_window);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static function purge($interval) {
|
||||
return SubscriberIP::whereRaw(
|
||||
'(`created_at` < NOW() - INTERVAL ? SECOND)',
|
||||
array($interval)
|
||||
)->deleteMany();
|
||||
}
|
||||
}
|
@ -10,6 +10,10 @@ class Hooks {
|
||||
return self::callWithFallback('apply_filters', func_get_args());
|
||||
}
|
||||
|
||||
static function removeFilter() {
|
||||
return self::callWithFallback('remove_filter', func_get_args());
|
||||
}
|
||||
|
||||
static function addAction() {
|
||||
return self::callWithFallback('add_action', func_get_args());
|
||||
}
|
||||
@ -18,6 +22,10 @@ class Hooks {
|
||||
return self::callWithFallback('do_action', func_get_args());
|
||||
}
|
||||
|
||||
static function removeAction() {
|
||||
return self::callWithFallback('remove_action', func_get_args());
|
||||
}
|
||||
|
||||
private static function callWithFallback($func, $args) {
|
||||
$local_func = __NAMESPACE__ . '\\' . $func;
|
||||
if(function_exists($local_func)) {
|
||||
|
@ -25,6 +25,7 @@ $models = array(
|
||||
'Subscriber',
|
||||
'SubscriberCustomField',
|
||||
'SubscriberSegment',
|
||||
'SubscriberIP',
|
||||
'StatisticsOpens',
|
||||
'StatisticsClicks',
|
||||
'StatisticsNewsletters',
|
||||
|
@ -8,6 +8,7 @@ use MailPoet\API\JSON\Response as APIResponse;
|
||||
use MailPoet\Form\Util\FieldNameObfuscator;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberIP;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Setting;
|
||||
|
||||
@ -516,7 +517,7 @@ class SubscribersTest extends \MailPoetTest {
|
||||
));
|
||||
$this->fail('It should not be possible to subscribe a second time so soon');
|
||||
} catch(\Exception $e) {
|
||||
expect($e->getMessage())->equals('You need to wait before subscribing again.');
|
||||
expect($e->getMessage())->equals('You need to wait 60 seconds before subscribing again.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -544,12 +545,13 @@ class SubscribersTest extends \MailPoetTest {
|
||||
));
|
||||
$this->fail('It should not be possible to resubscribe a second time so soon');
|
||||
} catch(\Exception $e) {
|
||||
expect($e->getMessage())->equals('You need to wait before subscribing again.');
|
||||
expect($e->getMessage())->equals('You need to wait 60 seconds before subscribing again.');
|
||||
}
|
||||
}
|
||||
|
||||
function _after() {
|
||||
Segment::deleteMany();
|
||||
Subscriber::deleteMany();
|
||||
SubscriberIP::deleteMany();
|
||||
}
|
||||
}
|
||||
|
60
tests/unit/Subscription/ThrottlingTest.php
Normal file
60
tests/unit/Subscription/ThrottlingTest.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Subscription;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use MailPoet\Models\SubscriberIP;
|
||||
use MailPoet\Subscription\Throttling;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
class ThrottlingTest extends \MailPoetTest {
|
||||
function testItProgressivelyThrottlesSubscriptions() {
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
expect(Throttling::throttle())->equals(false);
|
||||
expect(Throttling::throttle())->equals(60);
|
||||
for($i = 0; $i < 11; $i++) {
|
||||
$ip = SubscriberIP::create();
|
||||
$ip->ip = '127.0.0.1';
|
||||
$ip->created_at = Carbon::now()->subMinutes($i);
|
||||
$ip->save();
|
||||
}
|
||||
expect(Throttling::throttle())->equals(MINUTE_IN_SECONDS * pow(2, 10));
|
||||
}
|
||||
|
||||
function testItDoesNotThrottleIfDisabledByAHook() {
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
Hooks::addFilter('mailpoet_subscription_limit_enabled', '__return_false');
|
||||
expect(Throttling::throttle())->equals(false);
|
||||
expect(Throttling::throttle())->equals(false);
|
||||
Hooks::removeFilter('mailpoet_subscription_limit_enabled', '__return_false');
|
||||
expect(Throttling::throttle())->greaterThan(0);
|
||||
}
|
||||
|
||||
function testItDoesNotThrottleForLoggedInUsers() {
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$wp_users = get_users();
|
||||
wp_set_current_user($wp_users[0]->ID);
|
||||
expect(Throttling::throttle())->equals(false);
|
||||
expect(Throttling::throttle())->equals(false);
|
||||
wp_set_current_user(0);
|
||||
expect(Throttling::throttle())->greaterThan(0);
|
||||
}
|
||||
|
||||
function testItPurgesOldSubscriberIps() {
|
||||
$ip = SubscriberIP::create();
|
||||
$ip->ip = '127.0.0.1';
|
||||
$ip->save();
|
||||
|
||||
$ip2 = SubscriberIP::create();
|
||||
$ip2->ip = '127.0.0.1';
|
||||
$ip2->created_at = Carbon::now()->subDays(1)->subSeconds(1);
|
||||
$ip2->save();
|
||||
|
||||
expect(SubscriberIP::count())->equals(2);
|
||||
Throttling::throttle();
|
||||
expect(SubscriberIP::count())->equals(1);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
SubscriberIP::deleteMany();
|
||||
}
|
||||
}
|
@ -24,6 +24,11 @@ class HooksTest extends \MailPoetTest {
|
||||
Hooks::doAction($this->action, $test_value, $test_value2);
|
||||
|
||||
expect($called)->true();
|
||||
|
||||
$called = false;
|
||||
Hooks::removeAction($this->action, $callback);
|
||||
Hooks::doAction($this->action);
|
||||
expect($called)->false();
|
||||
}
|
||||
|
||||
function testItCanProcessFilters() {
|
||||
@ -41,5 +46,10 @@ class HooksTest extends \MailPoetTest {
|
||||
|
||||
expect($called)->true();
|
||||
expect($result)->equals($test_value);
|
||||
|
||||
$called = false;
|
||||
Hooks::removeFilter($this->filter, $callback);
|
||||
Hooks::applyFilters($this->filter, $test_value);
|
||||
expect($called)->false();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user