-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
CoreUpdater: prevent token creation/disclosure via oneClickResults #24240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
plugins/CoreUpdater/tests/Integration/ControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| namespace Piwik\Plugins\CoreUpdater\tests\Integration; | ||
|
|
||
| use Piwik\Config; | ||
| use Piwik\Nonce; | ||
| use Piwik\Plugins\CoreUpdater\Controller; | ||
| use Piwik\Plugins\CoreUpdater\Updater; | ||
| use Piwik\Tests\Framework\Mock\FakeAccess; | ||
| use Piwik\Tests\Framework\TestCase\IntegrationTestCase; | ||
|
|
||
| /** | ||
| * @group Plugins | ||
| * @group CoreUpdater | ||
| */ | ||
| class ControllerTest extends IntegrationTestCase | ||
| { | ||
| private $originalGet = []; | ||
| private $originalPost = []; | ||
| private $originalEnableAutoUpdate; | ||
| private $originalEnableInternetFeatures; | ||
| private $originalUpdateDetailsToken; | ||
| private $hadEnableAutoUpdate = false; | ||
| private $hadEnableInternetFeatures = false; | ||
| private $hadUpdateDetailsToken = false; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->originalGet = $_GET; | ||
| $this->originalPost = $_POST; | ||
|
|
||
| $general = Config::getInstance()->General; | ||
| $this->hadEnableAutoUpdate = array_key_exists('enable_auto_update', $general); | ||
| $this->hadEnableInternetFeatures = array_key_exists('enable_internet_features', $general); | ||
| $this->hadUpdateDetailsToken = array_key_exists('update_details_token', $general); | ||
| $this->originalEnableAutoUpdate = $general['enable_auto_update'] ?? null; | ||
| $this->originalEnableInternetFeatures = $general['enable_internet_features'] ?? null; | ||
| $this->originalUpdateDetailsToken = $general['update_details_token'] ?? null; | ||
|
|
||
| Config::getInstance()->General['enable_auto_update'] = 1; | ||
| Config::getInstance()->General['enable_internet_features'] = 1; | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| $_GET = $this->originalGet; | ||
| $_POST = $this->originalPost; | ||
|
|
||
| FakeAccess::clearAccess(); | ||
|
|
||
| $config = Config::getInstance(); | ||
| if ($this->hadEnableAutoUpdate) { | ||
| $config->General['enable_auto_update'] = $this->originalEnableAutoUpdate; | ||
| } else { | ||
| unset($config->General['enable_auto_update']); | ||
| } | ||
|
|
||
| if ($this->hadEnableInternetFeatures) { | ||
| $config->General['enable_internet_features'] = $this->originalEnableInternetFeatures; | ||
| } else { | ||
| unset($config->General['enable_internet_features']); | ||
| } | ||
|
|
||
| if ($this->hadUpdateDetailsToken) { | ||
| $config->General['update_details_token'] = $this->originalUpdateDetailsToken; | ||
| } else { | ||
| unset($config->General['update_details_token']); | ||
| } | ||
|
|
||
| $config->forceSave(); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testOneClickResultsDoesNotCreateOrExposeTokenForAnonymousRequest() | ||
| { | ||
| FakeAccess::clearAccess(false); | ||
| unset(Config::getInstance()->General['update_details_token']); | ||
|
|
||
| $_GET = []; | ||
| $_POST = []; | ||
|
|
||
| $result = $this->buildController()->oneClickResults(); | ||
|
|
||
| self::assertStringNotContainsString('updateDetailsToken=', $result); | ||
| self::assertEmpty(Config::getInstance()->General['update_details_token'] ?? null); | ||
| } | ||
|
|
||
| public function testOneClickResultsOnlyExposesStoredTokenForSuperUserContext() | ||
| { | ||
| $token = '1234567890abcdef1234567890abcdef'; | ||
| Config::getInstance()->General['update_details_token'] = $token; | ||
| Config::getInstance()->forceSave(); | ||
|
|
||
| $_GET = []; | ||
| $_POST = []; | ||
|
|
||
| FakeAccess::clearAccess(true); | ||
| $superUserResult = $this->buildController()->oneClickResults(); | ||
| self::assertStringContainsString('updateDetailsToken=' . urlencode($token), $superUserResult); | ||
|
|
||
| FakeAccess::clearAccess(false); | ||
| $anonymousResult = $this->buildController()->oneClickResults(); | ||
| self::assertStringNotContainsString('updateDetailsToken=' . urlencode($token), $anonymousResult); | ||
| } | ||
|
|
||
| public function testOneClickResultsCreatesAndExposesTokenForSuperUserWhenMissing() | ||
| { | ||
| FakeAccess::clearAccess(true); | ||
| unset(Config::getInstance()->General['update_details_token']); | ||
|
|
||
| $_GET = []; | ||
| $_POST = []; | ||
|
|
||
| $result = $this->buildController()->oneClickResults(); | ||
| $createdToken = Config::getInstance()->General['update_details_token'] ?? null; | ||
|
|
||
| self::assertNotEmpty($createdToken); | ||
| self::assertStringContainsString('updateDetailsToken=' . urlencode($createdToken), $result); | ||
| } | ||
|
|
||
| public function testOneClickUpdateRotatesStoredUpdateDetailsTokenAfterSuccessfulUpdate() | ||
| { | ||
| FakeAccess::clearAccess(true); | ||
| Config::getInstance()->General['update_details_token'] = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; | ||
|
|
||
| $_GET = []; | ||
| $_POST = []; | ||
| $_GET['nonce'] = Nonce::getNonce('oneClickUpdate'); | ||
|
|
||
| ob_start(); | ||
| try { | ||
| $this->buildController()->oneClickUpdate(); | ||
| $output = ob_get_clean(); | ||
| } catch (\Throwable $e) { | ||
| ob_end_clean(); | ||
| throw $e; | ||
| } | ||
|
|
||
| $rotatedToken = Config::getInstance()->General['update_details_token'] ?? ''; | ||
|
|
||
| self::assertStringContainsString('action=oneClickResults', $output); | ||
| self::assertNotSame('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', $rotatedToken); | ||
| self::assertSame(1, preg_match('/^[a-f0-9]{32}$/', $rotatedToken)); | ||
| } | ||
|
|
||
| public function provideContainerConfig() | ||
| { | ||
| return array( | ||
| 'Piwik\Access' => new FakeAccess(), | ||
| ); | ||
| } | ||
|
|
||
| private function buildController(): Controller | ||
| { | ||
| $updater = $this->createMock(Updater::class); | ||
| $updater->method('updatePiwik')->willReturn(array()); | ||
|
|
||
| return new Controller($updater); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.