Files
UNIT3D-Community-Edition/app/Helpers/Linkify.php
Roardom ad716d7cc3 update: remove XSS cleaner and remove XSS vulnerabilities
We've been mostly relying on the 3rd party xss cleaner to make sure user submitted content is clean. This PR fixes up any leftover holes in the bbcode parser that allow xss vulnerabilities, and as a result, the 3rd party library isn't needed anymore. It cleans responsibly by first, running `htmlspecialchars()` over the content, followed by sanitizing the untrusted urls and whitelisting their protocol.
2025-01-20 02:52:42 +00:00

51 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Helpers;
use VStelmakh\UrlHighlight\Highlighter\HtmlHighlighter;
use VStelmakh\UrlHighlight\UrlHighlight;
use VStelmakh\UrlHighlight\Validator\Validator;
class Linkify
{
public function linky(string $text): string
{
$validator = new Validator(
false, // bool - if should use top level domain to match urls without scheme
[], // string[] - array of blacklisted schemes
[
'http',
'https',
'irc',
'ftp',
'sftp',
'magnet',
], // string[] - array of whitelisted schemes
true // bool - if should match emails (if match by TLD set to "false" - will match only "mailto" urls)
);
$highlighter = new HtmlHighlighter(
'http', // string - scheme to use for urls matched by top level domain
['rel' => 'noopener noreferrer'], // string[] - key/value map of tag attributes
'', // string - content to add before highlight: {here}<a...
'' // string - content to add after highlight: ...</a>{here}
);
return (new UrlHighlight($validator, $highlighter))->highlightUrls($text);
}
}