Security: Fix CWE-95 (Eval Injection) in _get_torch_dtype()#1964
Open
Security: Fix CWE-95 (Eval Injection) in _get_torch_dtype()#1964
Conversation
Replace dangerous eval() with secure lookup table to prevent arbitrary code execution. - Vulnerability: The _get_torch_dtype() method used eval() with insufficient validation, allowing arbitrary code execution through the torch_dtype parameter via __globals__ chain - Severity: HIGH (CVSS 7.8) - CWE: CWE-95 (Eval Injection) Changes: - src/unitxt/inference.py: Replace eval() with explicit whitelist of 21 valid torch dtypes - tests/inference/test_inference_engine.py: Add comprehensive security tests - test_torch_dtype_security_fix(): Full integration test - test_torch_dtype_security_fix_fast(): Fast unit test (1.7s) Security improvements: - Blocks arbitrary code execution via __globals__ chain - Rejects malicious payloads without executing any code - No breaking changes - all legitimate torch dtypes continue to work - Better performance (dict lookup vs eval) - Clearer error messages listing supported values Reported by: External security researcher via IBM PSIRT Signed-off-by: Yoav Katz <katz@il.ibm.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: CWE-95 (Eval Injection)
Summary
Fixed a HIGH severity security vulnerability (CVSS 7.8) in the
_get_torch_dtype()method that could allow arbitrary code execution.Vulnerability Details
src/unitxt/inference.py:550(before fix)The
_get_torch_dtype()method used Python'seval()function with insufficient input validation, which could be exploited to execute arbitrary code.The Fix
Replaced the
eval()call with a secure lookup table containing an explicit whitelist of 21 valid torch dtypes:Changes
test_torch_dtype_security_fix()- Full integration testtest_torch_dtype_security_fix_fast()- Fast unit test (1.7s)Security Improvements
✅ Eliminates code injection vulnerability
✅ Validates input against explicit whitelist
✅ No breaking changes - all legitimate torch dtypes continue to work
✅ Better performance (dict lookup vs eval)
✅ Clearer error messages listing supported values
Testing
All tests pass including new security-specific tests:
pytest tests/inference/test_inference_engine.py -k test_torch_dtype_security -v # PASSEDImpact Assessment
Security Note
Detailed exploitation information has been shared privately with the security team and will be disclosed responsibly after the fix is released.