Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Util/RegexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final class RegexHelper
'|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*+\))';

public const REGEX_PUNCTUATION = '/^[\p{P}\p{S}]/u';
public const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i';
public const REGEX_UNSAFE_PROTOCOL = '/^(?:javascript|vbscript|file|data):/i';
public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i';
public const REGEX_NON_SPACE = '/[^ \t\f\v\r\n]/';

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Util/RegexHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ public static function blockTypesWithInvalidCloserRegexes(): iterable
yield [8];
}

/**
* @dataProvider dataForTestIsLinkPotentiallyUnsafe
*/
#[DataProvider('dataForTestIsLinkPotentiallyUnsafe')]
public function testIsLinkPotentiallyUnsafe(string $url, bool $expected): void
{
$this->assertSame($expected, RegexHelper::isLinkPotentiallyUnsafe($url));
}

/**
* @return iterable<array<mixed>>
*/
public static function dataForTestIsLinkPotentiallyUnsafe(): iterable
{
return [
// Dangerous leading schemes are unsafe
['javascript:alert(1)', true],
['JAVASCRIPT:alert(1)', true],
['vbscript:msgbox(1)', true],
['file:///etc/passwd', true],
['data:text/html,<script>alert(1)</script>', true],
['data:image/svg+xml,<svg onload=alert(1)>', true],
// Safe data: images are allowed
['data:image/png;base64,iVBORw0KGgo=', false],
// URLs merely containing those schemes elsewhere are safe
['https://example.com/view?src=data:image/png', false],
['https://example.com/download?to=file:report', false],
['https://example.com/wiki/vbscript:_basics', false],
['https://example.com/ok', false],
];
}

private function assertRegexMatches(string $pattern, string $string, string $message = ''): void
{
if (\method_exists($this, 'assertMatchesRegularExpression')) {
Expand Down