Navigation Menu

Skip to content
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

email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it #89402

Closed
benhoyt mannequin opened this issue Sep 18, 2021 · 6 comments
Closed

email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it #89402

benhoyt mannequin opened this issue Sep 18, 2021 · 6 comments
Labels
3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@benhoyt
Copy link
Mannequin

benhoyt mannequin commented Sep 18, 2021

BPO 45239
Nosy @warsaw, @benhoyt, @ambv, @miss-islington
PRs
  • bpo-45239: Fix parsedate_tz when time has more than 2 dots in it #28452
  • [3.9] bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) #28928
  • [3.10] bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) #28930
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-13.17:12:48.420>
    created_at = <Date 2021-09-18.22:35:53.284>
    labels = ['type-bug', 'library', '3.10', '3.11']
    title = 'email.utils.parsedate_tz raises UnboundLocalError if time has more than 2 dots in it'
    updated_at = <Date 2021-10-13.17:12:48.418>
    user = 'https://github.com/benhoyt'

    bugs.python.org fields:

    activity = <Date 2021-10-13.17:12:48.418>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-13.17:12:48.420>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2021-09-18.22:35:53.284>
    creator = 'benhoyt'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45239
    keywords = ['patch']
    message_count = 6.0
    messages = ['402140', '402141', '403847', '403852', '403856', '403857']
    nosy_count = 4.0
    nosy_names = ['barry', 'benhoyt', 'lukasz.langa', 'miss-islington']
    pr_nums = ['28452', '28928', '28930']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45239'
    versions = ['Python 3.10', 'Python 3.11']

    @benhoyt
    Copy link
    Mannequin Author

    benhoyt mannequin commented Sep 18, 2021

    In going through some standard library code, I found that the email.utils.parsedate_tz() function (https://docs.python.org/3/library/email.utils.html#email.utils.parsedate_tz) has a bug if the time value is in dotted format and has more than 2 dots in it, for example: "12.34.56.78". Per the docs, it should return None in the case of invalid input.

    This is happening because in the case that handles the '.' separator (instead of the normal ':'), there's no else clause to return None when there's not 2 or 3 segments.

    From

    if len(tm) == 2:
    [thh, tmm] = tm
    tss = '0'
    elif len(tm) == 3:
    [thh, tmm, tss] = tm
    elif len(tm) == 1 and '.' in tm[0]:
    # Some non-compliant MUAs use '.' to separate time elements.
    tm = tm[0].split('.')
    if len(tm) == 2:
    [thh, tmm] = tm
    tss = 0
    elif len(tm) == 3:
    [thh, tmm, tss] = tm
    else:
    return None
    :

        if len(tm) == 2:
            [thh, tmm] = tm
            tss = '0'
        elif len(tm) == 3:
            [thh, tmm, tss] = tm
        elif len(tm) == 1 and '.' in tm[0]:
            # Some non-compliant MUAs use '.' to separate time elements.
            tm = tm[0].split('.')
            if len(tm) == 2:
                [thh, tmm] = tm
                tss = 0
            elif len(tm) == 3:
                [thh, tmm, tss] = tm
            # HERE: need "else: return None"
        else:
            return None

    We simply need to include that additional "else: return None" block in the '.' handling case.

    I'll submit a pull request that fixes this soon (and adds a test for this case).

    @benhoyt benhoyt mannequin added 3.10 only security fixes stdlib Python modules in the Lib dir labels Sep 18, 2021
    @benhoyt
    Copy link
    Mannequin Author

    benhoyt mannequin commented Sep 18, 2021

    For reference, here's a repro case:

    $ python3.10 -c 'import email.utils; \
        email.utils.parsedate_tz("Wed, 3 Apr 2002 12.34.56.78+0800")'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.10/email/_parseaddr.py", line 50, in parsedate_tz
        res = _parsedate_tz(data)
      File "/usr/local/lib/python3.10/email/_parseaddr.py", line 134, in _parsedate_tz
        thh = int(thh)
    UnboundLocalError: local variable 'thh' referenced before assignment

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset b9e6876 by Ben Hoyt in branch 'main':
    bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452)
    b9e6876

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset 5638618 by Miss Islington (bot) in branch '3.9':
    bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) (GH-28928)
    5638618

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    New changeset f8473f6 by Łukasz Langa in branch '3.10':
    [3.10] bpo-45239: Fix parsedate_tz when time has more than 2 dots in it (GH-28452) (GH-28930)
    f8473f6

    @ambv
    Copy link
    Contributor

    ambv commented Oct 13, 2021

    Thanks, Ben! ✨ 🍰 ✨

    @ambv ambv added the 3.11 only security fixes label Oct 13, 2021
    @ambv ambv closed this as completed Oct 13, 2021
    @ambv ambv added type-bug An unexpected behavior, bug, or error 3.11 only security fixes labels Oct 13, 2021
    @ambv ambv closed this as completed Oct 13, 2021
    @ambv ambv added the type-bug An unexpected behavior, bug, or error label Oct 13, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant