Commit 4497127
authored
Security: Fix CWE-95 (Eval Injection) in _get_torch_dtype() (#1964)
fix: Security fix for CWE-95 (Eval Injection) in _get_torch_dtype()
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>1 parent 876b22a commit 4497127
2 files changed
Lines changed: 67 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
546 | 546 | | |
547 | 547 | | |
548 | 548 | | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
553 | | - | |
554 | | - | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
555 | 575 | | |
556 | | - | |
| 576 | + | |
557 | 577 | | |
558 | | - | |
559 | | - | |
| 578 | + | |
| 579 | + | |
560 | 580 | | |
561 | 581 | | |
562 | 582 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
658 | 658 | | |
659 | 659 | | |
660 | 660 | | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
0 commit comments