8000 Bug in MailgunPayloadConverter.php · Issue #51522 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Bug in MailgunPayloadConverter.php #51522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ovgray opened this issue Aug 30, 2023 · 2 comments
Closed

Bug in MailgunPayloadConverter.php #51522

ovgray opened this issue Aug 30, 2023 · 2 comments

Comments

@ovgray
Copy link
Contributor
ovgray commented Aug 30, 2023

Symfony version(s) affected

6.3

Description

MailgunPayloadConverter threw a RejectWebhookException ("Invalid date") when converting a valid Mailgun webhook in which the payload's timestamp value was 1693425964.0000026. json_encode changed that value to 1693425964.0, and the exception occurred because in line 53 the code

\DateTimeImmutable::createFromFormat('U.u', $payload['timestamp']

did not return a DateTimeImmutable.

How to reproduce

run

MailgunPayloadConverter::convert($payload)

with a payload containing a timestamp with a microsecond value less than 000050.

Possible Solution

Replace line 53 with

if (!$date = \DateTimeImmutable::createFromFormat('U.u', number_format($payload['timestamp'], 6, '.', ''))) {

Additional Context

php is version 8,1

@fabpot
Copy link
Member
fabpot commented Aug 31, 2023

Can you submit a PR to fix this bug?

@xabbuh xabbuh added the Mailer label Aug 31, 2023
@ovgray
Copy link
Contributor Author
ovgray commented Aug 31, 2023

@fabpot I have not submitted a PR before, but I will give it a try.

Meanwhile, I'll note here that replacing line 53 with

if (!$date = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F',$payload['timestamp']))) {

should also work.

@fabpot fabpot closed this as completed Sep 29, 2023
fabpot added a commit that referenced this issue Sep 29, 2023
…nt date value (DateTimeImmutable) in MailgunPayloadConverter (ovgray)

This PR was merged into the 6.3 branch.

Discussion
----------

[Mailer] [Mailgun] fix parsing of payload timestamp to event date value (DateTimeImmutable) in MailgunPayloadConverter

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| Tickets       | Fix #51522
| License       | MIT

Fix parsing of $payload['timestamp'] in Mailer/Bridge/Mailgun/RemoteEvent/MailgunPayloadConverter.php

Details:
MailgunPayloadConverter uses

`\DateTimeImmutable::createFromFormat('U.u', $payload['timestamp'])`

in line 53 to convert the payload timestamp value to a DateTimeImmutable.

`\DateTimeImmutable::createFromFormat expects a string as its second parameter. $payload['timestamp'] is a float.
`\DateTimeImmutable::createFromFormat will try to convert a float to a string, but does not always provide a string that the function then  recognizes as being in 'U.u' format.

This bugfix replaces $payload['timestamp'] with sprintf('%.6F', $payload['timestamp']) in MailgunPayloadConverter, lines 53 and 54.

It also changes the timestamp in  Mailer/Bridge/Mailgun/Tests/Webhook/Fixtures/delivered_messages.json to 1521472262.000002, which provides a case in which \DateTimeImmutable::createFromFormat('U.u', $payload['timestamp']) returns false (at least in my setup). Floats are replaced with strings in calls to  \DateTimeImmutable::createFromFormat in Mailer/Bridge/Mailgun/Tests/Webhook/Fixtures/*.php

Commits
-------

3225112 fix parsing of payload timestamp to DateTimeImmutable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0