Security hardening: replace unsafe eval() with AST-based evaluator in configure_optimizers#1306
Open
abhayrajjais01 wants to merge 1 commit intoweecology:mainfrom
Open
Security hardening: replace unsafe eval() with AST-based evaluator in configure_optimizers#1306abhayrajjais01 wants to merge 1 commit intoweecology:mainfrom
abhayrajjais01 wants to merge 1 commit intoweecology:mainfrom
Conversation
849a7c8 to
3a0056e
Compare
Contributor
Author
|
@henrykironde i have fixed the conflict, let me know if any changes required |
1bcfe8e to
9d9e2a2
Compare
… configure_optimizers
9d9e2a2 to
286c556
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1306 +/- ##
==========================================
- Coverage 87.34% 87.14% -0.20%
==========================================
Files 24 24
Lines 2978 3019 +41
==========================================
+ Hits 2601 2631 +30
- Misses 377 388 +11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
Replace the unsafe
eval(params.lr_lambda)call inconfigure_optimizers()with a restricted AST-based evaluator (_safe_eval_lr_lambda) that only permits numeric constants, theepochvariable, parentheses, unary +/- and arithmetic operators (+, -, *, /, //, %, **).This prevents arbitrary code execution via malicious config overrides (CWE-95) while preserving full flexibility for valid scheduler expressions like
0.95 ** epoch.Closes #1305
Changes
src/deepforest/main.py: Addedimport ast, replacedeval(params.lr_lambda)withself._safe_eval_lr_lambda(params.lr_lambda, epoch), and added the_safe_eval_lr_lambdastatic method.tests/test_main.py: Addedtest_configure_optimizers_rejects_unsafe_lr_lambdaregression test that verifies malicious expressions raiseValueError.Testing
test_configure_optimizerstests unaffected (valid expressions like0.95**epochcontinue to work)Used Ai to review the changes