Skip to content

Commit

Permalink
Validate Matomo URL in OptOutManager (#22936) (#22943)
Browse files Browse the repository at this point in the history
* Validate Matomo URL in OptOutManager

* apply review feedback
  • Loading branch information
sgiehl authored Jan 17, 2025
1 parent 072d360 commit 7d302ba
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions plugins/CoreAdminHome/OptOutManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Piwik\Request;
use Piwik\Tracker\IgnoreCookie;
use Piwik\Url;
use Piwik\UrlHelper;
use Piwik\View;

/*
Expand Down Expand Up @@ -205,6 +206,20 @@ public function getOptOutJSEmbedCode(
bool $applyStyling,
bool $showIntro
): string {
$parsedUrl = parse_url($matomoUrl);

if (
(!empty($matomoUrl) && false === $parsedUrl)
|| (!empty($parsedUrl['scheme']) && !in_array(strtolower($parsedUrl['scheme']), ['http', 'https']))
|| (empty($parsedUrl['host']) || !Url::isValidHost($parsedUrl['host']))
) {
throw new \Piwik\Exception\Exception('The provided URL is invalid.');
}

// We put together the url based on the parsed parameters manually to ensure it might not include unexpected values
// for protocol less urls starting with //, we need to prepend the double slash again
$matomoUrl = (strpos($matomoUrl, '//') === 0 ? '//' : '') . UrlHelper::getParseUrlReverse($parsedUrl);

return '<div id="matomo-opt-out"></div>
<script src="' . rtrim($matomoUrl, '/') . '/index.php?module=CoreAdminHome&action=optOutJS&divId=matomo-opt-out&language=' . $language . ($applyStyling ? '&backgroundColor=' . $backgroundColor . '&fontColor=' . $fontColor . '&fontSize=' . $fontSize . '&fontFamily=' . $fontFamily : '') . '&showIntro=' . ($showIntro ? '1' : '0') . '"></script>';
}
Expand Down

0 comments on commit 7d302ba

Please sign in to comment.