microsoft/onnxruntime · Archived

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.

First seen May 15, 2026

Installation

$ npx skills add microsoft/onnxruntime --skill python-kwargs-setattr-security

Summary

  • 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.

Stronger alternatives

This repository is archived — consider an actively maintained alternative.

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from microsoft/onnxruntime · top by installs.

npx skills add microsoft/onnxruntime

Browse all from microsoft/onnxruntime

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Repository health

Stars 21.8K
License LICENSE
Default branch main
Open issues 844
Status Archived

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 2,967 B
  • docs SUMMARY.md 334 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 4 installs

SKILL.md

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.

# 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.

# 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)

  • optimizedmodelfilepath — triggers Model::Save(), overwrites arbitrary files
  • profilefileprefix + enable_profiling — writes profiling JSON to arbitrary path
  • registercustomops_library — loads arbitrary shared libraries (method, not property)

Files in ONNX Runtime

  • onnxruntime/python/backend/backend.py — ALLOWEDSESSION_OPTIONS
  • onnxruntime/python/backend/backendrep.py — ALLOWEDRUNOPTIONS
  • Tests: onnxruntime/test/python/onnxruntimetestpython_backend.py — TestBackendKwargsAllowlist