Skip to content

Add a check for the presence of the parameter for the bin/magento downloadable:domains commands #38409

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
wants to merge 12 commits into
base: 2.4-develop
Choose a base branch
from
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
48 changes: 27 additions & 21 deletions app/code/Magento/Downloadable/Console/Command/DomainsAddCommand.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2019 Adobe
* All Rights Reserved.
*/
namespace Magento\Downloadable\Console\Command;

use Exception;
use InvalidArgumentException;
use Magento\Downloadable\Api\DomainManagerInterface as DomainManager;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Downloadable\Helper\Download as DownloadManager;

/**
* Class DomainsAddCommand
Expand All @@ -31,14 +32,22 @@ class DomainsAddCommand extends Command
*/
private $domainManager;

/**
* @var DownloadManager
*/
private $downloadManager;

/**
* DomainsAddCommand constructor.
* @param DomainManager $domainManager
* @param DownloadManager $downloadManager
*/
public function __construct(
DomainManager $domainManager
DomainManager $domainManager,
DownloadManager $downloadManager
) {
$this->domainManager = $domainManager;
$this->downloadManager = $downloadManager;
parent::__construct();
}

Expand Down Expand Up @@ -69,27 +78,24 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
if ($input->getArgument(self::INPUT_KEY_DOMAINS)) {
$whitelistBefore = $this->domainManager->getDomains();
$newDomains = $input->getArgument(self::INPUT_KEY_DOMAINS);
$newDomains = array_filter(array_map('trim', $newDomains), 'strlen');
$domains = $input->getArgument(self::INPUT_KEY_DOMAINS);

$this->downloadManager->validateDomains($domains);

$this->domainManager->addDomains($newDomains);
$whitelistBefore = $this->domainManager->getDomains();
$newDomains = array_filter(array_map('trim', $domains), 'strlen');

foreach (array_diff($this->domainManager->getDomains(), $whitelistBefore) as $newHost) {
$output->writeln(
$newHost . ' was added to the whitelist.' . PHP_EOL
);
}
$this->domainManager->addDomains($newDomains);

foreach (array_diff($this->domainManager->getDomains(), $whitelistBefore) as $newHost) {
$output->writeln($newHost . ' was added to the whitelist.' . PHP_EOL);
}

return Cli::RETURN_SUCCESS;
} catch (InvalidArgumentException $e) {
return $this->downloadManager->handleInvalidArgumentException($e, $output);
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}
return Cli::RETURN_FAILURE;
return $this->downloadManager->handleException($e, $output);
}

return Cli::RETURN_SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2019 Adobe
* All Rights Reserved.
*/
namespace Magento\Downloadable\Console\Command;

use Exception;
use InvalidArgumentException;
use Magento\Downloadable\Api\DomainManagerInterface as DomainManager;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Downloadable\Helper\Download as DownloadManager;

/**
* Class DomainsRemoveCommand
Expand All @@ -31,15 +32,23 @@ class DomainsRemoveCommand extends Command
*/
private $domainManager;

/**
* @var DownloadManager
*/
private $downloadManager;

/**
* DomainsRemoveCommand constructor.
*
* @param DomainManager $domainManager
* @param DownloadManager $downloadManager
*/
public function __construct(
DomainManager $domainManager
DomainManager $domainManager,
DownloadManager $downloadManager
) {
$this->domainManager = $domainManager;
$this->downloadManager = $downloadManager;
parent::__construct();
}

Expand Down Expand Up @@ -70,26 +79,24 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
if ($input->getArgument(self::INPUT_KEY_DOMAINS)) {
$whitelistBefore = $this->domainManager->getDomains();
$removeDomains = $input->getArgument(self::INPUT_KEY_DOMAINS);
$removeDomains = array_filter(array_map('trim', $removeDomains), 'strlen');
$this->domainManager->removeDomains($removeDomains);
$domains = $input->getArgument(self::INPUT_KEY_DOMAINS);

$this->downloadManager->validateDomains($domains);

foreach (array_diff($whitelistBefore, $this->domainManager->getDomains()) as $removedHost) {
$output->writeln(
$removedHost . ' was removed from the whitelist.'
);
}
$whitelistBefore = $this->domainManager->getDomains();
$removedDomains = array_filter(array_map('trim', $domains), 'strlen');

$this->domainManager->removeDomains($removedDomains);

foreach (array_intersect($removedDomains, $whitelistBefore) as $removedHost) {
$output->writeln($removedHost . ' was removed from the whitelist.' . PHP_EOL);
}

return Cli::RETURN_SUCCESS;
} catch (InvalidArgumentException $e) {
return $this->downloadManager->handleInvalidArgumentException($e, $output);
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}
return Cli::RETURN_FAILURE;
return $this->downloadManager->handleException($e, $output);
}

return Cli::RETURN_SUCCESS;
}
}
57 changes: 52 additions & 5 deletions app/code/Magento/Downloadable/Helper/Download.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/

namespace Magento\Downloadable\Helper;

use Exception;
use InvalidArgumentException;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\File\Mime;
use Magento\Framework\Filesystem;
use Magento\Framework\Exception\LocalizedException as CoreException;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\Console\Cli;

/**
* Downloadable Products Download Helper
Expand Down Expand Up @@ -141,7 +145,7 @@ public function __construct(
* Retrieve Resource file handle (socket, file pointer etc)
*
* @return \Magento\Framework\Filesystem\File\ReadInterface
* @throws CoreException|\Exception
* @throws CoreException|Exception
*/
protected function _getHandle()
{
Expand Down Expand Up @@ -243,14 +247,14 @@ public function getFilename()
* @param string $resourceFile
* @param string $linkType
* @return $this
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
{
if (self::LINK_TYPE_FILE == $linkType) {
//check LFI protection
if ($resourceFile && preg_match('#\.\.[\\\/]#', $resourceFile)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'Requested file may not include parent directory traversal ("../", "..\\" notation)'
);
}
Expand Down Expand Up @@ -304,4 +308,47 @@ public function getContentDisposition($store = null)
$store
);
}

/**
* Handle any exception thrown during command execution
*
* @param Exception $e
* @param OutputInterface $output
* @return int
*/
public function handleException(Exception $e, OutputInterface $output): int
{
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}
return Cli::RETURN_FAILURE;
}

/**
* Validate the input domains array
*
* @param array $domains
* @return void
* @throws InvalidArgumentException
*/
public function validateDomains(array $domains): void
{
if (empty($domains)) {
throw new InvalidArgumentException('Error: Domains parameter is missing.');
}
}

/**
* Handle the \InvalidArgumentException exception.
*
* @param InvalidArgumentException $e
* @param OutputInterface $output
* @return int
*/
public function handleInvalidArgumentException(InvalidArgumentException $e, OutputInterface $output): int
{
$output->writeln('<error>' . $e->getMessage() . '</error>');
return Cli::RETURN_FAILURE;
}
}