Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Fix/latest versions security #88

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions Classes/helpers/class.tx_caretaker_LatestVersionsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class tx_caretaker_LatestVersionsHelper
protected static $releaseJsonFeed = 'https://get.typo3.org/json';

/**
* @throws Exception
* @return bool
*/
public static function updateLatestTypo3VersionRegistry()
Expand All @@ -60,24 +61,18 @@ public static function updateLatestTypo3VersionRegistry()

if (!is_array($releases)) {
throw new Exception(
'It seems like ' . self::$releaseJsonFeed .
' did not return the json string for the TYPO3 releases. Maybe it has been moved!?'
'It seems like ' . self::$releaseJsonFeed .
' did not return the json string for the TYPO3 releases. Maybe it has been moved!?'
);
}

$max = array();
$stable = array();
foreach ($releases as $major => $details) {
if (is_array($details) && !empty($details['latest'])) {
$max[$major] = $details['latest'];
}
$latestVersions = self::getLatestFromReleases($releases);

if (is_array($details) && !empty($details['stable'])) {
$stable[$major] = $details['stable'];
}
}
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Registry')->set('tx_caretaker', 'TYPO3versions', $max);
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Registry')->set('tx_caretaker', 'TYPO3versionsStable', $stable);
/** @var \TYPO3\CMS\Core\Registry $registry */
$registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Registry');
$registry->set('tx_caretaker', 'TYPO3versions', $latestVersions['max']);
$registry->set('tx_caretaker', 'TYPO3versionsStable', $latestVersions['stable']);
$registry->set('tx_caretaker', 'TYPO3versionsSecurity', $latestVersions['security']);

return true;
}
Expand Down Expand Up @@ -113,4 +108,38 @@ protected static function curlRequest($requestUrl = false)

return $response;
}

/**
* @param array $releases
* @return array
*/
public static function getLatestFromReleases($releases)
{
$max = array();
$stable = array();
$security = array();
foreach ($releases as $major => $details) {
if (is_array($details) && !empty($details['latest'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!empty($details['latest'])) is sufficient here

$max[$major] = $details['latest'];
}

if (is_array($details) && !empty($details['stable'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!empty($details['stable'])) is sufficient here

$stable[$major] = $details['stable'];
}
if (is_array($details) && is_array($details['releases'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!empty($details['releases'])) is sufficient here

foreach ($details['releases'] as $version => $versionDetails) {
$security[$major] = $version;
if ($versionDetails['type'] === 'security') {
break;
}
}
}
}

return array(
'max' => $max,
'stable' => $stable,
'security' => $security,
);
}
}
88 changes: 88 additions & 0 deletions Tests/Unit/Fixtures/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"3": {
"releases": {
"3.0.2": {
"version": "3.0.2",
"type": "regular"
},
"3.0.1": {
"version": "3.0.1",
"type": "regular"
},
"3.0.0": {
"version": "3.0.0",
"type": "release"
}
},
"latest": "3.0.2",
"stable": "3.0.1",
"active": true
},
"2": {
"releases": {
"2.1.2": {
"version": "2.1.2",
"type": "regular"
},
"2.1.1": {
"version": "2.1.1",
"type": "security"
},
"2.1.0": {
"version": "2.1.0",
"type": "release"
},
"2.0.2": {
"version": "2.0.2",
"type": "regular"
},
"2.0.1": {
"version": "2.0.1",
"type": "security"
},
"2.0.0": {
"version": "2.0.0",
"type": "release"
}
},
"latest": "2.1.2",
"stable": "2.1.2",
"active": true
},
"1": {
"releases": {
"1.1.2": {
"version": "1.1.2",
"type": "regular"
},
"1.1.1": {
"version": "1.1.1",
"type": "security"
},
"1.1.0": {
"version": "1.1.0",
"type": "release"
},
"1.0.2": {
"version": "1.0.2",
"type": "regular"
},
"1.0.1": {
"version": "1.0.1",
"type": "security"
},
"1.0.0": {
"version": "1.0.0",
"type": "release"
}
},
"latest": "1.1.2",
"stable": "1.1.2",
"active": true
},

"latest_stable": "2.1.2",
"latest_old_stable": "1.1.2",
"latest_lts": "2.1.2",
"latest_old_lts": "1.1.2"
}
68 changes: 68 additions & 0 deletions Tests/Unit/LatestVersionsHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace Caretaker\Caretaker\Tests\Unit;

use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/***************************************************************
* Copyright notice
*
* (c) 2009-2011 by n@work GmbH and networkteam GmbH
*
* All rights reserved
*
* This script is part of the Caretaker project. The Caretaker project
* is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
* This is a file of the caretaker project.
* http://forge.typo3.org/projects/show/extension-caretaker
*
* Project sponsored by:
* n@work GmbH - http://www.work.de
* networkteam GmbH - http://www.networkteam.com/
*/
class LatestVersionsHelperTest extends UnitTestCase
{
/**
* @var array
*/
protected $releases;

public function setup()
{
$json = file_get_contents(GeneralUtility::getFileAbsFileName('EXT:caretaker/Tests/Unit/Fixtures/versions.json'));
$this->releases = json_decode($json, true);
}

public function testLatest()
{
$latest = \tx_caretaker_LatestVersionsHelper::getLatestFromReleases($this->releases);

$this->assertEquals('3.0.2', $latest['max']['3'], 'v3: latest release');
$this->assertEquals('3.0.1', $latest['stable']['3'], 'v3: stable release');
$this->assertEquals('3.0.0', $latest['security']['3'], 'v3: no security release falls back to the first release');

$this->assertEquals('2.1.2', $latest['max']['2'], 'v2: latest release');
$this->assertEquals('2.1.2', $latest['stable']['2'], 'v2: stable release');
$this->assertEquals('2.1.1', $latest['security']['2'], 'v2: security release');

$this->assertEquals('1.1.2', $latest['max']['1'], 'v1: latest release');
$this->assertEquals('1.1.2', $latest['stable']['1'], 'v1: stable release');
$this->assertEquals('1.1.1', $latest['security']['1'], 'v1: security release');
}
}