mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-06 06:07:26 +02:00
[2.1.0] Further refactoring of AttrDef_URI, creation of new URIFilter and URIDefinition subsystems.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1335 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -48,6 +48,64 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
|
||||
$this->assertDef('javascript:foobar();', false);
|
||||
}
|
||||
|
||||
function test_validate_configDisableExternal() {
|
||||
|
||||
$this->def = new HTMLPurifier_AttrDef_URI();
|
||||
|
||||
$this->config->set('URI', 'DisableExternal', true);
|
||||
$this->config->set('URI', 'Host', 'sub.example.com');
|
||||
|
||||
$this->assertDef('/foobar.txt');
|
||||
$this->assertDef('http://google.com/', false);
|
||||
$this->assertDef('http://sub.example.com/alas?foo=asd');
|
||||
$this->assertDef('http://example.com/teehee', false);
|
||||
$this->assertDef('http://www.example.com/#man', false);
|
||||
$this->assertDef('http://go.sub.example.com/perhaps?p=foo');
|
||||
|
||||
}
|
||||
|
||||
function test_validate_configDisableExternalResources() {
|
||||
|
||||
$this->config->set('URI', 'DisableExternalResources', true);
|
||||
|
||||
$this->assertDef('http://sub.example.com/alas?foo=asd');
|
||||
$this->assertDef('/img.png');
|
||||
|
||||
$this->def = new HTMLPurifier_AttrDef_URI(true);
|
||||
|
||||
$this->assertDef('http://sub.example.com/alas?foo=asd', false);
|
||||
$this->assertDef('/img.png');
|
||||
|
||||
}
|
||||
|
||||
function test_validate_configBlacklist() {
|
||||
|
||||
$this->config->set('URI', 'HostBlacklist', array('example.com', 'moo'));
|
||||
|
||||
$this->assertDef('foo.txt');
|
||||
$this->assertDef('http://www.google.com/example.com/moo');
|
||||
|
||||
$this->assertDef('http://example.com/#23', false);
|
||||
$this->assertDef('https://sub.domain.example.com/foobar', false);
|
||||
$this->assertDef('http://example.com.example.net/?whoo=foo', false);
|
||||
$this->assertDef('ftp://moo-moo.net/foo/foo/', false);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
function test_validate_configWhitelist() {
|
||||
|
||||
$this->config->set('URI', 'HostPolicy', 'DenyAll');
|
||||
$this->config->set('URI', 'HostWhitelist', array(null, 'google.com'));
|
||||
|
||||
$this->assertDef('http://example.com/fo/google.com', false);
|
||||
$this->assertDef('server.txt');
|
||||
$this->assertDef('ftp://www.google.com/?t=a');
|
||||
$this->assertDef('http://google.com.tricky.spamsite.net', false);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIParser.php';
|
||||
|
||||
/**
|
||||
* All-use harness, use this rather than SimpleTest's
|
||||
*/
|
||||
@@ -12,18 +14,46 @@ class HTMLPurifier_Harness extends UnitTestCase
|
||||
|
||||
var $config, $context;
|
||||
|
||||
/**
|
||||
* Generates easily accessible default config/context
|
||||
*/
|
||||
function setUp() {
|
||||
list($this->config, $this->context) = $this->createCommon();
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts config and context and prepares them into a valid state
|
||||
* @param &$config Reference to config variable
|
||||
* @param &$context Reference to context variable
|
||||
*/
|
||||
function prepareCommon(&$config, &$context) {
|
||||
$config = HTMLPurifier_Config::create($config);
|
||||
if (!$context) $context = new HTMLPurifier_Context();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates default configuration and context objects
|
||||
* @return Defaults in form of array($config, $context)
|
||||
*/
|
||||
function createCommon() {
|
||||
return array(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
|
||||
}
|
||||
|
||||
/**
|
||||
* If $expect is false, ignore $result and check if status failed.
|
||||
* Otherwise, check if $status if true and $result === $expect.
|
||||
* @param $status Boolean status
|
||||
* @param $result Mixed result from processing
|
||||
* @param $expect Mixed expectation for result
|
||||
*/
|
||||
function assertEitherFailOrIdentical($status, $result, $expect) {
|
||||
if ($expect === false) {
|
||||
$this->assertFalse($status, 'Expected false result, got true');
|
||||
} else {
|
||||
$this->assertTrue($status, 'Expected true result, got false');
|
||||
$this->assertIdentical($result, $expect);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
34
tests/HTMLPurifier/URIDefinitionTest.php
Normal file
34
tests/HTMLPurifier/URIDefinitionTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIHarness.php';
|
||||
require_once 'HTMLPurifier/URIDefinition.php';
|
||||
|
||||
class HTMLPurifier_URIDefinitionTest extends HTMLPurifier_URIHarness
|
||||
{
|
||||
|
||||
function createFilterMock($expect = true, $result = true) {
|
||||
generate_mock_once('HTMLPurifier_URIFilter');
|
||||
$mock = new HTMLPurifier_URIFilterMock();
|
||||
if ($expect) $mock->expectOnce('filter');
|
||||
else $mock->expectNever('filter');
|
||||
$mock->setReturnValue('filter', $result);
|
||||
return $mock;
|
||||
}
|
||||
|
||||
function test_filter() {
|
||||
$def = new HTMLPurifier_URIDefinition();
|
||||
$def->filters[] = $this->createFilterMock();
|
||||
$def->filters[] = $this->createFilterMock();
|
||||
$uri = $this->createURI('test');
|
||||
$this->assertTrue($def->filter($uri, $this->config, $this->context));
|
||||
}
|
||||
|
||||
function test_filter_earlyAbortIfFail() {
|
||||
$def = new HTMLPurifier_URIDefinition();
|
||||
$def->filters[] = $this->createFilterMock(true, false);
|
||||
$def->filters[] = $this->createFilterMock(false); // never called
|
||||
$uri = $this->createURI('test');
|
||||
$this->assertFalse($def->filter($uri, $this->config, $this->context));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIFilter/DisableExternalTest.php';
|
||||
require_once 'HTMLPurifier/URIFilter/DisableExternalResources.php';
|
||||
|
||||
class HTMLPurifier_URIFilter_DisableExternalResourcesTest extends
|
||||
HTMLPurifier_URIFilter_DisableExternalTest
|
||||
{
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$var = true;
|
||||
$this->context->register('EmbeddedURI', $var);
|
||||
}
|
||||
|
||||
function testPreserveWhenNotEmbedded() {
|
||||
$this->context->destroy('EmbeddedURI'); // undo setUp
|
||||
$this->assertFiltering(
|
||||
'http://example.com'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
47
tests/HTMLPurifier/URIFilter/DisableExternalTest.php
Normal file
47
tests/HTMLPurifier/URIFilter/DisableExternalTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIFilter/DisableExternal.php';
|
||||
require_once 'HTMLPurifier/URIFilterHarness.php';
|
||||
|
||||
class HTMLPurifier_URIFilter_DisableExternalTest extends HTMLPurifier_URIFilterHarness
|
||||
{
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->filter = new HTMLPurifier_URIFilter_DisableExternal();
|
||||
}
|
||||
|
||||
function testRemoveExternal() {
|
||||
$this->assertFiltering(
|
||||
'http://example.com', false
|
||||
);
|
||||
}
|
||||
|
||||
function testPreserveInternal() {
|
||||
$this->assertFiltering(
|
||||
'/foo/bar'
|
||||
);
|
||||
}
|
||||
|
||||
function testPreserveOurHost() {
|
||||
$this->config->set('URI', 'Host', 'example.com');
|
||||
$this->assertFiltering(
|
||||
'http://example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function testPreserveOurSubdomain() {
|
||||
$this->config->set('URI', 'Host', 'example.com');
|
||||
$this->assertFiltering(
|
||||
'http://www.example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function testRemoveSuperdomain() {
|
||||
$this->config->set('URI', 'Host', 'www.example.com');
|
||||
$this->assertFiltering(
|
||||
'http://example.com', false
|
||||
);
|
||||
}
|
||||
|
||||
}
|
15
tests/HTMLPurifier/URIFilterHarness.php
Normal file
15
tests/HTMLPurifier/URIFilterHarness.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIHarness.php';
|
||||
|
||||
class HTMLPurifier_URIFilterHarness extends HTMLPurifier_URIHarness
|
||||
{
|
||||
|
||||
function assertFiltering($uri, $expect_uri = true) {
|
||||
$this->prepareURI($uri, $expect_uri);
|
||||
$this->filter->prepare($this->config, $this->context);
|
||||
$result = $this->filter->filter($uri, $this->config, $this->context);
|
||||
$this->assertEitherFailOrIdentical($result, $uri, $expect_uri);
|
||||
}
|
||||
|
||||
}
|
31
tests/HTMLPurifier/URIHarness.php
Normal file
31
tests/HTMLPurifier/URIHarness.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URIParser.php';
|
||||
|
||||
class HTMLPurifier_URIHarness extends HTMLPurifier_Harness
|
||||
{
|
||||
|
||||
/**
|
||||
* Prepares two URIs into object form
|
||||
* @param &$uri Reference to string input URI
|
||||
* @param &$expect_uri Reference to string expectation URI
|
||||
* @note If $expect_uri is false, it will stay false
|
||||
*/
|
||||
function prepareURI(&$uri, &$expect_uri) {
|
||||
$parser = new HTMLPurifier_URIParser();
|
||||
if ($expect_uri === true) $uri = $expect_uri;
|
||||
$uri = $parser->parse($uri);
|
||||
if ($expect_uri !== false) {
|
||||
$expect_uri = $parser->parse($expect_uri);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a URI object from the corresponding string
|
||||
*/
|
||||
function createURI($uri) {
|
||||
$parser = new HTMLPurifier_URIParser();
|
||||
return $parser->parse($uri);
|
||||
}
|
||||
|
||||
}
|
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URI.php';
|
||||
require_once 'HTMLPurifier/URIParser.php';
|
||||
|
||||
require_once 'HTMLPurifier/URIScheme.php';
|
||||
require_once 'HTMLPurifier/URISchemeRegistry.php';
|
||||
@@ -16,25 +15,15 @@ require_once 'HTMLPurifier/URIScheme/nntp.php';
|
||||
// WARNING: All the URI schemes are far to relaxed, we need to tighten
|
||||
// the checks.
|
||||
|
||||
class HTMLPurifier_URISchemeTest extends HTMLPurifier_Harness
|
||||
class HTMLPurifier_URISchemeTest extends HTMLPurifier_URIHarness
|
||||
{
|
||||
|
||||
function assertValidation($uri, $expect_uri = true) {
|
||||
$parser = new HTMLPurifier_URIParser();
|
||||
if ($expect_uri === true) $uri = $expect_uri;
|
||||
$uri = $parser->parse($uri);
|
||||
if ($expect_uri !== false) {
|
||||
$expect_uri = $parser->parse($expect_uri);
|
||||
}
|
||||
$this->prepareURI($uri, $expect_uri);
|
||||
// convenience hack: the scheme should be explicitly specified
|
||||
$scheme = $uri->getSchemeObj($this->config, $this->context);
|
||||
$result = $scheme->validate($uri, $this->config, $this->context);
|
||||
if ($expect_uri !== false) {
|
||||
$this->assertTrue($result);
|
||||
$this->assertIdentical($uri, $expect_uri);
|
||||
} else {
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
$this->assertEitherFailOrIdentical($result, $uri, $expect_uri);
|
||||
}
|
||||
|
||||
function test_http_regular() {
|
||||
|
@@ -3,7 +3,7 @@
|
||||
require_once 'HTMLPurifier/URI.php';
|
||||
require_once 'HTMLPurifier/URIParser.php';
|
||||
|
||||
class HTMLPurifier_URITest extends HTMLPurifier_Harness
|
||||
class HTMLPurifier_URITest extends HTMLPurifier_URIHarness
|
||||
{
|
||||
|
||||
function createURI($uri) {
|
||||
@@ -179,62 +179,4 @@ class HTMLPurifier_URITest extends HTMLPurifier_Harness
|
||||
$this->assertValidation('http://[2001:0db8:85z3:08d3:1319:8a2e:0370:7334]', '');
|
||||
}
|
||||
|
||||
function test_validate_configDisableExternal() {
|
||||
|
||||
$this->def = new HTMLPurifier_AttrDef_URI();
|
||||
|
||||
$this->config->set('URI', 'DisableExternal', true);
|
||||
$this->config->set('URI', 'Host', 'sub.example.com');
|
||||
|
||||
$this->assertValidation('/foobar.txt');
|
||||
$this->assertValidation('http://google.com/', false);
|
||||
$this->assertValidation('http://sub.example.com/alas?foo=asd');
|
||||
$this->assertValidation('http://example.com/teehee', false);
|
||||
$this->assertValidation('http://www.example.com/#man', false);
|
||||
$this->assertValidation('http://go.sub.example.com/perhaps?p=foo');
|
||||
|
||||
}
|
||||
|
||||
function test_validate_configDisableExternalResources() {
|
||||
|
||||
$this->config->set('URI', 'DisableExternalResources', true);
|
||||
|
||||
$this->assertValidation('http://sub.example.com/alas?foo=asd');
|
||||
$this->assertValidation('/img.png');
|
||||
|
||||
$embeds = true; // passed by reference
|
||||
$this->context->register('EmbeddedURI', $embeds);
|
||||
$this->assertValidation('http://sub.example.com/alas?foo=asd', false);
|
||||
$this->assertValidation('/img.png');
|
||||
|
||||
}
|
||||
|
||||
function test_validate_configBlacklist() {
|
||||
|
||||
$this->config->set('URI', 'HostBlacklist', array('example.com', 'moo'));
|
||||
|
||||
$this->assertValidation('foo.txt');
|
||||
$this->assertValidation('http://www.google.com/example.com/moo');
|
||||
|
||||
$this->assertValidation('http://example.com/#23', false);
|
||||
$this->assertValidation('https://sub.domain.example.com/foobar', false);
|
||||
$this->assertValidation('http://example.com.example.net/?whoo=foo', false);
|
||||
$this->assertValidation('ftp://moo-moo.net/foo/foo/', false);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
function test_validate_configWhitelist() {
|
||||
|
||||
$this->config->set('URI', 'HostPolicy', 'DenyAll');
|
||||
$this->config->set('URI', 'HostWhitelist', array(null, 'google.com'));
|
||||
|
||||
$this->assertValidation('http://example.com/fo/google.com', false);
|
||||
$this->assertValidation('server.txt');
|
||||
$this->assertValidation('ftp://www.google.com/?t=a');
|
||||
$this->assertValidation('http://google.com.tricky.spamsite.net', false);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user