Skip to content

Commit 9439f71

Browse files
committed
Wrapped more code in an exception block, in order not to continue processing after an XML parse exception. Fixes issue ietf-tools#2885.
- Legacy-Id: 17305
1 parent aabd237 commit 9439f71

1 file changed

Lines changed: 105 additions & 100 deletions

File tree

ietf/submit/forms.py

Lines changed: 105 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -187,115 +187,120 @@ def format_messages(where, e, log):
187187
self.xmltree = parser.parse(remove_comments=False, quiet=True)
188188
self.xmlroot = self.xmltree.getroot()
189189
xml_version = self.xmlroot.get('version', '2')
190-
except Exception as e:
191-
self.add_error('xml', "An exception occurred when trying to parse the XML file: %s" % e)
192-
193-
draftname = self.xmlroot.attrib.get('docName')
194-
if draftname is None:
195-
self.add_error('xml', "No docName attribute found in the xml root element")
196-
name_error = validate_submission_name(draftname)
197-
if name_error:
198-
self.add_error('xml', name_error)
199-
revmatch = re.search("-[0-9][0-9]$", draftname)
200-
if revmatch:
201-
self.revision = draftname[-2:]
202-
self.filename = draftname[:-3]
203-
else:
204-
self.revision = None
205-
self.filename = draftname
206-
self.title = self.xmlroot.findtext('front/title').strip()
207-
if type(self.title) is six.text_type:
208-
self.title = unidecode(self.title)
209-
self.title = normalize_text(self.title)
210-
self.abstract = (self.xmlroot.findtext('front/abstract') or '').strip()
211-
if type(self.abstract) is six.text_type:
212-
self.abstract = unidecode(self.abstract)
213-
author_info = self.xmlroot.findall('front/author')
214-
for author in author_info:
215-
info = {
216-
"name": author.attrib.get('fullname'),
217-
"email": author.findtext('address/email'),
218-
"affiliation": author.findtext('organization'),
219-
"country": author.findtext('address/postal/country'),
220-
}
221-
for item in info:
222-
if info[item]:
223-
info[item] = info[item].strip()
224-
self.authors.append(info)
225-
226-
# --- Prep the xml ---
227-
file_name['xml'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s%s' % (self.filename, self.revision, ext))
228-
try:
229-
if xml_version == '3':
230-
prep = xml2rfc.PrepToolWriter(self.xmltree, quiet=True, liberal=True, keep_pis=[xml2rfc.V3_PI_TARGET])
231-
prep.options.accept_prepped = True
232-
self.xmltree.tree = prep.prep()
233-
if self.xmltree.tree == None:
234-
self.add_error('xml', "Error from xml2rfc (prep): %s" % prep.errors)
235-
except Exception as e:
236-
msgs = format_messages('prep', e, xml2rfc.log)
237-
self.add_error('xml', msgs)
238190

239-
# --- Convert to txt ---
240-
if not ('txt' in self.cleaned_data and self.cleaned_data['txt']):
241-
file_name['txt'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s.txt' % (self.filename, self.revision))
191+
draftname = self.xmlroot.attrib.get('docName')
192+
if draftname is None:
193+
self.add_error('xml', "No docName attribute found in the xml root element")
194+
name_error = validate_submission_name(draftname)
195+
if name_error:
196+
self.add_error('xml', name_error)
197+
revmatch = re.search("-[0-9][0-9]$", draftname)
198+
if revmatch:
199+
self.revision = draftname[-2:]
200+
self.filename = draftname[:-3]
201+
else:
202+
self.revision = None
203+
self.filename = draftname
204+
self.title = self.xmlroot.findtext('front/title').strip()
205+
if type(self.title) is six.text_type:
206+
self.title = unidecode(self.title)
207+
self.title = normalize_text(self.title)
208+
self.abstract = (self.xmlroot.findtext('front/abstract') or '').strip()
209+
if type(self.abstract) is six.text_type:
210+
self.abstract = unidecode(self.abstract)
211+
author_info = self.xmlroot.findall('front/author')
212+
for author in author_info:
213+
info = {
214+
"name": author.attrib.get('fullname'),
215+
"email": author.findtext('address/email'),
216+
"affiliation": author.findtext('organization'),
217+
"country": author.findtext('address/postal/country'),
218+
}
219+
for item in info:
220+
if info[item]:
221+
info[item] = info[item].strip()
222+
self.authors.append(info)
223+
224+
# --- Prep the xml ---
225+
file_name['xml'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s%s' % (self.filename, self.revision, ext))
242226
try:
243-
if xml_version != '3':
244-
self.xmltree = parser.parse(remove_comments=True, quiet=True)
245-
self.xmlroot = self.xmltree.getroot()
246-
pagedwriter = xml2rfc.PaginatedTextRfcWriter(self.xmltree, quiet=True)
247-
pagedwriter.write(file_name['txt'])
248-
else:
249-
writer = xml2rfc.TextWriter(self.xmltree, quiet=True)
250-
writer.options.accept_prepped = True
251-
writer.write(file_name['txt'])
252-
log.log("In %s: xml2rfc %s generated %s from %s (version %s)" %
227+
if xml_version == '3':
228+
prep = xml2rfc.PrepToolWriter(self.xmltree, quiet=True, liberal=True, keep_pis=[xml2rfc.V3_PI_TARGET])
229+
prep.options.accept_prepped = True
230+
self.xmltree.tree = prep.prep()
231+
if self.xmltree.tree == None:
232+
self.add_error('xml', "Error from xml2rfc (prep): %s" % prep.errors)
233+
except Exception as e:
234+
msgs = format_messages('prep', e, xml2rfc.log)
235+
self.add_error('xml', msgs)
236+
237+
# --- Convert to txt ---
238+
if not ('txt' in self.cleaned_data and self.cleaned_data['txt']):
239+
file_name['txt'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s.txt' % (self.filename, self.revision))
240+
try:
241+
if xml_version != '3':
242+
self.xmltree = parser.parse(remove_comments=True, quiet=True)
243+
self.xmlroot = self.xmltree.getroot()
244+
pagedwriter = xml2rfc.PaginatedTextRfcWriter(self.xmltree, quiet=True)
245+
pagedwriter.write(file_name['txt'])
246+
else:
247+
writer = xml2rfc.TextWriter(self.xmltree, quiet=True)
248+
writer.options.accept_prepped = True
249+
writer.write(file_name['txt'])
250+
log.log("In %s: xml2rfc %s generated %s from %s (version %s)" %
251+
( os.path.dirname(file_name['xml']),
252+
xml2rfc.__version__,
253+
os.path.basename(file_name['txt']),
254+
os.path.basename(file_name['xml']),
255+
xml_version))
256+
except Exception as e:
257+
msgs = format_messages('txt', e, xml2rfc.log)
258+
log.log('\n'.join(msgs))
259+
self.add_error('xml', msgs)
260+
261+
# --- Convert to html ---
262+
if xml_version == '3':
263+
try:
264+
file_name['html'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s.html' % (self.filename, self.revision))
265+
writer = xml2rfc.HtmlWriter(self.xmltree, quiet=True)
266+
writer.write(file_name['html'])
267+
self.file_types.append('.html')
268+
log.log("In %s: xml2rfc %s generated %s from %s (version %s)" %
253269
( os.path.dirname(file_name['xml']),
254270
xml2rfc.__version__,
255-
os.path.basename(file_name['txt']),
271+
os.path.basename(file_name['html']),
256272
os.path.basename(file_name['xml']),
257273
xml_version))
258-
except Exception as e:
259-
msgs = format_messages('txt', e, xml2rfc.log)
260-
log.log('\n'.join(msgs))
261-
self.add_error('xml', msgs)
274+
except Exception as e:
275+
msgs = format_messages('html', e, xml2rfc.log)
276+
self.add_error('xml', msgs)
262277

263-
# --- Convert to html ---
264-
if xml_version == '3':
278+
if xml_version == '2':
279+
ok, errors = self.xmltree.validate()
280+
else:
281+
ok, errors = True, ''
282+
283+
if not ok:
284+
# Each error has properties:
285+
#
286+
# message: the message text
287+
# domain: the domain ID (see lxml.etree.ErrorDomains)
288+
# type: the message type ID (see lxml.etree.ErrorTypes)
289+
# level: the log level ID (see lxml.etree.ErrorLevels)
290+
# line: the line at which the message originated (if applicable)
291+
# column: the character column at which the message originated (if applicable)
292+
# filename: the name of the file in which the message originated (if applicable)
293+
self.add_error('xml',
294+
[ forms.ValidationError("One or more XML validation errors occurred when processing the XML file:") ] +
295+
[ forms.ValidationError("%s: Line %s: %s" % (xml_file.name, r.line, r.message), code="%s"%r.type) for r in errors ]
296+
)
297+
except Exception as e:
265298
try:
266-
file_name['html'] = os.path.join(settings.IDSUBMIT_STAGING_PATH, '%s-%s.html' % (self.filename, self.revision))
267-
writer = xml2rfc.HtmlWriter(self.xmltree, quiet=True)
268-
writer.write(file_name['html'])
269-
self.file_types.append('.html')
270-
log.log("In %s: xml2rfc %s generated %s from %s (version %s)" %
271-
( os.path.dirname(file_name['xml']),
272-
xml2rfc.__version__,
273-
os.path.basename(file_name['html']),
274-
os.path.basename(file_name['xml']),
275-
xml_version))
276-
except Exception as e:
277-
msgs = format_messages('html', e, xml2rfc.log)
299+
msgs = format_messages('txt', e, xml2rfc.log)
300+
log.log('\n'.join(msgs))
278301
self.add_error('xml', msgs)
279-
280-
if xml_version == '2':
281-
ok, errors = self.xmltree.validate()
282-
else:
283-
ok, errors = True, ''
284-
285-
if not ok:
286-
# Each error has properties:
287-
#
288-
# message: the message text
289-
# domain: the domain ID (see lxml.etree.ErrorDomains)
290-
# type: the message type ID (see lxml.etree.ErrorTypes)
291-
# level: the log level ID (see lxml.etree.ErrorLevels)
292-
# line: the line at which the message originated (if applicable)
293-
# column: the character column at which the message originated (if applicable)
294-
# filename: the name of the file in which the message originated (if applicable)
295-
self.add_error('xml',
296-
[ forms.ValidationError("One or more XML validation errors occurred when processing the XML file:") ] +
297-
[ forms.ValidationError("%s: Line %s: %s" % (xml_file.name, r.line, r.message), code="%s"%r.type) for r in errors ]
298-
)
302+
except Exception:
303+
self.add_error('xml', "An exception occurred when trying to process the XML file: %s" % e)
299304
finally:
300305
os.close(tfh)
301306
os.unlink(tfn)

0 commit comments

Comments
 (0)