Skip to content

Commit 162dfe2

Browse files
[Messenger] Add support for message signing
1 parent c805014 commit 162dfe2

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

messenger.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,88 @@ contains many useful information such as the exit code or the output of the
24172417
process. You can refer to the page dedicated on
24182418
:ref:`handler results <messenger-getting-handler-results>` for more information.
24192419

2420+
Securing Messages with Signatures
2421+
---------------------------------
2422+
2423+
When messages are sent to message queues, there's a potential security risk
2424+
if an attacker can inject forged payloads into the queue. While message queues
2425+
should be secured to prevent unauthorized access, Symfony provides an additional
2426+
layer of security by supporting message signing.
2427+
2428+
This is especially important for handlers that execute commands or processes,
2429+
which is why the ``RunProcessHandler`` has message signing **enabled by default**.
2430+
2431+
Enabling Message Signing
2432+
~~~~~~~~~~~~~~~~~~~~~~~~
2433+
2434+
To enable message signing for your handler, set the ``sign`` option to ``true``:
2435+
2436+
.. configuration-block::
2437+
2438+
.. code-block:: php-attributes
2439+
2440+
// src/MessageHandler/SmsNotificationHandler.php
2441+
namespace App\MessageHandler;
2442+
2443+
use App\Message\SmsNotification;
2444+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2445+
2446+
#[AsMessageHandler(sign: true)]
2447+
class SmsNotificationHandler
2448+
{
2449+
public function __invoke(SmsNotification $message): void
2450+
{
2451+
// ... handle message
2452+
}
2453+
}
2454+
2455+
.. code-block:: yaml
2456+
2457+
# config/services.yaml
2458+
services:
2459+
App\MessageHandler\SmsNotificationHandler:
2460+
tags:
2461+
- { name: messenger.message_handler, sign: true }
2462+
2463+
.. code-block:: xml
2464+
2465+
<!-- config/services.xml -->
2466+
<?xml version="1.0" encoding="UTF-8" ?>
2467+
<container xmlns="http://symfony.com/schema/dic/services"
2468+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2469+
xsi:schemaLocation="http://symfony.com/schema/dic/services
2470+
https://symfony.com/schema/dic/services/services-1.0.xsd">
2471+
2472+
<services>
2473+
<service id="App\MessageHandler\SmsNotificationHandler">
2474+
<tag name="messenger.message_handler" sign="true"/>
2475+
</service>
2476+
</services>
2477+
</container>
2478+
2479+
.. code-block:: php
2480+
2481+
// config/services.php
2482+
use App\MessageHandler\SmsNotificationHandler;
2483+
2484+
$container->register(SmsNotificationHandler::class)
2485+
->addTag('messenger.message_handler', ['sign' => true]);
2486+
2487+
When signing is enabled:
2488+
2489+
1. Messages are signed using an HMAC signature computed with your application's
2490+
secret key (``kernel.secret`` parameter).
2491+
2. The signature is added to the message headers (``Body-Sign`` and ``Sign-Algo``)
2492+
when the message is sent to a transport.
2493+
3. When the message is received and decoded, the signature is automatically verified.
2494+
4. If the signature is missing or invalid, an
2495+
:class:`Symfony\\Component\\Messenger\\Exception\\InvalidMessageSignatureException`
2496+
is thrown and the message will not be handled.
2497+
2498+
.. versionadded:: 7.4
2499+
2500+
The support for message signing was introduced in Symfony 7.4.
2501+
24202502
Pinging A Webservice
24212503
--------------------
24222504

@@ -2663,6 +2745,15 @@ Possible options to configure with tags are:
26632745
Defines the order in which the handler is executed when multiple handlers
26642746
can process the same message; those with higher priority run first.
26652747

2748+
``sign``
2749+
Whether messages handled by this handler should be cryptographically signed
2750+
to prevent tampering. When enabled, messages are signed using HMAC with the
2751+
application's secret key. Default: ``false``.
2752+
2753+
.. versionadded:: 7.4
2754+
2755+
The ``sign`` option was introduced in Symfony 7.4.
2756+
26662757
.. _handler-subscriber-options:
26672758

26682759
Handling Multiple Messages

0 commit comments

Comments
 (0)