Title: Add serialized XML interface to avoid libxml2 ABI conflicts with lxml · Issue #356 · xmlsec/python-xmlsec · GitHub
Open Graph Title: Add serialized XML interface to avoid libxml2 ABI conflicts with lxml · Issue #356 · xmlsec/python-xmlsec
X Title: Add serialized XML interface to avoid libxml2 ABI conflicts with lxml · Issue #356 · xmlsec/python-xmlsec
Description: python-xmlsec currently relies on passing raw xmlNodePtr objects between lxml (which builds on libxml2) and xmlsec1 (which also uses libxml2). This creates a fragile situation where different versions of libxml2 may be loaded into the sa...
Open Graph Description: python-xmlsec currently relies on passing raw xmlNodePtr objects between lxml (which builds on libxml2) and xmlsec1 (which also uses libxml2). This creates a fragile situation where different versi...
X Description: python-xmlsec currently relies on passing raw xmlNodePtr objects between lxml (which builds on libxml2) and xmlsec1 (which also uses libxml2). This creates a fragile situation where different versi...
Opengraph URL: https://github.com/xmlsec/python-xmlsec/issues/356
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add serialized XML interface to avoid libxml2 ABI conflicts with lxml","articleBody":"`python-xmlsec` currently relies on passing raw `xmlNodePtr` objects between `lxml` (which builds on libxml2) and `xmlsec1` (which also uses libxml2). This creates a fragile situation where different versions of `libxml2` may be loaded into the same process, leading to:\n\n- Segfaults or memory corruption due to incompatible struct layouts \n- Invalid memory free errors (e.g., double-free or mismatched allocators) \n- Signature verification failures caused by inconsistent parser state \n- Undefined behavior from mismatched `libxml2` global configuration \n\nThis occurs because:\n\n- `lxml` **bundles its own `libxml2` and `libxslt`** (especially in binary wheels) to ease installation for users on Windows, macOS, and some Linux platforms.\n- `python-xmlsec` binds to **`xmlsec1`**, which in turn links to the **system's `libxml2`**.\n- Pointers like `xmlNodePtr` created by `lxml` are then passed to `python-xmlsec` functions like `tree.find_node()` or `SignatureContext.sign()`.\n\nIf the `libxml2` versions are not ABI-compatible, this can easily lead to crashes, unpredictable behavior, or memory corruption.\n\n---\n\n## Proposed Solution: Decoupling via Canonicalized XML\n\nInstead of passing `xmlNodePtr` from `lxml` to `python-xmlsec`, we should support passing **serialized XML (as `bytes`)**, ideally using [Canonical XML (C14N)](https://www.w3.org/TR/xml-c14n) where appropriate. This isolates the XML parsing and memory management between the two libraries.\n\n### Example Usage\n\n```python\nfrom lxml import etree\nimport xmlsec\n\ndoc = etree.fromstring(\"\u003cRoot\u003e\u003cSignature/\u003e\u003c/Root\u003e\")\nc14n_bytes = etree.tostring(doc, method=\"c14n\", exclusive=True)\n\n# Proposed new API:\nsigned_bytes = xmlsec.sign_serialized(c14n_bytes, key_file=\"key.pem\")\n\n# Parse back with lxml if needed\nsigned_doc = etree.fromstring(signed_bytes)","author":{"url":"https://github.com/mxamin","@type":"Person","name":"mxamin"},"datePublished":"2025-07-10T19:06:31.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/356/python-xmlsec/issues/356"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:df6a37ff-20aa-7d5a-2459-939887669e65 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B9CA:33C049:EF5976:14789FD:6969F384 |
| html-safe-nonce | 2d5df26a91a827c499a1e7c37890ad819c851030507e0fcc995b4efa81f660cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOUNBOjMzQzA0OTpFRjU5NzY6MTQ3ODlGRDo2OTY5RjM4NCIsInZpc2l0b3JfaWQiOiIxNTM3NzMwMzEyNDQxMjk1NzQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 506319e771fc75b7060f1375f3489568488665fe17107791f82740fa94435283 |
| hovercard-subject-tag | issue:3220381392 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/xmlsec/python-xmlsec/356/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b182381aa3779f75e02f840bfa449315ac33af0d3215ef33c1c18bcc2b4026fe/xmlsec/python-xmlsec/issues/356 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b182381aa3779f75e02f840bfa449315ac33af0d3215ef33c1c18bcc2b4026fe/xmlsec/python-xmlsec/issues/356 |
| og:image:alt | python-xmlsec currently relies on passing raw xmlNodePtr objects between lxml (which builds on libxml2) and xmlsec1 (which also uses libxml2). This creates a fragile situation where different versi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mxamin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| turbo-cache-control | no-preview |
| go-import | github.com/xmlsec/python-xmlsec git https://github.com/xmlsec/python-xmlsec.git |
| octolytics-dimension-user_id | 111647685 |
| octolytics-dimension-user_login | xmlsec |
| octolytics-dimension-repository_id | 27934955 |
| octolytics-dimension-repository_nwo | xmlsec/python-xmlsec |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27934955 |
| octolytics-dimension-repository_network_root_nwo | xmlsec/python-xmlsec |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width