Skip to content

Commit

Permalink
Merge pull request #52 from maykinmedia/chore/replace-deprecated-defu…
Browse files Browse the repository at this point in the history
…sedxml-lxml-module-usage

👽 Replace deprecated defusedxml.lxml module usage
  • Loading branch information
sergei-maertens authored Feb 15, 2024
2 parents 04c9e4e + 1e68b0f commit d45b517
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
18 changes: 18 additions & 0 deletions digid_eherkenning/_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
XML parsing with DTD/Entities blocking.
Inspired by https://github.com/mvantellingen/python-zeep/pull/1179/ as their solution
for the deprecated defusedxml.lxml module and the defaults applied in defusedxml.lxml.
"""

from lxml.etree import XMLParser, parse as _parse


def parse(source):
"""
Parse an LXML etree from source without resolving entities.
Resolving entities is a security risk, which is why we disable it.
"""
parser = XMLParser(resolve_entities=False)
return _parse(source, parser)
3 changes: 1 addition & 2 deletions digid_eherkenning/saml2/eherkenning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from django.urls import reverse
from django.utils import timezone

from defusedxml.lxml import tostring
from furl.furl import furl
from lxml.builder import ElementMaker
from lxml.etree import Element
from lxml.etree import Element, tostring
from OpenSSL import crypto

from ..choices import AssuranceLevels
Expand Down
3 changes: 2 additions & 1 deletion digid_eherkenning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from django.conf import settings

from defusedxml.lxml import parse
from lxml import etree

from ._xml import parse


def get_client_ip(request):
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
Expand Down
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ install_requires =
django-sessionprofile
django-simple-certmanager
django-solo
lxml
defusedxml>=0.7.0
lxml >= 4.7.1
furl
maykin-python3-saml
maykin-python3-saml >= 1.16.0
tests_require =
django-test-migrations
freezegun
Expand Down
1 change: 1 addition & 0 deletions tests/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
TODO: replace with pytest fixtures?
"""

from digid_eherkenning.models import DigidConfiguration, EherkenningConfiguration


Expand Down

0 comments on commit d45b517

Please sign in to comment.