From 58905ffe4efb5704f463a56f7339ec8f4e82cdf9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 19 Sep 2024 17:07:28 +0200 Subject: [PATCH] Validate licenses passed into init command (#12115) Fixes #10838 --- src/Composer/Command/InitCommand.php | 5 +++++ tests/Composer/Test/Command/InitCommandTest.php | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 4f1398726..0829eb09d 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -21,6 +21,7 @@ use Composer\Pcre\Preg; use Composer\Repository\CompositeRepository; use Composer\Repository\PlatformRepository; use Composer\Repository\RepositoryFactory; +use Composer\Spdx\SpdxLicenses; use Composer\Util\Filesystem; use Composer\Util\Silencer; use Symfony\Component\Console\Input\ArrayInput; @@ -398,6 +399,10 @@ EOT 'License ['.$license.']: ', $license ); + $spdx = new SpdxLicenses(); + if (!$spdx->validate($license)) { + throw new \InvalidArgumentException('Invalid license provided: '.$license.'. Only SPDX license identifiers (https://spdx.org/licenses/) or "proprietary" are accepted.'); + } $input->setOption('license', $license); $io->writeError(['', 'Define your dependencies.', '']); diff --git a/tests/Composer/Test/Command/InitCommandTest.php b/tests/Composer/Test/Command/InitCommandTest.php index 41860f628..67b1fd169 100644 --- a/tests/Composer/Test/Command/InitCommandTest.php +++ b/tests/Composer/Test/Command/InitCommandTest.php @@ -711,7 +711,7 @@ class InitCommandTest extends TestCase 'Mr. Test ', // Author 'stable', // Minimum stability 'library', // Type - 'Custom License', // License + 'AGPL-3.0-only', // License 'no', // Define dependencies 'no', // Define dev dependencies 'n', // Add PSR-4 autoload mapping @@ -726,7 +726,7 @@ class InitCommandTest extends TestCase 'name' => 'vendor/pkg', 'description' => 'my desciption', 'type' => 'library', - 'license' => 'Custom License', + 'license' => 'AGPL-3.0-only', 'authors' => [['name' => 'Mr. Test', 'email' => 'test@example.org']], 'minimum-stability' => 'stable', 'require' => [],