Skip to main content
Menu
Advisories

_get_fake_spatial_shape() ast.Name-only allowlist bypassable via Attribute chains — eval sandbox escape

GHSA-H89G-R5PC-WXFMPublished Updated GitHub

Description

F004 — _get_fake_spatial_shape eval() sandbox bypass

IDsecurin0days-MONAI-F004
SeverityLOW (internal API, requires attacker to influence shape parameter)
CVSSv3.1CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:H/A:H — Base 6.0
CVSSv4.0CVSS:4.0/AV:L/AC:H/AT:P/PR:L/UI:N/VC:H/VI:H/VA:H — Base 6.0
Exploit maturityCODE-EXEC (in a non-default flow)
CWECWE-95 Eval Injection
Pattern0day-patterns.md P11

1. Sink

monai/bundle/scripts.py:141-167

def _get_fake_spatial_shape(shape, p=1, n=1, any=1):
    ret = []
    for i in shape:
        if isinstance(i, int):
            ret.append(i)
        elif isinstance(i, str):
            if i == "*":
                ret.append(any)
            else:
                for c in _get_var_names(i):
                    if c not in ["p", "n"]:
                        raise ValueError(...)
                ret.append(eval(i, {"p": p, "n": n}))    # ← sink

2. Sandbox bypass

_get_var_names() walks the AST collecting ast.Name nodes only. Expressions whose tokens are ast.Attribute / ast.Subscript / ast.Call on constants pass the filter because they have no ast.Name references.

PoC strings that bypass the filter:

"(1).__class__.__bases__[0].__subclasses__()"     # leaks all subclasses incl. os module pieces
"(1).__class__.__mro__[1].__subclasses__()[0](...)" # construct via subclass chain
"int.__class__.__init__.__globals__"               # globals leak

Each contains zero ast.Name references — passes the c not in ["p", "n"] check trivially. eval() then evaluates the dotted attribute chain, returning a reference to e.g. the subprocess.Popen class.

3. Reachability

_get_fake_spatial_shape is called from:

  • monai/bundle/scripts.py:_get_real_input_data and verify_net_in_out — both invoked through bundle verify_net_in_out CLI, with shape coming from the metadata file. Bundle metadata is attacker-controlled in the same threat model as F002.

4. Recommended fix

Replace the ast.Name-only filter with ast.literal_eval:

import ast
try:
    expr_ast = ast.parse(i, mode="eval")
    # Only allow Name/Constant/BinOp/UnaryOp with restricted ops
    ALLOWED = (ast.Expression, ast.Name, ast.Load, ast.Constant,
               ast.BinOp, ast.UnaryOp, ast.Add, ast.Sub, ast.Mult, ast.Div,
               ast.Pow, ast.Mod, ast.USub, ast.UAdd)
    if any(not isinstance(node, ALLOWED) for node in ast.walk(expr_ast)):
        raise ValueError("disallowed AST node")
    ret.append(eval(compile(expr_ast, "<shape>", "eval"), {"p": p, "n": n}))
except Exception as e:
    raise ValueError(f"invalid shape expression: {i}") from e

5. Regression check

N/A — no git history.

---

Reporter

Submitted via SecurinDisclose. Coordinated-disclosure window: 90 days from 2026-05-29 (public disclosure target 2026-08-27).

Affected versions

References

GitHub · Project-MONAI/MONAI