$limit) { return array( 'allowed' => false, 'remaining' => 0, 'reset_in' => $resetIn, 'retry_after' => $resetIn, ); } return array( 'allowed' => true, 'remaining' => max(0, $limit - $count), 'reset_in' => $resetIn, 'retry_after' => 0, ); } // Without one the counter is a row in a small table, updated by a // single INSERT ... ON DUPLICATE KEY UPDATE, so simultaneous // requests are all counted. $row = RateLimitStore::hit($transientKey, $windowSeconds, $now); if (null !== $row) { if ($row['hits'] > $limit) { return array( 'allowed' => false, 'remaining' => 0, 'reset_in' => $row['reset_in'], 'retry_after' => $row['reset_in'], ); } return array( 'allowed' => true, 'remaining' => max(0, $limit - $row['hits']), 'reset_in' => $row['reset_in'], 'retry_after' => 0, ); } // The table is not available: a transient, best effort, because two // simultaneous requests can read the same count. $bucket = get_transient($transientKey); if ($bucket === false || !is_array($bucket) || $bucket['reset_at'] <= $now) { $bucket = array( 'count' => 0, 'reset_at' => $now + $windowSeconds, ); } $resetIn = $bucket['reset_at'] - $now; if ($bucket['count'] >= $limit) { return array( 'allowed' => false, 'remaining' => 0, 'reset_in' => max(1, $resetIn), 'retry_after' => max(1, $resetIn), ); } $bucket['count']++; set_transient($transientKey, $bucket, $resetIn); return array( 'allowed' => true, 'remaining' => max(0, $limit - $bucket['count']), 'reset_in' => $resetIn, 'retry_after' => 0, ); } /** * Build rate limit key for a request * * @param string $route Request route (e.g. /subtleforms/v1/forms) * @param string $method HTTP method (GET, POST, etc.) * @param int|null $userId WordPress user ID if authenticated * @param string $ip Client IP address * @return string Unique rate limit key */ public static function buildKey(string $route, string $method, ?int $userId, string $ip): string { // Prefer user-based limiting for authenticated requests if ($userId) { return sprintf('subtleforms_rl_user_%d_%s_%s', $userId, $method, md5($route)); } // Callers pass \SubtleForms\Security\ClientIp::get(), which already // honours the trusted proxy settings. // Fall back to IP-based limiting for anonymous requests $sanitizedIp = self::sanitizeIp($ip); return sprintf('subtleforms_rl_ip_%s_%s_%s', $sanitizedIp, $method, md5($route)); } /** * Get rate limit policy for a route and method * * @param string $route Request route * @param string $method HTTP method * @return array { * @type int $limit Maximum requests allowed * @type int $window Time window in seconds * } */ public static function policy(string $route, string $method): array { // Normalize route (strip leading namespace) $route = preg_replace('#^/subtleforms/v1#', '', $route); // Check if user is authenticated admin $isAuthenticatedAdmin = is_user_logged_in() && current_user_can('manage_options'); // PUBLIC SUBMIT ENDPOINTS (strictest) // The live form posts to /submit (FF-28); /forms/{id}/submit is the older route. if (preg_match('#^(/submit|/forms/[^/]+/submit)/?$#', $route) && $method === 'POST') { return array('limit' => 10, 'window' => 60); } // Trading a page ticket for a submit token: once per visitor and form, // plus a retry, so a low limit per client address. if (preg_match('#^/forms/[^/]+/spam-token/?$#', $route) && $method === 'POST') { return array('limit' => 20, 'window' => 60); } // FORM SCHEMA ENDPOINTS (very lenient for authenticated admins due to autosave) if (preg_match('#^/forms/[^/]+/schema$#', $route) && $isAuthenticatedAdmin) { return array('limit' => 300, 'window' => 60); } // SETTINGS ENDPOINTS (strict for writes) if (preg_match('#^/settings#', $route) && in_array($method, array('POST', 'PUT', 'PATCH'), true)) { return array('limit' => 10, 'window' => 60); } // AUTHENTICATED ADMIN WRITE ENDPOINTS (lenient for builder UX) if ($isAuthenticatedAdmin && in_array($method, array('POST', 'PUT', 'PATCH', 'DELETE'), true)) { return array('limit' => 120, 'window' => 60); } // UNAUTHENTICATED WRITE ENDPOINTS (moderate) if (in_array($method, array('POST', 'PUT', 'PATCH', 'DELETE'), true)) { return array('limit' => 30, 'window' => 60); } // READ ENDPOINTS (lenient) if ($method === 'GET') { return array('limit' => 200, 'window' => 60); } // DEFAULT (safe fallback) return array('limit' => 60, 'window' => 60); } /** * Generate HTTP headers for rate limit response * * @param array $result Result from check() method * @param int $limit The limit that was checked against * @return array HTTP headers */ public static function headers(array $result, int $limit): array { $headers = array( 'X-RateLimit-Limit' => (string) $limit, 'X-RateLimit-Remaining' => (string) $result['remaining'], 'X-RateLimit-Reset' => (string) (time() + $result['reset_in']), ); // Add Retry-After header only when blocked if (!$result['allowed']) { $headers['Retry-After'] = (string) $result['retry_after']; } return $headers; } /** * Sanitize transient key to meet WordPress constraints * * WordPress transient keys must be: * - Max 172 characters (wp_options.option_name is varchar(191), minus prefix) * - Alphanumeric + underscores only * * @param string $key Raw key * @return string Sanitized transient key */ private static function sanitizeTransientKey(string $key): string { // Replace invalid characters with underscores $key = preg_replace('/[^a-zA-Z0-9_]/', '_', $key); // Truncate to safe length (leaving room for transient prefix) return substr($key, 0, 150); } /** * Sanitize IP address for use in transient keys * * @param string $ip Raw IP address * @return string Sanitized IP (hashed for privacy and key safety) */ private static function sanitizeIp(string $ip): string { // Validate IP format if (!filter_var($ip, FILTER_VALIDATE_IP)) { $ip = '0.0.0.0'; } // Hash IP for privacy and to ensure valid transient key format return md5($ip); } }