Repository navigation
Replace exec with safe AST evaluation in read_python - #71
Conversation
7f9d152 to
577d816
Compare
rossant
left a comment
There was a problem hiding this comment.
Thanks for working on this! I agree with making config loading safer by default. Before merging, I’d like us to preserve useful Python expressions and avoid returning partial metadata.
Could you revise this along these lines?
- Support literals and containers, previously assigned variables, arithmetic, indexing/slicing, and bounded list/dict/set comprehensions, with a small documented set of helpers such as
range. - Reject unsupported constructs with a clear filename/line error instead of skipping them. An unsupported reassignment can otherwise leave an earlier sample rate or channel definition active.
- Add a keyword-only
allow_python_execution=Falseoption for trusted legacy configs. Phy could expose this as--allow-python-execution. There should be no automatic fallback, and enabling it should warn that the file can execute arbitrary Python. - Add compatibility and rejection tests, including limits on iteration and allocation.
Existing probe templates use more than literals—for example, this one uses a dictionary comprehension, variable references, and indexing.
Could you propose the supported syntax here and update the PR? Please also identify the companion Phy changes needed to expose the option through its loading commands.
|
Thanks for the quick turnaround on this — the One compatibility edge case, from hitting the identical pattern in the sibling fix (SpikeInterface/probeinterface#476): numpy >= 2 scalar reprs. Under numpy 2.x a numpy scalar's >>> import numpy as np
>>> repr(np.int64(385))
'np.int64(385)'
>>> f"{[np.int64(0), np.int64(1)]}"
'[np.int64(0), np.int64(1)]'So any file read by In probeinterface this surfaced as a Two options:
If it's useful I'm happy to open a small follow-up PR with the unwrap plus two regression tests: a numpy-scalar round-trip case, and one asserting that a non-literal statement is skipped rather than evaluated. Just say the word. Thanks again! |
|
Thanks, this can affect phylib too. I’ve added a focused commit for allowlisted NumPy scalar wrappers and regression tests: e9a07ea. Unsupported constructs should still raise rather than be skipped, per my earlier review; that broader change remains pending. |
|
Thanks for the review. Updated in f5db0e3 and ab4164e, rebased on e9a07ea. Safe mode (default): only top-level
Iterations and container sizes are each capped at 1e6. Anything else raises
Tests: an N-Form-style probe file, rejections, reassignment, iteration/allocation limits, and execution mode. Phy companion changes:
|
|
Thanks Olivier—the update addresses most of my earlier requests. An independent audit found three remaining issues:
Could you address these and add regression tests? The safe-default design and explicit trusted-execution option look good; I’d keep both. |
…_python_execution option
ab4164e to
7b024ca
Compare
rossant
left a comment
There was a problem hiding this comment.
Earlier requested compatibility and bounded-evaluation changes are addressed, including explicit trusted execution and documented practical limits. Current-head CI is green. This approval reflects the scoped maintenance review and is not a comprehensive security audit.
Python configuration loading previously executed the contents of
params.pyand probe.prbfiles. This change uses a restricted expression evaluator by default and raises a filename/line error for unsupported syntax instead of returning partial metadata.Supported expressions include literals and containers, previous assignments, arithmetic, comparisons, indexing/slicing, bounded list/set/dict comprehensions, a documented helper allowlist, and supported NumPy scalar representations. Generator expressions are rejected. Per-object size checks and iteration accounting provide practical limits; this is not a hard total memory/runtime bound or a comprehensive security sandbox.
Trusted legacy files can opt into Python execution with the keyword-only
allow_python_execution=Trueoption onread_python,load_probe,get_template_params,load_model,Merger, andEphysAlfCreator.convert. This logs a warning and has no automatic fallback.Regression coverage includes probe/config compatibility, NumPy scalar representations, unsupported statements and reassignment, pre-allocation checks, iterator/helper consumption, and explicit trusted execution. All nine CI checks pass on head
5c3b8d4b1870b7cf73808d52530a6f64f7b6653f.Stack: merge this PR first, preserving commit ancestry, then retarget #72 to
master. Companion Phy work will expose the opt-in through commands that load these configs, test compatibility, and document migration before the phylib release. No package publication is part of this PR.