Skip to content

security fix: Improve expression evaluation#827

Open
phbrgnomo wants to merge 5 commits intopolakowo:masterfrom
phbrgnomo:rce-fix
Open

security fix: Improve expression evaluation#827
phbrgnomo wants to merge 5 commits intopolakowo:masterfrom
phbrgnomo:rce-fix

Conversation

@phbrgnomo
Copy link

Draft PR: phbrgnomo#10

Vulnerability Fixes:

Remote code execution (RCE) from untrusted input evaluated by eval

Risk: RCE lets attackers execute arbitrary code, access sensitive data, pivot the environment, or fully compromise the process when untrusted input reaches eval.

Cause: eval executes strings as code. Passing data derived from external sources without strict validation or a strict allowlist enables arbitrary code injection.

Fix
Remove eval usage. If parsing literals, use ast.literal_eval(). For calculations or logic, implement explicit handlers or a whitelisted function map. Validate inputs strictly. If isolation is unavoidable, use a sandbox like RestrictedPython with minimal, immutable globals.

Note
Dynamic expression execution will be removed or restricted; inputs that previously executed arbitrary expressions may be rejected or behave differently.

Remote code execution (RCE) from external input evaluated by exec

Risk: Attackers could execute arbitrary code on the server, exfiltrate data, modify state, or fully compromise the host process.

Cause: User-controlled strings are passed to exec without strict validation or isolation, enabling injected code to run with application privileges.

Fix
Remove exec usage. Replace dynamic code execution with explicit functions or a dispatch map. For data-only evaluation, use ast.literal_eval. If expression evaluation is required, implement a strict whitelist parser and never pass user input to exec.

Note
If the application relied on executing arbitrary expressions, those scripts will no longer run; only explicitly allowed operations will execute.

Summary by Sourcery

Harden expression evaluation and remove unsafe dynamic code execution from the codebase.

Bug Fixes:

  • Replace template expression eval() usage with a restricted AST-based evaluator to prevent remote code execution from untrusted expressions.
  • Eliminate eval() in API docs generation when resolving default values for selected global objects, using an explicit whitelist mapping instead.

Enhancements:

  • Introduce a conservative allowlist for callable attributes (e.g., np.prod) that templates are permitted to access or invoke during expression evaluation.

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.

1 participant