> */ public static function defaults() { return array( // More than 5 attempts in 10 minutes: challenge. 'ip_attempts' => array( 'limit' => 5, 'window' => 600, ), // 3 or more declines in 10 minutes: block 1 hour. 'ip_declines' => array( 'limit' => 3, 'window' => 600, 'block' => self::HOUR, ), // 10 or more declines in 10 minutes: block the prefix 1 hour. 'prefix_declines' => array( 'limit' => 10, 'window' => 600, 'block' => self::HOUR, ), // 3 declines in 24 hours: challenge. 'email_declines_soft' => array( 'limit' => 3, 'window' => self::DAY, ), // 5 declines in 24 hours: block 24 hours. 'email_declines' => array( 'limit' => 5, 'window' => self::DAY, 'block' => self::DAY, ), // 3 or more declines in 1 hour: block 24 hours. 'device_declines' => array( 'limit' => 3, 'window' => self::HOUR, 'block' => self::DAY, ), // 4 or more different emails or billing names from one address in 1 hour: +25 risk. 'ip_identities' => array( 'limit' => 4, 'window' => self::HOUR, ), // 3 or more failed payment retries on one order: lock that order 1 hour. 'order_retries' => array( 'limit' => 3, 'window' => self::DAY, 'block' => self::HOUR, ), ); } /** * Applies the limits to an identity's counts. * * @param array $counts Counts keyed ip_attempts, ip_declines, prefix_declines, * email_declines, device_declines, order_retries. * @param array|null $limits Limits; the defaults when null. * @return array Rules that fired. */ public static function evaluate( array $counts, $limits = null ) { $limits = is_array( $limits ) ? $limits : self::defaults(); $get = static function ( $key ) use ( $counts ) { return isset( $counts[ $key ] ) ? (int) $counts[ $key ] : 0; }; $fired = array(); $blocks = array( 'ip_declines' => array( 'ip', 'velocity_ip_declines' ), 'prefix_declines' => array( 'prefix', 'velocity_prefix_declines' ), 'email_declines' => array( 'email', 'velocity_email_declines' ), 'device_declines' => array( 'device', 'velocity_device_declines' ), 'order_retries' => array( 'order', 'order_locked' ), ); foreach ( $blocks as $key => $meta ) { if ( isset( $limits[ $key ] ) && $get( $key ) >= (int) $limits[ $key ]['limit'] ) { $fired[] = array( 'rule' => $meta[1], 'action' => 'block', 'ktype' => $meta[0], 'n' => $get( $key ), 'base' => (int) $limits[ $key ]['block'], ); } } if ( isset( $limits['ip_attempts'] ) && $get( 'ip_attempts' ) > (int) $limits['ip_attempts']['limit'] ) { $fired[] = array( 'rule' => 'velocity_ip_attempts', 'action' => 'challenge', 'ktype' => 'ip', 'n' => $get( 'ip_attempts' ), 'base' => 0, ); } if ( isset( $limits['email_declines_soft'], $limits['email_declines'] ) && $get( 'email_declines' ) >= (int) $limits['email_declines_soft']['limit'] && $get( 'email_declines' ) < (int) $limits['email_declines']['limit'] ) { $fired[] = array( 'rule' => 'velocity_email_declines', 'action' => 'challenge', 'ktype' => 'email', 'n' => $get( 'email_declines' ), 'base' => 0, ); } return $fired; } /** * How long a block lasts. Each repeat on the same key doubles it: 1 hour, 2, 4 … up to 7 days. * * @param int $base Length of the first block in seconds. * @param int $level How many times this key was blocked before. * @return int Seconds. */ public static function block_seconds( $base, $level ) { $base = max( 1, (int) $base ); $level = max( 0, min( 20, (int) $level ) ); return (int) min( self::MAX_BLOCK, $base * pow( 2, $level ) ); } }