Fix code injection (CWE-94) in PythonInitializer and argument injection (CWE-88) in ConsoleLeanOptimizer#9506
Closed
alanturing881 wants to merge 1 commit into
Conversation
…soleLeanOptimizer
PythonInitializer.AddPythonPaths() (CWE-94): replaced string-interpolated
PythonEngine.Exec() call with direct Python.NET API calls (sys.path.insert via
PyString), eliminating injection risk from paths containing single quotes or newlines.
ConsoleLeanOptimizer.RunLean() (CWE-88): replaced unquoted --parameters {parameterSet}
in ProcessStartInfo.Arguments with ProcessStartInfo.ArgumentList, which handles
per-argument escaping automatically and prevents injection via parameter values
containing spaces and known CLI flag names.
Co-Authored-By: iaohkut <thb2601@gmail.com>
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.
Security Fix — Code Injection (CWE-94) + Argument Injection (CWE-88)
Summary
PythonInitializer.AddPythonPaths(){parameterSet}inConsoleLeanOptimizer.RunLean()VULN-001 — Python Code Injection (
Common/Python/PythonInitializer.cs:143)Root cause: Path strings were directly interpolated into a Python code string passed to
PythonEngine.Exec(). On Linux, directory names can contain newlines and single quotes — both of which allow breaking out of the string literal and injecting arbitrary Python code.Fix: Pass each path as a
PyStringobject directly via the Python.NET API — no string interpolation needed, no injection possible.VULN-002 — Argument Injection (
Optimizer.Launcher/ConsoleLeanOptimizer.cs:93)Root cause: The
--parameters {parameterSet}segment was embedded inProcessStartInfo.Argumentswithout quoting. Since--parametersis registered asCommandOptionType.MultipleValuein the Lean argument parser, any space-separated token matching a known CLI flag is parsed as a new argument. AStaticOptimizationParametervalue containing spaces (e.g.,1 --algorithm-location /evil.py) injects additional arguments into every spawned backtest child process.Fix: Use
ProcessStartInfo.ArgumentList(.NET 5+) which handles per-argument quoting automatically.Impact
python-additional-pathsAffected versions
All versions
<= v2.4.0.1References