CVE-2017-20157: Server-Side Request Forgery (SSRF) (CWE-918)
value: A vulnerability was found in Ariadne Component Library up to 2.x. It has been classified as critical. Affected is an unknown function of the file src/url/Url.php. The manipulation leads to server-side request forgery. Upgrading to version 3.0 is able to address this issue. It is recommended to upgrade the affected component. The identifier of this vulnerability is VDB-217140.
CWE description: The web server receives a URL or similar request from an upstream component and retrieves the contents of this URL, but it does not sufficiently ensure that the request is being sent to the expected destination.
CVSS v3 base score: 9.8
Published: 2022-12-31T10:15Z
Repository: https://github.com/Ariadne-CMS/arc-web
Language: PHP
Files changed: src/url/Url.php, tests/url.Test.php
Commit date: 2017-09-26 11:47:59 +0200
Commit message: added checks for evil URL's (url's that could trigger parse errors and ssrf attacks)
added test for double port number ssrf attack and prevent it

Diff: diff --git a/src/url/Url.php b/src/url/Url.php
index 7f34414..d371262 100644
--- a/src/url/Url.php
+++ b/src/url/Url.php
@@ -10,40 +10,44 @@
  */
 
 namespace arc\url;
 
 /**
- *	Url parses a URL string and returns an object with the seperate parts. You can change
- *	these and when cast to a string Url will regenerate the URL string and make sure it
- *	is valid.
+ * Url parses a URL string and returns an object with the seperate parts. You can change
+ * these and when cast to a string Url will regenerate the URL string and make sure it
+ * is valid.
  *
- *	Usage:
- *		$url = new \arc\url\Url( 'http://www.ariadne-cms.org/' );
- *		$url->path = '/docs/search/';
- *		$url->query = 'a=1&a=2';
- *		echo $url; // => 'http://www.ariadne-cms.org/docs/search/?a=1&a=2'
+ * Usage:
+ *    $url = new \arc\url\Url( 'http://www.ariadne-cms.org/' );
+ *    $url->path = '/docs/search/';
+ *    $url->query = 'a=1&a=2';
+ *    echo $url; // => 'http://www.ariadne-cms.org/docs/search/?a=1&a=2'
  * @property Query $query The query arguments
  */
 class Url
 {
     /**
-     *	All parts of the URL format, as returned by parse_url.
-     *	scheme://user:pass@host:port/path?query#fragment
+     * All parts of the URL format, as returned by parse_url.
+     * scheme://user:pass@host:port/path?query#fragment
      */
     public $scheme, $user, $pass, $host, $port, $path, $fragment;
     private $query;
 
     /**
-     *	@param string $url The URL to parse, the query part will remain a string.
-     *  @param QueryInterface queryObject Optional. An object that parses the query string.
+     * @param string $url The URL to parse, the query part will remain a string.
+     * @param QueryInterface queryObject Optional. An object that parses the query string.
      */
     public function __construct($url, $queryObject = null)
     {
         $componentList = [
             'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment'
         ];
         $this->importUrlComponents( parse_url( $url ), $componentList );
+        if ($this->scheme!='ldap' && strpos($this->host, ':')) {
+            // parse_url allows ':' in host when it occurs more than once\
+            $this->host = substr($this->host, 0, strpos($this->host, ':'));
+        }
         if ( isset( $queryObject ) ) {
             $this->query = $queryObject->import( $this->query );
         }
     }
 
diff --git a/tests/url.Test.php b/tests/url.Test.php
index 25043a1..961483a 100644
--- a/tests/url.Test.php
+++ b/tests/url.Test.php
@@ -107,6 +107,61 @@
             }, $commonUrls);
 
             $this->assertEquals( $commonUrls, $parsedUrls);
         }
 
+      function testEvilURL1()
+      {
+         $evilURL = 'http://127.0.0.1:11211:80/';
+         $parsed = \arc\url::url($evilURL);
+         $this->assertEquals( $parsed->port, 80 );
+         $parsedSafe = \arc\url::safeUrl($evilURL);
+         $this->assertEquals( $parsedSafe->port, 80 );
+         $safeURL = (string) $parsed;
+         $this->assertEquals( 'http://127.0.0.1:80/', $safeURL);
+      }
+
+      function testEvilURL2()
+      {
+         $evilURL = 'http://google.com#@evil.com/';
+         $parsed = \arc\url::url($evilURL);
+         $this->assertEquals( $parsed->host, 'google.com' );
+         $parsedSafe = \arc\url::safeUrl($evilURL);
+         $this->assertEquals( $parsedSafe->host, 'google.com');
+         $safeURL = (string) $parsed;
+         $this->assertEquals( 'http://google.com#%40evil.com%2F', $safeURL);
+      }
+
+      function testEvilURL3()
+      {
+         $evilURL = 'http://foo@evil.com:80@google.com/';
+         $parsed = \arc\url::url($evilURL);
+         $this->assertEquals( $parsed->host, 'google.com' );
+         $parsedSafe = \arc\url::safeUrl($evilURL);
+         $this->assertEquals( $parsedSafe->host, 'google.com');
+         $safeURL = (string) $parsed;
+         $this->assertEquals( 'http://foo%40evil.com:80@google.com/', $safeURL);
+      }
+
+      function testEvilURL4()
+      {
+         $evilURL = 'http://foo@127.0.0.1 @google.com/';
+         $parsed = \arc\url::url($evilURL);
+         $this->assertEquals( $parsed->host, 'google.com' );
+         $parsedSafe = \arc\url::safeUrl($evilURL);
+         $this->assertEquals( $parsedSafe->host, 'google.com');
+         $safeURL = (string) $parsed;
+         $this->assertEquals( 'http://foo%40127.0.0.1%20@google.com/', $safeURL);
+      }
+
+      function testEvilURL5()
+      {
+         $evilURL = 'http://127.0.0.1:11211#@google.com:80/';
+         $parsed = \arc\url::url($evilURL);
+         $this->assertEquals( $parsed->host, '127.0.0.1' );
+         $parsedSafe = \arc\url::safeUrl($evilURL);
+         $this->assertEquals( $parsedSafe->host, '127.0.0.1');
+         $safeURL = (string) $parsed;
+         $this->assertEquals( 'http://127.0.0.1:11211#%40google.com%3A80%2F', $safeURL);
+      }
+
     }

Vulnerable code:  *	Url parses a URL string and returns an object with the seperate parts. You can change
 *	these and when cast to a string Url will regenerate the URL string and make sure it
 *	is valid.
 *	Usage:
 *		$url = new \arc\url\Url( 'http://www.ariadne-cms.org/' );
 *		$url->path = '/docs/search/';
 *		$url->query = 'a=1&a=2';
 *		echo $url; // => 'http://www.ariadne-cms.org/docs/search/?a=1&a=2'
     *	All parts of the URL format, as returned by parse_url.
     *	scheme://user:pass@host:port/path?query#fragment
     *	@param string $url The URL to parse, the query part will remain a string.
     *  @param QueryInterface queryObject Optional. An object that parses the query string.
Fixed code:  * Url parses a URL string and returns an object with the seperate parts. You can change
 * these and when cast to a string Url will regenerate the URL string and make sure it
 * is valid.
 * Usage:
 *    $url = new \arc\url\Url( 'http://www.ariadne-cms.org/' );
 *    $url->path = '/docs/search/';
 *    $url->query = 'a=1&a=2';
 *    echo $url; // => 'http://www.ariadne-cms.org/docs/search/?a=1&a=2'
     * All parts of the URL format, as returned by parse_url.
     * scheme://user:pass@host:port/path?query#fragment
     * @param string $url The URL to parse, the query part will remain a string.
     * @param QueryInterface queryObject Optional. An object that parses the query string.
        if ($this->scheme!='ldap' && strpos($this->host, ':')) {
            // parse_url allows ':' in host when it occurs more than once\
            $this->host = substr($this->host, 0, strpos($this->host, ':'));
        }
      function testEvilURL1()
      {
         $evilURL = 'http://127.0.0.1:11211:80/';
         $parsed = \arc\url::url($evilURL);
         $this->assertEquals( $parsed->port, 80 );
         $parsedSafe = \arc\url::safeUrl($evilURL);
         $this->assertEquals( $parsedSafe->port, 80 );
         $safeURL = (string) $parsed;
         $this->assertEquals( 'http://127.0.0.1:80/', $safeURL);
      }

      function testEvilURL2()
      {
         $evilURL = 'http://google.com#@evil.com/';
         $parsed = \arc\url::url($evilURL);
         $this->assertEquals( $parsed->host, 'google.com' );
         $parsedSafe = \arc\url::safeUrl($evilURL);
         $this->assertEquals( $parsedSafe->host, 'google.com');
         $safeURL = (string) $parsed;
         $this->assertEquals( 'http://google.com#%40evil.com%2F', $safeURL);
      }

      function testEvilURL3()
      {
         $evilURL = 'http://foo@evil.com:80@google.com/';
         $parsed = \arc\url::url($evilURL);
         $this->assertEquals( $parsed->host, 'google.com' );
         $parsedSafe = \arc\url::safeUrl($evilURL);
         $this->assertEquals( $parsedSafe->host, 'google.com');
         $safeURL = (string) $parsed;
         $this->assertEquals( 'http://foo%40evil.com:80@google.com/', $safeURL);
      }

      function testEvilURL4()
      {
         $evilURL = 'http://foo@127.0.0.1 @google.com/';
         $parsed = \arc\url::url($evilURL);
         $this->assertEquals( $parsed->host, 'google.com' );
         $parsedSafe = \arc\url::safeUrl($evilURL);
         $this->assertEquals( $parsedSafe->host, 'google.com');
         $safeURL = (string) $parsed;
         $this->assertEquals( 'http://foo%40127.0.0.1%20@google.com/', $safeURL);
      }

      function testEvilURL5()
      {
         $evilURL = 'http://127.0.0.1:11211#@google.com:80/';
         $parsed = \arc\url::url($evilURL);
         $this->assertEquals( $parsed->host, '127.0.0.1' );
         $parsedSafe = \arc\url::safeUrl($evilURL);
         $this->assertEquals( $parsedSafe->host, '127.0.0.1');
         $safeURL = (string) $parsed;
         $this->assertEquals( 'http://127.0.0.1:11211#%40google.com%3A80%2F', $safeURL);
      }