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 the signal score >= queue_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. // "Allow" is for a declared agent on an otherwise clean request. Once the // request's own signals reach queue_threshold (the score that holds anyone // else), a line of purpose text must not buy delivery // (security review 2026-11-18). $action = $this->declared_agent_action(); $source = self::SOURCE_DECLARED_ACTION; if ( Verdict::ACTION_ALLOW === $action && $this->signal_score( $verdict ) >= $this->queue_threshold() ) { $action = Verdict::ACTION_QUEUE; $source = self::SOURCE_QUEUE_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; } elseif ( empty( $verdict->meta['agent_id'] ) && 'signature_agent' !== ( $verdict->meta['agent_matched_by'] ?? '' ) && $verdict->score >= $this->block_threshold() ) { // Only an agent from the bundled directory (or a signing domain) is held // instead of blocked. The word "bot" in a free-text User-Agent is not // enough to soften a block-level score (security review 2026-11-18). $action = Verdict::ACTION_BLOCK; $source = self::SOURCE_THRESHOLD; } 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 ); } /** * Score from the request's own signals, before the class floor. A declared agent * carries at least 50 as its class score; the Scorer keeps the uncapped sum of the * reason weights in meta['raw_score']. Falls back to the class score for verdicts * built without it. * * @param Verdict $verdict Verdict. * @return int */ private function signal_score( Verdict $verdict ): int { if ( isset( $verdict->meta['raw_score'] ) && is_numeric( $verdict->meta['raw_score'] ) ) { return (int) $verdict->meta['raw_score']; } return (int) $verdict->score; } /** * 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 ) ); } }