Skip to content

Conversation

@encukou
Copy link
Member

@encukou encukou commented Dec 16, 2025

Element uses __slots__, which means that the ownerDocument
attribute is not inherited from the superclass.
All other slots (except _localName) are set in __init__; not
including ownerDocument was apparently an oversight.

Note that creating Element directly is not supported; you're supposed
to use Document APIs. AFAICS, all supported ways of creating elements do
set ownerDocument.
However, many projects ignore the documentation, and a security fix that
relies on ownerDocument can break them.

Element uses __slots__, which means that the attribute is not inherited
from the superclass.
All other slots (except _localName) are set in __init__; not including
ownerDocument was an oversight.

Note that creating Element directly is not supported; you're supposed
to use Document APIs. All supported ways of creating elements do set
ownerDocument.
However, many projects ignore the documentation.
@encukou encukou changed the title gh-142754: Ensure that Elements have the ownerDocument attribute gh-142754: Ensure that Element & Attr instances have the ownerDocument attribute Dec 16, 2025
@hugovk
Copy link
Member

hugovk commented Dec 16, 2025

Here's a test case:

    def testSetAttributeNodeWithoutOwnerDocument(self):
        elem = Element("test")
        attr = Attr("id")
        attr.value = "test-id"

        elem.setAttributeNode(attr)

        self.assertEqual(elem.getAttribute("id"), "test-id")

@encukou encukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Dec 16, 2025
@hugovk hugovk enabled auto-merge (squash) December 16, 2025 11:59
@hugovk hugovk merged commit 1cc7551 into python:main Dec 16, 2025
50 checks passed
@miss-islington-app
Copy link

Thanks @encukou for the PR, and @hugovk for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 16, 2025
…ocument attribute (pythonGH-142794)

(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 16, 2025
…ocument attribute (pythonGH-142794)

(cherry picked from commit 1cc7551)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 16, 2025

GH-142818 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Dec 16, 2025
@bedevere-app
Copy link

bedevere-app bot commented Dec 16, 2025

GH-142819 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Dec 16, 2025
@hugovk
Copy link
Member

hugovk commented Dec 16, 2025

Thanks for the fix!

@jacobtylerwalls
Copy link
Contributor

Lovely. Does this also close #67624?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants