Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of valid XMLs with non-ASCII characters #334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions winrm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def _clean_error_msg(self, msg):
try:
# remove the namespaces from the xml for easier processing
msg_xml = self._strip_namespace(msg_xml)
root = ET.fromstring(msg_xml)
string_xml = msg_xml.decode('437')
root = ET.fromstring(string_xml)
# the S node is the error message, find all S nodes
nodes = root.findall("./S")
new_msg = ""
Expand All @@ -78,7 +79,8 @@ def _clean_error_msg(self, msg):
# the hex chars represent CRLF so we replace with newline
new_msg += s.text.replace("_x000D__x000A_", "\n")
except Exception as e:
# if any of the above fails, the msg was not true xml
# if any of the above fails, the msg was either not a true xml
# or the decoding failed.
# print a warning and return the original string
warnings.warn(
"There was a problem converting the Powershell error "
Expand Down
6 changes: 6 additions & 0 deletions winrm/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def test_decode_clixml_no_errors():
actual = s._clean_error_msg(msg)
assert actual == expected

def test_decode_clixml_non_ascii_valid_xml_no_error():
s = Session('windows-host.example.com', auth=('john.smith', 'secret'))
msg = b'#< CLIXML\r\n<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Module werden f\x81r erstmalige Verwendung vorbereitet.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj></Objs>'
expected = b""
actual = s._clean_error_msg(msg)
assert actual == expected

def test_decode_clixml_invalid_xml():
s = Session('windows-host.example.com', auth=('john.smith', 'secret'))
Expand Down