FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Harden XML parsing with secure lxml parser configuration by ssaurabhkumarsinha38sks-art · Pull Request #171 · AuthorizeNet/sdk-python · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 11 additions & 3 deletions authorizenet/apicontrollersbase.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys
import xml.dom.minidom
import requests
from lxml import objectify
from lxml import etree, objectify

from authorizenet.constants import constants
from authorizenet import apicontractsv1
Expand Down Expand Up @@ -142,21 +142,29 @@ def execute(self):
self._httpResponse.encoding = constants.response_encoding
self._httpResponse = self._httpResponse.text[3:] #strip BOM
self.afterexecute()

# Create secure XML parser to prevent XXE attacks
secure_parser = etree.XMLParser(
resolve_entities=False,
no_network=True,
dtd_validation=False,
load_dtd=False
)
try:
self._response = apicontractsv1.CreateFromDocument(self._httpResponse)
#objectify code
xmlResponse= self._response.toxml(encoding=constants.xml_encoding, element_name=self.getrequesttype())
xmlResponse = xmlResponse.replace(constants.nsNamespace1, b'')
xmlResponse = xmlResponse.replace(constants.nsNamespace2, b'')
self._mainObject = objectify.fromstring(xmlResponse)
self._mainObject = objectify.fromstring(xmlResponse, parser=secure_parser)

except Exception as objectifyexception:
anetLogger.error( 'Create Document Exception: %s, %s', type(objectifyexception), objectifyexception.args )
responseString = self._httpResponse

# removing encoding attribute as objectify fails if it is present
responseString = responseString.replace('encoding=\"utf-8\"', '')
self._mainObject = objectify.fromstring(responseString)
self._mainObject = objectify.fromstring(responseString, parser=secure_parser)
else:
if type(self.getresponseclass()) != type(self._mainObject):
if self._response.messages.resultCode == "Error":
Expand Down

Back | FazBrowse Home | New Git URL