T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/openapi-gen.sh:155
- Finding
- Unescaped User-Controlled Values Permit YAML and OpenAPI Document Injection<![CDATA[ ## Vulnerability Details **File Location**: `scripts/openapi-gen.sh`, lines 155–185 **Vulnerability Type**: YAML document injection through unsafe string interpolation **Risk Level**: Medium ### Vulnerable Code ```bash cmd_generate() { local source="${1:-}" output="openapi.yaml" api_title="" api_version="1.0.0" format="yaml" server_url="" shift 2>/dev/null || true [ -z "$source" ] && die "Usage: openapi-gen.sh generate <source> [--output <path>] [--title ...]" while [ $# -gt 0 ]; do case "$1" in --output) output="$2"; shift 2 ;; --title) api_title="$2"; shift 2 ;; --version) api_version="$2"; shift 2 ;; --format) format="$2"; shift 2 ;; --server) server_url="$2"; shift 2 ;; *) shift ;; esac done [ -z "$api_title" ] && api_title="$(basename "$source" | tr '[:lower:]' '[:upper:]' | head -c20) API" echo "=== OpenAPI Spec Generation ===" echo "Title: $api_title" echo "Version: $api_version" echo "Source: $source" echo "Output: $output" echo "" cat > "$output" <<YAML openapi: 3.0.3 info: title: "${api_title}" version: "${api_version}" description: "Auto-generated by openapi-gen.sh v${VERSION}" servers: - url: "${server_url:-http://localhost:8080}" ``` ### Technical Analysis The `--title`, `--version`, and `--server` arguments are controlled by the caller and are inserted directly into a YAML here-document. Quotation marks, newlines, backslashes, and other YAML-significant characters are not escaped. Wrapping an interpolated value in double quotes does not make this construction safe. An attacker can include a quote and newline that terminate the intended scalar and introduce additional YAML or OpenAPI properties. The resulting document could contain attacker-selected server entries, paths, extension fields, or external references. This does not directly execute commands in the shell because here-document expansion does not recursively evaluate command syntax found i ...[truncated 1608 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Generate YAML or JSON through a structured serializer rather than string interpolation. 2. Pass title, version, and server URL values as data to the serializer. 3. Reject carriage returns, line feeds, control characters, and invalid Unicode in scalar-only command-line options. 4. Validate `--server` as an absolute HTTP or HTTPS URL and apply an explicit policy for allowed schemes and hosts. 5. Validate the version and title against documented length and character constraints. 6. Parse and structurally validate the completed document before moving it to the requested output path. 7. Write to a securely created temporary file and atomically rename it only after successful validation. 8. Add regression tests using embedded quotes, multiline input, YAML tags, anchors, comments, and attempted field injection. ]]>
