Skip to content

Commit 8079714

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweaks [Messenger] Add support for message signing
2 parents 0ede18f + 59bb782 commit 8079714

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
@@ -2009,6 +2009,88 @@ contains many useful information such as the exit code or the output of the
20092009
process. You can refer to the page dedicated on
20102010
:ref:`handler results <messenger-getting-handler-results>` for more information.
20112011

2012+
Securing Messages with Signatures
2013+
---------------------------------
2014+
2015+
When messages are sent to message queues, there is a potential security risk
2016+
if an attacker injects forged payloads into the queue. Although message queues
2017+
should be properly secured to prevent unauthorized access, Symfony adds an extra
2018+
layer of protection by supporting message signing.
2019+
2020+
This is particularly important for handlers that execute commands or processes,
2021+
which is why the ``RunProcessHandler`` has message signing **enabled by default**.
2022+
2023+
Enabling Message Signing
2024+
~~~~~~~~~~~~~~~~~~~~~~~~
2025+
2026+
To enable message signing for your handler, set the ``sign`` option to ``true``:
2027+
2028+
.. configuration-block::
2029+
2030+
.. code-block:: php-attributes
2031+
2032+
// src/MessageHandler/SmsNotificationHandler.php
2033+
namespace App\MessageHandler;
2034+
2035+
use App\Message\SmsNotification;
2036+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
2037+
2038+
#[AsMessageHandler(sign: true)]
2039+
class SmsNotificationHandler
2040+
{
2041+
public function __invoke(SmsNotification $message): void
2042+
{
2043+
// ... handle message
2044+
}
2045+
}
2046+
2047+
.. code-block:: yaml
2048+
2049+
# config/services.yaml
2050+
services:
2051+
App\MessageHandler\SmsNotificationHandler:
2052+
tags:
2053+
- { name: messenger.message_handler, sign: true }
2054+
2055+
.. code-block:: xml
2056+
2057+
<!-- config/services.xml -->
2058+
<?xml version="1.0" encoding="UTF-8" ?>
2059+
<container xmlns="http://symfony.com/schema/dic/services"
2060+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2061+
xsi:schemaLocation="http://symfony.com/schema/dic/services
2062+
https://symfony.com/schema/dic/services/services-1.0.xsd">
2063+
2064+
<services>
2065+
<service id="App\MessageHandler\SmsNotificationHandler">
2066+
<tag name="messenger.message_handler" sign="true"/>
2067+
</service>
2068+
</services>
2069+
</container>
2070+
2071+
.. code-block:: php
2072+
2073+
// config/services.php
2074+
use App\MessageHandler\SmsNotificationHandler;
2075+
2076+
$container->register(SmsNotificationHandler::class)
2077+
->addTag('messenger.message_handler', ['sign' => true]);
2078+
2079+
When signing is enabled:
2080+
2081+
1. Messages are signed using an HMAC signature computed with your application's
2082+
secret key (``kernel.secret`` parameter).
2083+
2. The signature is added to the message headers (``Body-Sign`` and ``Sign-Algo``)
2084+
when the message is sent to a transport.
2085+
3. When the message is received and decoded, the signature is automatically verified.
2086+
4. If the signature is missing or invalid, an
2087+
:class:`Symfony\\Component\\Messenger\\Exception\\InvalidMessageSignatureException`
2088+
is thrown, and the message will not be handled.
2089+
2090+
.. versionadded:: 7.4
2091+
2092+
Message signing support was introduced in Symfony 7.4.
2093+
20122094
Pinging A Webservice
20132095
--------------------
20142096

@@ -2233,6 +2315,15 @@ Possible options to configure with tags are:
22332315
Defines the order in which the handler is executed when multiple handlers
22342316
can process the same message; those with higher priority run first.
22352317

2318+
``sign``
2319+
Whether messages handled by this handler should be cryptographically signed
2320+
to prevent tampering. When enabled, messages are signed using HMAC with the
2321+
application's secret key. Default: ``false``.
2322+
2323+
.. versionadded:: 7.4
2324+
2325+
The ``sign`` option was introduced in Symfony 7.4.
2326+
22362327
.. _handler-subscriber-options:
22372328

22382329
Handling Multiple Messages

0 commit comments

Comments
 (0)