T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/json_schema.py:180
- Finding
- Unsanitized schema names and property names allow generated-code injection<![CDATA[ ## Vulnerability Details **File Location**: `scripts/json_schema.py:180-187`, `scripts/json_schema.py:207-209`, and `scripts/json_schema.py:239-249` **Vulnerability Type**: Generated source-code injection **Risk Level**: High ### Vulnerable Code ```python def schema_to_typescript(schema, name="Root", indent=0): """Convert JSON Schema to TypeScript interface.""" pad = " " * indent lines = [] schema_type = schema.get("type", "any") if schema_type == "object": lines.append(f"{pad}interface {name} {{") props = schema.get("properties", {}) required = set(schema.get("required", [])) for key, prop in props.items(): opt = "" if key in required else "?" ts_type = _ts_type(prop) lines.append(f"{pad} {key}{opt}: {ts_type};") ``` ```python parts = [] required = set(schema.get("required", [])) for k, v in props.items(): opt = "" if k in required else "?" parts.append(f"{k}{opt}: {_ts_type(v)}") return "{ " + "; ".join(parts) + " }" ``` ```python if schema.get("type") == "object": lines.append("@dataclass") lines.append(f"class {name}:") props = schema.get("properties", {}) required = set(schema.get("required", [])) if not props: lines.append(" pass") for key, prop in props.items(): py_t = _py_type(prop) if key not in required: lines.append(f" {key}: Optional[{py_t}] = None") else: lines.append(f" {key}: {py_t}") ``` ### Technical Analysis The converter directly interpolates the CLI-controlled `--name` argument and schema-controlled property names into generated Python and TypeScript source code. It does not validate that these values are legal identifiers, escape special characters, safely quote property names, or account for reserved words. An attacker can therefore construc ...[truncated 1715 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Validate `--name` against a strict target-language identifier policy before generation. 2. Reject names containing whitespace, line breaks, punctuation, declaration delimiters, or other characters outside the permitted identifier syntax. 3. Maintain target-specific reserved-word lists and reject or deterministically rename reserved identifiers. 4. For TypeScript properties, emit safely escaped string-literal keys, such as JSON-encoded property names, when a key is not a valid unquoted identifier. 5. For Python dataclasses, do not interpolate arbitrary JSON property names as field identifiers. Generate safe field names and preserve the original JSON key through explicit serialization metadata or a key-mapping table. 6. Prefer rejection with a clear error over silently producing invalid or unsafe source. 7. Add tests using malicious names and keys containing newlines, quotes, braces, semicolons, comments, decorators, and reserved words. 8. Parse or compile generated artifacts during testing to verify that user-controlled text cannot create additional declarations or executable statements. ]]>
