Skip to content

htmlparser unclosed script tag causes data loss #86155

Description

@waylan
mannequin
BPO 41989
Nosy @terryjreedy, @ezio-melotti, @waylan
PRs
  • gh-86155: Fix htmlparser "unclosed script tag causes data loss" #22658
  • Files
  • test_html.py: A simple test
  • 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 = 'https://github.com/ezio-melotti'
    closed_at = None
    created_at = <Date 2020-10-10.01:08:29.514>
    labels = ['type-bug', 'library', '3.10']
    title = 'htmlparser unclosed script tag causes data loss'
    updated_at = <Date 2020-10-16.20:42:44.634>
    user = 'https://github.com/waylan'

    bugs.python.org fields:

    activity = <Date 2020-10-16.20:42:44.634>
    actor = 'terry.reedy'
    assignee = 'ezio.melotti'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-10-10.01:08:29.514>
    creator = 'waylan'
    dependencies = []
    files = ['49505']
    hgrepos = []
    issue_num = 41989
    keywords = ['patch']
    message_count = 2.0
    messages = ['378359', '378748']
    nosy_count = 3.0
    nosy_names = ['terry.reedy', 'ezio.melotti', 'waylan']
    pr_nums = ['22658']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41989'
    versions = ['Python 3.10']

    Linked PRs

    Activity

    1. waylan commented on Oct 10, 2020

      waylanmannequin
      MannequinAuthor

      When the close method of the HtmlParser is called, any cached text data is generally flushed and passed to a data event; except when in data_mode. Specifically, if an unclosed script or style tag has been encountered, a call to close does not flush the data.

      A simple test which demonstrates the issue is attached.

      I see that in Lib/html/parser.py#L244-L249 there are two nested if statements which both check for not self.cdata_elem. Obviously, if we got past the first one, that situation will never exist for the nested one. Somehow this block of code needs a branch for when self.cdata_elem is True.

      I should note that the input is invalid HTML. However, the existing behavior results in data loss. Within any other unclosed tag (other than script or style) any data is still flushed and passed to a data event. I would expect the same behavior here. Although, the data escaping behavior should perhaps be applied as it is with data within properly closed tags.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Oct 10, 2020
    3. 18 remaining items

    4. added 2 commits that reference this issue on May 10, 2025
    5. added a commit that references this issue on Jul 4, 2025
    6. added a commit that references this issue on Jul 4, 2025
    7. added 3 commits that reference this issue on Jul 4, 2025
    8. added 3 commits that reference this issue on Jul 12, 2025
    9. added a commit that references this issue on Jul 12, 2025
    10. added a commit that references this issue on Jul 30, 2025
    11. added a commit that references this issue on Aug 4, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions