@@ -3031,6 +3031,104 @@ If you need to know the reason why a signed URI is invalid, you can use the
3031
3031
Support for :doc: `Symfony Clock </components/clock >` in ``UriSigner `` was
3032
3032
introduced in Symfony 7.3.
3033
3033
3034
+ Another way to validate incoming requests is to use the ``#[IsSignatureValid] `` attribute.
3035
+
3036
+ In the following example, all incoming requests to this controller action will be verified for
3037
+ a valid signature. If the signature is missing or invalid,
3038
+ a **404 Not Found ** response will be returned by default:
3039
+
3040
+ .. code-block :: php-attributes
3041
+
3042
+ // src/Controller/SomeController.php
3043
+ // ...
3044
+
3045
+ use App\Security\Attribute\IsSignatureValid;
3046
+
3047
+ #[IsSignatureValid]
3048
+ public function someAction(): Response
3049
+ {
3050
+ // ...
3051
+ }
3052
+
3053
+
3054
+ You can customize the failure behavior by changing the HTTP status code returned
3055
+ when validation fails, using the ``validationFailedStatusCode `` argument::
3056
+
3057
+
3058
+ // src/Controller/SomeController.php
3059
+ // ...
3060
+
3061
+ use App\Security\Attribute\IsSignatureValid;
3062
+
3063
+ #[IsSignatureValid(validationFailedStatusCode: 401)]
3064
+ public function someAction(): Response
3065
+ {
3066
+ // ...
3067
+ }
3068
+
3069
+ To restrict signature validation to specific HTTP methods,
3070
+ use the ``methods `` argument. This can be a string or an array of methods::
3071
+
3072
+ // Only validate POST requests
3073
+ #[IsSignatureValid(methods: 'POST')]
3074
+ public function createItem(): Response
3075
+ {
3076
+ // ...
3077
+ }
3078
+
3079
+ // Validate both POST and PUT requests
3080
+ #[IsSignatureValid(methods: ['POST', 'PUT'])]
3081
+ public function updateItem(): Response
3082
+ {
3083
+ // ...
3084
+ }
3085
+
3086
+ If you prefer to throw an exception instead of returning a response,
3087
+ pass ``throw: true ``. This is useful when you want to handle the failure globally
3088
+ (e.g., via an exception listener)::
3089
+
3090
+ // src/Controller/SomeController.php
3091
+ // ...
3092
+
3093
+ use App\Security\Attribute\IsSignatureValid;
3094
+
3095
+ #[IsSignatureValid(throw: true)]
3096
+ public function someAction(): Response
3097
+ {
3098
+ // ...
3099
+ }
3100
+
3101
+ You can also apply ``#[IsSignatureValid] `` at the controller class level.
3102
+ This way, all actions within the controller will automatically
3103
+ be protected by signature validation::
3104
+
3105
+ // src/Controller/SecureController.php
3106
+ // ...
3107
+
3108
+ use App\Security\Attribute\IsSignatureValid;
3109
+
3110
+ #[IsSignatureValid]
3111
+ class SecureController extends AbstractController
3112
+ {
3113
+ public function index(): Response
3114
+ {
3115
+ // ...
3116
+ }
3117
+
3118
+ public function submit(): Response
3119
+ {
3120
+ // ...
3121
+ }
3122
+ }
3123
+
3124
+
3125
+ This attribute provides a declarative way to enforce request signature validation directly
3126
+ at the controller level, helping to keep your security logic consistent and maintainable.
3127
+
3128
+ .. versionadded :: 7.4
3129
+
3130
+ The ``#[IsSignatureValid] `` attribute was introduced in Symfony 7.4.
3131
+
3034
3132
Troubleshooting
3035
3133
---------------
3036
3134
0 commit comments