Skip to content
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
6 changes: 0 additions & 6 deletions phpstan-baseline-7.4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,6 @@ parameters:
count: 1
path: src/lib/MVC/Symfony/Matcher/ContentBased/UrlAlias.php

-
message: '#^Method Ibexa\\Core\\Persistence\\Legacy\\Content\\FieldValue\\Converter\\DateAndTimeConverter\:\:getDateIntervalFromXML\(\) should return DateInterval but returns DateInterval\|false\.$#'
identifier: return.type
count: 1
path: src/lib/Persistence/Legacy/Content/FieldValue/Converter/DateAndTimeConverter.php

-
message: '#^Cannot access property \$ownerDocument on DOMElement\|false\.$#'
identifier: property.nonObject
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline-lte-8.1.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
parameters:
ignoreErrors:
-
message: '#^Method Ibexa\\Core\\Persistence\\Legacy\\Content\\FieldValue\\Converter\\DateAndTimeConverter\:\:getDateIntervalFromXML\(\) should return DateInterval but returns DateInterval\|false\.$#'
identifier: return.type
count: 1
path: src/lib/Persistence/Legacy/Content/FieldValue/Converter/DateAndTimeConverter.php

-
message: "#^Class SensitiveParameterValue not found\\.$#"
count: 1
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -69666,6 +69666,12 @@ parameters:
count: 1
path: tests/lib/Repository/Service/Mock/ContentTest.php

-
message: '#^Call to new Ibexa\\Core\\Repository\\ContentService\(\) on a separate line has no effect\.$#'
identifier: new.resultUnused
count: 1
path: tests/lib/Repository/Service/Mock/ContentTest.php

-
message: '#^Method Ibexa\\Tests\\Core\\Repository\\Service\\Mock\\ContentTest\:\:assertForCreateContentContentValidationException\(\) has no return type specified\.$#'
identifier: missingType.return
Expand Down
52 changes: 32 additions & 20 deletions src/lib/MVC/Symfony/Controller/Content/DownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

use Ibexa\Bundle\IO\BinaryStreamResponse;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as RepositoryNotFoundException;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\Helper\TranslationHelper;
use Ibexa\Core\IO\IOServiceInterface;
use Ibexa\Core\MVC\Symfony\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

class DownloadController extends Controller
{
Expand Down Expand Up @@ -48,15 +50,15 @@ public function downloadBinaryFileByIdAction(Request $request, int $contentId, i
$versionNo = $request->query->has('version') ? $request->query->getInt('version') : null;
$language = $request->query->has('inLanguage') ? $request->query->get('inLanguage') : null;

$content = $this->contentService->loadContent(
$contentId,
$language !== null ? [$language] : null,
$versionNo,
);
try {
$content = $this->contentService->loadContent(
$contentId,
$language !== null ? [$language] : null,
$versionNo,
);
$field = $this->findFieldInContent($fieldId, $content);
} catch (InvalidArgumentException $e) {
throw new NotFoundException('File', $fieldId);
} catch (RepositoryNotFoundException | InvalidArgumentException $e) {
throw $this->createFileNotFoundException($e);
}

return $this->downloadBinaryFileAction($contentId, $field->fieldDefIdentifier, $field->value->fileName, $request);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we also need to block or harden the /content/download/{contentId}/{fieldId} route? It seems to bypass the new filename validation because downloadBinaryFileByIdAction() forwards the persisted file name to downloadBinaryFileAction().

@wiewiurdp wiewiurdp Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. Blocking/removing it in this PR would likely be BC.

For this PR, I would keep the route available. It is already harden by normalizing its validation/not-found responses in the same way as the filename-based route, so it does not reveal whether a guessed content/field combination exists.

I agree that deprecating it and eventually moving fully to the filename-based route would be worth considering, but I think that should be handled in a separate task. Wdyt @konradoboza

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Expand Down Expand Up @@ -90,18 +92,22 @@ protected function findFieldInContent(int $fieldId, Content $content): Field
*/
public function downloadBinaryFileAction(int $contentId, string $fieldIdentifier, string $filename, Request $request): BinaryStreamResponse
{
if ($request->query->has('version')) {
$version = (int) $request->query->get('version');
if ($version <= 0) {
throw new NotFoundException('File', $filename);
try {
if ($request->query->has('version')) {
$version = (int) $request->query->get('version');
if ($version <= 0) {
throw $this->createFileNotFoundException();
}
$content = $this->contentService->loadContent($contentId, null, $version);
} else {
$content = $this->contentService->loadContent($contentId);
}
$content = $this->contentService->loadContent($contentId, null, $version);
} else {
$content = $this->contentService->loadContent($contentId);
} catch (RepositoryNotFoundException $e) {
throw $this->createFileNotFoundException($e);
}

if ($content->contentInfo->isTrashed()) {
throw new NotFoundException('File', $filename);
throw $this->createFileNotFoundException();
}

$field = $this->translationHelper->getTranslatedField(
Expand All @@ -110,10 +116,11 @@ public function downloadBinaryFileAction(int $contentId, string $fieldIdentifier
$request->query->has('inLanguage') ? $request->query->get('inLanguage') : null
);
if (!$field instanceof Field) {
throw new InvalidArgumentException(
'$fieldIdentifier',
"'{$fieldIdentifier}' field not present on content #{$content->contentInfo->id} '{$content->contentInfo->name}'"
);
throw $this->createFileNotFoundException();
}

if ($field->value->fileName !== $filename) {
throw $this->createFileNotFoundException();
}

$response = new BinaryStreamResponse($this->ioService->loadBinaryFile($field->value->id), $this->ioService);
Expand All @@ -125,6 +132,11 @@ public function downloadBinaryFileAction(int $contentId, string $fieldIdentifier

return $response;
}

private function createFileNotFoundException(?Throwable $previous = null): NotFoundHttpException
{
return new NotFoundHttpException('File not found', $previous);
}
}

class_alias(DownloadController::class, 'eZ\Publish\Core\MVC\Symfony\Controller\Content\DownloadController');
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?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 Ibexa\Tests\Integration\Core\MVC\Symfony\Controller\Content;

use DateTime;
use Ibexa\Bundle\IO\BinaryStreamResponse;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Test\IbexaKernelTestCase;
use Ibexa\Core\FieldType\BinaryFile\Value as BinaryFileValue;
use Ibexa\Core\Helper\TranslationHelper;
use Ibexa\Core\IO\IOServiceInterface;
use Ibexa\Core\IO\Values\BinaryFile;
use Ibexa\Core\MVC\Symfony\Controller\Content\DownloadController;
use Ibexa\Core\Repository\Values\Content\Content;
use Ibexa\Core\Repository\Values\Content\VersionInfo;
use Ibexa\Tests\Integration\Core\MVC\Symfony\InternalRoutingTestKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
* @group integration
*
* @covers \Ibexa\Core\MVC\Symfony\Controller\Content\DownloadController
*/
final class DownloadControllerRequestFlowTest extends IbexaKernelTestCase
{
private const FILENAME = 'Q1 report #1 + 100%.jpg';

/** @var \Ibexa\Contracts\Core\Repository\ContentService&\PHPUnit\Framework\MockObject\MockObject */
private ContentService $contentService;

/** @var \Ibexa\Core\IO\IOServiceInterface&\PHPUnit\Framework\MockObject\MockObject */
private IOServiceInterface $ioService;

/** @var \Ibexa\Core\Helper\TranslationHelper&\PHPUnit\Framework\MockObject\MockObject */
private TranslationHelper $translationHelper;

protected static function getKernelClass(): string
{
return InternalRoutingTestKernel::class;
}

protected function setUp(): void
{
parent::setUp();

self::bootKernel();

$this->contentService = $this->createMock(ContentService::class);
$this->ioService = $this->createMock(IOServiceInterface::class);
$this->translationHelper = $this->createMock(TranslationHelper::class);
}

public function testDownloadsFileWithUrlEncodedFilename(): void
{
$routes = $this->createRouteCollection();
$context = new RequestContext();
$url = (new UrlGenerator($routes, $context))->generate(
'ibexa.content.download',
[
'contentId' => 42,
'fieldIdentifier' => 'file',
'filename' => self::FILENAME,
'inLanguage' => 'eng-GB',
]
);

self::assertStringContainsString('Q1%20report%20%231%20+%20100%25.jpg', $url);

$content = $this->createContent();
$field = $content->getField('file', 'eng-GB');
self::assertInstanceOf(Field::class, $field);

$binaryFile = new BinaryFile([
'id' => 'binary-file-id',
'mtime' => new DateTime(),
'size' => 123,
'uri' => 'binary-file-uri',
]);

$this->contentService
->expects(self::once())
->method('loadContent')
->with(42)
->willReturn($content);
$this->translationHelper
->expects(self::once())
->method('getTranslatedField')
->with($content, 'file', 'eng-GB')
->willReturn($field);
$this->ioService
->expects(self::once())
->method('loadBinaryFile')
->with('binary-file-id')
->willReturn($binaryFile);

$this->configureDownloadController($routes);

$response = $this->createHttpKernel($routes, $context)->handle(
Request::create($url),
HttpKernelInterface::MAIN_REQUEST,
false
);

self::assertSame(Response::HTTP_OK, $response->getStatusCode());
self::assertInstanceOf(BinaryStreamResponse::class, $response);
}

private function configureDownloadController(RouteCollection $routes): void
{
$route = $routes->get('ibexa.content.download');
self::assertNotNull($route);
$route->setDefault('_controller', [$this->createController(), 'downloadBinaryFileAction']);
}

private function createController(): DownloadController
{
return new DownloadController(
$this->contentService,
$this->ioService,
$this->translationHelper
);
}

private function createHttpKernel(RouteCollection $routes, RequestContext $context): HttpKernel
{
$requestStack = new RequestStack();
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new RouterListener(
new UrlMatcher($routes, $context),
$requestStack,
$context
));

$controllerResolver = self::getContainer()->get('controller_resolver');
self::assertInstanceOf(ControllerResolverInterface::class, $controllerResolver);

return new HttpKernel(
$dispatcher,
$controllerResolver,
$requestStack,
new ArgumentResolver()
);
}

private function createRouteCollection(): RouteCollection
{
$routes = new RouteCollection();
$routes->add('ibexa.content.download', new Route(
'/content/download/{contentId}/{fieldIdentifier}/{filename}',
[],
['contentId' => '\d+']
));

return $routes;
}

private function createContent(): Content
{
return new Content([
'internalFields' => [
new Field([
'fieldDefIdentifier' => 'file',
'languageCode' => 'eng-GB',
'value' => new BinaryFileValue([
'id' => 'binary-file-id',
'fileName' => self::FILENAME,
]),
]),
],
'versionInfo' => new VersionInfo([
'contentInfo' => new ContentInfo([
'id' => 42,
'mainLanguageCode' => 'eng-GB',
'name' => 'Test content',
'status' => ContentInfo::STATUS_PUBLISHED,
]),
]),
]);
}
}
34 changes: 34 additions & 0 deletions tests/integration/Core/MVC/Symfony/InternalRoutingTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 Ibexa\Tests\Integration\Core\MVC\Symfony;

use Ibexa\Contracts\Core\Test\IbexaTestKernel;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class InternalRoutingTestKernel extends IbexaTestKernel
{
public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

$loader->load(static function (ContainerBuilder $container): void {
self::loadRouting($container);
});
}

private static function loadRouting(ContainerBuilder $container): void
{
$container->loadFromExtension('framework', [
'router' => [
'resource' => '@IbexaCoreBundle/Resources/config/routing/internal.yml',
],
]);
}
}
Loading
Loading