Repository navigation
Stop evaluating model-file parameter expressions as Python (#118) - #119
Merged
Merged
Conversation
ModelBlock.add_item, ParameterBlock.add_item and the network ParameterBlock.add_item passed every parameter expression to sympy.sympify(), which ends in eval(). A BNG-XML file with a crafted expr attribute therefore ran arbitrary code when loaded with bionetgen.bngmodel(). Add bionetgen.core.utils.safe_expr.fold_numeric, which checks the expression with ast and hands it to SymPy only if it is arithmetic on numbers, names and a short list of math functions; anything else is left as text. All three call sites use it. Constant expressions such as 2*3, 2^3 and exp(0) still fold to numbers as before. Fixes #118. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
CI note: the |
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 #118. Thanks to @r3vpwnx for the clear report and proof of concept.
The problem
ModelBlock.add_item,ParameterBlock.add_item(bionetgen/modelapi/blocks.py) and the networkParameterBlock.add_item(bionetgen/network/blocks.py) passed every parameter expression tosympy.sympify()to fold constants to numbers.sympifyon a string ends in Pythoneval, so a BNG-XML file with a craftedexprattribute ran arbitrary code when loaded withbionetgen.bngmodel(). The code came in with #104 (July 2026) and is in 0.8.7.The fix
A new helper,
bionetgen.core.utils.safe_expr.fold_numeric, parses the expression withastfirst. It calls SymPy only if the expression is plain arithmetic: numbers, names that do not start with_,+ - * / ** ^, unary+/-, and calls to a short list of math functions (exp,ln,log,sqrt,sin,min,maxand similar). Attribute access, subscripts, strings, lambdas, keyword arguments, other calls, and anything over 1,000 characters are rejected. Rejected expressions are left unchanged as text, which is what the old code did whensympifyraised. All three call sites now use the helper, and the localimport sympyblocks are gone.Behavior for legitimate models is unchanged:
2*3gives6.0,2^3gives8.0,exp(0)gives1.0, and symbolic expressions such ask1*2stay as text.Tests
tests/test_safe_expr.py(21 tests):bionetgen.bngmodel(), and asserts that the marker is never written. This test fails on main (the marker is written) and passes here;2*3gives6.0);Full suite on this branch (macOS, Python 3.12): 603 passed, 13 skipped, 1 failed. The failure,
test_run_atomize_tool.py::test_runAtomizeTool_write_scts_and_graphs, isModuleNotFoundError: No module named 'pyyed'in my environment, and it fails the same way on main. Black is applied to the new files.Not in this PR (follow-ups)
sympify/parse_exprcalls on file-derived text: the atomizer (sbml2bngl.py,bngModel.py,libsbml2bngl.py, on SBML math rendered by libsbml) andmodelapi/sympy_odes.py. Their inputs are more constrained, but they deserve the same review.9^9^9^9passes the check, and SymPy may hang computing it. That is a denial of service rather than code execution, and the old code had the same behavior.SECURITY.mdso that future reports can come in privately (GitHub private vulnerability reporting).pip install -e .fails in a fresh Python 3.12 venv, becausesetup.pyrunspip install numpyinside the isolated build. This is likely related to pkg_resources update breaks bngl #59 and PyBioNetGen fails with setuptools ≥81 (Python 3.12) #61.🤖 Generated with Claude Code