Unverified Commit 7f4ae8c6 authored by Mustafa Bal's avatar Mustafa Bal Committed by GitHub
Browse files

Added defusedxml to parse untrusted XML data (#3636)



* Added defusedxml to parse untrusted XML data

* Added typecheck disable for defusedxml
Co-authored-by: default avatarNicolas Hug <nicolashug@fb.com>
parent e79a74e1
...@@ -63,3 +63,7 @@ ignore_missing_imports = True ...@@ -63,3 +63,7 @@ ignore_missing_imports = True
[mypy-av.*] [mypy-av.*]
ignore_missing_imports = True ignore_missing_imports = True
[mypy-defusedxml.*]
ignore_missing_imports = True
...@@ -2,7 +2,11 @@ import os ...@@ -2,7 +2,11 @@ import os
import tarfile import tarfile
import collections import collections
from .vision import VisionDataset from .vision import VisionDataset
import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element as ET_Element
try:
from defusedxml.ElementTree import parse as ET_parse
except ImportError:
from xml.etree.ElementTree import parse as ET_parse
from PIL import Image from PIL import Image
from typing import Any, Callable, Dict, Optional, Tuple, List from typing import Any, Callable, Dict, Optional, Tuple, List
from .utils import download_and_extract_archive, verify_str_arg from .utils import download_and_extract_archive, verify_str_arg
...@@ -203,14 +207,14 @@ class VOCDetection(_VOCBase): ...@@ -203,14 +207,14 @@ class VOCDetection(_VOCBase):
tuple: (image, target) where target is a dictionary of the XML tree. tuple: (image, target) where target is a dictionary of the XML tree.
""" """
img = Image.open(self.images[index]).convert("RGB") img = Image.open(self.images[index]).convert("RGB")
target = self.parse_voc_xml(ET.parse(self.annotations[index]).getroot()) target = self.parse_voc_xml(ET_parse(self.annotations[index]).getroot())
if self.transforms is not None: if self.transforms is not None:
img, target = self.transforms(img, target) img, target = self.transforms(img, target)
return img, target return img, target
def parse_voc_xml(self, node: ET.Element) -> Dict[str, Any]: def parse_voc_xml(self, node: ET_Element) -> Dict[str, Any]:
voc_dict: Dict[str, Any] = {} voc_dict: Dict[str, Any] = {}
children = list(node) children = list(node)
if children: if children:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment