Skip to content

Commit 7f4ae8c

Browse files
mstfblNicolasHug
andauthored
Added defusedxml to parse untrusted XML data (#3636)
* Added defusedxml to parse untrusted XML data * Added typecheck disable for defusedxml Co-authored-by: Nicolas Hug <[email protected]>
1 parent e79a74e commit 7f4ae8c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ ignore_missing_imports = True
6363
[mypy-av.*]
6464

6565
ignore_missing_imports = True
66+
67+
[mypy-defusedxml.*]
68+
69+
ignore_missing_imports = True

torchvision/datasets/voc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import tarfile
33
import collections
44
from .vision import VisionDataset
5-
import xml.etree.ElementTree as ET
5+
from xml.etree.ElementTree import Element as ET_Element
6+
try:
7+
from defusedxml.ElementTree import parse as ET_parse
8+
except ImportError:
9+
from xml.etree.ElementTree import parse as ET_parse
610
from PIL import Image
711
from typing import Any, Callable, Dict, Optional, Tuple, List
812
from .utils import download_and_extract_archive, verify_str_arg
@@ -203,14 +207,14 @@ def __getitem__(self, index: int) -> Tuple[Any, Any]:
203207
tuple: (image, target) where target is a dictionary of the XML tree.
204208
"""
205209
img = Image.open(self.images[index]).convert("RGB")
206-
target = self.parse_voc_xml(ET.parse(self.annotations[index]).getroot())
210+
target = self.parse_voc_xml(ET_parse(self.annotations[index]).getroot())
207211

208212
if self.transforms is not None:
209213
img, target = self.transforms(img, target)
210214

211215
return img, target
212216

213-
def parse_voc_xml(self, node: ET.Element) -> Dict[str, Any]:
217+
def parse_voc_xml(self, node: ET_Element) -> Dict[str, Any]:
214218
voc_dict: Dict[str, Any] = {}
215219
children = list(node)
216220
if children:

0 commit comments

Comments
 (0)