npx skills add ...
npx skills add microsoft/onnxruntime --skill python-kwargs-setattr-security
npx skills add microsoft/onnxruntime --skill python-kwargs-setattr-security
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.
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.
Define a module-level frozenset of safe attribute names. Raise RuntimeError for known-but-blocked attrs; silently ignore unknown keys.
hasattr(options, k) — never hasattr(ClassName(), k) (creates throwaway C++ objects per iteration)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._ALLOWED_<CLASSNAME> — ALL_CAPS, Google Styleoptimized_model_filepath — triggers Model::Save(), overwrites arbitrary filesprofile_file_prefix + enable_profiling — writes profiling JSON to arbitrary pathregister_custom_ops_library — loads arbitrary shared libraries (method, not property)onnxruntime/python/backend/backend.py — _ALLOWED_SESSION_OPTIONSonnxruntime/python/backend/backend_rep.py — _ALLOWED_RUN_OPTIONSonnxruntime/test/python/onnxruntime_test_python_backend.py — TestBackendKwargsAllowlist