Skip to content

Commit 18f3c78

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [FrameworkBundle] Simplify usage of the Target attribute [HttpFoundation] Add documentation for #[IsSignatureValid] attribute with usage examples and options
2 parents db273ba + 556bfb0 commit 18f3c78

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

lock.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ For example, to inject the ``invoice`` package defined earlier::
315315

316316
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
317317
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
318-
a target called "asset package name" + ``.lock.factory`` suffix.
318+
a target with the same name as the lock.
319319

320320
For example, to select the ``invoice`` lock defined earlier::
321321

@@ -325,8 +325,13 @@ For example, to select the ``invoice`` lock defined earlier::
325325
class SomeService
326326
{
327327
public function __construct(
328-
#[Target('invoice.lock.factory')] private LockFactory $lockFactory
328+
#[Target('invoice')] private LockFactory $lockFactory
329329
): void {
330330
// ...
331331
}
332332
}
333+
334+
.. versionadded:: 7.4
335+
336+
Before Symfony 7.4, the target name had to include the ``.lock.factory``
337+
suffix (e.g. ``#[Target('invoice.lock.factory')]``).

rate_limiter.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ argument named ``$anonymousApiLimiter``::
257257

258258
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
259259
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
260-
a target called "rate limiter name" + ``.limiter`` suffix.
260+
a target with the same name as the rate limiter.
261261

262262
For example, to select the ``anonymous_api`` limiter defined earlier, use
263263
``anonymous_api.limiter`` as the target::
@@ -268,7 +268,7 @@ For example, to select the ``anonymous_api`` limiter defined earlier, use
268268
class ApiController extends AbstractController
269269
{
270270
public function index(
271-
#[Target('anonymous_api.limiter')] RateLimiterFactoryInterface $rateLimiter
271+
#[Target('anonymous_api')] RateLimiterFactoryInterface $rateLimiter
272272
): Response
273273
{
274274
// ...

reference/configuration/framework.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ to inject the ``foo_package`` package defined earlier::
263263

264264
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
265265
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
266-
a target called "asset package name" + ``.package`` suffix.
266+
a target with the same name as the asset package.
267267

268268
For example, to select the ``foo_package`` package defined earlier::
269269

@@ -273,12 +273,17 @@ For example, to select the ``foo_package`` package defined earlier::
273273
class SomeService
274274
{
275275
public function __construct(
276-
#[Target('foo_package.package')] private PackageInterface $package
276+
#[Target('foo_package')] private PackageInterface $package
277277
): void {
278278
// ...
279279
}
280280
}
281281

282+
.. versionadded:: 7.4
283+
284+
Before Symfony 7.4, the target name had to include the ``.package``
285+
suffix (e.g. ``#[Target('foo_package.package')]``).
286+
282287
.. _reference-framework-assets-packages:
283288

284289
packages

routing.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,67 @@ If you need to know the reason why a signed URI is invalid, you can use the
30873087
expirations. This allows you to :ref:`mock the current time in your tests
30883088
<clock_writing-tests>`.
30893089

3090+
Another way to validate incoming requests is to use the ``#[IsSignatureValid]`` attribute.
3091+
3092+
In the following example, all incoming requests to this controller action will be verified for
3093+
a valid signature. If the signature is missing or invalid,
3094+
a ``SignedUriException`` will be thrown::
3095+
3096+
// src/Controller/SomeController.php
3097+
// ...
3098+
3099+
use App\Security\Attribute\IsSignatureValid;
3100+
3101+
#[IsSignatureValid]
3102+
public function someAction(): Response
3103+
{
3104+
// ...
3105+
}
3106+
3107+
To restrict signature validation to specific HTTP methods,
3108+
use the ``methods`` argument. This can be a string or an array of methods::
3109+
3110+
// Only validate POST requests
3111+
#[IsSignatureValid(methods: 'POST')]
3112+
public function createItem(): Response
3113+
{
3114+
// ...
3115+
}
3116+
3117+
// Validate both POST and PUT requests
3118+
#[IsSignatureValid(methods: ['POST', 'PUT'])]
3119+
public function updateItem(): Response
3120+
{
3121+
// ...
3122+
}
3123+
3124+
You can also apply ``#[IsSignatureValid]`` at the controller class level.
3125+
This way, all actions within the controller will automatically
3126+
be protected by signature validation::
3127+
3128+
// src/Controller/SecureController.php
3129+
// ...
3130+
3131+
use App\Security\Attribute\IsSignatureValid;
3132+
3133+
#[IsSignatureValid]
3134+
class SecureController extends AbstractController
3135+
{
3136+
public function index(): Response
3137+
{
3138+
// ...
3139+
}
3140+
3141+
public function submit(): Response
3142+
{
3143+
// ...
3144+
}
3145+
}
3146+
3147+
3148+
This attribute provides a declarative way to enforce request signature validation directly
3149+
at the controller level, helping to keep your security logic consistent and maintainable.
3150+
30903151
Troubleshooting
30913152
---------------
30923153

0 commit comments

Comments
 (0)