2048) return false; $parts = wp_parse_url($url); $home = wp_parse_url(home_url('/')); if (!$parts || isset($parts['query']) || isset($parts['fragment']) || isset($parts['user']) || isset($parts['pass']) || strtolower($parts['scheme'] ?? '') !== strtolower($home['scheme']) || strtolower($parts['host'] ?? '') !== strtolower($home['host']) || ($parts['port'] ?? 443) !== ($home['port'] ?? 443)) return false; $path = $parts['path'] ?? '/'; $base = rtrim($home['path'] ?? '/', '/'); if ($path === '/' || rtrim($path, '/') === $base || ($base && strpos($path, $base . '/') !== 0) || preg_match('#[\\\\\s]|//|%(?:2e|2f|5c|25)|(?:^|/)\.{1,2}(?:/|$)#i', $path) || preg_match('#(?:^|/)(?:wp-admin|wp-json|wp-login\.php|wp-cron\.php|xmlrpc\.php|feed|robots\.txt|[^/]*sitemap[^/]*)(?:/|$)#i', $path)) return false; return $path; } function ai4ranks_redirect_key($path) { return 'ai4ranks_redirect_' . hash('sha256', $path); } function ai4ranks_redirect_destination($url) { $path = ai4ranks_redirect_path($url); if (!$path || get_option(ai4ranks_redirect_key($path), false)) return false; $id = url_to_postid($url); $post = $id ? get_post($id) : null; return $post && $post->post_status === 'publish' && in_array($post->post_type, array('page', 'post', 'product'), true) && get_permalink($id) === $url; } function ai4ranks_redirect_request($request) { $params = $request->get_json_params(); if (!is_array($params)) return new WP_REST_Response(array('message' => 'A JSON request body is required'), 400); $source = $params['source'] ?? null; $path = ai4ranks_redirect_path($source); if (!$path) return new WP_REST_Response(array('message' => 'Only exact public same-site paths are supported'), 422); $key = ai4ranks_redirect_key($path); $mapping = get_option($key, false); $mode = isset($params['mode']) && is_string($params['mode']) ? $params['mode'] : ''; $operation = isset($params['operation_id']) && is_string($params['operation_id']) ? strtolower($params['operation_id']) : ''; if (!preg_match('/^[a-f0-9]{64}$/D', $operation)) return new WP_REST_Response(array('message' => 'A signed operation_id is required'), 401); if ($mode === 'inspect') return new WP_REST_Response(array('source' => $source, 'mapping' => $mapping ?: null), 200); // Old mappings stored redirect_ before per-request signatures were // introduced. A signed remove may name that immutable mapping explicitly; // new installs always store the signed 64-hex operation id. $mapping_operation = $params['mapping_operation_id'] ?? null; if (!is_string($mapping_operation) || $mapping_operation === '') $mapping_operation = $operation; if (!preg_match('/^(?:[a-f0-9]{64}|redirect_[a-f0-9]{32})$/D', $mapping_operation)) return new WP_REST_Response(array('message' => 'Invalid mapping operation'), 422); if ($mode !== 'remove' && !hash_equals($operation, $mapping_operation)) return new WP_REST_Response(array('message' => 'Invalid mapping operation'), 422); if ($mode === 'remove') { if (!$mapping || ($mapping['operation_id'] ?? '') !== $mapping_operation) return new WP_REST_Response(array('message' => 'Mapping changed; removal blocked'), 409); global $wpdb; $deleted = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->options} WHERE option_name = %s AND BINARY option_value = BINARY %s", $key, maybe_serialize($mapping))); if ($deleted !== 1) return new WP_REST_Response(array('message' => 'Mapping changed; removal blocked'), 409); wp_cache_delete($key, 'options'); wp_cache_delete('alloptions', 'options'); return new WP_REST_Response(array('source' => $source, 'mapping' => null), 200); } $target = $params['target'] ?? null; $code = $params['code'] ?? null; if ($mode !== 'install' || !in_array($code, array(301, 308), true) || !ai4ranks_redirect_destination($target) || rtrim($source, '/') === rtrim($target, '/') || url_to_postid($source)) return new WP_REST_Response(array('message' => 'Source or destination is no longer eligible'), 409); $value = array('source' => $source, 'target' => $target, 'code' => $code, 'operation_id' => $mapping_operation); // Unique option_name prevents concurrent proposals from replacing another mapping. if (!add_option($key, $value, '', false)) return new WP_REST_Response(array('message' => 'An existing redirect must be reviewed first'), 409); return new WP_REST_Response(array('source' => $source, 'mapping' => $value), 200); } function ai4ranks_serve_redirect() { if (is_admin() || !is_404() || !in_array($_SERVER['REQUEST_METHOD'] ?? '', array('GET', 'HEAD'), true)) return; if (!isset($_SERVER['REQUEST_URI']) || !is_string($_SERVER['REQUEST_URI'])) return; $request_uri = esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])); // URL sanitization preserves encoded path octets. Reject any request it // changes so malformed input can never resolve to a different exact path. if ($request_uri === '' || $request_uri[0] !== '/' || strpos($request_uri, '?') !== false || !hash_equals($request_uri, wp_unslash($_SERVER['REQUEST_URI']))) return; $home = wp_parse_url(home_url('/')); $origin = $home['scheme'] . '://' . $home['host'] . (isset($home['port']) ? ':' . $home['port'] : ''); $path = ai4ranks_redirect_path($origin . $request_uri); if (!$path) return; $mapping = get_option(ai4ranks_redirect_key($path), false); if (!$mapping || !in_array($mapping['code'] ?? 0, array(301, 308), true) || !ai4ranks_redirect_destination($mapping['target'] ?? '')) return; if (wp_safe_redirect($mapping['target'], $mapping['code'], 'AI4Ranks')) exit; } add_action('template_redirect', 'ai4ranks_serve_redirect', 0);