lists.arthurdejong.org
RSS feed

python-pskc branch master updated. 0.5-17-g4ed4e11

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-pskc branch master updated. 0.5-17-g4ed4e11



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-pskc".

The branch, master has been updated
       via  4ed4e115469e756b879e4cc788391eca0d656d1c (commit)
       via  b90faeb117163eeda3db42720bf465b7bba6be64 (commit)
      from  7272e549e442b59ec3129105246c637b88bef083 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-pskc/commit/?id=4ed4e115469e756b879e4cc788391eca0d656d1c

commit 4ed4e115469e756b879e4cc788391eca0d656d1c
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Sep 24 12:06:20 2017 +0200

    Support hashlib from Python 2.7.3
    
    Some Python versions don't have the algorithms_available property but do
    have the algorithms property in hashlib.

diff --git a/tests/test_mac.doctest b/tests/test_mac.doctest
index 2951ca9..15a0609 100644
--- a/tests/test_mac.doctest
+++ b/tests/test_mac.doctest
@@ -76,7 +76,10 @@ and HMAC-SHA-512.
 Some test cases from RFC 2857 for HMAC-RIPEMD160 but not all versions of
 hashlib have RIPEMD-160.
 
->>> if 'ripemd160' in hashlib.algorithms_available:
+>>> hashlib_algorithms = (
+...     getattr(hashlib, 'algorithms_available', None) or
+...     getattr(hashlib, 'algorithms', None))
+>>> if 'ripemd160' in hashlib_algorithms:
 ...     key = a2b_hex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')
 ...     b2a_hex(mac('HMAC-RIPEMD160', key, b'Hi There')) == \
 ...         '24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668'

https://arthurdejong.org/git/python-pskc/commit/?id=b90faeb117163eeda3db42720bf465b7bba6be64

commit b90faeb117163eeda3db42720bf465b7bba6be64
Author: Arthur de Jong <arthur@arthurdejong.org>
Date:   Sun Sep 24 11:40:40 2017 +0200

    Use defusedxml if available
    
    This uses the defusedxml library if available to defend agains a number
    of XML-based attacks.

diff --git a/pskc/xml.py b/pskc/xml.py
index 65bb174..bc10c81 100644
--- a/pskc/xml.py
+++ b/pskc/xml.py
@@ -25,11 +25,22 @@ This module provides some utility functions for parsing XML 
files.
 
 from __future__ import absolute_import
 
-# try to find a usable ElementTree module
-try:
-    from lxml import etree
+# try to find a usable ElementTree implementation
+try:  # pragma: no cover (different implementations)
+    from lxml.etree import parse as xml_parse, tostring as xml_tostring
+    from lxml.etree import register_namespace, Element, SubElement
+    try:
+        from defusedxml.lxml import parse as xml_parse  # noqa: F811
+    except ImportError:
+        pass
 except ImportError:  # pragma: no cover (different implementations)
-    import xml.etree.ElementTree as etree
+    from xml.etree.ElementTree import (
+        parse as xml_parse, tostring as xml_tostring)
+    from xml.etree.ElementTree import register_namespace, Element, SubElement
+    try:
+        from defusedxml.ElementTree import parse as xml_parse  # noqa: F811
+    except ImportError:
+        pass
 
 
 # the relevant XML namespaces for PSKC
@@ -50,7 +61,7 @@ namespaces = dict(
 def register_namespaces():
     """Register the namespaces so the correct short names will be used."""
     for ns, namespace in namespaces.items():
-        etree.register_namespace(ns, namespace)
+        register_namespace(ns, namespace)
 
 
 register_namespaces()
@@ -58,7 +69,7 @@ register_namespaces()
 
 def parse(source):
     """Parse the provided file and return an element tree."""
-    return etree.parse(source)
+    return xml_parse(source)
 
 
 def remove_namespaces(tree):
@@ -165,9 +176,9 @@ def mk_elem(parent, tag=None, text=None, empty=False, 
**kwargs):
         ns, name = tag.split(':', 1)
         tag = '{%s}%s' % (namespaces[ns], name)
     if parent is None:
-        element = etree.Element(tag)
+        element = Element(tag)
     else:
-        element = etree.SubElement(parent, tag)
+        element = SubElement(parent, tag)
     # set text of element
     if text is not None:
         element.text = _format(text)
@@ -188,10 +199,10 @@ def tostring(element):
         for e in element.iter():
             nsmap.update(e.nsmap)
         # replace toplevel element with all namespaces
-        e = etree.Element(element.tag, attrib=element.attrib, nsmap=nsmap)
+        e = Element(element.tag, attrib=element.attrib, nsmap=nsmap)
         for a in element:
             e.append(a)
         element = e
-    xml = etree.tostring(element, encoding='UTF-8')
+    xml = xml_tostring(element, encoding='UTF-8')
     return minidom.parseString(xml).toprettyxml(
         indent=' ', encoding='UTF-8').strip()
diff --git a/setup.py b/setup.py
index 97576c4..7670ec8 100755
--- a/setup.py
+++ b/setup.py
@@ -63,4 +63,8 @@ setup(
     ],
     packages=find_packages(),
     install_requires=['pycrypto', 'python-dateutil'],
+    extras_require={
+        'lxml':  ['lxml'],
+        'defuse':  ['defusedxml'],
+    },
 )
diff --git a/tox.ini b/tox.ini
index c5c4383..fd591cf 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = py{27,34,35,36}{,-lxml},flake8
+envlist = py{27,34,35,36}{,-lxml}{,-defusedxml},flake8
 skip_missing_interpreters = True
 
 [testenv]
@@ -8,6 +8,7 @@ deps = nose
        pycrypto
        python-dateutil
        lxml: lxml
+       defusedxml: defusedxml
 commands = nosetests
 setenv=
     PYTHONWARNINGS=all

-----------------------------------------------------------------------

Summary of changes:
 pskc/xml.py            | 31 +++++++++++++++++++++----------
 setup.py               |  4 ++++
 tests/test_mac.doctest |  5 ++++-
 tox.ini                |  3 ++-
 4 files changed, 31 insertions(+), 12 deletions(-)


hooks/post-receive
-- 
python-pskc
-- 
To unsubscribe send an email to
python-pskc-commits-unsubscribe@lists.arthurdejong.org or see
https://lists.arthurdejong.org/python-pskc-commits/