Skip to content
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
3 changes: 2 additions & 1 deletion ofxtools/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def parse_header(source: BinaryIO) -> Tuple[OFXHeaderType, str]:

# OFX header is read by nice clean machines, not meatbags -
# should not contain 💩, 漢字, or what have you.
line = source.readline().decode("ascii")
# However, the first line may contain the XML body, which CAN contain non-ascii.
line = source.readline().decode("ascii", "replace")
if line.strip():
found_header = True
break
Expand Down
10 changes: 10 additions & 0 deletions tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ def testParseHeaderV2NoNewlineBetweenHeaderAndBody(self):
self.assertIsNone(root.text)
self.assertEqual(len(root), 1)

def testParseHeaderV2NoNewlineBetweenHeaderAndBodyWithUnicode(self):
"""OFXv2 may contain non-ascii characters in the first line,
if the header is not separated from the body by newlines."""
ofxtools.header.parse_header(BytesIO(
b'<?xml version="1.0" encoding="utf-8" ?>'
b'<?OFX OFXHEADER="200" VERSION="202" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE" ?>'
b'Dummy unicode data: A:\xc3\x83\xe2\x80\x9e\xc3\x83\xc2\xa4, '
b'O:\xc3\x83\xe2\x80\x93\xc3\x83\xc2\xb6, U:\xc3\x83\xc5\x93\xc3\x83\xc2\xbc, and SZ:\xc3\x83\xc5\xb8'
))

def testParseInvalid(self):
header = str(self.headerClass(self.defaultVersion))
with self.assertRaises(ofxtools.header.OFXHeaderError):
Expand Down