-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-11681: Added filename validation in DownloadController and corresponding tests #756
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
Open
wiewiurdp
wants to merge
2
commits into
4.6
Choose a base branch
from
IBX-11681-Possibility-to-download-all-files-using-a-loop-which-may-cause-DDoS
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
tests/integration/Core/MVC/Symfony/Controller/Content/DownloadControllerRequestFlowTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,196 @@ | ||||||
| <?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; | ||||||
|
|
||||||
| /** @var \Ibexa\Core\IO\IOServiceInterface&\PHPUnit\Framework\MockObject\MockObject */ | ||||||
| private $ioService; | ||||||
|
|
||||||
| /** @var \Ibexa\Core\Helper\TranslationHelper&\PHPUnit\Framework\MockObject\MockObject */ | ||||||
| private $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($this->once()) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All occurrences should be fixed:
Suggested change
|
||||||
| ->method('loadContent') | ||||||
| ->with(42) | ||||||
| ->willReturn($content); | ||||||
| $this->translationHelper | ||||||
| ->expects($this->once()) | ||||||
| ->method('getTranslatedField') | ||||||
| ->with($content, 'file', 'eng-GB') | ||||||
| ->willReturn($field); | ||||||
| $this->ioService | ||||||
| ->expects($this->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()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Newlines for arguments. |
||||||
| } | ||||||
|
|
||||||
| 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
34
tests/integration/Core/MVC/Symfony/InternalRoutingTestKernel.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| ], | ||
| ]); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please adjust all the similar: