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: Skipped files with corrupted filenames when loading and deleting #400

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
64 changes: 64 additions & 0 deletions eZ/Publish/API/Repository/Tests/FieldType/ImageIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace eZ\Publish\API\Repository\Tests\FieldType;

use Doctrine\DBAL\ParameterType;
use DOMDocument;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Field;
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
Expand Down Expand Up @@ -760,6 +762,68 @@ public function testRemovingDraftRemovesOldImage(): void
);
}

public function testDeleteImageWithCorruptedName(): void
{
$content = $this->publishNewImage(
__METHOD__,
new ImageValue(
[
'inputUri' => __DIR__ . '/_fixtures/image.jpg',
'fileName' => 'image.jpg',
'fileSize' => filesize(__DIR__ . '/_fixtures/image.jpg'),
'alternativeText' => 'Alternative',
]
),
[2]
);

$imageFieldDefinition = $content->getContentType()->getFieldDefinition('image');

$connection = $this->getRawDatabaseConnection();
$contentObjectAttributeTable = 'ezcontentobject_attribute';
$corruptedChar = '­';

$query = $connection->createQueryBuilder();
$query
->select('data_text')
->from($contentObjectAttributeTable)
barw4 marked this conversation as resolved.
Show resolved Hide resolved
->andWhere('contentclassattribute_id = :contentclassattribute_id')
->andWhere('version = :version')
->andWhere('contentobject_id = :contentobject_id')
->setParameter('contentclassattribute_id', $imageFieldDefinition->id, ParameterType::INTEGER)
->setParameter('version', $content->getVersionInfo()->versionNo, ParameterType::INTEGER)
->setParameter('contentobject_id', $content->id, ParameterType::INTEGER);
$result = $query->execute();
$row = $result->fetchAssociative();

$document = new DOMDocument('1.0', 'utf-8');
$document->loadXML($row['data_text']);
$elements = $document->getElementsByTagName('ezimage');
$element = $elements->item(0);
$element->setAttribute('filename', $element->getAttribute('filename') . $corruptedChar);
$element->setAttribute('url', $element->getAttribute('url') . $corruptedChar);

$query = $connection->createQueryBuilder();
$query
->update($contentObjectAttributeTable)
->set('data_text', ':data_text')
->setParameter('data_text', $document->saveXML(), ParameterType::STRING)
->andWhere('contentclassattribute_id = :contentclassattribute_id')
->andWhere('version = :version')
->andWhere('contentobject_id = :contentobject_id')
->setParameter('contentclassattribute_id', $imageFieldDefinition->id, ParameterType::INTEGER)
->setParameter('version', $content->getVersionInfo()->versionNo, ParameterType::INTEGER)
->setParameter('contentobject_id', $content->id, ParameterType::INTEGER);
$query->execute();
barw4 marked this conversation as resolved.
Show resolved Hide resolved

$repository = $this->getRepository(false);
$contentService = $repository->getContentService();

$contentService->deleteContent($content->getVersionInfo()->getContentInfo());

// Expect no League\Flysystem\CorruptedPathDetected thrown
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
Expand Down
3 changes: 2 additions & 1 deletion eZ/Publish/Core/IO/IOBinarydataHandler/Flysystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use eZ\Publish\Core\IO\UrlDecorator;
use eZ\Publish\SPI\IO\BinaryFileCreateStruct;
use League\Flysystem\AdapterInterface;
use League\Flysystem\CorruptedPathDetected;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException as FlysystemNotFoundException;
use League\Flysystem\FilesystemInterface;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function delete($spiBinaryFileId)
{
try {
$this->filesystem->delete($spiBinaryFileId);
} catch (FlysystemNotFoundException $e) {
} catch (FlysystemNotFoundException|CorruptedPathDetected $e) {
throw new BinaryFileNotFoundException($spiBinaryFileId, $e);
}
}
Expand Down
9 changes: 7 additions & 2 deletions eZ/Publish/Core/IO/IOMetadataHandler/Flysystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use eZ\Publish\Core\IO\IOMetadataHandler;
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
use League\Flysystem\CorruptedPathDetected;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;

Expand Down Expand Up @@ -47,7 +48,7 @@ public function load($spiBinaryFileId)
{
try {
$info = $this->filesystem->getMetadata($spiBinaryFileId);
} catch (FileNotFoundException $e) {
} catch (FileNotFoundException|CorruptedPathDetected $e) {
throw new BinaryFileNotFoundException($spiBinaryFileId);
}

Expand All @@ -64,7 +65,11 @@ public function load($spiBinaryFileId)

public function exists($spiBinaryFileId)
{
return $this->filesystem->has($spiBinaryFileId);
try {
return $this->filesystem->has($spiBinaryFileId);
} catch (CorruptedPathDetected $e) {
return false;
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}
}

public function getMimeType($spiBinaryFileId)
Expand Down
Loading