From a5efbbc94ad93ad4a0ef880b87beced9b0e7a272 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 27 Jun 2021 14:44:26 +0200 Subject: [PATCH] Validate schema before writing the file, fixes #9986 --- src/Composer/Command/InitCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 8332542a4..214cae359 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -14,6 +14,7 @@ namespace Composer\Command; use Composer\Factory; use Composer\Json\JsonFile; +use Composer\Json\JsonValidationException; use Composer\Package\BasePackage; use Composer\Package\CompletePackageInterface; use Composer\Package\Package; @@ -27,6 +28,7 @@ use Composer\Repository\RepositorySet; use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; use Composer\Semver\Constraint\Constraint; +use Composer\Util\Silencer; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -162,6 +164,16 @@ EOT } $file->write($options); + try { + $file->validateSchema(JsonFile::LAX_SCHEMA); + } catch (JsonValidationException $e) { + $io->writeError('Schema validation error, aborting'); + $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); + $io->writeError($e->getMessage() . ':' . PHP_EOL . $errors); + Silencer::call('unlink', $file->getPath()); + + return 1; + } // --autoload - Create src folder if ($autoloadPath) {