Skip to content

bpo-30681: Support invalid date format or value in email Date header #10783

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
wants to merge 4 commits into from

Conversation

timb07
Copy link
Contributor

@timb07 timb07 commented Nov 29, 2018

In email.utils.parsedate_to_datetime(), a failure to parse the date, or invalid date components (such as hour outside 0..23) raises an exception. Document this behaviour, and add tests to test_email/test_utils.py to confirm this behaviour.

In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() raises an exception and add a new defect InvalidDateDefect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.

Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.

This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon, @Mariatta and @warsaw, and replaces the earlier PR #2254.

https://bugs.python.org/issue30681

Copy link

@hobbestigrou hobbestigrou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for me. I agree.

Copy link
Contributor

@maxking maxking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment for documenting the new defect.

Also, @bitdancer @warsaw I think this addresses all the comments on bpo-30681 and previous PR.

Thanks @timb07 for fixing this :)

@@ -108,3 +108,6 @@ class NonASCIILocalPartDefect(HeaderDefect):
"""local_part contains non-ASCII characters"""
# This defect only occurs during unicode parsing, not when
# parsing messages decoded from binary.

class InvalidDateDefect(HeaderDefect):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also please add this to documentation under email.errors.rst ?

@@ -48,6 +48,22 @@ def test_parsedate_to_datetime_naive(self):
utils.parsedate_to_datetime(self.datestring + ' -0000'),
self.naive_dt)

def test_parsedate_to_datetime_with_invalid_raises_typeerror(self):
with self.assertRaises(TypeError):
Copy link
Member

@pganssle pganssle Jun 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine as is, but I think it would be improved if you used a loop and subtests:

invalid_dates = ['', '0', 'A Complete Waste of Time']
for dtstr in invalid_dates:
    with self.subTest(dtstr=dtstr):
        with self.assertRaises(TypeError):
            utils.parsedate_to_datetime(dtstr)

Obviously that's pretty nested (though you can probably cut out one layer of nesting by using self.assertRaises(TypeError, utils.parsedate_to_datetime, dtstr) rather than the context manager - I still prefer the context manager, myself), so I suppose it's a matter of taste whether DRY and cleaner separation of test cases matters more than keeping code flat.

Same comment also applies to the test for ValueError below.

@pganssle
Copy link
Member

Very nice PR @timb07, thanks for this! Other than addressing @maxking's comment about the documentation I have no objections to this. I made one suggestion about some of the tests, but I consider it a matter of taste and not something that should block merging.

@csabella
Copy link
Contributor

csabella commented Jul 3, 2019

Hi @timb07, just a friendly ping. Looks like if you can address the code reviews, then this will be merged. Thanks!

@csabella
Copy link
Contributor

This PR was very close to being merged, but seems to have been abandoned, so I am going to close it. It can be reopened if the original author is still interested in it or a new PR can be created to replace it. If someone else creates a new PR, please credit the original author in the comment message.

@csabella csabella closed this Jan 25, 2020
miss-islington pushed a commit that referenced this pull request Oct 27, 2020
…H-22090)

I am re-submitting an older PR which was abandoned but is still relevant, #10783 by @timb07.

The issue being solved () is still relevant. The original PR #10783 was closed as
the final request changes were not applied and since abandoned.

In this new PR I have re-used the original patch plus applied both comments from the review, by @maxking and @pganssle.


For reference, here is the original PR description:
In email.utils.parsedate_to_datetime(), a failure to parse the date, or invalid date components (such as hour outside 0..23) raises an exception. Document this behaviour, and add tests to test_email/test_utils.py to confirm this behaviour.

In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() raises an exception and add a new defect InvalidDateDefect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.

Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.

This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon, @Mariatta and @warsaw, and replaces the earlier PR #2254.

Automerge-Triggered-By: GH:warsaw
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
…ythonGH-22090)

I am re-submitting an older PR which was abandoned but is still relevant, python#10783 by @timb07.

The issue being solved () is still relevant. The original PR python#10783 was closed as
the final request changes were not applied and since abandoned.

In this new PR I have re-used the original patch plus applied both comments from the review, by @maxking and @pganssle.


For reference, here is the original PR description:
In email.utils.parsedate_to_datetime(), a failure to parse the date, or invalid date components (such as hour outside 0..23) raises an exception. Document this behaviour, and add tests to test_email/test_utils.py to confirm this behaviour.

In email.headerregistry.DateHeader.parse(), check when parsedate_to_datetime() raises an exception and add a new defect InvalidDateDefect; preserve the invalid value as the string value of the header, but set the datetime attribute to None.

Add tests to test_email/test_headerregistry.py to confirm this behaviour; also added test to test_email/test_inversion.py to confirm emails with such defective date headers round trip successfully.

This pull request incorporates feedback gratefully received from @bitdancer, @brettcannon, @Mariatta and @warsaw, and replaces the earlier PR python#2254.

Automerge-Triggered-By: GH:warsaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants