Skip to content

Commit d6fa9a8

Browse files
bug symfony#59376 [RemoteEvent][Webhook] Fix SendgridRequestParser and SendgridPayloadConverter (ericabouaf)
This PR was merged into the 6.4 branch. Discussion ---------- [RemoteEvent][Webhook] Fix `SendgridRequestParser` and `SendgridPayloadConverter` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT According to https://www.twilio.com/docs/sendgrid/for-developers/tracking-events/event#event-objects, not all webhook events contains a `sg_message_id` in the payload. In the case of a delayed or [asynchronous bounce](https://www.twilio.com/docs/sendgrid/ui/sending-email/bounces#asynchronous-bounces), the message ID will be unavailable. The current implementation rejects the webhook call with "Payload is malformed". We should use the always present `sg_event_id` instead. For BC reasons, I did not want to change the id of the RemoteEvent, so I kept `sg_message_id` if present, but fallback to `sg_event_id` instead so that the webhook is not rejected. Commits ------- 93a4398 [RemoteEvent][Webhook] fix SendgridRequestParser & SendgridPayloadConverter in case of missing sg_message_id
2 parents 82011ee + 93a4398 commit d6fa9a8

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Symfony/Component/Mailer/Bridge/Sendgrid/RemoteEvent/SendgridPayloadConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function convert(array $payload): AbstractMailerEvent
3131
'deferred' => MailerDeliveryEvent::DEFERRED,
3232
'bounce' => MailerDeliveryEvent::BOUNCE,
3333
};
34-
$event = new MailerDeliveryEvent($name, $payload['sg_message_id'], $payload);
34+
$event = new MailerDeliveryEvent($name, $payload['sg_message_id'] ?? $payload['sg_event_id'], $payload);
3535
$event->setReason($payload['reason'] ?? '');
3636
} else {
3737
$name = match ($payload['event']) {
@@ -41,7 +41,7 @@ public function convert(array $payload): AbstractMailerEvent
4141
'spamreport' => MailerEngagementEvent::SPAM,
4242
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['event'])),
4343
};
44-
$event = new MailerEngagementEvent($name, $payload['sg_message_id'], $payload);
44+
$event = new MailerEngagementEvent($name, $payload['sg_message_id'] ?? $payload['sg_event_id'], $payload);
4545
}
4646

4747
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['timestamp'])) {

src/Symfony/Component/Mailer/Bridge/Sendgrid/Tests/RemoteEvent/SendgridPayloadConverterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,19 @@ public function testInvalidDate()
9797
'email' => 'test@example.com',
9898
]);
9999
}
100+
101+
public function testAsynchronousBounce()
102+
{
103+
$converter = new SendgridPayloadConverter();
104+
105+
$event = $converter->convert([
106+
'event' => 'bounce',
107+
'sg_event_id' => '123456',
108+
'timestamp' => '123456789',
109+
'email' => 'test@example.com',
110+
]);
111+
112+
$this->assertInstanceOf(MailerDeliveryEvent::class, $event);
113+
$this->assertSame('123456', $event->getId());
114+
}
100115
}

src/Symfony/Component/Mailer/Bridge/Sendgrid/Webhook/SendgridRequestParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function doParse(Request $request, string $secret): ?AbstractMailerEve
4848
!isset($content[0]['email'])
4949
|| !isset($content[0]['timestamp'])
5050
|| !isset($content[0]['event'])
51-
|| !isset($content[0]['sg_message_id'])
51+
|| !isset($content[0]['sg_event_id'])
5252
) {
5353
throw new RejectWebhookException(406, 'Payload is malformed.');
5454
}

0 commit comments

Comments
 (0)