From bcfbb8338c63718cd92c49b0c659333f9d6f0f46 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 10 Apr 2011 13:09:24 +0100 Subject: [PATCH] URI.Munge munges https to http URIs. Signed-off-by: Edward Z. Yang --- NEWS | 2 ++ library/HTMLPurifier/URIFilter/Munge.php | 11 +++++++++-- library/HTMLPurifier/URIScheme.php | 6 ++++++ library/HTMLPurifier/URIScheme/https.php | 1 + tests/HTMLPurifier/URIFilter/MungeTest.php | 17 +++++++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index a90f38b6..7d1e8a94 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier ========================== 4.3.1, unknown release date +# URI.Munge now munges URIs inside the same host that go from https + to http. Reported by Neike Taika-Tessaro. - Color keywords are now case insensitive. Thanks Yzmir Ramirez for reporting. diff --git a/library/HTMLPurifier/URIFilter/Munge.php b/library/HTMLPurifier/URIFilter/Munge.php index efa10a64..40d059b2 100644 --- a/library/HTMLPurifier/URIFilter/Munge.php +++ b/library/HTMLPurifier/URIFilter/Munge.php @@ -23,9 +23,16 @@ class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter if (is_null($uri->host) || empty($scheme_obj->browsable)) { return true; } + $uri_definition = $config->getDefinition('URI'); // don't redirect if target host is our host - if ($uri->host === $config->getDefinition('URI')->host) { - return true; + if ($uri->host === $uri_definition->host) { + // but do redirect if we're currently on a secure scheme, + // and the target scheme is insecure + $current_scheme_obj = HTMLPurifier_URISchemeRegistry::instance()->getScheme($uri_definition->defaultScheme, $config, $context); + if ($scheme_obj->secure || !$current_scheme_obj->secure) { + return true; + } + // target scheme was not secure, but we were secure } $this->makeReplace($uri, $config, $context); diff --git a/library/HTMLPurifier/URIScheme.php b/library/HTMLPurifier/URIScheme.php index 25eb8410..7be95814 100644 --- a/library/HTMLPurifier/URIScheme.php +++ b/library/HTMLPurifier/URIScheme.php @@ -20,6 +20,12 @@ abstract class HTMLPurifier_URIScheme public $browsable = false; /** + * Whether or not data transmitted over this scheme is encrypted. + * https is secure, http is not. + */ + public $secure = false; + + /** * Whether or not the URI always uses , resolves edge cases * with making relative URIs absolute */ diff --git a/library/HTMLPurifier/URIScheme/https.php b/library/HTMLPurifier/URIScheme/https.php index 29e38091..159c2874 100644 --- a/library/HTMLPurifier/URIScheme/https.php +++ b/library/HTMLPurifier/URIScheme/https.php @@ -6,6 +6,7 @@ class HTMLPurifier_URIScheme_https extends HTMLPurifier_URIScheme_http { public $default_port = 443; + public $secure = true; } diff --git a/tests/HTMLPurifier/URIFilter/MungeTest.php b/tests/HTMLPurifier/URIFilter/MungeTest.php index 09624b07..1acf11ea 100644 --- a/tests/HTMLPurifier/URIFilter/MungeTest.php +++ b/tests/HTMLPurifier/URIFilter/MungeTest.php @@ -117,6 +117,23 @@ class HTMLPurifier_URIFilter_MungeTest extends HTMLPurifier_URIFilterHarness $this->assertFiltering('http://example.com/foobar'); } + function testMungeIgnoreSameDomainInsecureToSecure() { + $this->setMunge('http://example.com/%s'); + $this->assertFiltering('https://example.com/foobar'); + } + + function testMungeIgnoreSameDomainSecureToSecure() { + $this->config->set('URI.Base', 'https://example.com'); + $this->setMunge('http://example.com/%s'); + $this->assertFiltering('https://example.com/foobar'); + } + + function testMungeSameDomainSecureToInsecure() { + $this->config->set('URI.Base', 'https://example.com'); + $this->setMunge('/%s'); + $this->assertFiltering('http://example.com/foobar', '/http%3A%2F%2Fexample.com%2Ffoobar'); + } + function testMungeIgnoresSourceHost() { $this->config->set('URI.Host', 'foo.example.com'); $this->setMunge('http://example.com/%s'); -- 2.11.4.GIT