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

update B405 rules #1120

Closed
wants to merge 1 commit into from
Closed

update B405 rules #1120

wants to merge 1 commit into from

Conversation

kiraware
Copy link

@kiraware kiraware commented Mar 14, 2024

make B405 rules more specific. Because not all in the module is related to parse xml. Some of them is needed for typing, for example Element. the reason why only the ElementTree module is updated is because defusedxml says in its readme that cElementTree is deprecated.

This PR update the B405 rules, and make it more specific by add these parse(), iterparse(), fromstring(), XMLParser.
This is necessary because the contents of the ElementTree module are not all related to parsing xml. Some of them are needed for typing, such as Element.

From the docs stated that there is more functions or classes that relates with parsing. Here are some, but i may miss some.

The main reason why only these parse(), iterparse(), fromstring(), XMLParser that is included in B405 rules, because defusedxml document it and the test example files test it. Maybe some functions or classes listed above could be included. So this PR will change the B405 rules which previously would give warnings about the entire contents of the ElementTree module, to just

Issue: #709

Tagging people related on issue: @vanschelven @seanmceligot to find out what is missed in this PR.

make B405 rules more specific. Because not all in the module is related
to parse xml. Some of them is needed for typing, for example Element.
Copy link
Member

@sigmavirus24 sigmavirus24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this fixes the things you actually want fixed

Comment on lines +317 to +319
"xml.etree.ElementTree.fromstring",
"xml.etree.ElementTree.iterparse",
"xml.etree.ElementTree.parse",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe you can import fromstring, iterparse, or parse as a result this doesn't do what you want.

Copy link
Author

@kiraware kiraware Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, looks like B314 rules already cover it. dumb me.

Copy link
Author

@kiraware kiraware Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think, because B405 rules are too general and make the entire contents of the ElementTree module seem dangerous from a security perspective even though there are functions or classes that are not dangerous, for example Element and that is needed for typing. Do I need to add all the functions or classes that are dangerous and not include those that have nothing to do with security as I have listed above?

Before the changes in this PR, this code will warn B405 rules. But after changes in this PR, this code will not warn B405.

import xml.etree.ElementTree

And after changes in this PR too, this code will warn B405, although bandit only warn the fromstring and the rest is not. Looks weird.

from xml.etree.ElementTree import fromstring, parse, iterparse, XMLParser

If there was a way for bandit to raise a warning to a module but some of the contents of that module didn't raise a warning things will become simple and and I don't have to give # nosec: B405 everywhere.

I don't believe you can import fromstring, iterparse, or parse as a result this doesn't do what you want.

This code will result in ModuleNotFoundError.

import xml.etree.ElementTree.fromstring
import xml.etree.ElementTree.parse
import xml.etree.ElementTree.iterparse
import xml.etree.ElementTree.XMLParser

instead someone will use this

from xml.etree.ElementTree import fromstring, parse, iterparse, XMLParser

@kiraware kiraware marked this pull request as draft March 15, 2024 04:01
@kiraware kiraware closed this Mar 28, 2024
@kiraware kiraware deleted the improve-B405 branch March 28, 2024 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants