From a3ea9ab59fd2684555269c86d3ab8a4f187954db Mon Sep 17 00:00:00 2001 From: Ryan Jacobs Date: Fri, 7 Aug 2015 16:11:06 -0500 Subject: [PATCH] Issue #2547185 by rjacobs: Ensure standard ports are not included on proto-relative URLs. --- pathologic.module | 17 +++++++++++++---- pathologic.test | 5 +++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pathologic.module b/pathologic.module index 21cf0e6..5975d5e 100644 --- a/pathologic.module +++ b/pathologic.module @@ -452,11 +452,20 @@ function _pathologic_replace($matches) { * * As the Drupal core url() function doesn't support protocol-relative URLs, we * work around it by just creating a full URL and then running it through this - * to strip off the protocol. + * to strip off the scheme and port as necessary. * - * Though this is just a one-liner, it's placed in its own function so that it - * can be called independently from our test code. + * This is placed in its own function so that it can be called independently + * from our test code. */ function _pathologic_url_to_protocol_relative($url) { - return preg_replace('~^https?://~', '//', $url); + // Strip the scheme. + $url_rel = preg_replace('~^https?://~', '//', $url); + if ($url != $url_rel) { + // Also strip the port if it's explicitly declared in the URL. We only do + // this for default ports that accompany http/https schemes as they can + // cause conflicts for scheme-relative URLs. + // @see https://www.drupal.org/node/2547185 + $url_rel = preg_replace('~:(80|443)([^0-9@][^@]*)?$~', '$2', $url_rel); +} + return $url_rel; } diff --git a/pathologic.test b/pathologic.test index 3c28e16..8949a09 100644 --- a/pathologic.test +++ b/pathologic.test @@ -38,6 +38,11 @@ class PathologicTestCase extends DrupalWebTestCase { '//example.org/baz', t('Protocol-relative URL creation with https:// URL') ); + $this->assertEqual( + _pathologic_url_to_protocol_relative('http://user:80pass@example.org:80/baz'), + '//user:80pass@example.org/baz', + t('Protocol-relative URL creation with http:// URL and an explicit port number') + ); // Build a phony filter $filter = new stdClass; -- 1.9.5 (Apple Git-50.3)