createChallenge(new HGCFVendor\AltchaOrg\Altcha\CreateChallengeOptions( algorithm: new HGCFVendor\AltchaOrg\Altcha\Algorithm\Pbkdf2(), cost: 1000, keyPrefix: '00', expiresAt: time()+20*MINUTE_IN_SECONDS, data: ['page'=>(int)$page] ))->toArray(); } /** Validate before derivation, then atomically claim a single-use answer. */ function hgcf_verify_altcha($raw, $page) { if (!is_string($raw) || $raw === '' || strlen($raw)>8192) return false; $decoded = base64_decode($raw, true); if ($decoded === false) return false; $payload = json_decode($decoded, true, 12); if (!is_array($payload) || !is_array($payload['challenge']??null) || !is_array($payload['solution']??null)) return false; $challenge=$payload['challenge']; $p=$challenge['parameters']??null; $s=$payload['solution']; if (!is_array($p) || ($p['algorithm']??'')!=='PBKDF2/SHA-256' || ($p['cost']??null)!==1000 || ($p['keyLength']??null)!==32 || ($p['keyPrefix']??'')!=='00') return false; if (isset($p['keySignature']) || isset($p['memoryCost']) || isset($p['parallelism'])) return false; if (!is_array($p['data']??null) || ($p['data']['page']??null)!==(int)$page) return false; $expires=$p['expiresAt']??null; if (!is_int($expires) || $expires<=time() || $expires>time()+20*MINUTE_IN_SECONDS) return false; foreach (['nonce','salt'] as $key) if (!is_string($p[$key]??null) || !preg_match('/^[a-f0-9]{32}$/D',$p[$key])) return false; if (!is_string($challenge['signature']??null) || !preg_match('/^[a-f0-9]{64}$/D',$challenge['signature'])) return false; if (!is_int($s['counter']??null) || $s['counter']<0 || $s['counter']>1000000 || !is_string($s['derivedKey']??null) || !preg_match('/^[a-f0-9]{64}$/D',$s['derivedKey'])) return false; try { $result=hgcf_altcha_engine()->verifySolution(new HGCFVendor\AltchaOrg\Altcha\VerifySolutionOptions( payload: $payload, algorithm: new HGCFVendor\AltchaOrg\Altcha\Algorithm\Pbkdf2() )); if (!$result->verified) return false; } catch (Throwable $error) { return false; } // Unique option_name index prevents simultaneous replays. No form data is stored. return add_option('hgcf_pow_used_'.hash('sha256',$challenge['signature']), $expires, '', false); } function hgcf_altcha_cleanup() { global $wpdb; $keys=$wpdb->get_col($wpdb->prepare("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s AND CAST(option_value AS UNSIGNED) < %d LIMIT 1000", $wpdb->esc_like('hgcf_pow_used_').'%', time())); foreach($keys as $key)delete_option($key); if(!hgcf_altcha_enabled()&&!$keys)wp_clear_scheduled_hook('hgcf_altcha_cleanup'); } add_action('hgcf_altcha_cleanup','hgcf_altcha_cleanup'); function hgcf_altcha_schedule() { if (hgcf_altcha_enabled()) { if (!wp_next_scheduled('hgcf_altcha_cleanup')) wp_schedule_event(time()+HOUR_IN_SECONDS,'hourly','hgcf_altcha_cleanup'); } } add_action('init','hgcf_altcha_schedule'); register_deactivation_hook(HGCF_FILE, function(){wp_clear_scheduled_hook('hgcf_altcha_cleanup');hgcf_altcha_cleanup();}); function hgcf_altcha_endpoint($request) { if (!hgcf_altcha_enabled()) return new WP_Error('hgcf_disabled','Not found.',['status'=>404]); $page=(int)$request->get_param('page'); $post=get_post($page); if (!$post || !is_post_publicly_viewable($post) || (!has_shortcode($post->post_content,'hg_contactform') && !has_shortcode($post->post_content,'mlh_contact'))) return new WP_Error('hgcf_page','Invalid form page.',['status'=>404]); $key='hgcf_pow_rate_'.hash_hmac('sha256',$_SERVER['REMOTE_ADDR']??'unknown',wp_salt('auth')); $rate=get_transient($key); if ($rate && $rate['count']>=30) return new WP_Error('hgcf_rate','Please try again in a few minutes.',['status'=>429]); $rate=$rate?:['count'=>0,'until'=>time()+5*MINUTE_IN_SECONDS]; $rate['count']++; set_transient($key,$rate,max(1,$rate['until']-time())); try { return new WP_REST_Response(hgcf_create_altcha_challenge($page),200,['Cache-Control'=>'no-store, private, max-age=0']); } catch (Throwable $error) { return new WP_Error('hgcf_unavailable','Verification temporarily unavailable.',['status'=>503]); } } add_action('rest_api_init',function(){ register_rest_route('hg-contactform/v1','/challenge',[ 'methods'=>'GET','callback'=>'hgcf_altcha_endpoint','permission_callback'=>'__return_true', 'args'=>['page'=>['required'=>true,'type'=>'integer','minimum'=>1]] ]); }); add_filter('rest_post_dispatch',function($response,$server,$request){ if ($request->get_route()==='/hg-contactform/v1/challenge' && $response instanceof WP_REST_Response) $response->header('Cache-Control','no-store, private, max-age=0'); return $response; },10,3); add_filter('script_loader_tag',function($tag,$handle){ if ($handle!=='hgcf-altcha') return $tag; $tag=preg_replace('/\s+type=([\'"])text\/javascript\1/','',$tag); return str_replace('