Projet

Général

Profil

0002-utils.zip-add-check-for-XML-syntax-36848.patch

Benjamin Dauvergne, 11 octobre 2019 14:03

Télécharger (2,42 ko)

Voir les différences:

Subject: [PATCH 2/2] utils.zip: add check for XML syntax (#36848)

 passerelle/utils/zip.py | 8 +++++++-
 tests/test_utils_zip.py | 4 ++--
 2 files changed, 9 insertions(+), 3 deletions(-)
passerelle/utils/zip.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
from __future__ import unicode_literals
17
from __future__ import unicode_literals, absolute_import
18 18

  
19 19
import io
20 20
import os.path
21 21
import json
22
import xml.etree.ElementTree as ET
22 23
import zipfile
23 24

  
24 25
from jsonschema import validate, ValidationError
......
122 123
            except TemplateDoesNotExist as e:
123 124
                raise ZipTemplateDoesNotExist('part template %s not found' % template_path, e)
124 125
            content = template.render(self.ctx)
126
            if name.endswith('.xml'):
127
                try:
128
                    ET.fromstring(content)
129
                except ET.ParseError as e:
130
                    raise ZipTemplateSyntaxError('XML syntax error in part template %s' % template_path, e)
125 131
            yield name, content
126 132

  
127 133
    def render_to_bytes(self):
tests/test_utils_zip.py
98 98
    ZipTemplate(
99 99
        tpl_builder(
100 100
            '{{ name }}-{{ counter }}.zip',
101
            ('{{ name }}-{{ counter }}-part1.xml', '<?xml?><body>{{ body }}</body>'),
101
            ('{{ name }}-{{ counter }}-part1.xml', '<?xml version="1.0"?><body>{{ body }}</body>'),
102 102
        ),
103 103
        ctx={'name': 'coucou', 'counter': 10, 'body': 'blabla'}).render_to_path(dest)
104 104

  
......
106 106
    with full_path.open() as fd:
107 107
        with zipfile.ZipFile(fd) as zi:
108 108
            assert zi.namelist() == ['coucou-10-part1.xml']
109
            assert zi.open('coucou-10-part1.xml').read() == '<?xml?><body>blabla</body>'
109
            assert zi.open('coucou-10-part1.xml').read() == '<?xml version="1.0"?><body>blabla</body>'
110
-