50, 'network_listed' => 40, 'headless' => 30, 'unknown_origin' => 30, 'many_emails' => 25, 'fast_submit' => 20, 'hosting' => 20, 'small_basket' => 15, 'recent_decline' => 15, 'disposable_email' => 15, 'country_mismatch' => 10, 'account_age' => -20, 'paid_device' => -30, ); /** * Score from which an attempt is challenged and from which it is blocked. * * @param string $level relaxed, balanced or strict. * @param bool $lockdown Whether the store is in lockdown. * @return array{0:int,1:int} Challenge threshold, block threshold. */ public static function thresholds( $level, $lockdown ) { $challenge = $lockdown ? 15 : 30; $block = $lockdown ? 40 : 60; if ( 'relaxed' === $level ) { $challenge += 10; $block += 10; } elseif ( 'strict' === $level ) { $challenge -= 10; $block -= 10; } return array( $challenge, $block ); } /** * The decision a score alone leads to. * * @param int $score Risk score. * @param string $level relaxed, balanced or strict. * @param bool $lockdown Whether the store is in lockdown. * @return string allow, challenge or block. */ public static function decide( $score, $level, $lockdown ) { list( $challenge, $block ) = self::thresholds( $level, $lockdown ); if ( $score >= $block ) { return self::BLOCK; } return $score >= $challenge ? self::CHALLENGE : self::ALLOW; } /** * Decides one attempt. * * Facts ($f), all optional: * trusted '' or why the visitor is always allowed: 'staff', 'customer', 'allowlist' * honeypot bool the hidden trap field was filled in * blocked '' or the reason code of a block already in force for this visitor * token 'ok', 'missing', 'invalid' or 'reused' * token_age int|null seconds since the token was issued (only when its signature is right) * network int 0–100 score on the shared list, 0 when not listed * headless bool scripted client * origin_unknown bool order attribution says the order came from nowhere * ip_identities int different emails or billing names from this address in the last hour * hosting bool address belongs to a hosting company * guest bool * small_basket bool guest buying one item for under the small-basket total * basket_text string the basket total as text, for the label * recent_decline bool email or device declined in the last 24 hours * disposable bool throwaway email domain * ip_country string two-letter code or '' * billing_country string two-letter code or '' * account_age_days int|null age of the logged-in account * paid_device bool this device has paid here before * velocity array rules fired, from Limits::evaluate() * has_pass bool a valid "I'm human" pass came with the attempt * total float|null basket total * * Options ($o): level, lockdown, challenge_guests, block_unknown, guest_minimum (float|null), * identities_limit (int). * * @param array $f Facts. * @param array $o Options. * @return array{decision:string,score:int,signals:array,reasons:string[],passed:bool,policy_only:bool} */ public static function evaluate( array $f, array $o = array() ) { $f = array_merge( array( 'trusted' => '', 'honeypot' => false, 'blocked' => '', 'token' => 'ok', 'token_age' => null, 'network' => 0, 'headless' => false, 'origin_unknown' => false, 'ip_identities' => 0, 'hosting' => false, 'guest' => true, 'small_basket' => false, 'basket_text' => '', 'recent_decline' => false, 'disposable' => false, 'ip_country' => '', 'billing_country' => '', 'account_age_days' => null, 'paid_device' => false, 'velocity' => array(), 'has_pass' => false, 'total' => null, ), $f ); $o = array_merge( array( 'level' => 'balanced', 'lockdown' => false, 'challenge_guests' => true, 'block_unknown' => true, 'guest_minimum' => null, 'identities_limit' => 4, ), $o ); $signals = array(); $add = static function ( $code, $points, $args = array() ) use ( &$signals ) { $signals[] = array( 'code' => $code, 'points' => (int) $points, 'args' => $args, ); }; // Always allowed: staff, customers with a completed order, the merchant's allowlist. if ( '' !== $f['trusted'] ) { $add( 'trusted_' . $f['trusted'], 0 ); return self::result( self::ALLOW, 0, $signals, false, false ); } $force_block = false; $force_challenge = false; if ( $f['honeypot'] ) { $add( 'honeypot', 100 ); $force_block = true; } if ( '' !== $f['blocked'] ) { $add( $f['blocked'], 'manual_block' === $f['blocked'] ? 100 : 60, array( 'standing' => 1 ) ); $force_block = true; } foreach ( (array) $f['velocity'] as $rule ) { if ( $rule['rule'] === $f['blocked'] ) { continue; } if ( 'block' === $rule['action'] ) { $add( $rule['rule'], 60, array( 'n' => (int) $rule['n'] ) ); $force_block = true; } else { $add( $rule['rule'], 15, array( 'n' => (int) $rule['n'] ) ); $force_challenge = true; } } $token_bad = 'ok' !== $f['token']; if ( $token_bad ) { $add( 'reused' === $f['token'] ? 'token_reused' : 'no_token', self::POINTS['no_token'] ); } if ( null !== $f['token_age'] && $f['token_age'] < Token::FAST_SUBMIT ) { $add( 'fast_submit', self::POINTS['fast_submit'], array( 'n' => max( 0, (int) $f['token_age'] ) ) ); } if ( $f['network'] > 0 ) { $add( 'network_listed', (int) round( self::POINTS['network_listed'] * min( 100, (int) $f['network'] ) / 100 ) ); } if ( $f['headless'] ) { $add( 'headless', self::POINTS['headless'] ); } if ( $f['origin_unknown'] ) { $add( 'unknown_origin', self::POINTS['unknown_origin'] ); } if ( $f['ip_identities'] >= $o['identities_limit'] ) { $add( 'many_emails', self::POINTS['many_emails'], array( 'n' => (int) $f['ip_identities'] ) ); } if ( $f['hosting'] ) { // Relaxed halves this one: real customers on a VPN are common for some stores. $add( 'hosting', 'relaxed' === $o['level'] ? (int) ( self::POINTS['hosting'] / 2 ) : self::POINTS['hosting'] ); } if ( $f['guest'] && $f['small_basket'] ) { $add( 'small_basket', self::POINTS['small_basket'], array( 'total' => (string) $f['basket_text'] ) ); } if ( $f['recent_decline'] ) { $add( 'recent_decline', self::POINTS['recent_decline'] ); } if ( $f['disposable'] ) { $add( 'disposable_email', self::POINTS['disposable_email'] ); } $countries_known = '' !== $f['ip_country'] && '' !== $f['billing_country']; if ( $countries_known && strtoupper( $f['ip_country'] ) !== strtoupper( $f['billing_country'] ) ) { $add( 'country_mismatch', self::POINTS['country_mismatch'] ); } if ( null !== $f['account_age_days'] && $f['account_age_days'] > 30 ) { $add( 'account_age', self::POINTS['account_age'], array( 'days' => (int) $f['account_age_days'] ) ); } if ( $f['paid_device'] ) { $add( 'paid_device', self::POINTS['paid_device'] ); } $score = 0; foreach ( $signals as $signal ) { $score += $signal['points']; } $score = max( 0, $score ); $decision = self::decide( $score, $o['level'], $o['lockdown'] ); $scored = $decision; if ( $o['lockdown'] ) { // In lockdown a missing token is a block, not just +50. if ( $token_bad ) { $force_block = true; } if ( $o['block_unknown'] && $f['origin_unknown'] ) { $force_block = true; } if ( null !== $o['guest_minimum'] && $f['guest'] && null !== $f['total'] && (float) $f['total'] < (float) $o['guest_minimum'] ) { $add( 'lockdown_min_total', 0 ); $force_block = true; } } if ( $force_block ) { $decision = self::BLOCK; } elseif ( $force_challenge && self::ALLOW === $decision ) { $decision = self::CHALLENGE; } // Policy checks: asked of guests even when nothing looks wrong. $policy_only = false; if ( self::ALLOW === $decision && $f['guest'] ) { if ( $o['lockdown'] && $o['challenge_guests'] ) { $add( 'lockdown_guest', 0 ); $decision = self::CHALLENGE; $policy_only = true; } elseif ( 'strict' === $o['level'] && ! $f['paid_device'] ) { $add( 'strict_guest', 0 ); $decision = self::CHALLENGE; $policy_only = true; } } // A valid pass answers a challenge. It never lifts a block. $passed = false; if ( self::CHALLENGE === $decision && $f['has_pass'] ) { $decision = self::ALLOW; $passed = true; $add( 'human_check_passed', 0 ); } // Harmless signs, so the merchant sees the whole picture. if ( ! $token_bad ) { $add( 'browsed', 0 ); } if ( $f['guest'] ) { $add( 'guest', 0 ); } if ( $countries_known && strtoupper( $f['ip_country'] ) === strtoupper( $f['billing_country'] ) ) { $add( 'same_country', 0 ); } if ( null !== $f['total'] && ! $f['small_basket'] ) { $add( 'normal_basket', 0 ); } // A forced block reads as a likely bot on the "bot chance" bar; the hidden trap is certain. if ( self::BLOCK === $decision && self::BLOCK !== $scored && $score < 60 ) { $score = 60; } if ( $f['honeypot'] || 'manual_block' === $f['blocked'] ) { $score = 100; } return self::result( $decision, $score, $signals, $passed, $policy_only ); } /** * Shapes the result and picks the top three reasons. * * @param string $decision Decision. * @param int $score Risk score. * @param array $signals Signals. * @param bool $passed Whether a pass answered a challenge. * @param bool $policy_only Whether the challenge is only the "ask every guest" policy. * @return array */ private static function result( $decision, $score, array $signals, $passed, $policy_only ) { // Strongest first; equal points keep the order they were noticed in. $order = array(); foreach ( $signals as $index => $signal ) { $order[ $index ] = array( -$signal['points'], $index ); } asort( $order ); $sorted = array(); foreach ( array_keys( $order ) as $index ) { $sorted[] = $signals[ $index ]; } $reasons = array(); foreach ( $sorted as $signal ) { if ( $signal['points'] > 0 || in_array( $signal['code'], array( 'lockdown_guest', 'strict_guest', 'lockdown_min_total' ), true ) ) { $reasons[] = $signal['code']; } } if ( empty( $reasons ) ) { foreach ( $sorted as $signal ) { if ( $signal['points'] < 0 || 0 === strpos( $signal['code'], 'trusted_' ) ) { $reasons[] = $signal['code']; } } } if ( empty( $reasons ) ) { $reasons[] = 'normal_shopper'; } return array( 'decision' => $decision, 'score' => (int) min( 100, max( 0, $score ) ), 'signals' => $sorted, 'reasons' => array_slice( array_values( array_unique( $reasons ) ), 0, 3 ), 'passed' => (bool) $passed, 'policy_only' => (bool) $policy_only, ); } }