mirror of
https://github.com/filp/whoops.git
synced 2026-09-16 04:36:20 +00:00
Merge pull request #794 from dualfroz/dualfroz/fix-escapeuris-scheme-allowlist
Restrict escapeButPreserveUris() to http/https schemes (fixes #791)
This commit is contained in:
@@ -80,6 +80,9 @@ class TemplateHelper
|
||||
* Escapes a string for output in an HTML document, but preserves
|
||||
* URIs within it, and converts them to clickable anchor elements.
|
||||
*
|
||||
* Only http and https URIs are linkified; other schemes are left as
|
||||
* plain escaped text so that a link cannot execute script when clicked.
|
||||
*
|
||||
* @param string $raw
|
||||
* @return string
|
||||
*/
|
||||
@@ -87,7 +90,7 @@ class TemplateHelper
|
||||
{
|
||||
$escaped = $this->escape($raw);
|
||||
return preg_replace(
|
||||
"@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
|
||||
"@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@i",
|
||||
"<a href=\"$1\" target=\"_blank\" rel=\"noreferrer noopener\">$1</a>",
|
||||
$escaped
|
||||
);
|
||||
|
||||
@@ -63,6 +63,44 @@ class TemplateHelperTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Util\TemplateHelper::escapeButPreserveUris
|
||||
*/
|
||||
public function testEscapeButPreserveUrisRejectsDangerousSchemes()
|
||||
{
|
||||
$dangerous = [
|
||||
'javascript://x.co/?%0Aalert(1)',
|
||||
'vbscript://x.co/?%0Amsgbox(1)',
|
||||
'data://text/html,<script>alert(1)</script>',
|
||||
];
|
||||
|
||||
foreach ($dangerous as $payload) {
|
||||
$output = $this->helper->escapeButPreserveUris($payload);
|
||||
|
||||
$this->assertStringNotContainsString(
|
||||
'<a href=',
|
||||
$output,
|
||||
"Payload '$payload' must not be turned into a clickable link"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Util\TemplateHelper::escapeButPreserveUris
|
||||
*/
|
||||
public function testEscapeButPreserveUrisAllowsHttpAndHttpsSchemes()
|
||||
{
|
||||
$this->assertEquals(
|
||||
"<a href=\"http://google.com\" target=\"_blank\" rel=\"noreferrer noopener\">http://google.com</a>",
|
||||
$this->helper->escapeButPreserveUris('http://google.com')
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
"<a href=\"https://google.com\" target=\"_blank\" rel=\"noreferrer noopener\">https://google.com</a>",
|
||||
$this->helper->escapeButPreserveUris('https://google.com')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Whoops\Util\TemplateHelper::breakOnDelimiter
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user