Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Enforce implicit Route Model scoping #39440

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function resolveForRoute($container, $route)
? 'resolveSoftDeletableRouteBinding'
: 'resolveRouteBinding';

if ($parent instanceof UrlRoutable && in_array($parameterName, array_keys($route->bindingFields()))) {
if ($parent instanceof UrlRoutable && ($route->enforcesScoping() || array_key_exists($parameterName, $route->bindingFields()))) {
$childRouteBindingMethod = $route->allowsTrashedBindings()
? 'resolveSoftDeletableChildRouteBinding'
: 'resolveChildRouteBinding';
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,16 @@ public function excludedMiddleware()
return (array) ($this->action['excluded_middleware'] ?? []);
}

/**
* Determine if the route should enforce scoping of multiple Eloquent bindings.
*
* @return bool
*/
public function enforcesScoping()
{
return (bool) ($this->action['scoping'] ?? false);
}

/**
* Specify that the route should not allow concurrent requests from the same session.
*
Expand Down
110 changes: 100 additions & 10 deletions tests/Integration/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ protected function defineDatabaseMigrations(): void
$table->softDeletes();
});

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->timestamps();
});

$this->beforeApplicationDestroyed(function () {
Schema::dropIfExists('users');
Schema::dropIfExists('posts');
});
}

Expand All @@ -60,14 +67,14 @@ public function testWithRouteCachingEnabled()
$this->defineCacheRoutes(<<<PHP
<?php

use Illuminate\Tests\Integration\Routing\ImplicitBindingModel;
use Illuminate\Tests\Integration\Routing\ImplicitBindingUser;

Route::post('/user/{user}', function (ImplicitBindingModel \$user) {
Route::post('/user/{user}', function (ImplicitBindingUser \$user) {
return \$user;
})->middleware('web');
PHP);

$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$response = $this->postJson("/user/{$user->id}");

Expand All @@ -79,11 +86,11 @@ public function testWithRouteCachingEnabled()

public function testWithoutRouteCachingEnabled()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web']);

Expand All @@ -97,13 +104,13 @@ public function testWithoutRouteCachingEnabled()

public function testSoftDeletedModelsAreNotRetrieved()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$user->delete();

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web']);

Expand All @@ -114,13 +121,13 @@ public function testSoftDeletedModelsAreNotRetrieved()

public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
{
$user = ImplicitBindingModel::create(['name' => 'Dries']);
$user = ImplicitBindingUser::create(['name' => 'Dries']);

$user->delete();

config(['app.key' => str_repeat('a', 32)]);

Route::post('/user/{user}', function (ImplicitBindingModel $user) {
Route::post('/user/{user}', function (ImplicitBindingUser $user) {
return $user;
})->middleware(['web'])->withTrashed();

Expand All @@ -131,13 +138,96 @@ public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
'name' => $user->name,
]);
}

public function testEnforceScopingImplicitRouteBindings()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

config(['app.key' => str_repeat('a', 32)]);

Route::group(['scoping' => true], function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser $user, ImplicitBindingPost $post) {
return [$user, $post];
})->middleware(['web']);
});

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");

$response->assertNotFound();
}

public function testEnforceScopingImplicitRouteBindingsWithRouteCachingEnabled()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

$this->defineCacheRoutes(<<<PHP
<?php

use Illuminate\Tests\Integration\Routing\ImplicitBindingUser;
use Illuminate\Tests\Integration\Routing\ImplicitBindingPost;

Route::group(['scoping' => true], function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser \$user, ImplicitBindingPost \$post) {
return [\$user, \$post];
})->middleware(['web']);
});
PHP);

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");

$response->assertNotFound();
}

public function testWithoutEnforceScopingImplicitRouteBindings()
{
$user = ImplicitBindingUser::create(['name' => 'Dries']);
$post = ImplicitBindingPost::create(['user_id' => 2]);
$this->assertEmpty($user->posts);

config(['app.key' => str_repeat('a', 32)]);

Route::group(['scoping' => false], function () {
Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser $user, ImplicitBindingPost $post) {
return [$user, $post];
})->middleware(['web']);
});

$response = $this->getJson("/user/{$user->id}/post/{$post->id}");
$response->assertOk();
$response->assertJson([
[
'id' => $user->id,
'name' => $user->name,
],
[
'id' => 1,
'user_id' => 2,
],
]);
}
}

class ImplicitBindingModel extends Model
class ImplicitBindingUser extends Model
{
use SoftDeletes;

public $table = 'users';

protected $fillable = ['name'];

public function posts()
{
return $this->hasMany(ImplicitBindingPost::class, 'user_id');
}
}

class ImplicitBindingPost extends Model
{
public $table = 'posts';

protected $fillable = ['user_id'];
}