Skip to content

Parse params.py with ast instead of exec in read_python - #4916

Merged
alejoe91 merged 2 commits into
SpikeInterface:mainfrom
tritsystem:fix-read-python-exec
Nov 9, 2026
Merged

alejoe91 merged 2 commits into
SpikeInterface:mainfrom
tritsystem:fix-read-python-exec

Conversation

@tritsystem

Copy link
Copy Markdown
Contributor

What

read_python() (core/core_tools.py) parsed a Python-style config file by exec()-ing its contents. That means reading a Phy/Kilosort folder (via PhySortingExtractor / read_phy, which reads params.py) or a Klusta config executes whatever is in that file. Since these outputs are routinely shared and downloaded, and params.py is expected to be a simple dictionary of values (sample_rate, n_channels, ...), this replaces the exec with a small ast-based parser that evaluates only literal assignments (numbers/strings/bools/None/list/tuple/dict, plus range()/list(range()) with integer args) and ignores everything else.

Why

We don't need to exec the params since it's a simple dictionary; this avoids executing arbitrary code from a params/config file.

Changes

  • read_python() rewritten to parse via ast (no exec). Fixes both the phy and klusta call sites. A realistic params.py parses unchanged; a params.py containing e.g. import os; os.system(...) is no longer executed.
  • Added a small regression test (core/tests/test_read_python_safe.py): a config with a side-effecting line is not executed, and the reader's source contains no exec/eval.

Note

I used Claude Code as an assistant to investigate and prepare this fix; I reviewed the analysis, the change, and the test before submitting.

@alejoe91 alejoe91 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@alejoe91 alejoe91 added the extractors Related to extractors module label Nov 9, 2026
@alejoe91
alejoe91 merged commit ad31636 into SpikeInterface:main Nov 9, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

extractors Related to extractors module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants