Is it possible to strip everything after the video ID from a YouTube URL? I have noticed when people are posting YouTube links they are not sanitizing them: everything after the video ID is included. Of particular concern is the "si" variable that will link your feed with the feed of the user that posted the link with the "si" variable. These tokens can also be used for tracking.
Came here to echo this. I've seen a few lately and it seems a lot of people don't realize the SI parameter is an identifier that can be traced back to them. At least a line at the top with maintenance and announcements would increase awareness, but you can also have it automatically stripped with something along the lines of the below and would work for both posting links and embeds:
function stripYouTubeSi(string $message): string
{
$pattern = '~\bhttps?://(?:[a-z0-9-]+\.)*'
. '(?:youtube\.com|youtu\.be)'
. '(?=[/?\#]|$)[^\s<>"\'\[\]]*~i';
return preg_replace_callback($pattern, function ($match)
{
$url = $match[0];
$parts = explode('#', $url, 2);
$url = $parts[0];
$fragment = isset($parts[1]) ? '#' . $parts[1] : '';
$parts = explode('?', $url, 2);
if (!isset($parts[1]))
{
return $match[0];
}
$parameters = explode('&', $parts[1]);
$parameters = array_filter($parameters, function ($parameter)
{
$name = explode('=', $parameter, 2)[0];
return rawurldecode($name) !== 'si';
});
$query = implode('&', $parameters);
return $parts[0]
. ($query !== '' ? '?' . $query : '')
. $fragment;
}, $message);
}