T09 · Insecure Skill Coding Practices
- Location
scripts/rule_engine.py:91- Finding
Arbitrary Python Expression Evaluation in Dynamically Loaded Rules
- Content
View full analysis
bool: """安全求值 | Safe evaluation""" # 替换变量 | Replace variables expr = self._replace_variables(expression) # 解析逻辑运算符 | Parse logical operators expr = self._parse_logical_operators(expr) # 安全求值 | Safe evaluation try: result = eval(expr, {"__builtins__": {}}, {}) return bool(result) except: return False ``` Attacker-controlled rule conditions can reach this operation through JSON loading and DSL parsing: ```python def load_rules_from_json(self, json_str: str) -> None: rules_data = json.loads(json_str) for rule_data in rules_data: rule = Rule.from_dict(rule_data) self.add_rule(rule) ``` ```python if match: name = match.group(1) condition = match.group(2).strip() action_text = match.group(3).strip() ``` ### Technical Analysis The evaluator passes rule condition text to Python's `eval()` instead of interpreting it through a constrained business-rule grammar. Rule conditions may be loaded dynamically from JSON or parsed from DSL text and are not validated against an allowlist of permitted syntax. Supplying `{"__builtins__": {}}` does not create a reliable Python sandbox. Python expressions can access attributes and traverse the runtime object model. Depending on the objects and classes loaded into the hosting process, a crafted expression may reach powerful classes, callable objects, or globals and obtain capabilities that were intentionally removed from the evaluation namespace. The `_replace_variables()` operation is textual string replacement rather than syntactic parsing. It therefore neither enforces token boundaries nor prevents attribute access, function calls, comprehensions, dunder attributes, or other unsaf ...[truncated 2195 chars]- Remediation
View remediation
