Skip to content

Commit

Permalink
better validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Jan 13, 2025
1 parent cdbb244 commit 86c1f07
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 18 deletions.
61 changes: 43 additions & 18 deletions src/OpenMage/ComposerPlugin/Copy/Unpkg/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,21 @@ public function getValidatedConfig($packageConfig): ?array
return null;
}

if (!$this->validateConfigFiles($packageConfig)) {
$files = $this->validateConfigFiles($packageConfig);
if (!$files) {
return null;
}

if (!$this->validateConfigVersion($packageConfig)) {
$version = $this->validateConfigVersion($packageConfig);
if (!$version) {
return null;
}

$source = '';
if (array_key_exists(self::CONFIG_SOURCE, $packageConfig) && is_string($packageConfig[self::CONFIG_SOURCE])) {
$source = $packageConfig[self::CONFIG_SOURCE];
}

$target = '';
if (array_key_exists(self::CONFIG_TARGET, $packageConfig) && is_string($packageConfig[self::CONFIG_TARGET])) {
$target = $packageConfig[self::CONFIG_TARGET];
}
$source = $this->validateConfigSource($packageConfig);
$target = $this->validateConfigTarget($packageConfig);

/** @var string[] $files */
$files = $packageConfig[self::CONFIG_FILES];
return [
'version' => $packageConfig[self::CONFIG_VERSION],
'version' => $version,
'source' => $source,
'target' => $target,
'files' => $files,
Expand All @@ -73,18 +66,50 @@ public function getValidatedConfig($packageConfig): ?array

/**
* @param array<mixed> $packageConfig
* @return string[]|null
*/
private function validateConfigFiles(array $packageConfig): bool
private function validateConfigFiles(array $packageConfig): ?array
{
return array_key_exists(self::CONFIG_FILES, $packageConfig) && is_array($packageConfig[self::CONFIG_FILES]);
if (array_key_exists(self::CONFIG_FILES, $packageConfig) && is_array($packageConfig[self::CONFIG_FILES])) {
/** @var string[] $files */
$files = $packageConfig[self::CONFIG_FILES];
return $files;
}
return null;
}

/**
* @param array<mixed> $packageConfig
*/
private function validateConfigVersion(array $packageConfig): bool
private function validateConfigVersion(array $packageConfig): ?string
{
return array_key_exists(self::CONFIG_VERSION, $packageConfig) && is_string($packageConfig[self::CONFIG_VERSION]);
if (array_key_exists(self::CONFIG_VERSION, $packageConfig) && is_string($packageConfig[self::CONFIG_VERSION])) {
return trim($packageConfig[self::CONFIG_VERSION]);
}
return null;
}

/**
* @param array<mixed> $packageConfig
*/
private function validateConfigSource(array $packageConfig): string
{
if (array_key_exists(self::CONFIG_SOURCE, $packageConfig) && is_string($packageConfig[self::CONFIG_SOURCE])) {
return trim($packageConfig[self::CONFIG_SOURCE]);
}
return '';
}

/**
* @param array<mixed> $packageConfig
*/
private function validateConfigTarget(array $packageConfig): string
{
if (array_key_exists(self::CONFIG_TARGET, $packageConfig) && is_string($packageConfig[self::CONFIG_TARGET])) {
$target = str_replace(['../', './'], '', $packageConfig[self::CONFIG_TARGET]);
return trim($target);
}
return '';
}

public function getUnpkgName(): string
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/Copy/Unpkg/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ public function provideGetValidatedConfig(): Generator
'target' => 'target',
],
];

yield 'target sub-directory' => [
[
'version' => '1',
'source' => 'source',
'target' => 'target',
'files' => [
'test.file',
],
],
[
'files' => [
'test.file',
],
'version' => '1',
'source' => 'source',
'target' => './../target',
],
];

yield 'target w/ spaces' => [
[
'version' => '1',
'source' => 'source',
'target' => 'target',
'files' => [
'test.file',
],
],
[
'files' => [
'test.file',
],
'version' => '1',
'source' => 'source',
'target' => ' target ',
],
];
}

/**
Expand Down

0 comments on commit 86c1f07

Please sign in to comment.