-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Merged
+259
−12
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b6fa95
security: replace unrestricted setattr with allowlist in Python backend
titaiwangms 5ec20bf
test: clean up stale docstrings and hardcoded path in allowlist tests
titaiwangms 99ec4e2
test: clarify which code path test_blocked_run_option_terminate_raise…
titaiwangms 4e74727
polish: address reviewer feedback on PR #28083
titaiwangms d277b35
docs: clarify run_model() docstring for pre-built model branches
titaiwangms 342ff99
docs: fix docstrings in backend.py and backend_rep.py
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: {', '.join(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 one path is OK when kwargs are forwarded to both paths**: `run_model()` passes the same kwargs dict to both `prepare()` (validates SessionOptions) and `rep.run()` (validates RunOptions). A RunOptions kwarg unknown to SessionOptions is silently ignored by `prepare()` — this is correct because `rep.run()` will validate it. Only raise RuntimeError when the attr exists on the target object but is blocked. | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.