Skip to content

Commit

Permalink
Sanity check service data in legacy service instantiator (#5629)
Browse files Browse the repository at this point in the history
* Validate that service data looks reasonable before instantiating any services. Skip any service file that we do not have confidence in.

* Add docblock comments

* Code style

* Fix typo and add return typehint

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>

* Emit messages as "info" not "warning" level

---------

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
  • Loading branch information
greg-1-anderson and weitzman committed Jun 7, 2023
1 parent d789732 commit bbd54a6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Boot/DrupalBoot8.php
Expand Up @@ -257,7 +257,7 @@ public function addDrupalModuleDrushCommands(BootstrapManager $manager): void
// Legacy service adapters for drush.services.yml files.
$serviceFinder = new LegacyServiceFinder($moduleHandler, Drush::config());
$drushServiceFiles = $serviceFinder->getDrushServiceFiles();
$legacyServiceInstantiator = new LegacyServiceInstantiator($container);
$legacyServiceInstantiator = new LegacyServiceInstantiator($container, $this->logger);
$legacyServiceInstantiator->loadServiceFiles($drushServiceFiles);

// Find the containerless commands, and command info alterers
Expand Down
52 changes: 50 additions & 2 deletions src/Runtime/LegacyServiceInstantiator.php
Expand Up @@ -11,6 +11,7 @@
use Composer\Autoload\ClassLoader;
use Drush\Command\DrushCommandInfoAlterer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Yaml\Yaml;
use Robo\Robo;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
Expand All @@ -31,8 +32,9 @@ class LegacyServiceInstantiator
protected array $instantiatedDrushServices = [];
protected array $tags = [];

public function __construct(protected ContainerInterface $container)
public function __construct(protected ContainerInterface $container, LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
Expand All @@ -55,12 +57,58 @@ public function loadServiceFiles(array $serviceFiles)
$serviceFileData = Yaml::parse($serviceFileContents);
}

if (isset($serviceFileData['services'])) {
if ($this->isValidServiceData($serviceFile, $serviceFileData)) {
$this->instantiateServices($serviceFileData['services']);
}
}
}

/**
* Validate service data before using it.
*
* @param string $serviceFile Path to service file being checked
* @param array $serviceFileData Parsed data from drush.services.yml
*/
protected function isValidServiceData(string $serviceFile, array $serviceFileData): bool
{
// If there are no services, then silently skip this service file.
if (!isset($serviceFileData['services'])) {
return false;
}

// We don't support auto-wiring
if (!empty($serviceFileData['services']['_defaults']['autowire'])) {
$this->logger->info(dt('Autowire not supported; skipping @file', ['@file' => $serviceFile]));
return false;
}

// Every entry in services must have a 'class' entry
if (!$this->allServicesHaveClassElement($serviceFile, $serviceFileData['services'])) {
return false;
}

// If we didn't find anything wrong, then assume it's probably okay
return true;
}

/**
* Check all elements for required "class" elements.
*
* @param string $serviceFile Path to service file being checked
* @param array $services List of data from 'services' element from drush.services.yml
*/
protected function allServicesHaveClassElement(string $serviceFile, array $services): bool
{
foreach ($services as $service => $data) {
if (!isset($data['class'])) {
$this->logger->info(dt('Service @service does not have a class element; skipping @file', ['@service' => $service, '@file' => $serviceFile]));
return false;
}
}

return true;
}

/**
* Given a drush.services.yml file (parsed into an array),
* instantiate all of the services referenced therein.
Expand Down

0 comments on commit bbd54a6

Please sign in to comment.