Skip to content

Commit e3da417

Browse files
authored
Sanitize quoted modifier attributes (#403)
1 parent 7fec017 commit e3da417

File tree

8 files changed

+53
-10
lines changed

8 files changed

+53
-10
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ protected function buildDefinition(Model $model)
198198
$definition
199199
);
200200
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
201-
$default = $column->defaultValue() ?? "'{}'";
201+
$default = $column->defaultValue() ?? "{}";
202202
if (Blueprint::isLaravel8OrHigher()) {
203-
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => {$default}," . PHP_EOL;
203+
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => '{$default}'," . PHP_EOL;
204204
} else {
205-
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => {$default}," . PHP_EOL;
205+
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => '{$default}'," . PHP_EOL;
206206
}
207207
} elseif ($column->dataType() === 'morphs') {
208208
if ($column->isNullable()) {

src/Generators/MigrationGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ protected function buildDefinition(Model $model)
198198

199199
foreach ($modifiers as $modifier) {
200200
if (is_array($modifier)) {
201-
$column_definition .= '->'.key($modifier).'('.current($modifier).')';
201+
$column_definition .= sprintf("->%s('%s')", key($modifier), addslashes(current($modifier)));
202202
} elseif ($modifier === 'unsigned' && Str::startsWith($dataType, 'unsigned')) {
203203
continue;
204204
} elseif ($modifier === 'nullable' && Str::startsWith($dataType, 'nullable')) {

src/Lexers/ModelLexer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ private function buildColumn(string $name, string $definition)
241241

242242
if (isset(self::$modifiers[strtolower($value)])) {
243243
$modifierAttributes = $parts[1] ?? null;
244-
if ($modifierAttributes === null) {
244+
if (is_null($modifierAttributes)) {
245245
$modifiers[] = self::$modifiers[strtolower($value)];
246246
} else {
247-
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes];
247+
$modifiers[] = [self::$modifiers[strtolower($value)] => preg_replace('~^[\'"]?(.*?)[\'"]?$~', '$1', $modifierAttributes)];
248248
}
249249
}
250250
}

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ public function modelTreeDataProvider()
790790
['drafts/with-timezones.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
791791
['drafts/relationships.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
792792
['drafts/indexes.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/indexes.php'],
793+
['drafts/custom-indexes.yaml', 'database/migrations/timestamp_create_cooltables_table.php', 'migrations/custom-indexes.php'],
793794
['drafts/unconventional.yaml', 'database/migrations/timestamp_create_teams_table.php', 'migrations/unconventional.php'],
794795
['drafts/optimize.yaml', 'database/migrations/timestamp_create_optimizes_table.php', 'migrations/optimize.php'],
795796
['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,12 @@ public function modifierAttributesProvider()
664664
['default:0.00', 'default', 0.00],
665665
['default:0', 'default', 0],
666666
['default:string', 'default', 'string'],
667-
["default:'empty'", 'default', "'empty'"],
668-
['default:""', 'default', '""'],
667+
["default:'empty'", 'default', 'empty'],
668+
['default:""', 'default', ''],
669669
['charset:utf8', 'charset', 'utf8'],
670670
['collation:utf8_unicode', 'collation', 'utf8_unicode'],
671-
['default:"space between"', 'default', '"space between"'],
671+
['default:"space between"', 'default', 'space between'],
672+
["default:'[]'", 'default', '[]'],
672673
];
673674
}
674675
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
cooltable:
3+
timestamps: false
4+
coolcool: id foreign:coolcool.id index:custom_index_coolcool
5+
foobar: id foreign:foobars.id index:custom_index_foobar

tests/fixtures/migrations/columns-with-comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('professions', function (Blueprint $table) {
1717
$table->id();
18-
$table->string('title', 400)->comment("Some title for the profession");
18+
$table->string('title', 400)->comment('Some title for the profession');
1919
$table->string('description', 400)->nullable()->comment('Some description for the profession');
2020
$table->timestamps();
2121
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCooltablesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::disableForeignKeyConstraints();
17+
18+
Schema::create('cooltables', function (Blueprint $table) {
19+
$table->id();
20+
$table->foreignId('coolcool')->constrained('coolcool')->cascadeOnDelete()->index('custom_index_coolcool');
21+
$table->foreignId('foobar')->constrained('foobars')->cascadeOnDelete()->index('custom_index_foobar');
22+
});
23+
24+
Schema::enableForeignKeyConstraints();
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('cooltables');
35+
}
36+
}

0 commit comments

Comments
 (0)