Skip to content

Commit e0ab8ec

Browse files
authored
Error 401 When Accessing Parent Folder with "+" in Name (#138)
* test * fix parent * fix parent * fix parent
1 parent 28c2755 commit e0ab8ec

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed

Controller/ManagerController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function deleteAction(Request $request): RedirectResponse {
327327
}
328328

329329
$this->dispatch(FileManagerEvents::POST_DELETE_FOLDER);
330-
$queryParameters['route'] = \dirname($fileManager->getCurrentRoute());
330+
$queryParameters['route'] = \dirname($fileManager->getRoute());
331331
if ($queryParameters['route'] == '/') {
332332
unset($queryParameters['route']);
333333
}
@@ -409,7 +409,7 @@ private function retrieveSubDirectories(FileManager $fileManager, string $path,
409409
'href' => $fileName ? $this->generateUrl('file_manager', $queryParameters) : $this->generateUrl('file_manager', $queryParametersRoute),
410410
],
411411
'state' => [
412-
'selected' => $fileManager->getCurrentRoute() === $fileName,
412+
'selected' => $fileManager->getRoute() === $fileName,
413413
'opened' => true,
414414
],
415415
];

Helpers/FileManager.php

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,34 @@
1313
/**
1414
* @author Arthur Gribet <a.gribet@gmail.com>
1515
*/
16-
class FileManager {
16+
class FileManager
17+
{
1718
const VIEW_THUMBNAIL = 'thumbnail';
1819
const VIEW_LIST = 'list';
1920

2021
/**
2122
* FileManager constructor.
2223
*/
23-
public function __construct(private array $queryParameters, private array $configuration, private RouterInterface $router, private EventDispatcherInterface $dispatcher, private string $webDir) {
24+
public function __construct(private array $queryParameters, private array $configuration, private RouterInterface $router, private EventDispatcherInterface $dispatcher, private string $webDir)
25+
{
2426
// Check Security
2527
$this->checkSecurity();
2628
}
2729

28-
public function getDirName(): string {
30+
public function getDirName(): string
31+
{
2932
return \dirname($this->getBasePath());
3033
}
3134

32-
public function getBaseName(): string {
35+
public function getBaseName(): string
36+
{
3337
return basename($this->getBasePath());
3438
}
3539

36-
public function getRegex(): string {
40+
public function getRegex(): string
41+
{
3742
if (isset($this->configuration['regex'])) {
38-
return '/'.$this->configuration['regex'].'/i';
43+
return '/' . $this->configuration['regex'] . '/i';
3944
}
4045

4146
return match ($this->getType()) {
@@ -45,28 +50,29 @@ public function getRegex(): string {
4550
};
4651
}
4752

48-
public function getCurrentRoute(): ?string {
49-
if ($this->getRoute()) {
50-
return urldecode($this->getRoute());
51-
}
52-
53-
return null;
54-
}
53+
// public function getCurrentRoute(): ?string {
54+
// if ($this->getRoute()) {
55+
// return urldecode($this->getRoute());
56+
// }
57+
//
58+
// return null;
59+
// }
5560

56-
public function getCurrentPath(): bool|string {
57-
return realpath($this->getBasePath().$this->getCurrentRoute());
61+
public function getCurrentPath(): bool|string
62+
{
63+
return realpath($this->getBasePath() . $this->getRoute());
5864
}
5965

6066
// parent url
61-
public function getParent(): ?string {
67+
public function getParent(): ?string
68+
{
6269
$queryParentParameters = $this->queryParameters;
6370

64-
if ($this->getCurrentRoute()) {
65-
66-
$parentRoute = \dirname($this->getCurrentRoute());
71+
if ($this->getRoute()) {
6772

73+
$parentRoute = \dirname($this->getRoute());
6874
if (\DIRECTORY_SEPARATOR !== $parentRoute) {
69-
$queryParentParameters['route'] = \dirname($this->getCurrentRoute());
75+
$queryParentParameters['route'] = \dirname($this->getRoute());
7076
} else {
7177
unset($queryParentParameters['route']);
7278
}
@@ -79,28 +85,33 @@ public function getParent(): ?string {
7985
return null;
8086
}
8187

82-
public function getImagePath(): bool|string {
88+
public function getImagePath(): bool|string
89+
{
8390
$baseUrl = $this->getBaseUrl();
91+
8492
if ($baseUrl) {
85-
return $baseUrl.$this->getCurrentRoute().'/';
93+
$routePath = $this->getRoutePath();
94+
return $baseUrl . $routePath . '/';
8695
}
8796

8897
return false;
8998
}
9099

91-
private function getBaseUrl(): bool|string {
100+
private function getBaseUrl(): bool|string
101+
{
92102
$webPath = $this->webDir;
93103
$dirl = new \SplFileInfo($this->getConfiguration()['dir']);
94104
$base = $dirl->getPathname();
95105

96-
if (0 === mb_strpos($base, $webPath)) {
106+
if (str_starts_with($base, $webPath)) {
97107
return mb_substr($base, mb_strlen($webPath));
98108
}
99109

100110
return false;
101111
}
102112

103-
private function checkSecurity(): void {
113+
private function checkSecurity(): void
114+
{
104115
if (!isset($this->configuration['dir'])) {
105116
throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Please define a "dir" parameter in your config.yml');
106117
}
@@ -115,67 +126,86 @@ private function checkSecurity(): void {
115126
$currentPath = $this->getCurrentPath();
116127

117128
// check Path security
118-
if (false === $currentPath || 0 !== mb_strpos($currentPath, $this->getBasePath())) {
129+
if (false === $currentPath || !str_starts_with($currentPath, $this->getBasePath())) {
119130
throw new HttpException(Response::HTTP_UNAUTHORIZED, 'You are not allowed to access this folder.');
120131
}
121132
$event = new GenericEvent($this, ['path' => $currentPath]);
122133
$this->dispatcher->dispatch($event, FileManagerEvents::POST_CHECK_SECURITY);
123134

124135
}
125136

126-
public function getModule(): ?string {
137+
public function getModule(): ?string
138+
{
127139
return $this->getQueryParameters()['module'] ?? null;
128140
}
129141

130-
public function getType(): ?string {
142+
public function getType(): ?string
143+
{
131144
return $this->mergeConfAndQuery('type');
132145
}
133146

134-
public function getRoute(): ?string {
147+
public function getRoute(): ?string
148+
{
135149
return isset($this->getQueryParameters()['route']) && '/' !== $this->getQueryParameters()['route'] ? $this->getQueryParameters()['route'] : null;
136150
}
137151

138-
public function getBasePath(): bool|string {
152+
public function getRoutePath(): ?string
153+
{
154+
return implode('/', array_map('rawurlencode', explode('/', $this->getRoute())));
155+
}
156+
157+
public function getBasePath(): bool|string
158+
{
139159
return realpath($this->getConfiguration()['dir']);
140160
}
141161

142-
public function getQueryParameters(): array {
162+
public function getQueryParameters(): array
163+
{
143164
return $this->queryParameters;
144165
}
145166

146-
public function getRouter(): RouterInterface {
167+
public function getRouter(): RouterInterface
168+
{
147169
return $this->router;
148170
}
149171

150-
public function setRouter(RouterInterface $router): void {
172+
public function setRouter(RouterInterface $router): void
173+
{
151174
$this->router = $router;
152175
}
153176

154-
public function getConfiguration(): array {
177+
public function getConfiguration(): array
178+
{
155179
return $this->configuration;
156180
}
157181

158-
public function setConfiguration(array $configuration): void {
182+
public function setConfiguration(array $configuration): void
183+
{
159184
$this->configuration = $configuration;
160185
}
161186

162-
public function getTree(): bool {
187+
public function getTree(): bool
188+
{
163189
return $this->mergeQueryAndConf('tree', true);
164190
}
165191

166-
public function getView(): string {
192+
public function getView(): string
193+
{
167194
return $this->mergeQueryAndConf('view', 'list');
168195
}
169196

170-
public function getQueryParameter(string $parameter) {
197+
public function getQueryParameter(string $parameter)
198+
{
171199
return $this->getQueryParameters()[$parameter] ?? null;
172200
}
173201

174-
public function getConfigurationParameter(string $parameter) {
202+
public function getConfigurationParameter(string $parameter)
203+
{
175204
return $this->getConfiguration()[$parameter] ?? null;
176205
}
177206

178-
private function mergeQueryAndConf(string $parameter, $default = null) {
207+
private function mergeQueryAndConf(string $parameter, $default = null)
208+
{
179209
if (null !== $this->getQueryParameter($parameter)) {
180210
return $this->getQueryParameter($parameter);
181211
}
@@ -186,7 +216,8 @@ private function mergeQueryAndConf(string $parameter, $default = null) {
186216
return $default;
187217
}
188218

189-
private function mergeConfAndQuery(string $parameter, $default = null) {
219+
private function mergeConfAndQuery(string $parameter, $default = null)
220+
{
190221
if (null !== $this->getConfigurationParameter($parameter)) {
191222
return $this->getConfigurationParameter($parameter);
192223
}

Service/FileTypeService.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public function __construct(private RouterInterface $router, private Environment
2020
}
2121

2222
public function preview(FileManager $fileManager, SplFileInfo $file) {
23+
2324
if ($fileManager->getImagePath()) {
24-
$filePath = $fileManager->getImagePath().$file->getFilename();
25+
$filePath = $fileManager->getImagePath().rawurlencode($file->getFilename());
2526
} else {
2627
$filePath = $this->router->generate(
2728
'file_manager_file',
28-
array_merge($fileManager->getQueryParameters(), ['fileName' => rawurlencode($file->getFilename())])
29+
array_merge($fileManager->getQueryParameters(), ['fileName' => $file->getFilename()])
2930
);
3031
}
3132
$extension = $file->getExtension();
@@ -36,10 +37,11 @@ public function preview(FileManager $fileManager, SplFileInfo $file) {
3637
return $this->fileIcon($filePath, $extension, $size, true, $fileManager->getConfigurationParameter('twig_extension'), $fileManager->getConfigurationParameter('cachebreaker'));
3738
}
3839
if ('dir' === $type) {
40+
3941
$href = $this->router->generate(
4042
'file_manager', array_merge(
4143
$fileManager->getQueryParameters(),
42-
['route' => $fileManager->getRoute().'/'.rawurlencode($file->getFilename())]
44+
['route' => $fileManager->getRoute().'/'.$file->getFilename()]
4345
)
4446
);
4547

@@ -67,6 +69,7 @@ public function accept($type): bool|string {
6769
}
6870

6971
public function fileIcon(string $filePath,?string $extension = null, ?int $size = 75, ?bool $lazy = false, ?string $twigExtension = null, ?bool $cachebreaker = null): array {
72+
7073
$imageTemplate = null;
7174

7275
if (null === $extension) {

0 commit comments

Comments
 (0)