mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 03:10:09 +02:00
[2.1.0] Create new URI object and migrate URI validation systems to use it. URIScheme interface changed.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1334 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/URI.php';
|
||||
require_once 'HTMLPurifier/URIParser.php';
|
||||
|
||||
require_once 'HTMLPurifier/URIScheme.php';
|
||||
require_once 'HTMLPurifier/URISchemeRegistry.php';
|
||||
|
||||
require_once 'HTMLPurifier/URIScheme/http.php';
|
||||
require_once 'HTMLPurifier/URIScheme/ftp.php';
|
||||
@@ -15,142 +19,140 @@ require_once 'HTMLPurifier/URIScheme/nntp.php';
|
||||
class HTMLPurifier_URISchemeTest extends HTMLPurifier_Harness
|
||||
{
|
||||
|
||||
function test_http() {
|
||||
$scheme = new HTMLPurifier_URIScheme_http();
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$context = new HTMLPurifier_Context();
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'www.example.com', null, '/', 's=foobar', $config, $context),
|
||||
array(null, 'www.example.com', null, '/', 's=foobar')
|
||||
);
|
||||
|
||||
// absorb default port and userinfo
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'www.example.com', 80, '/', 's=foobar', $config, $context),
|
||||
array(null, 'www.example.com', null, '/', 's=foobar')
|
||||
);
|
||||
|
||||
// do not absorb non-default port
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'www.example.com', 8080, '/', 's=foobar', $config, $context),
|
||||
array(null, 'www.example.com', 8080, '/', 's=foobar')
|
||||
);
|
||||
|
||||
// https is basically the same
|
||||
|
||||
$scheme = new HTMLPurifier_URIScheme_https();
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'www.example.com', 443, '/', 's=foobar', $config, $context),
|
||||
array(null, 'www.example.com', null, '/', 's=foobar')
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
function test_ftp() {
|
||||
|
||||
$scheme = new HTMLPurifier_URIScheme_ftp();
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$context = new HTMLPurifier_Context();
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'www.example.com', 21, '/', 's=foobar', $config, $context),
|
||||
array('user', 'www.example.com', null, '/', null)
|
||||
);
|
||||
|
||||
// valid typecode
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'www.example.com', null, '/file.txt;type=a', null, $config, $context),
|
||||
array(null, 'www.example.com', null, '/file.txt;type=a', null)
|
||||
);
|
||||
|
||||
// remove invalid typecode
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'www.example.com', null, '/file.txt;type=z', null, $config, $context),
|
||||
array(null, 'www.example.com', null, '/file.txt', null)
|
||||
);
|
||||
|
||||
// encode errant semicolons
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'www.example.com', null, '/too;many;semicolons=1', null, $config, $context),
|
||||
array(null, 'www.example.com', null, '/too%3Bmany%3Bsemicolons=1', null)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function test_news() {
|
||||
|
||||
$scheme = new HTMLPurifier_URIScheme_news();
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$context = new HTMLPurifier_Context();
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, null, null, 'gmane.science.linguistics', null, $config, $context),
|
||||
array(null, null, null, 'gmane.science.linguistics', null)
|
||||
);
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, null, null, '642@eagle.ATT.COM', null, $config, $context),
|
||||
array(null, null, null, '642@eagle.ATT.COM', null)
|
||||
);
|
||||
|
||||
// test invalid field removal
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'www.google.com', 80, 'rec.music', 'path=foo', $config, $context),
|
||||
array(null, null, null, 'rec.music', null)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function test_nntp() {
|
||||
|
||||
$scheme = new HTMLPurifier_URIScheme_nntp();
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$context = new HTMLPurifier_Context();
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, 'news.example.com', null, '/alt.misc/12345', null, $config, $context),
|
||||
array(null, 'news.example.com', null, '/alt.misc/12345', null)
|
||||
);
|
||||
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'news.example.com', 119, '/alt.misc/12345', 'foo=asdf', $config, $context),
|
||||
array(null, 'news.example.com', null, '/alt.misc/12345', null)
|
||||
function test_http_regular() {
|
||||
$this->assertValidation(
|
||||
'http://example.com/?s=q#fragment'
|
||||
);
|
||||
}
|
||||
|
||||
function test_mailto() {
|
||||
|
||||
$scheme = new HTMLPurifier_URIScheme_mailto();
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$context = new HTMLPurifier_Context();
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
null, null, null, 'bob@example.com', null, $config, $context),
|
||||
array(null, null, null, 'bob@example.com', null)
|
||||
function test_http_removeDefaultPort() {
|
||||
$this->assertValidation(
|
||||
'http://example.com:80',
|
||||
'http://example.com'
|
||||
);
|
||||
|
||||
$this->assertIdentical(
|
||||
$scheme->validateComponents(
|
||||
'user', 'example.com', 80, 'bob@example.com', 'subject=Foo!', $config, $context),
|
||||
array(null, null, null, 'bob@example.com', 'subject=Foo!')
|
||||
}
|
||||
|
||||
function test_http_removeUserInfo() {
|
||||
$this->assertValidation(
|
||||
'http://bob@example.com',
|
||||
'http://example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function test_http_preserveNonDefaultPort() {
|
||||
$this->assertValidation(
|
||||
'http://example.com:8080'
|
||||
);
|
||||
}
|
||||
|
||||
function test_https_regular() {
|
||||
$this->assertValidation(
|
||||
'https://user@example.com:443/?s=q#frag',
|
||||
'https://example.com/?s=q#frag'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_regular() {
|
||||
$this->assertValidation(
|
||||
'ftp://user@example.com/path'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_removeDefaultPort() {
|
||||
$this->assertValidation(
|
||||
'ftp://example.com:21',
|
||||
'ftp://example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_removeQueryString() {
|
||||
$this->assertValidation(
|
||||
'ftp://example.com?s=q',
|
||||
'ftp://example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_preserveValidTypecode() {
|
||||
$this->assertValidation(
|
||||
'ftp://example.com/file.txt;type=a'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_removeInvalidTypecode() {
|
||||
$this->assertValidation(
|
||||
'ftp://example.com/file.txt;type=z',
|
||||
'ftp://example.com/file.txt'
|
||||
);
|
||||
}
|
||||
|
||||
function test_ftp_encodeExtraSemicolons() {
|
||||
$this->assertValidation(
|
||||
'ftp://example.com/too;many;semicolons=1',
|
||||
'ftp://example.com/too%3Bmany%3Bsemicolons=1'
|
||||
);
|
||||
}
|
||||
|
||||
function test_news_regular() {
|
||||
$this->assertValidation(
|
||||
'news:gmane.science.linguistics'
|
||||
);
|
||||
}
|
||||
|
||||
function test_news_explicit() {
|
||||
$this->assertValidation(
|
||||
'news:642@eagle.ATT.COM'
|
||||
);
|
||||
}
|
||||
|
||||
function test_news_removeNonPathComponents() {
|
||||
$this->assertValidation(
|
||||
'news://user@example.com:80/rec.music?path=foo#frag',
|
||||
'news:/rec.music#frag'
|
||||
);
|
||||
}
|
||||
|
||||
function test_nntp_regular() {
|
||||
$this->assertValidation(
|
||||
'nntp://news.example.com/alt.misc/42#frag'
|
||||
);
|
||||
}
|
||||
|
||||
function test_nntp_removalOfRedundantOrUselessComponents() {
|
||||
$this->assertValidation(
|
||||
'nntp://user@news.example.com:119/alt.misc/42?s=q#frag',
|
||||
'nntp://news.example.com/alt.misc/42#frag'
|
||||
);
|
||||
}
|
||||
|
||||
function test_mailto_regular() {
|
||||
$this->assertValidation(
|
||||
'mailto:bob@example.com'
|
||||
);
|
||||
}
|
||||
|
||||
function test_mailto_removalOfRedundantOrUselessComponents() {
|
||||
$this->assertValidation(
|
||||
'mailto://user@example.com:80/bob@example.com?subject=Foo#frag',
|
||||
'mailto:/bob@example.com?subject=Foo#frag'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user