-
-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(laravel): parameter validator + security
- Loading branch information
Showing
4 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ParameterNotFound; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\State\Util\ParameterParserTrait; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\ValidationException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Validates parameters using the Symfony validator. | ||
* | ||
* @experimental | ||
*/ | ||
final class ParameterValidatorProvider implements ProviderInterface | ||
{ | ||
use ParameterParserTrait; | ||
use ValidationErrorTrait; | ||
|
||
public function __construct( | ||
private readonly ?ProviderInterface $decorated = null, | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
if (!($request = $context['request']) instanceof Request) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
$operation = $request->attributes->get('_api_operation') ?? $operation; | ||
if (!($operation->getQueryParameterValidationEnabled() ?? true)) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
$allConstraints = []; | ||
foreach ($operation->getParameters() ?? [] as $parameter) { | ||
if (!$constraints = $parameter->getConstraints()) { | ||
continue; | ||
} | ||
|
||
$key = $parameter->getKey(); | ||
$value = $parameter->getValue(); | ||
if ($value instanceof ParameterNotFound) { | ||
$value = null; | ||
} | ||
|
||
foreach ($constraints as $c) { | ||
$allConstraints[] = $c; | ||
} | ||
} | ||
|
||
$validator = Validator::make($request->query(), $allConstraints); | ||
if ($validator->fails()) { | ||
throw $this->getValidationError($validator, new ValidationException($validator)); | ||
} | ||
|
||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Laravel\State; | ||
|
||
use ApiPlatform\Laravel\ApiResource\ValidationError; | ||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
trait ValidationErrorTrait | ||
{ | ||
private function getValidationError(Validator $validator, ValidationException $e): ValidationError | ||
{ | ||
$errors = $validator->errors(); | ||
$violations = []; | ||
$id = hash('xxh3', implode(',', $errors->keys())); | ||
foreach ($errors->messages() as $prop => $message) { | ||
$violations[] = ['propertyPath' => $prop, 'message' => implode(\PHP_EOL, $message)]; | ||
} | ||
|
||
return new ValidationError($e->getMessage(), $id, $e, $violations); | ||
} | ||
} |