python-kwargs-setattr-security

द्वारा microsoft

जब Python कोड की समीक्षा या सुधार कर रहे हों जो C++ एक्सटेंशन ऑब्जेक्ट्स (SessionOptions, RunOptions, आदि) को कॉन्फ़िगर करने के लिए उपयोगकर्ता-नियंत्रित kwargs के साथ setattr() का उपयोग करता है...

npx skills add https://github.com/microsoft/onnxruntime --skill python-kwargs-setattr-security

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)

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

microsoft की और Skills

oss-growth
microsoft
OSS ग्रोथ हैकर व्यक्तित्व
official
accessibility-aria-expert
microsoft
React/Fluent UI वेबव्यू में पहुँच संबंधी समस्याओं का पता लगाता है और उन्हें ठीक करता है। स्क्रीन रीडर संगतता के लिए कोड की समीक्षा करते समय, ARIA लेबल ठीक करते समय, सुनिश्चित करते समय उपयोग करें…
official
generate-canvas-app
microsoft
[पुराना हो चुका है — इसके बजाय canvas-app का उपयोग करें] एक पूर्ण Power Apps कैनवास ऐप जनरेट करें।
official
django
microsoft
Django वेब डेवलपमेंट के लिए सर्वोत्तम अभ्यास जिसमें मॉडल, व्यू, टेम्पलेट और परीक्षण शामिल हैं।
official
github-issue-creator
microsoft
कच्चे नोट्स, एरर लॉग्स, वॉइस डिक्टेशन या स्क्रीनशॉट को साफ-सुथरे GitHub-फ्लेवर्ड मार्कडाउन इश्यू रिपोर्ट्स में बदलें। तब उपयोग करें जब उपयोगकर्ता बग जानकारी, एरर…
official
python-package-management
microsoft
निर्भरता प्रबंधन के लिए uv और कार्य स्वचालन के लिए poethepoet का उपयोग करता है।
official
runtime-validation
microsoft
माइग्रेटेड एप्लिकेशन के लिए रनटाइम सत्यापन — परीक्षण रणनीति (योजना चरण) और परीक्षण निष्पादन (सत्यापन चरण) को शामिल करता है: स्टार्टअप सत्यापन,…
official
azure-postgres-ts
microsoft
Azure Database for PostgreSQL Flexible Server से pg (node-postgres) पैकेज का उपयोग करके कनेक्ट करें, जिसमें पासवर्ड और Microsoft Entra ID (पासवर्डलेस) प्रमाणीकरण के लिए समर्थन है।
official