Skip to content
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

IBX-7485: Implemented custom Filesystem that handles corrupted filepaths #399

Closed
wants to merge 1 commit into from
Closed
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
79 changes: 79 additions & 0 deletions eZ/Publish/Core/IO/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\IO;

use League\Flysystem\Filesystem as FlysystemFilesystem;
use LogicException;

class Filesystem extends FlysystemFilesystem
{
public function getMetadata($path)
{
$path = $this->normalizeRelativePath($path);
$this->assertPresent($path);

return $this->getAdapter()->getMetadata($path);
}

public function has($path): bool
{
$path = $this->normalizeRelativePath($path);

return !(strlen($path) === 0) && (bool)$this->getAdapter()->has($path);
}

public function getMimetype($path)
{
$path = $this->normalizeRelativePath($path);
$this->assertPresent($path);

if ((!$object = $this->getAdapter()->getMimetype($path)) || !array_key_exists('mimetype', $object)) {
return false;
}

return $object['mimetype'];
}

public function delete($path)
{
$path = $this->normalizeRelativePath($path);
$this->assertPresent($path);

return $this->getAdapter()->delete($path);
}

private function normalizeRelativePath(string $path): string
{
$path = str_replace('\\', '/', $path);
$parts = [];

foreach (explode('/', $path) as $part) {
switch ($part) {
case '':
case '.':
break;

case '..':
if (empty($parts)) {
throw new LogicException(
'Path is outside of the defined root, path: [' . $path . ']'
);
}
array_pop($parts);
break;

default:
$parts[] = $part;
break;
}
}

return implode('/', $parts);
}
}
2 changes: 1 addition & 1 deletion eZ/Publish/Core/settings/io.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
- "@ezpublish.core.io.default_url_decorator"

ezpublish.core.io.flysystem.base_filesystem:
class: League\Flysystem\Filesystem
class: eZ\Publish\Core\IO\Filesystem
abstract: true

ezpublish.core.io.flysystem.default_filesystem:
Expand Down
Loading