T09 · Insecure Skill Coding Practices
- Location
scripts/create_dxf.py:204- Finding
Unsanitized Layer Names Permit DXF Structure Injection
- Content
View full analysis
List[str]: return [ "0", "CIRCLE", "8", layer, "10", f"{x:.6f}", "20", f"{y:.6f}", "30", "0.0", "40", f"{r:.6f}", ] def dxf_lwpolyline(layer: str, verts: List[Tuple[float, float, float]], closed: bool = True) -> List[str]: # verts: [(x,y,bulge), ...] out = [ "0", "LWPOLYLINE", "8", layer, "90", str(len(verts)), "70", "1" if closed else "0", ] ``` ### Technical Analysis DXF is a line-oriented format consisting of alternating group-code and value records. The application treats layer names as trusted values and places them directly after DXF group code `8`. Because no validation rejects c ...[truncated 2202 chars]- Remediation
View remediation
str: if not isinstance(value, str): raise ValueError(f"{field} must be a string") if not _DXF_LAYER_RE.fullmatch(value): raise ValueError( f"{field} must contain only letters, digits, underscore, " "period, or hyphen and must be 1-64 characters long" ) return value ``` 2. Replace direct string conversion with explicit validation: ```python layer_profile=validate_dxf_layer_name( layers.get("profile", "CUT_OUTER"), "layers.profile" ), layer_holes=validate_dxf_layer_name( layers.get("holes", "CUT_INNER"), "layers.holes" ), layer_notes=validate_dxf_layer_name( layers.get("notes", "NOTES"), "layers.notes" ), ``` Apply the same validation to `layers.outline`, `layers.etch`, and the top-level polyline `layer` field. 3. Reject carriage returns, line feeds, null bytes, tabs, and other control characters explicitly, even if the allowlist is later relaxed. 4. Do not silently coerce arbitrary values with `str()`. Require actual JSON strings so malformed or nested values fail validation. 5. Add regression tests using layer values containing: - `\n` and `\r\n` - DXF group-code sequences - Null bytes and tabs - Empty strings - Extremely long strings - Non-string JSON values 6. Parse the generated DXF with an independent DXF parser during testing and verify that the number and type of entities exactly match the validated specification. ]]>
