fix: validate class name input and check file write in Make commands

Add path traversal protection via PHP class name regex validation to all
four Make* commands, check file_put_contents() return value for write
failures, and remove the unnecessary \$defer = false property from
CartServiceProvider.
This commit is contained in:
Joel Male
2026-05-27 21:49:08 +10:00
parent ce51042edf
commit fd77347542
5 changed files with 56 additions and 6 deletions
-2
View File
@@ -14,8 +14,6 @@
class CartServiceProvider extends ServiceProvider class CartServiceProvider extends ServiceProvider
{ {
protected $defer = false;
public function boot(): void public function boot(): void
{ {
if ($this->app->runningInConsole()) { if ($this->app->runningInConsole()) {
+14 -1
View File
@@ -15,11 +15,19 @@ class MakeCouponCommand extends Command
public function handle(): int public function handle(): int
{ {
$name = $this->argument('name'); $name = $this->argument('name');
if (! preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $name)) {
$this->error("Invalid class name: [{$name}]. Must be a valid PHP class name.");
return self::FAILURE;
}
$directory = app_path('Cart/Coupons'); $directory = app_path('Cart/Coupons');
$path = "{$directory}/{$name}.php"; $path = "{$directory}/{$name}.php";
if (file_exists($path)) { if (file_exists($path)) {
$this->error("Coupon already exists: app/Cart/Coupons/{$name}.php"); $this->error("Coupon already exists: app/Cart/Coupons/{$name}.php");
return self::FAILURE; return self::FAILURE;
} }
@@ -27,7 +35,12 @@ public function handle(): int
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
file_put_contents($path, static::stub($name)); if (file_put_contents($path, static::stub($name)) === false) {
$this->error("Failed to write file: {$path}");
return self::FAILURE;
}
$this->info("Coupon created: app/Cart/Coupons/{$name}.php"); $this->info("Coupon created: app/Cart/Coupons/{$name}.php");
return self::SUCCESS; return self::SUCCESS;
+14 -1
View File
@@ -15,11 +15,19 @@ class MakeDriverCommand extends Command
public function handle(): int public function handle(): int
{ {
$name = $this->argument('name'); $name = $this->argument('name');
if (! preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $name)) {
$this->error("Invalid class name: [{$name}]. Must be a valid PHP class name.");
return self::FAILURE;
}
$directory = app_path('Cart/Drivers'); $directory = app_path('Cart/Drivers');
$path = "{$directory}/{$name}.php"; $path = "{$directory}/{$name}.php";
if (file_exists($path)) { if (file_exists($path)) {
$this->error("Driver already exists: app/Cart/Drivers/{$name}.php"); $this->error("Driver already exists: app/Cart/Drivers/{$name}.php");
return self::FAILURE; return self::FAILURE;
} }
@@ -27,7 +35,12 @@ public function handle(): int
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
file_put_contents($path, static::stub($name)); if (file_put_contents($path, static::stub($name)) === false) {
$this->error("Failed to write file: {$path}");
return self::FAILURE;
}
$this->info("Driver created: app/Cart/Drivers/{$name}.php"); $this->info("Driver created: app/Cart/Drivers/{$name}.php");
$this->line(" Register it in config/cart.php under <comment>drivers</comment>."); $this->line(" Register it in config/cart.php under <comment>drivers</comment>.");
+14 -1
View File
@@ -15,11 +15,19 @@ class MakeShippingCommand extends Command
public function handle(): int public function handle(): int
{ {
$name = $this->argument('name'); $name = $this->argument('name');
if (! preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $name)) {
$this->error("Invalid class name: [{$name}]. Must be a valid PHP class name.");
return self::FAILURE;
}
$directory = app_path('Cart/Shipping'); $directory = app_path('Cart/Shipping');
$path = "{$directory}/{$name}.php"; $path = "{$directory}/{$name}.php";
if (file_exists($path)) { if (file_exists($path)) {
$this->error("Shipping rate already exists: app/Cart/Shipping/{$name}.php"); $this->error("Shipping rate already exists: app/Cart/Shipping/{$name}.php");
return self::FAILURE; return self::FAILURE;
} }
@@ -27,7 +35,12 @@ public function handle(): int
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
file_put_contents($path, static::stub($name)); if (file_put_contents($path, static::stub($name)) === false) {
$this->error("Failed to write file: {$path}");
return self::FAILURE;
}
$this->info("Shipping rate created: app/Cart/Shipping/{$name}.php"); $this->info("Shipping rate created: app/Cart/Shipping/{$name}.php");
return self::SUCCESS; return self::SUCCESS;
+14 -1
View File
@@ -15,11 +15,19 @@ class MakeTaxCommand extends Command
public function handle(): int public function handle(): int
{ {
$name = $this->argument('name'); $name = $this->argument('name');
if (! preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $name)) {
$this->error("Invalid class name: [{$name}]. Must be a valid PHP class name.");
return self::FAILURE;
}
$directory = app_path('Cart/Tax'); $directory = app_path('Cart/Tax');
$path = "{$directory}/{$name}.php"; $path = "{$directory}/{$name}.php";
if (file_exists($path)) { if (file_exists($path)) {
$this->error("Tax rule already exists: app/Cart/Tax/{$name}.php"); $this->error("Tax rule already exists: app/Cart/Tax/{$name}.php");
return self::FAILURE; return self::FAILURE;
} }
@@ -27,7 +35,12 @@ public function handle(): int
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
file_put_contents($path, static::stub($name)); if (file_put_contents($path, static::stub($name)) === false) {
$this->error("Failed to write file: {$path}");
return self::FAILURE;
}
$this->info("Tax rule created: app/Cart/Tax/{$name}.php"); $this->info("Tax rule created: app/Cart/Tax/{$name}.php");
return self::SUCCESS; return self::SUCCESS;