T03 · Remote Payload Retrieval and Execution
Error
- Location
- scripts/generator.py:259
- Finding
- Remote OpenAPI Fields Can Inject Executable Python into Generated Clients<![CDATA[ ## Vulnerability Details **File Location**: `scripts/generator.py:70-100, 259-318` **Vulnerability Type**: Untrusted remote input embedded into executable source code **Risk Level**: Critical ### Vulnerable Code ```python title=info.get('title', 'API').replace(' ', '-').lower(), ``` ```python endpoint = APIEndpoint( operation_id=operation.get( 'operationId', f"{method}_{path}" ).replace('/', '_').replace('{', '').replace('}', ''), method=method.upper(), path=path, summary=operation.get('summary', ''), description=operation.get('description', ''), parameters=operation.get('parameters', []), request_body=operation.get('requestBody'), responses=operation.get('responses', {}), auth_required=bool(spec_data.get('security', [])), tags=operation.get('tags', []), ) ``` ```python method_code = f''' def {endpoint.operation_id}(self, {param_str}): """ {endpoint.summary or endpoint.method + ' ' + endpoint.path} Method: {endpoint.method} Path: {endpoint.path} {chr(10).join(param_docs) if param_docs else ' '} Returns: dict: API response """ path = "{endpoint.path}" {self._generate_path_params_code(endpoint)} {self._generate_query_params_code(endpoint)} return self._request("{endpoint.method}", path){self._generate_body_code(endpoint)} ''' ``` ```python return f'''#!/usr/bin/env python3 """ {name} API Client Auto-generated from OpenAPI specification. """ ... class {name.replace('-', '').replace('_', '').title()}Client: ``` ### Technical Analysis The generator accepts OpenAPI documents from remote URLs and copies specification-controlled values into a Python source-code template. Values such as the API title, operation ID, endpoint path, summary, and parameter names are not validated as Python identifiers or safely serialized as Python string literals. Basic replacements of spaces, slashes, and braces do not preve ...[truncated 1786 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Validate every generated Python identifier against a strict rule such as: ```python ^[A-Za-z_][A-Za-z0-9_]*$ ``` Reject invalid values or convert them through a deterministic safe-identifier function. 2. Reject Python keywords using `keyword.iskeyword()`. 3. Never interpolate untrusted values directly into Python string literals or docstrings. Serialize literal values with `repr()` or generate an abstract syntax tree and emit code through a trusted code-generation library. 4. Keep human-readable OpenAPI text in non-executable data files rather than generated Python source where possible. 5. Parse the completed output with `ast.parse()` before writing or publishing it. This detects malformed output but must supplement, not replace, contextual escaping and identifier validation. 6. Treat remote specifications as untrusted content. Require explicit confirmation before generating executable artifacts and clearly identify their source. 7. Add adversarial tests for quotes, triple quotes, newlines, parentheses, colons, Unicode control characters, Python keywords, and exceptionally long field values. ]]>
