forked from simplepie/simplepie
-
Notifications
You must be signed in to change notification settings - Fork 1
Allow restricting CURLOPT_PROXY with get_curl_resolve_info()
#83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Inverle
wants to merge
2
commits into
FreshRSS:freshrss
Choose a base branch
from
Inverle:restrict-curlopt-proxy-ssrf
base: freshrss
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,8 @@ public function __construct(string $url, int $timeout = 10, int $redirects = 5, | |
| } | ||
| if (!$force_fsockopen && function_exists('curl_exec')) { | ||
| $resolve = false; // FreshRSS | ||
| $proxy = $curl_options[CURLOPT_PROXY] ?? null; // FreshRSS | ||
| $proxy_type = $curl_options[CURLOPT_PROXYTYPE] ?? null; // FreshRSS | ||
| if (empty($curl_options[CURLOPT_PROXY] ?? null)) { // FreshRSS | ||
| $resolve = $this->get_curl_resolve_info($url); | ||
| if ($resolve === null) { | ||
|
|
@@ -128,6 +130,52 @@ public function __construct(string $url, int $timeout = 10, int $redirects = 5, | |
| if (!empty($resolve)) { | ||
| $curl_options[CURLOPT_RESOLVE] = $resolve; // Prevent DNS rebinding | ||
| } | ||
| } else { // FreshRSS | ||
| defined('CURLPROXY_HTTPS') or define('CURLPROXY_HTTPS', 2); // Compatibility cURL 7.51 | ||
| $proxy_scheme = null; | ||
| switch ($proxy_type) { | ||
| case CURLPROXY_HTTP: | ||
| $proxy_scheme = 'http'; | ||
| break; | ||
| case CURLPROXY_HTTPS: | ||
| $proxy_scheme = 'https'; | ||
| break; | ||
| case CURLPROXY_SOCKS4: | ||
| $proxy_scheme = 'socks4'; | ||
| break; | ||
| case CURLPROXY_SOCKS4A: | ||
| $proxy_scheme = 'socks4a'; | ||
| break; | ||
| case CURLPROXY_SOCKS5: | ||
| $proxy_scheme = 'socks5'; | ||
| break; | ||
| case CURLPROXY_SOCKS5_HOSTNAME: | ||
| $proxy_scheme = 'socks5h'; | ||
| break; | ||
| }; | ||
| if ($proxy_scheme === null) { | ||
| $this->error = 'Unsupported proxy type'; | ||
| $this->success = false; | ||
| return; | ||
| } | ||
| $proxy_url = "$proxy_scheme://$proxy"; // CURLOPT_PROXY ($proxy) is formatted as user:pass@hostname:port, with the part before @ being optional | ||
| $resolve = $this->get_curl_resolve_info($proxy_url, true); | ||
| if ($resolve === null) { | ||
| $this->error = 'Failed to fetch this URL, because the proxy’s IP is not in the allowlist [' . | ||
| \SimplePie\Misc::url_remove_credentials($url) . '] [' . | ||
| \SimplePie\Misc::url_remove_credentials($proxy_url) . ']'; | ||
| $this->success = false; | ||
| return; | ||
| } elseif ($resolve === false) { | ||
| $this->error = 'Failed to resolve proxy hostname: ' . \SimplePie\Misc::url_remove_credentials($proxy_url); | ||
| $this->success = false; | ||
| return; | ||
| } | ||
| $curl_options[CURLOPT_PROXY] = $resolve; // Translate from a hostname:port value to ip:port, in order to prevent DNS rebinding | ||
| if (defined('CURLOPT_PROXY_SSL_VERIFYHOST')) { | ||
| // Available as of PHP 7.3.0 and cURL 7.52.0 | ||
| $curl_options[CURLOPT_PROXY_SSL_VERIFYHOST] = 0; // Skip verifying the hostname (a bit unsafe, but needed since there is no CURLOPT_RESOLVE equivalent for proxy hostnames) | ||
| } | ||
| } | ||
| $this->method = \SimplePie\SimplePie::FILE_SOURCE_REMOTE | \SimplePie\SimplePie::FILE_SOURCE_CURL; | ||
| $fp = self::curlInit($url, $timeout, $headers, $useragent, $curl_options); | ||
|
|
@@ -444,10 +492,27 @@ protected function on_http_response($response, array $curl_options = []): void | |
| /** | ||
| * Event to allow inheriting classes to control fetching certain URLs. | ||
| * @param string $url | ||
| * @return array<string>|null|false Returns a value for CURLOPT_RESOLVE as an array, null if no allowed IPs were found, false if the domain failed to resolve. | ||
| * @return array<string>|string|null|false Returns a value for CURLOPT_RESOLVE as an array, null if no allowed IPs were found, false if the domain failed to resolve. Can also be used for checking if the CURLOPT_PROXY value is allowed, by providing a proxy URL with the `for_proxy` parameter set to `true`. In that case, a string value will be returned with the hostname resolved to an IP if allowed. | ||
| */ | ||
| protected function get_curl_resolve_info(string $url) | ||
| protected function get_curl_resolve_info(string $url, bool $for_proxy = false) | ||
| { | ||
| if ($for_proxy) { | ||
| $parsed = parse_url($url); | ||
| if ($parsed === false) { | ||
| return false; | ||
| } | ||
| $credentials = ''; | ||
| $user = $parsed['user'] ?? null; | ||
| $pass = $parsed['pass'] ?? null; | ||
| if (is_string($user) && is_string($pass)) { | ||
| $credentials = "$user:$pass@"; | ||
| } | ||
| $proxy = $parsed['host'] ?? ''; | ||
| if (is_int($parsed['port'] ?? null)) { | ||
| $proxy .= ':' . $parsed['port']; | ||
| } | ||
| return $credentials . $proxy; | ||
| } | ||
|
Comment on lines
+499
to
+515
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be enough to just return the contents after |
||
| return []; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a case for
match?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not available in PHP 7