Skip to content

Stop evaluating model-file parameter expressions as Python (#118) - #119

Merged
jrfaeder merged 2 commits into
mainfrom
fix/118-no-eval-of-parameter-expressions
Nov 24, 2026
Merged

jrfaeder merged 2 commits into
mainfrom
fix/118-no-eval-of-parameter-expressions

Conversation

@jrfaeder

Copy link
Copy Markdown
Contributor

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 network ParameterBlock.add_item (bionetgen/network/blocks.py) passed every parameter expression to sympy.sympify() to fold constants to numbers. sympify on a string ends in Python eval, so a BNG-XML file with a crafted expr attribute ran arbitrary code when loaded with bionetgen.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 with ast first. 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, max and 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 when sympify raised. All three call sites now use the helper, and the local import sympy blocks are gone.

Behavior for legitimate models is unchanged: 2*3 gives 6.0, 2^3 gives 8.0, exp(0) gives 1.0, and symbolic expressions such as k1*2 stay as text.

Tests

tests/test_safe_expr.py (21 tests):

  • loads the reporter's proof-of-concept XML, with a payload that writes a marker file, through bionetgen.bngmodel(), and asserts that the marker is never written. This test fails on main (the marker is written) and passes here;
  • checks that a constant XML parameter still folds (2*3 gives 6.0);
  • tests folding of ordinary arithmetic, and rejection of a list of unsafe forms.

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, is ModuleNotFoundError: 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)

  • Other sympify/parse_expr calls on file-derived text: the atomizer (sbml2bngl.py, bngModel.py, libsbml2bngl.py, on SBML math rendered by libsbml) and modelapi/sympy_odes.py. Their inputs are more constrained, but they deserve the same review.
  • An expression such as 9^9^9^9 passes 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.
  • Add a SECURITY.md so that future reports can come in privately (GitHub private vulnerability reporting).
  • pip install -e . fails in a fresh Python 3.12 venv, because setup.py runs pip install numpy inside 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

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>
@jrfaeder

Copy link
Copy Markdown
Contributor Author

CI note: the deploy (ubuntu-latest, 3.8) and (ubuntu-latest, 3.9) jobs fail in actions/setup-python before any code runs: "The version '3.8' with architecture 'x64' was not found for Ubuntu 26.04." GitHub's ubuntu-latest image no longer provides those Python versions, so the matrix needs updating (drop 3.8/3.9 or pin an older Ubuntu image). That belongs in a separate PR. The last CI run on main was in September, before the image change.

@jrfaeder
jrfaeder merged commit cbfa145 into main Nov 24, 2026
15 checks passed
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.

PyBioNetGen: arbitrary code execution when loading a BNG-XML model, via sympy.sympify() of parameter expressions (CWE-95)

1 participant