Repository navigation
Python: Replace eval with safer alternatives - #7709
Merged
Merged
Conversation
In temporal algebra, compare values directly instead of eval of stringified comparisons, and route the td and global-variable comparisons through the same helper; the remaining eval of the internally built boolean string is documented and marked nosec. In the wxGUI, resolve wx ids and menu handlers with getattr instead of eval, and document the gmodeler loop-condition eval, which runs the model author's own expression. In i.tasscap, dispatch to the band combination function via globals() lookup. Fixes 14 Bandit B307 (eval) alerts.
cwhite911
commented
Jul 15, 2026
wenzeslaus
reviewed
Jul 15, 2026
wenzeslaus
reviewed
Jul 17, 2026
wenzeslaus
left a comment
Member
There was a problem hiding this comment.
The temporal expressions require further care and I didn't have time to explore it more fully before. The tests are still critical, but there are other issues which surfaced when I co-reviewed that with AI. See the individual comments.
- Rename eval_datetime_str to compare_values (it no longer evals or
takes strings) and keep a deprecated alias; add an explicit raise for
an unknown operator instead of falling through to UnboundLocalError.
- Drop the unnecessary float()/str() coercions at both call sites so
integer comparisons stay int-exact, matching the old eval semantics
and the documented direction of t_var comp_op t_var.
- Add a unit test exercising all six operators over int/float/date,
including != which no expression-level test reaches.
- gui menu: use str.removeprefix('self.') instead of lstrip, which
strips a character set rather than a prefix (latent bug).
- Tighten the B307 nosec justification by guarding the seed condition
value as a bool, so the claim that only booleans are evaluated holds.
Contributor
Author
|
Addressed in the latest push:
The |
Contributor
Author
|
@wenzeslaus take another look at this when you get a chance. I would like to merge or close these automated codeQL so I don't have so many open PRs. |
echoix
approved these changes
Sep 1, 2026
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.
Fixes the 14 Bandit B307 (
eval) code scanning alerts in non-vendored code. All flagged inputs are local (GRASS-shipped XML, parser tokens, model files), so this is a code quality cleanup rather than a vulnerability fix;evalis simply unnecessary at most of these sites.temporal_algebra.py, 9 alerts):eval_datetime_str()evaluatedeval(str(a < b))for each operator, which round-trips a bool through a string; it now compares directly. Thetd(A) == 1andstart_day() > 5style comparisons built Python source by string concatenation and eval'd it; they now call the same comparison helper withfloat(value). The one remaining eval (boolean expression string assembled fromisinstance(x, bool)-checked values and grammar tokens) is documented and marked# nosec B307.menutree.pyresolved<id>menu XML entries witheval("wx." + text), nowgetattr(wx, text)(verified: every<id>in the shipped menu XMLs resolves);gui_core/menu.pyresolved menu handlers witheval, nowgetattr(all<handler>entries in the shipped XMLs are plain attribute names), which also removes the one pre-existing# nosec B307. The gmodeler loop-condition eval stays (a model's loop expression is authored by the model creator; evaluating it is equivalent to running the model) and is now documented and suppressed.calc1bands<N>via eval of a formatted call string, now aglobals()lookup with a normal call.Verification: the three temporal algebra testsuites (
unittests_temporal_algebra.py,unittests_temporal_raster_algebra.py,unittests_temporal_vector_algebra.py) pass against nc_spm; i.tasscap runs end-to-end on synthetic bands; bandit 1.9.4 reports no B307 findings in the touched files.This is part of a larger effort to work through the open code scanning alerts, grouped into small PRs by issue type.
Written with the assistance of Claude Code.