Skip to content

Commit 64f1379

Browse files
Merge pull request #72 from matomo-org/AS-494
Added validation rule for apiMethod parameter in getTreemapData API call, #AS-494
2 parents 9258075 + 716d325 commit 64f1379

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

API.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
use Piwik\API\Request;
1414
use Piwik\Common;
1515
use Piwik\DataTable;
16+
use Piwik\Http\BadRequestException;
1617
use Piwik\Metrics;
1718
use Piwik\Period\Range;
19+
use Piwik\Piwik;
1820
use Piwik\Plugins\TreemapVisualization\Visualizations\Treemap;
1921

2022
/**
@@ -49,9 +51,15 @@ public function getTreemapData(
4951
$availableHeight = false,
5052
$show_evolution_values = false
5153
) {
52-
if (!Request::isCurrentApiRequestTheRootApiRequest()) {
54+
if (!Request::isCurrentApiRequestTheRootApiRequest() && !defined('PIWIK_TEST_MODE')) {
5355
return [];
5456
}
57+
list($module, $method) = explode('.', $apiMethod);
58+
$disAllowedApiActions = ['getBulkRequest'];
59+
// Block if API action does not start with get
60+
if (!$method || in_array($method, $disAllowedApiActions) || stripos($method, 'get') !== 0) {
61+
throw new BadRequestException(Piwik::translate('TreemapVisualization_InvalidApiMethodException'));
62+
}
5563

5664
if (
5765
$period == 'range'

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Changelog
22

3+
# 5.0.5 - 2026-02-16
4+
- Added validation rules for ApiAction
5+
36
# 5.0.4 - 2025-07-07
47
- Textual changes
58

lang/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"TreemapVisualization": {
33
"PluginDescription": "Visualise any report in Matomo as a Treemap. Click on the Treemap icon in each report to load the visualisation.",
4-
"Treemap": "Treemap"
4+
"Treemap": "Treemap",
5+
"InvalidApiMethodException": "Invalid API method."
56
}
67
}

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "TreemapVisualization",
3-
"version": "5.0.4",
3+
"version": "5.0.5",
44
"description": "Visualise any report in Matomo as a Treemap. Click on the Treemap icon in each report to load the visualisation.",
55
"keywords": ["treemap", "graph", "visualization", "infovis", "jit"],
66
"license": "GPL v3+",

tests/Integration/ApiTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
namespace Piwik\Plugins\TreemapVisualization\tests\Integration;
11+
12+
use Piwik\Http\BadRequestException;
13+
use Piwik\Plugins\TreemapVisualization\API;
14+
use Piwik\Tests\Framework\Fixture;
15+
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
16+
17+
/**
18+
* @group TreemapVisualization
19+
* @group ApiTest
20+
* @group Plugins
21+
*/
22+
class ApiTest extends IntegrationTestCase
23+
{
24+
private $api;
25+
26+
public function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
Fixture::createSuperUser();
31+
$this->api = API::getInstance();
32+
}
33+
34+
/**
35+
* @dataProvider validMethod
36+
*/
37+
public function testGetTreemapDataAllowsValidApiMethod($method): void
38+
{
39+
$result = $this->api->getTreemapData(
40+
$method,
41+
'nb_visits',
42+
'day',
43+
'2025-02-03'
44+
);
45+
46+
$this->assertIsArray($result);
47+
}
48+
49+
/**
50+
* @dataProvider inValidMethod
51+
*/
52+
public function testGetTreemapDataRejectsInvalidApiMethod($method): void
53+
{
54+
$this->expectException(BadRequestException::class);
55+
$this->api->getTreemapData(
56+
$method,
57+
'nb_visits',
58+
'day',
59+
'2025-02-03'
60+
);
61+
}
62+
63+
public function validMethod()
64+
{
65+
return [
66+
['API.getMatomoVersion'],
67+
['API.getPhpVersion'],
68+
];
69+
}
70+
71+
public function inValidMethod()
72+
{
73+
return [
74+
['API.getBulkRequest'],
75+
['UsersManager.deleteUser'],
76+
];
77+
}
78+
}

0 commit comments

Comments
 (0)