Don't use payload function when parsing form-data

main
Bob Mottram 2021-03-01 10:02:55 +00:00
parent 18088169e6
commit f76c173f59
2 changed files with 52 additions and 7 deletions

View File

@ -979,16 +979,21 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
return filename, attachmentMediaType return filename, attachmentMediaType
def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}: def extractTextFieldsInPOST(postBytes, boundary, debug: bool,
unitTestData=None) -> {}:
"""Returns a dictionary containing the text fields of a http form POST """Returns a dictionary containing the text fields of a http form POST
The boundary argument comes from the http header The boundary argument comes from the http header
""" """
msg = email.parser.BytesParser().parsebytes(postBytes) if not unitTestData:
msgBytes = email.parser.BytesParser().parsebytes(postBytes)
messageFields = msgBytes.decode('utf-8')
else:
messageFields = unitTestData
if debug: if debug:
print('DEBUG: POST arriving ' + print('DEBUG: POST arriving ' + messageFields)
msg.get_payload(decode=True).decode('utf-8'))
messageFields = msg.get_payload(decode=True) messageFields = messageFields.split(boundary)
messageFields = messageFields.decode('utf-8').split(boundary)
fields = {} fields = {}
# examine each section of the POST, separated by the boundary # examine each section of the POST, separated by the boundary
for f in messageFields: for f in messageFields:
@ -1002,7 +1007,8 @@ def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}:
postKey = postStr.split('"', 1)[0] postKey = postStr.split('"', 1)[0]
postValueStr = postStr.split('"', 1)[1] postValueStr = postStr.split('"', 1)[1]
if ';' in postValueStr: if ';' in postValueStr:
continue if postKey != 'message':
continue
if '\r\n' not in postValueStr: if '\r\n' not in postValueStr:
continue continue
postLines = postValueStr.split('\r\n') postLines = postValueStr.split('\r\n')

View File

@ -77,6 +77,7 @@ from inbox import jsonPostAllowsComments
from inbox import validInbox from inbox import validInbox
from inbox import validInboxFilenames from inbox import validInboxFilenames
from categories import guessHashtagCategory from categories import guessHashtagCategory
from content import extractTextFieldsInPOST
from content import validHashTag from content import validHashTag
from content import htmlReplaceEmailQuote from content import htmlReplaceEmailQuote
from content import htmlReplaceQuoteMarks from content import htmlReplaceQuoteMarks
@ -3330,9 +3331,47 @@ def testMarkdownToHtml():
'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.' 'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.'
def testExtractTextFieldsInPOST():
print('testExtractTextFieldsInPOST')
boundary = '-----------------------------116202748023898664511855843036'
formData = '-----------------------------116202748023898664511855' + \
'843036\r\nContent-Disposition: form-data; name="submitPost"' + \
'\r\n\r\nSubmit\r\n-----------------------------116202748023' + \
'898664511855843036\r\nContent-Disposition: form-data; name=' + \
'"subject"\r\n\r\n\r\n-----------------------------116202748' + \
'023898664511855843036\r\nContent-Disposition: form-data; na' + \
'me="message"\r\n\r\nThis is a ; test\r\n-------------------' + \
'----------116202748023898664511855843036\r\nContent-Disposi' + \
'tion: form-data; name="commentsEnabled"\r\n\r\non\r\n------' + \
'-----------------------116202748023898664511855843036\r\nCo' + \
'ntent-Disposition: form-data; name="eventDate"\r\n\r\n\r\n' + \
'-----------------------------116202748023898664511855843036' + \
'\r\nContent-Disposition: form-data; name="eventTime"\r\n\r' + \
'\n\r\n-----------------------------116202748023898664511855' + \
'843036\r\nContent-Disposition: form-data; name="location"' + \
'\r\n\r\n\r\n-----------------------------116202748023898664' + \
'511855843036\r\nContent-Disposition: form-data; name=' + \
'"imageDescription"\r\n\r\n\r\n-----------------------------' + \
'116202748023898664511855843036\r\nContent-Disposition: ' + \
'form-data; name="attachpic"; filename=""\r\nContent-Type: ' + \
'application/octet-stream\r\n\r\n\r\n----------------------' + \
'-------116202748023898664511855843036--\r\n'
debug = False
fields = extractTextFieldsInPOST(None, boundary, debug, formData)
assert fields['submitPost'] == 'Submit'
assert fields['subject'] == ''
assert fields['commentsEnabled'] == 'on'
assert fields['eventDate'] == ''
assert fields['eventTime'] == ''
assert fields['location'] == ''
assert fields['imageDescription'] == ''
assert fields['message'] == 'This is a ; test'
def runAllTests(): def runAllTests():
print('Running tests...') print('Running tests...')
testFunctions() testFunctions()
testExtractTextFieldsInPOST()
testMarkdownToHtml() testMarkdownToHtml()
testValidHashTag() testValidHashTag()
testPrepareHtmlPostNickname() testPrepareHtmlPostNickname()