Detect local subclasses of Markup in B704#1446
Open
ChihebBENCHEIKH1 wants to merge 1 commit into
Open
Conversation
The B704 check only flagged calls whose resolved name was one of the recognized Markup classes (markupsafe.Markup, flask.Markup, or a name added through the plugin configuration). A project that wraps Markup in its own subclass and then calls that subclass with dynamic data was not flagged, which is the false negative behind CVE-2025-54384. Track the base classes of every class definition while walking the module and expose them on the test context. B704 now walks that inheritance graph and treats a call to a local class that subclasses a Markup class, directly or through a chain of subclasses, the same way it treats a direct Markup call. Extend the example files with direct, nested, and configured-name subclasses and update the functional test counts accordingly. Resolves: PyCQA#1405 Signed-off-by: chiheb ben cheikh <bencheikh.chiheb@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
B704(markupsafe_markup_xss) only reports a call when the resolved name of the callee is one of the recognized Markup classes:markupsafe.Markup,flask.Markup, or a name listed underextend_markup_names. When a project defines its own subclass ofMarkupand passes dynamic data to it, the call is not reported. This is the false negative described in #1405 and tied to CVE-2025-54384.Reproduction
Before: only direct
Markup(...)calls are reported.After: calls to a local subclass of a Markup class are reported as well.
What changed
BanditNodeVisitornow records the resolved base classes of every class definition it walks, alongside the existing import tracking, and exposes them on the context ascontext.classes.B704walks that inheritance graph and treats a call to a local class that subclasses a Markup class, directly or through a chain of subclasses, the same way it treats a directMarkupcall. Subclasses of a configuredextend_markup_namesentry are covered too.Testing
stestr run(full suite): 273 passingflake8 bandit tests: cleanblack,reorder-python-imports,pyupgrade: no changesbandit -r bandit -ll -ii: no new findingsResolves #1405