Skip to content
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ where applicable.

- Initial Maker Bundle package implementation, documentation, and QA workflow.

## 1.0.4 — 2026-11-16

- Validate the `make:data-provider --method` option as a PHP identifier so it can no longer inject code into the generated class.
- Reject `make:asset-entry --path` values that leave the package (traversal segments, absolute paths, symlink escapes).
- Restrict `make:asset-entry --handle` to lowercase letters, digits, `-` and `_` so it cannot break out of the generated source comment.
- `make:block` now fails instead of silently overwriting an existing `block.json`, editor/frontend entry or view template.
- CI: pin `sympress/workflows` to v1.3.2.

## 1.0.3 — 2026-11-06

- Use the Symfony 8.1.11 security patches released on November 4.
5 changes: 5 additions & 0 deletions src/Maker/MakeAssetEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$name = (string) $input->getArgument('name');
$style = (bool) $input->getOption('style');
$handle = (string) ($input->getOption('handle') ?: $this->defaultHandle($context, $name));

if (preg_match('/^[a-z0-9][a-z0-9_-]*$/D', $handle) !== 1) {
throw new \InvalidArgumentException('Asset handle may only contain lowercase letters, digits, "-" and "_".');
}

$sourcePath = (string) ($input->getOption('path') ?: $this->defaultSourcePath($name, $style));
$location = $this->assetLocation((string) $input->getOption('location'));
$webpackUpdate = $this->webpackConfigurator->entry($context, $handle, $sourcePath, $style);
Expand Down
25 changes: 21 additions & 4 deletions src/Maker/MakeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'view_path' => sprintf('../../Resources/views/block/%s.php', $slug),
],
);
$generator->dumpFile(
$this->dumpNewFile(
$generator,
$context,
$context->packageRelativePath(sprintf('Resources/block/%s/block.json', $slug)),
$this->jsonFile->encode($this->blockMetadata(
$blockName,
Expand All @@ -105,7 +107,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$withFrontend ? $frontendHandle : null,
)),
);
$generator->dumpFile(
$this->dumpNewFile(
$generator,
$context,
$context->packageRelativePath(ltrim($editorSource, './')),
$this->editorSource($blockName, $title, $context->textDomain()),
);
Expand All @@ -121,7 +125,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
];

if ($withFrontend) {
$generator->dumpFile(
$this->dumpNewFile(
$generator,
$context,
$context->packageRelativePath(ltrim($frontendSource, './')),
$this->frontendSource($blockName),
);
Expand All @@ -136,7 +142,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
}

if ($withView) {
$generator->dumpFile(
$this->dumpNewFile(
$generator,
$context,
$context->packageRelativePath(sprintf('Resources/views/block/%s.php', $slug)),
'<div data-block="' . htmlspecialchars($blockName, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '"></div>' . "\n",
);
Expand Down Expand Up @@ -203,6 +211,15 @@ private function blockMetadata(
return $metadata;
}

private function dumpNewFile(Generator $generator, PackageContext $context, string $path, string $contents): void
{
if (file_exists(sprintf('%s/%s', $context->projectDir, $path))) {
throw new \RuntimeException(sprintf('The file "%s" can\'t be generated because it already exists.', $path));
}

$generator->dumpFile($path, $contents);
}

private function ensureConfigLoaderInterface(PackageContext $context, Generator $generator): void
{
$file = sprintf('%s/src/ConfigLoader/ConfigLoaderInterface.php', $context->packagePath);
Expand Down
5 changes: 4 additions & 1 deletion src/Maker/MakeDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use SymPress\MakerBundle\Util\PackageContextResolver;
use SymPress\MakerBundle\Util\PackageServiceConfigurator;
use SymPress\MakerBundle\Util\SourceLiteral;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
Expand Down Expand Up @@ -44,6 +45,8 @@ public function configureCommand(Command $command, InputConfiguration $inputConf

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$method = (string) $input->getOption('method');
SourceLiteral::identifier($method);
$classDetails = $generator->createClassNameDetails(
(string) $input->getArgument('class'),
'DataProvider\\Templating\\',
Expand All @@ -54,7 +57,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$classDetails->getFullName(),
__DIR__ . '/../../Resources/skeleton/data_provider/DataProvider.tpl.php',
[
'method_name' => (string) $input->getOption('method'),
'method_name' => $method,
],
);

Expand Down
4 changes: 2 additions & 2 deletions src/Util/PackageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function relativePath(string $path): string

public function packageRelativePath(string $path): string
{
return ltrim(sprintf(
return ProjectPath::relative($this->projectDir, ltrim(sprintf(
'%s/%s',
$this->relativePath($this->packagePath),
ltrim(str_replace('\\', '/', $path), '/'),
), '/');
), '/'));
}

public function tagPrefix(): string
Expand Down
179 changes: 179 additions & 0 deletions tests/Maker/MakerInputSafetyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

declare(strict_types=1);

namespace SymPress\MakerBundle\Tests\Maker;

use PHPUnit\Framework\TestCase;
use SymPress\MakerBundle\Composer\PackageAutoloadResolver;
use SymPress\MakerBundle\Maker\MakeAssetEntry;
use SymPress\MakerBundle\Maker\MakeBlock;
use SymPress\MakerBundle\Maker\MakeDataProvider;
use SymPress\MakerBundle\Tests\Fixtures\ArrayComposerPackageLocator;
use SymPress\MakerBundle\Util\JsonFile;
use SymPress\MakerBundle\Util\PackageAwareAutoloaderUtil;
use SymPress\MakerBundle\Util\PackageAwareGenerator;
use SymPress\MakerBundle\Util\PackageContextResolver;
use SymPress\MakerBundle\Util\PackageServiceConfigurator;
use SymPress\MakerBundle\Util\WebpackEncoreEntryConfigurator;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
use Symfony\Bundle\MakerBundle\Util\TemplateComponentGenerator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Filesystem\Filesystem;

final class MakerInputSafetyTest extends TestCase
{
private string $workspace;
private PackageAutoloadResolver $autoloadResolver;
private PackageContextResolver $contextResolver;

protected function setUp(): void
{
$this->workspace = sys_get_temp_dir() . '/sympress-maker-input-' . bin2hex(random_bytes(6));
mkdir($this->workspace . '/packages/plugin', 0777, true);
file_put_contents(
$this->workspace . '/packages/plugin/composer.json',
json_encode(
[
'name' => 'site/plugin',
'type' => 'wordpress-plugin',
'autoload' => ['psr-4' => ['Site\\Plugin\\' => 'src/']],
],
\JSON_THROW_ON_ERROR,
),
);
$locator = new ArrayComposerPackageLocator(['site/plugin' => $this->workspace . '/packages/plugin']);
$this->autoloadResolver = new PackageAutoloadResolver($locator);
$this->contextResolver = new PackageContextResolver($this->workspace, $locator, $this->autoloadResolver);
}

protected function tearDown(): void
{
(new Filesystem())->remove($this->workspace);
}

public function testDataProviderRejectsMethodNameThatIsNotAnIdentifier(): void
{
$maker = new MakeDataProvider($this->contextResolver, new PackageServiceConfigurator());

try {
$this->make($maker, [
'class' => 'Site\\Plugin\\DataProvider\\Templating\\EvilDataProvider',
'--method' => "provide(): array { return []; }\n public function __construct() { system('id'); }\n public function x",
'--no-service' => true,
]);
self::fail('Invalid method name accepted.');
} catch (\InvalidArgumentException) {
self::assertDirectoryDoesNotExist($this->workspace . '/packages/plugin/src');
}
}

public function testAssetEntryRejectsSourcePathOutsideThePackage(): void
{
mkdir($this->workspace . '/packages/plugin/Resources');

try {
$this->make($this->assetEntryMaker(), [
'package' => 'site/plugin',
'name' => 'frontend',
'--path' => 'Resources/../../../escape.ts',
'--no-loader' => true,
]);
self::fail('Traversal path accepted.');
} catch (\InvalidArgumentException) {
self::assertFileDoesNotExist($this->workspace . '/escape.ts');
self::assertFileDoesNotExist($this->workspace . '/packages/plugin/webpack.config.js');
}
}

public function testAssetEntryRejectsHandleThatBreaksOutOfTheSourceComment(): void
{
foreach (["evil\nalert(1)", 'evil */ body { } /*'] as $handle) {
try {
$this->make($this->assetEntryMaker(), [
'package' => 'site/plugin',
'name' => 'frontend',
'--handle' => $handle,
'--no-loader' => true,
]);
self::fail('Unsafe handle accepted.');
} catch (\InvalidArgumentException) {
self::assertDirectoryDoesNotExist($this->workspace . '/packages/plugin/Resources');
}
}
}

public function testBlockRefusesToOverwriteExistingBlockAssets(): void
{
$blockJson = $this->workspace . '/packages/plugin/Resources/block/hero/block.json';
mkdir(dirname($blockJson), 0777, true);
file_put_contents($blockJson, '{"name":"site/maintained"}');
$maker = new MakeBlock(
$this->contextResolver,
new PackageServiceConfigurator(),
new WebpackEncoreEntryConfigurator(),
new JsonFile(),
);

$arguments = ['class' => 'Site\\Plugin\\Block\\HeroBlock', '--no-service' => true, '--with-view' => true];

try {
$this->make($maker, $arguments);
$error = null;
} catch (\RuntimeException $exception) {
$error = $exception->getMessage();
}

self::assertStringContainsString('already exists', (string) $error);
self::assertSame('{"name":"site/maintained"}', file_get_contents($blockJson));
self::assertFileDoesNotExist($this->workspace . '/packages/plugin/src/Block/HeroBlock.php');

unlink($blockJson);
$this->make($maker, $arguments);
self::assertFileExists($blockJson);
self::assertFileExists($this->workspace . '/packages/plugin/Resources/views/block/hero.php');
}

/** @param array<string, mixed> $arguments */
private function make(AbstractMaker $maker, array $arguments): void
{
$command = new Command($maker::getCommandName());
$maker->configureCommand($command, new InputConfiguration());
$input = new ArrayInput($arguments, $command->getDefinition());
$maker->generate($input, new ConsoleStyle($input, new BufferedOutput()), $this->generator());
}

private function assetEntryMaker(): MakeAssetEntry
{
return new MakeAssetEntry(
$this->contextResolver,
new PackageServiceConfigurator(),
new WebpackEncoreEntryConfigurator(),
);
}

private function generator(): PackageAwareGenerator
{
$fileManager = new FileManager(
new Filesystem(),
new PackageAwareAutoloaderUtil($this->autoloadResolver),
new MakerFileLinkFormatter(),
$this->workspace,
null,
);

return new PackageAwareGenerator(
$fileManager,
'App',
$this->autoloadResolver,
null,
new TemplateComponentGenerator(true, false, 'App'),
);
}
}
Loading