display name. */ public static function operators() { $defaults = array( 'https://chatgpt.com' => 'ChatGPT agent' ); $custom = array(); foreach ( preg_split( '/[\r\n]+/', (string) Agentrue_Settings::get_text( 'operators' ) ) as $line ) { $line = trim( $line ); if ( '' === $line ) { continue; } $parts = array_map( 'trim', explode( '|', $line, 2 ) ); $origin = Agentrue_WebBotAuth::origin( $parts[0] ); if ( $origin ) { $custom[ $origin ] = isset( $parts[1] ) && '' !== $parts[1] ? $parts[1] : wp_parse_url( $origin, PHP_URL_HOST ); } } return array_merge( $defaults, $custom ); } /** User-agent tokens of well-known AI crawlers / agents (for impostor detection). */ public static function ai_user_agents() { return array( 'ChatGPT-User', 'ChatGPT Agent', 'OAI-SearchBot', 'GPTBot', 'Claude-User', 'ClaudeBot', 'Claude-SearchBot', 'PerplexityBot', 'Perplexity-User', 'Google-Agent', 'Operator' ); } public static function ua_claim( $ua ) { foreach ( self::ai_user_agents() as $token ) { if ( false !== stripos( (string) $ua, $token ) ) { return $token; } } return ''; } /** * Classify a request description (testable without globals). * * @param array $req Request (see Agentrue_WebBotAuth::current_request()). */ public static function classify( array $req ) { $ops = self::operators(); $result = Agentrue_WebBotAuth::verify( $req, array_keys( $ops ), Agentrue_Settings::on( 'verify_any' ) ); $ua = isset( $req['headers']['user-agent'] ) ? (string) $req['headers']['user-agent'] : ''; $claim = self::ua_claim( $ua ); if ( 'unsigned' === $result['status'] ) { $result['status'] = $claim ? 'impostor_ua' : 'human'; if ( $claim ) { $result['reason'] = sprintf( 'claims to be %s but is not signed', $claim ); } } $result['label'] = isset( $ops[ $result['operator'] ] ) ? $ops[ $result['operator'] ] : ( $result['operator'] ? wp_parse_url( $result['operator'], PHP_URL_HOST ) : $claim ); $result['ua'] = substr( $ua, 0, 200 ); return $result; } public static function evaluate_request() { if ( null !== self::$current || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return; } $req = Agentrue_WebBotAuth::current_request(); self::$current = self::classify( $req ); if ( 'human' !== self::$current['status'] ) { self::log( self::$current, $req ); } if ( self::should_block( self::$current, $req ) ) { status_header( 403 ); nocache_headers(); header( 'Content-Type: text/plain; charset=UTF-8' ); echo 'Forbidden: agent identity could not be verified.'; // phpcs:ignore WordPress.Security.EscapeOutput exit; } } /** @return array Current request classification. */ public static function current() { if ( null === self::$current ) { self::$current = self::classify( Agentrue_WebBotAuth::current_request() ); } return self::$current; } public static function is_verified_agent() { $c = self::current(); return 'verified' === $c['status']; } protected static function is_checkout_request( array $req ) { $path = $req['path']; if ( false !== strpos( $path, '/wc/store/' ) && false !== strpos( $path, 'checkout' ) ) { return true; } if ( false !== strpos( $req['query'], 'wc-ajax=checkout' ) ) { return true; } $checkout = function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'checkout' ) : 0; if ( $checkout > 0 && 'POST' === $req['method'] ) { $slug = get_post_field( 'post_name', $checkout ); if ( $slug && false !== strpos( $path, '/' . $slug ) ) { return true; } } return false; } /** * Enforce mode rules: * - invalid (forged / expired signature): blocked everywhere. * - impostor_ua: blocked on checkout only (they may be harmless crawlers elsewhere), if enabled. * - unverifiable / unknown_operator: never blocked. */ public static function should_block( array $c, array $req ) { if ( 'enforce' !== Agentrue_Settings::get( 'mode' ) ) { return false; } $block = false; if ( 'invalid' === $c['status'] ) { $block = true; } elseif ( 'impostor_ua' === $c['status'] && Agentrue_Settings::on( 'block_impostor_checkout' ) && self::is_checkout_request( $req ) ) { $block = true; } /** * Filter whether to block a request. * * @param bool $block Block? * @param array $c Classification. * @param array $req Request description. */ return (bool) apply_filters( 'agentrue_should_block', $block, $c, $req ); } public static function send_status_header() { $c = self::current(); if ( 'verified' === $c['status'] && ! headers_sent() ) { header( 'X-Agentrue: verified' ); } } /* ------------------------------------------------------------------ */ /* Orders */ /* ------------------------------------------------------------------ */ /** * @param WC_Order $order Order being created. */ public static function tag_order( $order ) { if ( ! $order instanceof WC_Order ) { return; } $c = self::current(); $order->update_meta_data( '_agentrue_status', $c['status'] ); if ( 'human' !== $c['status'] ) { $order->update_meta_data( '_agentrue_operator', $c['operator'] ); $order->update_meta_data( '_agentrue_label', $c['label'] ); $order->update_meta_data( '_agentrue_reason', $c['reason'] ); $order->add_order_note( self::order_note( $c ) ); } } public static function order_note( array $c ) { switch ( $c['status'] ) { case 'verified': /* translators: %s agent name */ return sprintf( __( 'Agentrue: placed by a verified AI agent (%s). Signature checked against the operator\'s published key.', 'agentrue' ), $c['label'] ); case 'invalid': /* translators: %s reason */ return sprintf( __( 'Agentrue: WARNING – request carried a forged or invalid agent signature (%s). Review before fulfilling.', 'agentrue' ), $c['reason'] ); case 'impostor_ua': /* translators: %s claimed agent */ return sprintf( __( 'Agentrue: request claimed to be %s but was not signed. Possible impostor – review before fulfilling.', 'agentrue' ), $c['label'] ); default: /* translators: 1 status 2 reason */ return sprintf( __( 'Agentrue: agent status %1$s (%2$s).', 'agentrue' ), $c['status'], $c['reason'] ); } } public static function status_labels() { return array( 'verified' => __( 'Verified agent', 'agentrue' ), 'invalid' => __( 'Forged signature', 'agentrue' ), 'impostor_ua' => __( 'Unsigned impostor', 'agentrue' ), 'unknown_operator' => __( 'Unlisted operator', 'agentrue' ), 'unverifiable' => __( 'Could not verify', 'agentrue' ), 'human' => __( 'Human', 'agentrue' ), ); } /* ------------------------------------------------------------------ */ /* Log */ /* ------------------------------------------------------------------ */ public static function log( array $c, array $req ) { $log = get_option( self::LOG_OPTION, array() ); if ( ! is_array( $log ) ) { $log = array(); } array_unshift( $log, array( 't' => time(), 'status' => $c['status'], 'label' => sanitize_text_field( $c['label'] ), 'reason' => sanitize_text_field( $c['reason'] ), 'method' => sanitize_key( $req['method'] ), 'path' => sanitize_text_field( substr( $req['path'], 0, 200 ) ), 'ua' => sanitize_text_field( $c['ua'] ), 'blocked'=> self::should_block( $c, $req ), ) ); update_option( self::LOG_OPTION, array_slice( $log, 0, self::LOG_MAX ), false ); } public static function log_entries() { $log = get_option( self::LOG_OPTION, array() ); return is_array( $log ) ? $log : array(); } public static function summary( $days = 7 ) { $since = time() - $days * DAY_IN_SECONDS; $out = array( 'by_status' => array(), 'by_label' => array(), 'blocked' => 0, ); foreach ( self::log_entries() as $e ) { if ( $e['t'] < $since ) { continue; } $out['by_status'][ $e['status'] ] = ( isset( $out['by_status'][ $e['status'] ] ) ? $out['by_status'][ $e['status'] ] : 0 ) + 1; if ( $e['label'] ) { $out['by_label'][ $e['label'] ] = ( isset( $out['by_label'][ $e['label'] ] ) ? $out['by_label'][ $e['label'] ] : 0 ) + 1; } if ( ! empty( $e['blocked'] ) ) { $out['blocked']++; } } arsort( $out['by_label'] ); return $out; } /* ------------------------------------------------------------------ */ /* Self-test */ /* ------------------------------------------------------------------ */ /** * Proves verification works on this server: generates a throwaway key, signs a request, * verifies it, then verifies a tampered copy (must fail). */ public static function self_test() { if ( ! function_exists( 'sodium_crypto_sign_keypair' ) ) { return array( 'ok' => false, 'message' => __( 'libsodium is missing on this server, so signatures cannot be verified. Ask your host to enable the PHP sodium extension.', 'agentrue' ) ); } $kp = sodium_crypto_sign_keypair(); $secret = sodium_crypto_sign_secretkey( $kp ); $pub = sodium_crypto_sign_publickey( $kp ); $origin = 'https://selftest.agentrue.invalid'; $filter = function ( $keys, $o ) use ( $origin, $pub ) { return $o === $origin ? array( array( 'kty' => 'OKP', 'crv' => 'Ed25519', 'x' => Agentrue_WebBotAuth::b64url_encode( $pub ) ) ) : $keys; }; add_filter( 'agentrue_pre_directory_keys', $filter, 10, 2 ); $req = array( 'method' => 'GET', 'authority' => (string) wp_parse_url( home_url(), PHP_URL_HOST ), 'scheme' => 'https', 'path' => '/', 'query' => '', 'headers' => array(), ); $req['headers'] = Agentrue_WebBotAuth::sign( $req, $secret, $origin ); $good = Agentrue_WebBotAuth::verify( $req, array( $origin ) ); $bad = $req; $bad['authority'] = 'attacker.invalid'; $bad_res = Agentrue_WebBotAuth::verify( $bad, array( $origin ) ); remove_filter( 'agentrue_pre_directory_keys', $filter, 10 ); $ok = 'verified' === $good['status'] && 'invalid' === $bad_res['status']; return array( 'ok' => $ok, 'message' => $ok ? __( 'Self-test passed: a correctly signed request was verified and a tampered one was rejected.', 'agentrue' ) /* translators: 1 good status 2 bad status */ : sprintf( __( 'Self-test failed (signed: %1$s, tampered: %2$s).', 'agentrue' ), $good['status'], $bad_res['status'] ), ); } } /** * Public helper for other plugins/themes: is the current request a verified AI agent? * Use it to skip CAPTCHA or bot challenges for verified agents only. * * @return bool */ function agentrue_is_verified_agent() { return class_exists( 'Agentrue_Gate' ) && Agentrue_Gate::is_verified_agent(); }