settings = $settings; } /** * Fill $verdict->action and return the same object. * * human -> allow * agent_verified -> policy_overrides[agent_id] or declared_agent_action * agent_declared -> declared_agent_action (no per-agent override: the identity is unverified; * allow becomes queue when score >= block_threshold) * agent_suspected -> queue (an override may tighten this to block, never loosen to allow, * because a User-Agent string is trivially spoofed) * bot -> block when score >= block_threshold, else queue * * @param Verdict $verdict Scored verdict. * @return Verdict */ public function decide( Verdict $verdict ): Verdict { $source = self::SOURCE_CLASS_DEFAULT; switch ( $verdict->class ) { case Verdict::CLASS_HUMAN: $action = Verdict::ACTION_ALLOW; break; case Verdict::CLASS_AGENT_VERIFIED: // Only a cryptographically verified identity may use a per-agent override. $override = $this->override_for( $verdict ); if ( null !== $override ) { $action = $override; $source = self::SOURCE_OVERRIDE; } else { $action = $this->declared_agent_action(); $source = self::SOURCE_DECLARED_ACTION; } break; case Verdict::CLASS_AGENT_DECLARED: // A declared purpose is a claim, not proof. No per-agent override here // (agent_id comes from a spoofable User-Agent or Signature-Agent header), // and never "allow" when the request already looks scripted. $action = $this->declared_agent_action(); $source = self::SOURCE_DECLARED_ACTION; if ( Verdict::ACTION_ALLOW === $action && $verdict->score >= $this->block_threshold() ) { $action = Verdict::ACTION_QUEUE; $source = self::SOURCE_THRESHOLD; } break; case Verdict::CLASS_AGENT_SUSPECTED: $action = Verdict::ACTION_QUEUE; $override = $this->override_for( $verdict ); if ( Verdict::ACTION_BLOCK === $override ) { $action = Verdict::ACTION_BLOCK; $source = self::SOURCE_OVERRIDE; } break; case Verdict::CLASS_BOT: if ( $verdict->score >= $this->block_threshold() ) { $action = Verdict::ACTION_BLOCK; $source = self::SOURCE_THRESHOLD; } else { $action = Verdict::ACTION_QUEUE; } break; default: // Unknown class: never deliver silently. $action = Verdict::ACTION_QUEUE; break; } $verdict->action = $action; $verdict->meta['policy_source'] = $source; $verdict->meta['block_threshold'] = $this->block_threshold(); return $verdict; } /** * Per-agent override from settings, if any. Looks up by agent_id first, * then by the Signature-Agent host, so owners can key either way. * * @param Verdict $verdict Verdict with meta. * @return string|null allow|queue|block or null. */ public function override_for( Verdict $verdict ): ?string { $overrides = isset( $this->settings['policy_overrides'] ) && is_array( $this->settings['policy_overrides'] ) ? $this->settings['policy_overrides'] : array(); if ( empty( $overrides ) ) { return null; } $keys = array(); if ( ! empty( $verdict->meta['agent_id'] ) && is_string( $verdict->meta['agent_id'] ) ) { $keys[] = $verdict->meta['agent_id']; } if ( ! empty( $verdict->meta['signature_agent'] ) && is_string( $verdict->meta['signature_agent'] ) ) { $keys[] = $verdict->meta['signature_agent']; } foreach ( $keys as $key ) { if ( ! isset( $overrides[ $key ] ) || ! is_string( $overrides[ $key ] ) ) { continue; } $candidate = strtolower( trim( $overrides[ $key ] ) ); if ( self::is_valid_action( $candidate ) ) { return $candidate; } } return null; } /** * Setting declared_agent_action, validated. * * @return string */ public function declared_agent_action(): string { if ( isset( $this->settings['declared_agent_action'] ) && is_string( $this->settings['declared_agent_action'] ) ) { $value = strtolower( trim( $this->settings['declared_agent_action'] ) ); if ( self::is_valid_action( $value ) ) { return $value; } } return self::DEFAULT_DECLARED_AGENT_ACTION; } /** * Setting block_threshold (0..100). * * @return int */ public function block_threshold(): int { return $this->setting_int( 'block_threshold', self::DEFAULT_BLOCK_THRESHOLD ); } /** * Setting queue_threshold (0..100). Exposed for the admin UI; the Scorer * applies it when mapping score to class. * * @return int */ public function queue_threshold(): int { return $this->setting_int( 'queue_threshold', self::DEFAULT_QUEUE_THRESHOLD ); } /** * Whether a string is one of allow|queue|block. * * @param string $action Candidate. * @return bool */ public static function is_valid_action( string $action ): bool { return in_array( $action, Verdict::actions(), true ); } /** * Integer setting clamped to 0..100. */ private function setting_int( string $key, int $default ): int { if ( ! isset( $this->settings[ $key ] ) || ! is_numeric( $this->settings[ $key ] ) ) { return $default; } $value = (int) $this->settings[ $key ]; return max( 0, min( 100, $value ) ); } }