Skip to content

Commit 7c56896

Browse files
authoredSep 17, 2024··
fix(laravel): call authorize on delete but not validation (#6618)
1 parent d74b2b5 commit 7c56896

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed
 

‎src/Laravel/State/ValidateProvider.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4242
$request = $context['request'];
4343
$body = $this->inner->provide($operation, $uriVariables, $context);
4444

45-
if (!$operation->canValidate() || $operation instanceof Error) {
45+
if ($operation instanceof Error) {
4646
return $body;
4747
}
4848

@@ -53,15 +53,23 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5353

5454
if (\is_string($rules) && is_a($rules, FormRequest::class, true)) {
5555
try {
56+
// this also throws an AuthorizationException
5657
$this->app->make($rules);
57-
// } catch (AuthorizationException $e) { // TODO: we may want to catch this to transform to an error
5858
} catch (ValidationException $e) { // @phpstan-ignore-line make->($rules) may throw this
59+
if (!$operation->canValidate()) {
60+
return $body;
61+
}
62+
5963
throw $this->getValidationError($e->validator, $e);
6064
}
6165

6266
return $body;
6367
}
6468

69+
if (!$operation->canValidate()) {
70+
return $body;
71+
}
72+
6573
if (!\is_array($rules)) {
6674
return $body;
6775
}

‎src/Laravel/Tests/AuthTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ public function testAuthenticatedPolicy(): void
4747
$response = $this->post('/api/vaults', [], ['accept' => ['application/ld+json'], 'content-type' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
4848
$response->assertStatus(403);
4949
}
50+
51+
public function testAuthenticatedDeleteWithPolicy(): void
52+
{
53+
$response = $this->post('/tokens/create');
54+
$token = $response->json()['token'];
55+
$response = $this->delete('/api/vaults/1', [], ['accept' => ['application/ld+json'], 'authorization' => 'Bearer '.$token]);
56+
$response->assertStatus(403);
57+
}
5058
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\App\Http\Requests;
15+
16+
use Illuminate\Foundation\Http\FormRequest;
17+
use Workbench\App\Models\Vault;
18+
19+
class VaultFormRequest extends FormRequest
20+
{
21+
public function authorize(): bool
22+
{
23+
return $this->user()->can('delete', new Vault());
24+
}
25+
26+
/**
27+
* Get the validation rules that apply to the request.
28+
*
29+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
30+
*/
31+
public function rules(): array
32+
{
33+
return [
34+
'secret' => 'required',
35+
];
36+
}
37+
}

‎src/Laravel/workbench/app/Models/Vault.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
namespace Workbench\App\Models;
1515

1616
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Delete;
1718
use ApiPlatform\Metadata\GetCollection;
1819
use ApiPlatform\Metadata\Post;
1920
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021
use Illuminate\Database\Eloquent\Model;
22+
use Workbench\App\Http\Requests\VaultFormRequest;
2123

2224
#[ApiResource(
2325
operations: [
@@ -30,6 +32,7 @@
3032
read: true,
3133
write: false
3234
),
35+
new Delete(middleware: 'auth:sanctum', rules: VaultFormRequest::class, provider: [self::class, 'provide']),
3336
]
3437
)]
3538
class Vault extends Model

‎src/Laravel/workbench/app/Policies/VaultPolicy.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function update(User $user, Vault $vault): bool
2222
{
2323
return false;
2424
}
25+
26+
public function delete(User $user): bool
27+
{
28+
return false;
29+
}
2530
}

0 commit comments

Comments
 (0)
Please sign in to comment.