Compare commits

...

11 commits

Author SHA1 Message Date
El RIDO
bd61a3d021
enable tests to pass
The path is only optional when it is / and the very last element, otherwise it is required. As soon as it is in the middle of a URL it helps the parser to identify which part is the username and domain and what is path and GET parameters. The @ sign is legitimate, if unusual, in the latter two.
2025-09-03 20:13:33 +02:00
rugk
a6034ace1b test: PHP considers this invalid 2025-09-03 14:25:04 +00:00
rugk
616635c66c style: scruintizer wants some trailing comma 2025-09-03 14:21:00 +00:00
rugk
e4f2383dd8 test: more test cases for testForeignUrlUsingUsernameTrick 2025-09-03 14:20:03 +00:00
rugk
25dca0838e style(codespaces): comment PHP unit testing setup for now 2025-09-03 14:14:08 +00:00
rugk
cfc687d62b style: fix indentation 2025-09-03 14:12:12 +00:00
rugk
168fed64b9 chore: apply Scruintizer diff 2025-09-03 14:11:35 +00:00
rugk
4f13d93af2 style: use explicit types 2025-09-03 13:53:51 +00:00
rugk
f76704a88c refactor: simplify tests 2025-09-03 13:48:28 +00:00
rugk
dbaa70ec11 test: move ftp example to rejected because of foreign URL 2025-09-03 13:45:30 +00:00
rugk
879b696f22 wipfix: correct contatenation of options 2025-09-03 13:43:57 +00:00
3 changed files with 36 additions and 18 deletions

View file

@ -8,8 +8,8 @@ ln -s ./conf.sample.php cfg/conf.php
composer install --no-dev --optimize-autoloader
# for PHP unit testing
composer require google/cloud-storage
composer install --optimize-autoloader
# composer require google/cloud-storage
# composer install --optimize-autoloader
sudo chmod a+x "$(pwd)" && sudo rm -rf /var/www/html && sudo ln -s "$(pwd)" /var/www/html

View file

@ -49,14 +49,14 @@ abstract class AbstractProxy
*/
public function __construct(Configuration $conf, string $link)
{
if (!filter_var($link, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED & FILTER_FLAG_QUERY_REQUIRED)) {
if (!filter_var($link, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED | FILTER_FLAG_QUERY_REQUIRED)) {
$this->_error = 'Invalid URL given.';
return;
}
if (!str_starts_with($link, $conf->getKey('basepath') . '?') ||
parse_url($link, PHP_URL_HOST) != parse_url($conf->getKey('basepath'), PHP_URL_HOST)
) {
) {
$this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
return;
}

View file

@ -45,28 +45,33 @@ class YourlsProxyTest extends TestCase
$yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
$this->assertFalse($yourls->isError());
$this->assertEquals($yourls->getUrl(), 'https://example.com/1');
$yourls = new YourlsProxy($this->_conf, 'https://example.com/?@foreign.malicious.example?foo#bar');
$this->assertFalse($yourls->isError());
$this->assertEquals($yourls->getUrl(), 'https://example.com/1');
}
/**
* @dataProvider providerInvalidUrl
*/
public function testImvalidUrl($uri)
public function testImvalidUrl($url): void
{
$yourls = new YourlsProxy($this->_conf, $uri);
$yourls = new YourlsProxy($this->_conf, $url);
$this->assertTrue($yourls->isError());
$this->assertEquals($yourls->getError(), 'Invalid URL given.');
}
public function providerInvalidUrl() {
public function providerInvalidUrl(): array
{
return array(
array(''),
array(' '),
array('foo'),
array('https://'),
array('ftp://example.com/?n=np'),
array('https://example.com'), // missing path and query parameter,
array('https://example.com/'), // missing query parameter
array('https://example.com?paste=something'), // missing path parameter
array('https://example.com@foreign.malicious.example?foo#bar'), // missing path parameter
);
}
@ -74,27 +79,40 @@ class YourlsProxyTest extends TestCase
* This tests for a trick using username of an URI, see:
* {@see https://cloud.google.com/blog/topics/threat-intelligence/url-obfuscation-schema-abuse/?hl=en}
*
* @return void
* @dataProvider providerForeignUrlUsernameTrick
*/
public function testForeignUrlUsingUsernameTrick()
public function testForeignUrlUsingUsernameTrick($url): void
{
$yourls = new YourlsProxy($this->_conf, 'https://example.com/@foreign.malicious.example?foo#bar');
$yourls = new YourlsProxy($this->_conf, $url);
$this->assertTrue($yourls->isError());
$this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
}
public function testForeignUrl()
public function providerForeignUrlUsernameTrick(): array
{
$yourls = new YourlsProxy($this->_conf, 'https://other.example.com/?foo#bar');
return array(
array('https://example.com@foreign.malicious.example/?foo#bar'),
array('https://example.com/@foreign.malicious.example?foo#bar'),
);
}
/**
* @dataProvider providerForeignUrl
*/
public function testForeignUrl($url): void
{
$yourls = new YourlsProxy($this->_conf, $url);
$this->assertTrue($yourls->isError());
$this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
}
public function testSneakyForeignUrl()
public function providerForeignUrl(): array
{
$yourls = new YourlsProxy($this->_conf, 'https://other.example.com/?q=https://example.com/?foo#bar');
$this->assertTrue($yourls->isError());
$this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
return array(
array('ftp://example.com/?n=np'), // wrong protocol
array('https://other.example.com/?foo#bar'), // wrong domain
array('https://other.example.com/?q=https://example.com/?foo#bar'), // domain included inside string
);
}
public function testYourlsError()