feat: add optional defusedxml backend for XML parsing#139
Conversation
|
I thought it would be worth it to start discussion about this with @damian5710 as our local BEL representative and then the folks on the MNE side of things: @larsoner @drammock @scott-huberty about this possibility of a patch to allow That said... I'm convinced it's not necessary given that I think we're all against complexity without a direct need. The questions in my mind are:
My vote would be that this is unnecessary. Separately I do think we should change the mffpy |
|
Thanks for starting the conversation @pmolfese. TL;DR: so far to me there's no clear/obvious best path. Below I say what my preferences are but I'm open to further discussion / willing to change my mind. For reference, MNE's switch from standard library's
On the MNE side, there are competing constraints. On one hand, we're generally interested in shrinking our dependency stack, so if a dependency like mffpy is already using lxml, that's a reason for us to also use lxml (all else being equal). On the other hand, the main vulnerability that MNE users might conceivably encounter is a malicious file posing as a real dataset, which they download and try to open; 1 claims that lxml by default allows entity expansion for local files, which opens the door to insecure usage if MNE or mffpy mess up the implementation. Do folks have a sense of whether the superior speed of lxml is a relevant consideration? I haven't benchmarked lxml vs defusedxml, but IIUC
If we end up on option 2, it would be great if mffpy also provided a way (e.g., a further reading: |
|
Thanks for that great overview and historical context @drammock! To my surprise, it looks like there's relatively little XML use across the MNE ecosystem! Because of the relatively small XML footprint in MNE, I think my preference would be (Potential choice 3) to keep If we go with choices 3 or 4, then mffpy could explicitly attempt (i.e. requires testing) to go even more strict in As we wait for anyone else to chime in, I think a quick sub-topic topic to bring up now is that it would make senes for the GSoC project to use the mffpy way of doing things (instead of mixing mffpy and |
yep, only a few of the file formats we read directly use XML.
Let's see what @larsoner thinks. One other point to consider is that the wheel for
agreed; the whole point of us switching to use mffpy is to avoid needing to parse the files directly with our own code. |
|
To me option (3) seems like the most future compatible given the maintenance status of |
|
Closing this as consensus seems to be "not needed" |
WIP - Work in progress
Adds
mffpy.set_backend()to allow switching the XML parsing backendfrom lxml (default) to defusedxml, which guards against XML security
vulnerabilities.
lxmlwithrecover=Truebehavior unchangedmffpy.set_backend('defusedxml')opts in to defusedxml parsingset_backend('defusedxml')raisesImportErrorwith a clear messageif defusedxml is not installed (
pip install defusedxml)XML.from_file(..., recover=True)with the defusedxml backendemits a
UserWarning(recover is silently ignored, since defusedxmldoes not support it)
ParseError, the exception message includes ahint to switch back to lxml for broken-but-recoverable files
pytest --xml-backend=defusedxmlruns the full test suite under thedefusedxml backend; lxml-specific tests (
recover=Truebehavior) areskipped automatically