Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.
fix: ssrf vultenaribility on add subscription (#1038)
Miguel Ribeiroe79f28be6be0
diff --git a/endpoints/ai/fetch_models.php b/endpoints/ai/fetch_models.phpindex 8debff2..4b11954 100644--- a/endpoints/ai/fetch_models.php+++ b/endpoints/ai/fetch_models.php@@ -15,6 +15,8 @@ $aiType = isset($data["type"]) ? trim($data["type"]) : '';$aiApiKey = isset($data["api_key"]) ? trim($data["api_key"]) : '';$aiOllamaHost = isset($data["ollama_host"]) ? trim($data["ollama_host"]) : '';+$ssrf = null; // Initialize to avoid errors for public API types+// Validate ai-typeif (!in_array($aiType, ['chatgpt', 'gemini', 'openrouter', 'ollama', 'openai-compatible'])) {echo json_encode(["success" => false, "message" => translate('error', $i18n)]);@@ -56,9 +58,8 @@ if ($aiType === 'chatgpt') {exit;}- $ssrf = validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n, $userId);- // API key is optional — local instances don't need oneif (!empty($aiApiKey)) {$headers[] = 'Authorization: Bearer ' . $aiApiKey;}@@ -82,7 +83,7 @@ if ($aiType === 'chatgpt') {exit;}- $ssrf = validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n, $userId);$apiUrl = rtrim($aiOllamaHost, '/') . '/api/tags';}@@ -92,6 +93,11 @@ $ch = curl_init($apiUrl);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_TIMEOUT, 60);++if ($ssrf) {+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);+}+$response = curl_exec($ch);if (curl_errno($ch)) {@@ -105,7 +111,6 @@ if (curl_errno($ch)) {$modelsData = json_decode($response, true);if ($aiType === 'gemini' && isset($modelsData['models']) && is_array($modelsData['models'])) {- // Normalize Gemini response$models = array_map(function ($model) {return ['id' => str_replace('models/', '', $model['name']),@@ -115,7 +120,6 @@ if (curl_errno($ch)) {$response = ["success" => true, "models" => $models];} elseif (isset($modelsData['data']) && is_array($modelsData['data'])) {- // OpenAI format (ChatGPT, OpenRouter, OpenAI Compatible)$models = array_map(function ($model) {return ['id' => $model['id'],@@ -125,7 +129,6 @@ if (curl_errno($ch)) {$response = ["success" => true, "models" => $models];} elseif (in_array($aiType, ['ollama', 'openai-compatible']) && isset($modelsData['models']) && is_array($modelsData['models'])) {- // Ollama native format — also a fallback for openai-compatible servers that return this shape$models = array_map(function ($model) {return ['id' => $model['name'],@@ -145,4 +148,4 @@ if (curl_errno($ch)) {}unset($ch);-echo json_encode($response);+echo json_encode($response);\ No newline at end of filediff --git a/endpoints/ai/generate_recommendations.php b/endpoints/ai/generate_recommendations.phpindex 3a3d283..87b2b94 100644--- a/endpoints/ai/generate_recommendations.php+++ b/endpoints/ai/generate_recommendations.php@@ -75,7 +75,7 @@ if (in_array($type, ['ollama', 'openai-compatible'])) {exit;}- $ssrf = validate_webhook_url_for_ssrf($host, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($host, $db, $i18n, $userId);if ($type === 'ollama') {$apiKey = '';diff --git a/endpoints/ai/save_settings.php b/endpoints/ai/save_settings.phpindex 0165367..976e1be 100644--- a/endpoints/ai/save_settings.php+++ b/endpoints/ai/save_settings.php@@ -49,7 +49,7 @@ if (in_array($aiType, ['ollama', 'openai-compatible'])) {exit;}- validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n);+ validate_webhook_url_for_ssrf($aiOllamaHost, $db, $i18n, $userId);if ($aiType === 'ollama') {// Ollama never uses an API keydiff --git a/endpoints/cronjobs/generaterecommendations.php b/endpoints/cronjobs/generaterecommendations.phpindex 977c7d6..26dc7fd 100644--- a/endpoints/cronjobs/generaterecommendations.php+++ b/endpoints/cronjobs/generaterecommendations.php@@ -93,7 +93,7 @@ foreach ($allAiSettings as $aiSettings) {$failures[] = ['user_id' => $tempUserId, 'reason' => 'invalid host URL'];continue;}- $ssrf = validate_webhook_url_for_ssrf($host, $db, []);+ $ssrf = validate_webhook_url_for_ssrf($host, $db, [], $tempUserId);if ($type === 'ollama') {$apiKey = '';}diff --git a/endpoints/cronjobs/sendcancellationnotifications.php b/endpoints/cronjobs/sendcancellationnotifications.phpindex 01d25c6..a23512e 100644--- a/endpoints/cronjobs/sendcancellationnotifications.php+++ b/endpoints/cronjobs/sendcancellationnotifications.php@@ -274,7 +274,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Discord notifications if enabledif ($discordNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($discord['webhook_url'], $db);+ $ssrf = is_url_safe_for_ssrf($discord['webhook_url'], $db, $userId);if (!$ssrf) {echo "Discord notification skipped: URL failed SSRF validation.<br />";} else {@@ -318,6 +318,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {'Content-Type: application/json']);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$response = curl_exec($ch);@@ -334,7 +335,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Gotify notifications if enabledif ($gotifyNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($gotify['serverUrl'], $db);+ $ssrf = is_url_safe_for_ssrf($gotify['serverUrl'], $db, $userId);if (!$ssrf) {echo "Gotify notification skipped: URL failed SSRF validation.<br />";} else {@@ -379,6 +380,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$result = curl_exec($ch);if ($result === false) {@@ -481,7 +483,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Ntfy notifications if enabledif ($ntfyNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($ntfy['host'], $db);+ $ssrf = is_url_safe_for_ssrf($ntfy['host'], $db, $userId);if (!$ssrf) {echo "Ntfy notification skipped: URL failed SSRF validation.<br />";} else {@@ -522,6 +524,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$response = curl_exec($ch);@@ -538,7 +541,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Webhook notifications if enabledif ($webhookNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($webhook['url'], $db);+ $ssrf = is_url_safe_for_ssrf($webhook['url'], $db, $userId);if (!$ssrf) {echo "Webhook notification skipped: URL failed SSRF validation.<br />";} else {@@ -584,6 +587,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);// Execute the cURL request$response = curl_exec($ch);diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.phpindex c26450f..221e43e 100644--- a/endpoints/cronjobs/sendnotifications.php+++ b/endpoints/cronjobs/sendnotifications.php@@ -377,7 +377,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Discord notifications if enabledif ($discordNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($discord['webhook_url'], $db);+ $ssrf = is_url_safe_for_ssrf($discord['webhook_url'], $db, $userId);if (!$ssrf) {echo "SSRF attempt detected for Discord webhook URL. Notifications not sent.<br />";} else {@@ -422,10 +422,11 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {'Content-Type: application/json']);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$response = curl_exec($ch);- if ($result === false) {+ if ($response === false) {echo "Error sending notifications: " . curl_error($ch) . "<br />";} else {echo "Discord Notifications sent<br />";@@ -438,7 +439,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Gotify notifications if enabledif ($gotifyNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($gotify['serverUrl'], $db);+ $ssrf = is_url_safe_for_ssrf($gotify['serverUrl'], $db, $userId);if (!$ssrf) {echo "SSRF attempt detected for Gotify server URL. Notifications not sent.<br />";} else {@@ -484,6 +485,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$result = curl_exec($ch);if ($result === false) {@@ -491,6 +493,8 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {} else {echo "Gotify Notifications sent<br />";}++ unset($ch);}}}@@ -541,6 +545,8 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {} else {echo "Telegram Notifications sent<br />";}++ unset($ch);}}@@ -609,7 +615,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Mattermost notifications if enabledif ($mattermostNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($mattermost['webhook_url'], $db);+ $ssrf = is_url_safe_for_ssrf($mattermost['webhook_url'], $db, $userId);if (!$ssrf) {echo "SSRF attempt detected for Mattermost webhook URL. Notifications not sent.<br />";} else {@@ -655,6 +661,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {'Content-Type: application/json'),);+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$result = curl_exec($ch);$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);@@ -719,7 +726,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Ntfy notifications if enabledif ($ntfyNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($ntfy['host'], $db);+ $ssrf = is_url_safe_for_ssrf($ntfy['host'], $db, $userId);if (!$ssrf) {echo "SSRF attempt detected for Ntfy host URL. Notifications not sent.<br />";} else {@@ -765,6 +772,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);$response = curl_exec($ch);@@ -781,7 +789,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {// Webhook notifications if enabledif ($webhookNotificationsEnabled) {- $ssrf = is_url_safe_for_ssrf($webhook['url'], $db);+ $ssrf = is_url_safe_for_ssrf($webhook['url'], $db, $userId);if (!$ssrf) {echo "SSRF attempt detected for webhook URL. Notifications not sent.<br />";;} else {@@ -829,6 +837,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);// Execute the cURL request$response = curl_exec($ch);@@ -912,6 +921,4 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {}-}--?>+}\ No newline at end of filediff --git a/endpoints/logos/search.php b/endpoints/logos/search.phpindex 1a1ae08..d2f2f10 100644--- a/endpoints/logos/search.php+++ b/endpoints/logos/search.php@@ -1,4 +1,6 @@<?php+require_once '../../includes/ssrf_helper.php';+if (isset($_GET['search'])) {$searchTerm = urlencode($_GET['search'] . " logo");@@ -20,17 +22,27 @@ if (isset($_GET['search'])) {$host = parse_url($url, PHP_URL_HOST);if (!in_array($host, $allowedHosts)) return null;+ $ip = gethostbyname($host);+ $port = parse_url($url, PHP_URL_PORT) ?: (parse_url($url, PHP_URL_SCHEME) === 'https' ? 443 : 80);++ $is_private = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false+ || is_cgnat_ip($ip);++ if ($is_private) return null;+$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');- // Explicitly disable proxy by default, then re-apply only from env (not $_SERVER)curl_setopt($ch, CURLOPT_PROXY, '');curl_setopt($ch, CURLOPT_NOPROXY, '*');if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);++ curl_setopt($ch, CURLOPT_RESOLVE, ["{$host}:{$port}:{$ip}"]);+applyProxy($ch);$response = curl_exec($ch);unset($ch);diff --git a/endpoints/notifications/savediscordnotifications.php b/endpoints/notifications/savediscordnotifications.phpindex b1a0a45..963afda 100644--- a/endpoints/notifications/savediscordnotifications.php+++ b/endpoints/notifications/savediscordnotifications.php@@ -21,7 +21,7 @@ if ($bot_username = $data["bot_username"];$bot_avatar_url = $data["bot_avatar"];- validate_webhook_url_for_ssrf($webhook_url, $db, $i18n);+ validate_webhook_url_for_ssrf($webhook_url, $db, $i18n, $userId);$query = "SELECT COUNT(*) FROM discord_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);diff --git a/endpoints/notifications/savegotifynotifications.php b/endpoints/notifications/savegotifynotifications.phpindex 5ce40af..673a1e7 100644--- a/endpoints/notifications/savegotifynotifications.php+++ b/endpoints/notifications/savegotifynotifications.php@@ -35,7 +35,7 @@ if (]));}- validate_webhook_url_for_ssrf($url, $db, $i18n);+ validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);$query = "SELECT COUNT(*) FROM gotify_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);diff --git a/endpoints/notifications/savemattermostnotifications.php b/endpoints/notifications/savemattermostnotifications.phpindex b2c97b2..4b55e12 100755--- a/endpoints/notifications/savemattermostnotifications.php+++ b/endpoints/notifications/savemattermostnotifications.php@@ -30,7 +30,7 @@ if (!isset($data["webhook_url"]) || $data["webhook_url"] == "") {]));}- validate_webhook_url_for_ssrf($webhook_url, $db, $i18n);+ validate_webhook_url_for_ssrf($webhook_url, $db, $i18n, $userId);$query = "SELECT COUNT(*) FROM mattermost_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);diff --git a/endpoints/notifications/saventfynotifications.php b/endpoints/notifications/saventfynotifications.phpindex 391e1cc..1def36d 100644--- a/endpoints/notifications/saventfynotifications.php+++ b/endpoints/notifications/saventfynotifications.php@@ -37,7 +37,7 @@ if (]));}- validate_webhook_url_for_ssrf($url, $db, $i18n);+ validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);$query = "SELECT COUNT(*) FROM ntfy_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);diff --git a/endpoints/notifications/savewebhooknotifications.php b/endpoints/notifications/savewebhooknotifications.phpindex 0903b92..00576e2 100644--- a/endpoints/notifications/savewebhooknotifications.php+++ b/endpoints/notifications/savewebhooknotifications.php@@ -35,7 +35,7 @@ if (]));}- validate_webhook_url_for_ssrf($url, $db, $i18n);+ validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);$query = "SELECT COUNT(*) FROM webhook_notifications WHERE user_id = :userId";$stmt = $db->prepare($query);diff --git a/endpoints/notifications/testdiscordnotifications.php b/endpoints/notifications/testdiscordnotifications.phpindex b0905f4..c99dd4d 100644--- a/endpoints/notifications/testdiscordnotifications.php+++ b/endpoints/notifications/testdiscordnotifications.php@@ -37,7 +37,7 @@ if (]));}- $ssrf = validate_webhook_url_for_ssrf($webhook_url, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($webhook_url, $db, $i18n, $userId);$postfields = ['content' => $message,diff --git a/endpoints/notifications/testgotifynotifications.php b/endpoints/notifications/testgotifynotifications.phpindex c745328..e00ecb6 100644--- a/endpoints/notifications/testgotifynotifications.php+++ b/endpoints/notifications/testgotifynotifications.php@@ -38,12 +38,12 @@ if (]));}- $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);$ch = curl_init();// Set the URL and other options- curl_setopt($ch, CURLOPT_URL, $url . "/message?token=" . $token);+ curl_setopt($ch, CURLOPT_URL, rtrim($url, '/') . "/message?token=" . $token);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);curl_setopt($ch, CURLOPT_RESOLVE, ["{$ssrf['host']}:{$ssrf['port']}:{$ssrf['ip']}"]);diff --git a/endpoints/notifications/testmattermostnotifications.php b/endpoints/notifications/testmattermostnotifications.phpindex 5527024..e814567 100755--- a/endpoints/notifications/testmattermostnotifications.php+++ b/endpoints/notifications/testmattermostnotifications.php@@ -39,7 +39,7 @@ if (]));}- $ssrf = validate_webhook_url_for_ssrf($webhook_url, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($webhook_url, $db, $i18n, $userId);$postfields = ['text' => $message,diff --git a/endpoints/notifications/testntfynotifications.php b/endpoints/notifications/testntfynotifications.phpindex 77ce582..ff22b73 100644--- a/endpoints/notifications/testntfynotifications.php+++ b/endpoints/notifications/testntfynotifications.php@@ -43,7 +43,7 @@ if (]));}- $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);// Set the message parameters$message = translate('test_notification', $i18n);diff --git a/endpoints/notifications/testwebhooknotifications.php b/endpoints/notifications/testwebhooknotifications.phpindex c1ab1c2..1e03283 100644--- a/endpoints/notifications/testwebhooknotifications.php+++ b/endpoints/notifications/testwebhooknotifications.php@@ -49,7 +49,7 @@ if (]));}- $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n);+ $ssrf = validate_webhook_url_for_ssrf($url, $db, $i18n, $userId);// Replace placeholders in the payload with fake subscription dataforeach ($fakeSubscription as $key => $value) {diff --git a/endpoints/payments/add.php b/endpoints/payments/add.phpindex 36ab5c6..4724104 100644--- a/endpoints/payments/add.php+++ b/endpoints/payments/add.php@@ -4,6 +4,7 @@ require_once '../../includes/connect_endpoint.php';require_once '../../includes/inputvalidation.php';require_once '../../includes/getsettings.php';require_once '../../includes/validate_endpoint.php';+require_once '../../includes/ssrf_helper.php';if (!file_exists('../../images/uploads/logos')) {mkdir('../../images/uploads/logos', 0777, true);@@ -26,53 +27,63 @@ function validateFileExtension($fileExtension)function getLogoFromUrl($url, $uploadDir, $name, $i18n, $settings){- if (!filter_var($url, FILTER_VALIDATE_URL) || !preg_match('/^https?:\/\//i', $url)) {- $response = [- "success" => false,- "message" => "Invalid URL format."- ];- echo json_encode($response);- exit();- }+ $currentUrl = $url;+ $maxRedirects = 3;- $host = parse_url($url, PHP_URL_HOST);- $ip = gethostbyname($host);- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {- $response = [- "success" => false,- "message" => "Invalid IP Address."- ];- echo json_encode($response);- exit();- }+ for ($i = 0; $i <= $maxRedirects; $i++) {+ if (!filter_var($currentUrl, FILTER_VALIDATE_URL) || !preg_match('/^https?:\/\//i', $currentUrl)) {+ return ["success" => false, "message" => "Invalid URL format."];+ }- $ch = curl_init($url);- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);- curl_setopt($ch, CURLOPT_TIMEOUT, 5);- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);- curl_setopt($ch, CURLOPT_MAXREDIRS, 3);+ $host = parse_url($currentUrl, PHP_URL_HOST);+ $port = parse_url($currentUrl, PHP_URL_PORT) ?: (parse_url($currentUrl, PHP_URL_SCHEME) === 'https' ? 443 : 80);++ $ip = gethostbyname($host);- $imageData = curl_exec($ch);+ $is_private = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false+ || is_cgnat_ip($ip);- if ($imageData !== false) {- $timestamp = time();- $fileName = $timestamp . '-payments-' . sanitizeFilename($name) . '.png';- $uploadDir = '../../images/uploads/logos/';- $uploadFile = $uploadDir . $fileName;+ if ($is_private) {+ return ["success" => false, "message" => "Invalid IP Address."];+ }- if (saveLogo($imageData, $uploadFile, $name, $settings)) {- unset($ch);- return $fileName;- } else {- echo translate('error_fetching_image', $i18n) . ": " . curl_error($ch);- unset($ch);- return "";+ $ch = curl_init($currentUrl);+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);+ curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Wallos/1.0');++ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);++ curl_setopt($ch, CURLOPT_RESOLVE, ["{$host}:{$port}:{$ip}"]);++ $imageData = curl_exec($ch);+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);++ if ($httpCode >= 300 && $httpCode < 400) {+ $redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);+ curl_close($ch);+ if (!$redirectUrl) break;++ $currentUrl = $redirectUrl;+ continue;}- } else {- echo translate('error_fetching_image', $i18n) . ": " . curl_error($ch);++ if ($imageData !== false && $httpCode === 200) {+ $timestamp = time();+ $fileName = $timestamp . '-payments-' . sanitizeFilename($name) . '.png';+ $uploadFile = rtrim($uploadDir, '/') . '/' . $fileName;++ if (saveLogo($imageData, $uploadFile, $name, $settings)) {+ unset($ch);+ return $fileName;+ }+ }+unset($ch);- return "";+ break;}++ return ["success" => false, "message" => "Failed to fetch image."];}diff --git a/endpoints/payments/search.php b/endpoints/payments/search.phpindex 7d82438..121bf26 100644--- a/endpoints/payments/search.php+++ b/endpoints/payments/search.php@@ -1,4 +1,5 @@<?php+require_once '../../includes/ssrf_helper.php';if (isset($_GET['search'])) {function applyProxy($ch) {@@ -16,17 +17,33 @@ if (isset($_GET['search'])) {function curlGet($url, $headers = []) {$allowedHosts = ['duckduckgo.com', 'search.brave.com'];- $host = parse_url($url, PHP_URL_HOST);+ $parsedUrl = parse_url($url);+ $host = $parsedUrl['host'] ?? '';+ $port = $parsedUrl['port'] ?? ($parsedUrl['scheme'] === 'https' ? 443 : 80);+if (!in_array($host, $allowedHosts, true)) {return null;}+ $ip = gethostbyname($host);+ $is_private = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false+ || is_cgnat_ip($ip);++ if ($is_private) {+ return null;+ }+$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);+curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);+ curl_setopt($ch, CURLOPT_MAXREDIRS, 3);+curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36');+ curl_setopt($ch, CURLOPT_RESOLVE, ["{$host}:{$port}:{$ip}"]);+if (!empty($headers)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);}diff --git a/endpoints/subscription/add.php b/endpoints/subscription/add.phpindex e4fc603..727fc50 100644--- a/endpoints/subscription/add.php+++ b/endpoints/subscription/add.php@@ -4,6 +4,7 @@ require_once '../../includes/connect_endpoint.php';require_once '../../includes/validate_endpoint.php';require_once '../../includes/inputvalidation.php';require_once '../../includes/getsettings.php';+require_once '../../includes/ssrf_helper.php';if (!file_exists('../../images/uploads/logos')) {mkdir('../../images/uploads/logos', 0777, true);@@ -37,16 +38,19 @@ function getLogoFromUrl($url, $uploadDir, $name, $settings, $i18n)$parts = parse_url($currentUrl);$host = $parts['host'];$port = $parts['port'] ?? ($parts['scheme'] === 'https' ? 443 : 80);+$ip = gethostbyname($host);- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {+ if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false+ || is_cgnat_ip($ip)) {return ['success' => false, 'message' => 'Invalid IP Address.'];}$ch = curl_init($currentUrl);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 5);- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // Manual handling to re-validate IPs+curl_setopt($ch, CURLOPT_RESOLVE, ["$host:$port:$ip"]);$imageData = curl_exec($ch);@@ -61,13 +65,13 @@ function getLogoFromUrl($url, $uploadDir, $name, $settings, $i18n)}$currentUrl = $redirectUrl;- continue;+ continue;}if ($imageData !== false && $httpCode === 200) {$timestamp = time();$fileName = $timestamp . '-' . sanitizeFilename($name) . '.png';- $uploadFile = '../../images/uploads/logos/' . $fileName;+ $uploadFile = $uploadDir . $fileName; // Note: Use the provided $uploadDir variableif (saveLogo($imageData, $uploadFile, $name, $settings)) {unset($ch);diff --git a/includes/ssrf_helper.php b/includes/ssrf_helper.phpindex e608e9e..69b5145 100644--- a/includes/ssrf_helper.php+++ b/includes/ssrf_helper.php@@ -6,6 +6,11 @@* Used by Tailscale and corporate CGNAT environments.*/function is_cgnat_ip($ip) {+ // Handle IPv4-mapped IPv6 addresses (::ffff:100.64.0.1)+ if (strpos($ip, ':') !== false) {+ $ip = str_replace('::ffff:', '', $ip);+ }+$long = ip2long($ip);return $long !== false&& $long >= ip2long('100.64.0.0')@@ -20,7 +25,7 @@ function is_cgnat_ip($ip) {* @param array $i18n The translation array* @return array Returns an array with ['host', 'ip', 'port'] for cURL hardening*/-function validate_webhook_url_for_ssrf($url, $db, $i18n) {+function validate_webhook_url_for_ssrf($url, $db, $i18n, $userId = null) {$parsedUrl = parse_url($url);// Fallback if parse_url fails completely@@ -50,6 +55,13 @@ function validate_webhook_url_for_ssrf($url, $db, $i18n) {$is_private = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false || is_cgnat_ip($ip);if ($is_private) {+ if ($userId != 1) {+ die(json_encode([+ "success" => false,+ "message" => "Security Block: Standard users are not permitted to use internal network addresses."+ ]));+ }+$stmt = $db->prepare("SELECT local_webhook_notifications_allowlist FROM admin LIMIT 1");$result = $stmt->execute();$row = $result->fetchArray(SQLITE3_ASSOC);@@ -89,7 +101,7 @@ function validate_webhook_url_for_ssrf($url, $db, $i18n) {* @param SQLite3 $db The database connection* @return array|false*/-function is_url_safe_for_ssrf($url, $db) {+function is_url_safe_for_ssrf($url, $db, $userId = null) {$parsedUrl = parse_url($url);if (!$parsedUrl || !isset($parsedUrl['host'])) return false;@@ -110,6 +122,10 @@ function is_url_safe_for_ssrf($url, $db) {|| is_cgnat_ip($ip);if ($is_private) {+ if ($userId != 1) {+ return false; // private and user is not admin — skip silently+ }+$stmt = $db->prepare("SELECT local_webhook_notifications_allowlist FROM admin LIMIT 1");$result = $stmt->execute();$row = $result->fetchArray(SQLITE3_ASSOC);diff --git a/includes/version.php b/includes/version.phpindex d00d351..8d46dc1 100644--- a/includes/version.php+++ b/includes/version.php@@ -1,3 +1,3 @@<?php-$version = "v4.8.0";+$version = "v4.8.1";?>\ No newline at end of file