Skip to content

Commit 7069601

Browse files
committed
Security issues, bugfix for relative resource reference, documentation
1 parent 7f2b5bb commit 7069601

File tree

29 files changed

+309
-211
lines changed

29 files changed

+309
-211
lines changed

Classes/TechDivision/DocViewer/AccessManager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ class AccessManager extends AbstractModuleController
1818
*/
1919
protected $packagesConfiguration;
2020

21+
/**
22+
* @Flow\Inject
23+
* @var \TYPO3\Flow\Package\PackageManagerInterface
24+
*/
25+
protected $packageManager;
26+
2127
/**
2228
* Determines if given package key should be accessable
2329
*
2430
* @param string $packageKey
2531
* @return bool
2632
*/
2733
public function isPackageAccessable($packageKey) {
28-
return !in_array($packageKey, $this->packagesConfiguration['hide']);
34+
return $this->packageManager->isPackageActive($packageKey) && !in_array($packageKey, $this->packagesConfiguration['hide']);
2935
}
36+
3037
}

Classes/TechDivision/DocViewer/Controller/ModuleController.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use TechDivision\DocViewer\File\Parser;
1010
use TechDivision\DocViewer\File\Tree;
1111

12+
use TechDivision\DocViewer\Util;
1213
use TYPO3\Flow\Annotations as Flow;
1314
use TYPO3\Neos\Controller\Module\AbstractModuleController;
1415

@@ -38,11 +39,23 @@ class ModuleController extends AbstractModuleController
3839
*/
3940
protected $accessManager;
4041

41-
/**
42-
* @return void
43-
*/
44-
public function indexAction()
45-
{
42+
/**
43+
* Routes to list or show action depending on configuration
44+
* @return void
45+
*/
46+
public function indexAction() {
47+
if(isset($this->packagesConfiguration['entryPackage']) && $this->accessManager->isPackageAccessable($this->packagesConfiguration['entryPackage'])) {
48+
$this->forward('show', null, null, array('package' => $this->packagesConfiguration['entryPackage']));
49+
} else {
50+
$this->forward('list');
51+
}
52+
}
53+
54+
/**
55+
* Lists packages with documentation depending on configuration
56+
* @return void
57+
*/
58+
public function listAction() {
4659

4760
$packageGroups = array();
4861

@@ -63,7 +76,7 @@ public function indexAction()
6376
continue;
6477
}
6578

66-
$tree = new Tree($packageGroup, $package->getPackageKey(), $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
79+
$tree = new Tree($package, $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
6780

6881
if(!$tree->isDirectoryWithContent()) {
6982
continue;
@@ -74,37 +87,31 @@ public function indexAction()
7487
'version' => $package->getInstalledVersion(),
7588
'name' => $package->getComposerManifest('name'),
7689
'type' => $package->getComposerManifest('type'),
77-
'description' => $package->getPackageMetaData()->getDescription(),
78-
'metaData' => $package->getPackageMetaData(),
79-
'isActive' => $this->packageManager->isPackageActive($package->getPackageKey()),
80-
'isFrozen' => $this->packageManager->isPackageFrozen($package->getPackageKey()),
81-
'isProtected' => $package->isProtected(),
82-
'hasDoc' => $tree->isDirectoryWithContent()
90+
'description' => $package->getPackageMetaData()->getDescription()
8391
);
8492

8593
}
8694

8795
$this->view->assign('packageGroups', $packageGroups);
88-
8996
}
9097

9198
/**
92-
* @param string $packageKey
93-
* @param string $packageType
99+
* Shows documentation of given package
100+
* @param string $package
94101
* @param string $filePath
102+
* @throws PackageNotAccessableException
103+
* @return void
95104
*/
96-
public function showAction($packageKey, $packageType, $filePath = null) {
97-
105+
public function showAction($package, $filePath = null) {
98106
$baseUri = $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri();
99107

100-
if (!$this->accessManager->isPackageAccessable($packageKey)) {
101-
throw new PackageNotAccessableException("You are not allowed to access the package " . $packageKey);
108+
if (!$this->accessManager->isPackageAccessable($package)) {
109+
throw new PackageNotAccessableException("You are not allowed to access the package " . $package);
102110
}
111+
$package = $this->packageManager->getPackage($package);
112+
$this->view->assign('packageKey', $package->getPackageKey());
103113

104-
$this->view->assign('packageKey', $packageKey);
105-
$this->view->assign('packageType', $packageType);
106-
107-
$tree = new Tree($packageType, $packageKey, $baseUri);
114+
$tree = new Tree($package, $baseUri);
108115

109116
if(!$tree->isDirectoryWithContent()) {
110117
$this->addFlashMessage('No documention could be found');

Classes/TechDivision/DocViewer/Controller/ResourceController.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,28 @@ class ResourceController extends \TYPO3\Flow\Mvc\Controller\ActionController
2525
protected $accessManager;
2626

2727
/**
28-
* @param string $packageType
29-
* @param string $packageKey
28+
* @Flow\Inject
29+
* @var \TYPO3\Flow\Package\PackageManagerInterface
30+
*/
31+
protected $packageManager;
32+
33+
/**
34+
* @param string $package
3035
* @param string $filePath
3136
* @return mixed
3237
*/
33-
public function rawAction($packageType, $packageKey, $filePath) {
38+
public function rawAction($package, $filePath) {
3439

35-
if (!$this->accessManager->isPackageAccessable($packageKey)) {
36-
throw new PackageNotAccessableException("You are not allowed to access the package " . $packageKey);
40+
if (!$this->accessManager->isPackageAccessable($package)) {
41+
throw new PackageNotAccessableException("You are not allowed to access the package " . $package);
3742
}
3843

39-
$docDir = Util::getDocumentPath($packageType, $packageKey);
44+
$docDir = Util::getDocumentPath($this->packageManager->getPackage($package));
4045
$filePath = realpath($docDir . DIRECTORY_SEPARATOR . Parser::urlDecodeFilePath($filePath));
4146

47+
// take care given file path is sub path of the doc dir of the package
4248
if(strpos($filePath, $docDir) === false) {
43-
throw new FileNotInsideDocumentationException("You are not allowed to acces files outside the documentation folder");
49+
throw new FileNotInsideDocumentationException("You are not allowed to access files outside the documentation folder");
4450
}
4551

4652
$contentType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filePath);

Classes/TechDivision/DocViewer/File/Node.php

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ class Node {
1616
*/
1717
protected $name;
1818

19-
/**
20-
* @var string
21-
*/
22-
protected $packageType;
2319

2420
/**
25-
* @var string
21+
* @var \TYPO3\Flow\Package\PackageInterface $package
2622
*/
27-
protected $packageKey;
23+
protected $package;
2824

2925
/**
3026
* @var boolean
@@ -63,21 +59,21 @@ class Node {
6359

6460
/**
6561
* Node constructor.
62+
* @param \TYPO3\Flow\Package\PackageInterface $package
6663
* @param $path
6764
*/
68-
public function __construct($packageType, $packageKey, $path)
65+
public function __construct(\TYPO3\Flow\Package\PackageInterface $package, $path)
6966
{
70-
$this->packageType = $packageType;
71-
$this->packageKey = $packageKey;
72-
$this->path = $path;
67+
$this->package = $package;
68+
$this->path = trim(str_replace(Util::getDocumentPath($package), '', $path), "/");
7369
$this->name = basename($path);
7470
$this->isDir = is_dir($path);
7571
$this->absolutePath = realpath($path);
7672

7773
if(!$this->absolutePath) {
7874
return null;
7975
}
80-
if(strpos($this->absolutePath, Util::getDocumentPath($packageType, $packageKey)) === false) {
76+
if(strpos($this->absolutePath, Util::getDocumentPath($package)) === false) {
8177
throw new FileNotInsideDocumentationException("You are not allowed to acces files outside the documentation folder");
8278
}
8379

@@ -110,14 +106,6 @@ public function getPath()
110106
return $this->path;
111107
}
112108

113-
/**
114-
* @param string $path
115-
*/
116-
public function setPath($path)
117-
{
118-
$this->path = $path;
119-
}
120-
121109
/**
122110
* @return string
123111
*/
@@ -182,36 +170,12 @@ public function setIsParseable($isParseable)
182170
$this->isParseable = $isParseable;
183171
}
184172

185-
/**
186-
* @return string
187-
*/
188-
public function getPackageType()
189-
{
190-
return $this->packageType;
191-
}
192-
193-
/**
194-
* @param string $packageType
195-
*/
196-
public function setPackageType($packageType)
197-
{
198-
$this->packageType = $packageType;
199-
}
200-
201173
/**
202174
* @return string
203175
*/
204176
public function getPackageKey()
205177
{
206-
return $this->packageKey;
207-
}
208-
209-
/**
210-
* @param string $packageKey
211-
*/
212-
public function setPackageKey($packageKey)
213-
{
214-
$this->packageKey = $packageKey;
178+
return $this->package->getPackageKey();
215179
}
216180

217181
}

Classes/TechDivision/DocViewer/File/Parser.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ public static function urlDecodeFilePath($path) {
7070
*/
7171
public static function buildResourceUrl($node, $path = null, $baseUri = '') {
7272
if(!$path) {
73+
// if no path given the node is the resource url itself
7374
$path = $node->getPath();
75+
} else {
76+
// build paths for relative resources
77+
$sourcePathElements = explode("/", $node->getPath());
78+
array_pop($sourcePathElements);
79+
array_push($sourcePathElements, $path);
80+
$path = join("/", $sourcePathElements);
7481
}
75-
return $baseUri . 'techdivision-docviewer/' . $node->getPackageType() . "/" . $node->getPackageKey() . "/" . self::urlEncodeFilePath($path);
82+
83+
return $baseUri . 'techdivision-docviewer/' . $node->getPackageKey() . "/" . self::urlEncodeFilePath($path);
7684
}
7785

7886
/**
@@ -107,7 +115,7 @@ function ($matches) use ($node) {
107115
$href = $matches[1];
108116
if(strpos($href, 'http') !== 0) {
109117
$href = trim($href, "./");
110-
$href = 'show?moduleArguments%5BpackageKey%5D=' . $node->getPackageKey() . '&moduleArguments%5BpackageType%5D=' . $node->getPackageType() . '&moduleArguments%5BfilePath%5D=' . $href;
118+
$href = 'show?moduleArguments%5Bpackage%5D=' . $node->getPackageKey() . '&moduleArguments%5BfilePath%5D=' . $href;
111119
}
112120
return 'href="' . $href . '"';
113121
},

Classes/TechDivision/DocViewer/File/Tree.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ class Tree {
2727
*/
2828
protected $parser;
2929

30-
public function __construct($packageType, $packageKey, $baseUri)
30+
/**
31+
* Tree constructor.
32+
* @param \TYPO3\Flow\Package\PackageInterface $package
33+
* @param $baseUri
34+
*/
35+
public function __construct(\TYPO3\Flow\Package\PackageInterface $package, $baseUri)
3136
{
3237
$this->parser = new Parser($baseUri);
33-
$this->rootNode = $this->buildFsNode($packageType, $packageKey);
38+
$this->rootNode = $this->buildFsNode($package);
3439
}
3540

3641
/**
@@ -95,23 +100,21 @@ public function isDirectoryWithContent() {
95100

96101
/**
97102
* Builds up given folder path as composite
98-
* @param string $packageType
99-
* @param string $packageKey
103+
* @param \TYPO3\Flow\Package\PackageInterface $package
100104
* @param string $path
101105
* @return null|Node
102106
*/
103-
protected function buildFsNode($packageType, $packageKey, $path = null) {
107+
protected function buildFsNode(\TYPO3\Flow\Package\PackageInterface $package, $path = null) {
104108

105109
if(!$path) {
106-
$path = Util::getDocumentPath($packageType, $packageKey);
110+
$path = Util::getDocumentPath($package);
107111
}
108112

109113
if(!file_exists($path)) {
110114
return null;
111115
}
112116

113-
$node = new Node($packageType, $packageKey, $path);
114-
$node->setPath(trim(str_replace(Util::getDocumentPath($packageType, $packageKey), '', $path), "/"));
117+
$node = new Node($package, $path);
115118
if($node->isIsDir()) {
116119

117120
$content = array();
@@ -121,7 +124,7 @@ protected function buildFsNode($packageType, $packageKey, $path = null) {
121124
if($element == '.' || $element == '..') {
122125
continue;
123126
}
124-
$content[] = $this->buildFsNode($packageType, $packageKey, $path . DIRECTORY_SEPARATOR . $element);
127+
$content[] = $this->buildFsNode($package, $path . DIRECTORY_SEPARATOR . $element);
125128
}
126129
$node->setContent($content);
127130
} else {

Classes/TechDivision/DocViewer/Util.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
class Util {
1010

1111

12+
/**
13+
* Directory of the documentation
14+
* @var string
15+
*/
16+
protected static $docDir = 'Documentation';
17+
1218
/**
1319
* Get the documentation path
1420
*
15-
* @param string $packageType
16-
* @param string $packageKey
21+
* @param \TYPO3\Flow\Package\PackageInterface $package
1722
* @return string
1823
*/
19-
public static function getDocumentPath($packageType, $packageKey) {
20-
$path = FLOW_PATH_PACKAGES . $packageType . DIRECTORY_SEPARATOR . $packageKey . '/Documentation';
24+
public static function getDocumentPath($package) {
25+
$path = $package->getPackagePath() . self::$docDir;
2126
if(!file_exists($path)) {
2227
return null;
2328
}

Classes/TechDivision/DocViewer/ViewHelpers/ResourceUrlViewHelper.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
88

99
/**
10-
* Renders a resource url by given packageType, packageKey and filePath
10+
* Renders a resource url by given packageKey and filePath
1111
*/
1212
class ResourceUrlViewHelper extends AbstractViewHelper
1313
{
1414
/**
15-
* @param $packageType
16-
* @param $packageKey
17-
* @param $filePath
15+
* @Flow\Inject
16+
* @var \TYPO3\Flow\Package\PackageManagerInterface
17+
*/
18+
protected $packageManager;
19+
20+
/**
21+
* @param string $package
22+
* @param string $filePath
1823
* @return string
1924
*/
20-
public function render($packageType, $packageKey, $filePath)
25+
public function render($package, $filePath)
2126
{
22-
return Parser::buildResourceUrl(new Node($packageType, $packageKey, $filePath), null, $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
27+
return Parser::buildResourceUrl(new Node($this->packageManager->getPackage($package), $filePath), null, $this->controllerContext->getRequest()->getHttpRequest()->getBaseUri());
2328
}
2429
}

Configuration/Routes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
-
33
name: 'Fileprovider'
4-
uriPattern: 'techdivision-docviewer/{packageType}/{packageKey}/{filePath}'
4+
uriPattern: 'techdivision-docviewer/{package}/{filePath}'
55
defaults:
66
'@package': 'TechDivision.DocViewer'
77
'@controller': 'Resource'

0 commit comments

Comments
 (0)