-
Notifications
You must be signed in to change notification settings - Fork 3.8k
security: replace unrestricted setattr with allowlist in Python backend #28083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
titaiwangms
wants to merge
9
commits into
main
Choose a base branch
from
fix/backend-setattr-security-allowlist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c3a45d
security: replace unrestricted setattr with allowlist in Python backend
titaiwangms b2809b5
test: add security tests for kwargs allowlist in Python backend
titaiwangms 729d516
test: add enable_profiling blocked-attr security test
titaiwangms f7a2a21
fix: address review findings — hasattr reuse and docstring updates
titaiwangms ce8bee8
docs: add skill for Python kwargs setattr security pattern
titaiwangms b0f5c9b
style: apply lintrunner formatting and review suggestions
titaiwangms c398a93
fix: split kwargs in run_model, move skill, add run_model tests
titaiwangms 0d96324
fix: move skill from .github/skills to .agents/skills
titaiwangms 66c66ca
fix: use tempfile instead of hardcoded paths in security tests
titaiwangms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| --- | ||
| name: python-kwargs-setattr-security | ||
| description: When reviewing or fixing Python code that uses setattr() with user-controlled kwargs to configure C++ extension objects (SessionOptions, RunOptions, etc.) in ONNX Runtime. Use this to apply the allowlist pattern that prevents arbitrary file writes and other attacks via reflected property access. | ||
| --- | ||
|
|
||
| ## Problem Pattern | ||
|
|
||
| Using `hasattr(obj, k) / setattr(obj, k, v)` with user-controlled kwargs is insecure. The `hasattr` check is NOT a security guard — it returns True for ALL exposed properties including dangerous ones. | ||
|
|
||
| ```python | ||
| # INSECURE — do not use | ||
| for k, v in kwargs.items(): | ||
| if hasattr(options, k): | ||
| setattr(options, k, v) | ||
| ``` | ||
|
|
||
| ## Fix: Explicit Allowlist | ||
|
|
||
| Define a module-level frozenset of safe attribute names. Raise RuntimeError for known-but-blocked attrs; silently ignore unknown keys. | ||
|
|
||
| ```python | ||
| # Define at module level, before the class | ||
| _ALLOWED_SESSION_OPTIONS = frozenset({ | ||
| "enable_cpu_mem_arena", | ||
| "enable_mem_pattern", | ||
| # ... only explicitly reviewed safe attrs | ||
| }) | ||
|
|
||
| # In the method | ||
| for k, v in kwargs.items(): | ||
| if k in _ALLOWED_SESSION_OPTIONS: | ||
| setattr(options, k, v) | ||
| elif hasattr(options, k): # reuse the existing instance, don't create new | ||
| raise RuntimeError( | ||
| f"SessionOptions attribute '{k}' is not permitted via the backend API. " | ||
| f"Allowed attributes: {sorted(_ALLOWED_SESSION_OPTIONS)}" | ||
| ) | ||
| # else: silently ignore (may be kwargs for a different config object) | ||
| ``` | ||
|
|
||
| ## Key Rules | ||
|
|
||
| 1. **Use the existing object** in `hasattr(options, k)` — never `hasattr(ClassName(), k)` (creates throwaway C++ objects per iteration) | ||
| 2. **RuntimeError** is the ORT convention for API misuse errors (not ValueError) | ||
| 3. **Silent ignore for RunOptions** when the same kwargs are forwarded from a SessionOptions path (e.g. `run_model()` sends kwargs to both `prepare` and `rep.run`) | ||
| 4. **Frozenset constant naming**: `_ALLOWED_<CLASSNAME>` — ALL_CAPS, Google Style | ||
| 5. **No type annotations** on module-level constants (ORT Python convention) | ||
|
|
||
| ## Dangerous SessionOptions Properties (never allowlist) | ||
|
|
||
| - `optimized_model_filepath` — triggers Model::Save(), overwrites arbitrary files | ||
| - `profile_file_prefix` + `enable_profiling` — writes profiling JSON to arbitrary path | ||
| - `register_custom_ops_library` — loads arbitrary shared libraries (method, not property) | ||
|
|
||
| ## Files in ONNX Runtime | ||
|
|
||
| - `onnxruntime/python/backend/backend.py` — `_ALLOWED_SESSION_OPTIONS` | ||
| - `onnxruntime/python/backend/backend_rep.py` — `_ALLOWED_RUN_OPTIONS` | ||
| - Tests: `onnxruntime/test/python/onnxruntime_test_python_backend.py` — `TestBackendKwargsAllowlist` |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.