Skip to main content

larzxml

Ergonomic XML builder + an XXE-safe parser — zero dependencies.

The standard library makes two XML tasks harder than they should be: building XML reads clumsily with ElementTree, and parsing untrusted XML is dangerous by default (XXE and billion-laughs attacks). larzxml fixes both — a fluent builder that escapes correctly, and a parser that refuses DTDs and entities, so external-entity and exponential-expansion attacks simply can't happen.

Install

pip install larzxml

Use

from larzxml import element, parse

root = element("order", id="42")
root.sub("item", text="Widget", sku="A1")
root.sub("item", text="Gadget & <friends>", sku="B2")   # escaped safely
print(root.to_xml(pretty=True))

tree = parse(xml_text)              # safe by default
tree.find("item").text
tree.findall("item")

Safe by construction

parse('<!DOCTYPE x [<!ENTITY e SYSTEM "file:///etc/passwd">]><x>&e;</x>')
# -> XMLError: DTD/DOCTYPE is not allowed (XXE-safe parser)

DTDs and entity declarations are rejected, so both XXE (reading local files / SSRF) and billion-laughs (exponential entity expansion) are impossible — you don't have to remember to harden anything.

Tests

python -m unittest discover -s tests -v   # 14 tests (incl. XXE + billion-laughs)

The Larz stack

One of 60+ pure-Python, zero-dependency libraries at github.com/larz-scripter. The XML sibling of larzhtml (XSS-safe HTML) and larzjson.

License

MIT (c) larz-scripter

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzxml-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

larzxml-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file larzxml-0.1.0.tar.gz.

File metadata

  • Download URL: larzxml-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzxml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f006cf3fefa6cc9325efef4b9be10b18d6fc15de4c23bb1803110fbecd6e910
MD5 c3d973ccb455ed1ae0b143506a4b1c52
BLAKE2b-256 2da374fad2e3cff62055faf258a26d6d0cee9b63a64cb8f234cbf642a4b38c98

See more details on using hashes here.

File details

Details for the file larzxml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzxml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzxml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a8cdba5ac745aea009491c08dfb33f6f8854338ac9721032a7a7e1a5b304ccf
MD5 57eac390f6dd69809a40221d872ba82e
BLAKE2b-256 649cf4b7b53806c3f4d8b581f6589af4d75a018ad9fd0466fdc9215acab40233

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page