T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/htaccess_gen.py:51
- Finding
- Apache Configuration Injection Through Unsanitized CLI Arguments<![CDATA[ ## Vulnerability Details **File Location**: `scripts/htaccess_gen.py:51-53`, `scripts/htaccess_gen.py:98-102`, `scripts/htaccess_gen.py:106-112`, `scripts/htaccess_gen.py:115-121`, `scripts/htaccess_gen.py:124-126`, `scripts/htaccess_gen.py:133-139`, and `scripts/htaccess_gen.py:142-147` **Vulnerability Type**: Apache configuration injection **Risk Level**: High ### Vulnerable Code ```python # CORS if args.cors: origin = args.cors_origin or "*" parts.append(section("CORS Headers")) parts.append("<IfModule mod_headers.c>") parts.append(f' Header set Access-Control-Allow-Origin "{origin}"') ``` ```python # Hotlink protection if args.hotlink_protection: domain = args.domain or "example.com" parts.append(section("Hotlink Protection")) parts.append("RewriteEngine On") parts.append("RewriteCond %{HTTP_REFERER} !^$") parts.append(f"RewriteCond %{{HTTP_REFERER}} !^https?://(www\\.)?{domain.replace('.', '\\.')} [NC]") parts.append("RewriteRule \\.(jpg|jpeg|png|gif|webp|svg)$ - [F,NC]") ``` ```python # IP blocking if args.block_ip: parts.append(section("IP Blocking")) parts.append("<RequireAll>") parts.append(" Require all granted") for ip in args.block_ip: parts.append(f" Require not ip {ip}") parts.append("</RequireAll>") ``` ```python # Directory index if args.index: parts.append(section("Directory Index")) parts.append(f"DirectoryIndex {args.index}") ``` ```python def redirect(args): code = args.type or 301 parts = [] parts.append("RewriteEngine On") parts.append(f"RewriteRule ^{args.from_path.lstrip('/')}$ {args.to} [L,R={code}]") print("\n".join(parts)) ``` ```python def rewrite(args): parts = [] parts.append("RewriteEngine On") if args.condition: for cond in args.condition: parts.append(f"RewriteCond {cond}") flags = args.flags or "[L,QSA]" parts.append(f"RewriteRule {args.pattern} {args.target} {flags}") ...[truncated 3267 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Reject `\r`, `\n`, and NUL characters in every argument before generating configuration. 2. Validate each structured option according to its intended grammar: - Parse `--block-ip` values with Python's `ipaddress.ip_address()` or `ipaddress.ip_network()`. - Restrict redirect status codes to an explicit allowlist such as `301` and `302`. - Parse CORS origins as URLs and permit only expected schemes and host syntax. - Validate domains as canonical hostnames rather than arbitrary strings. - Restrict index names to safe relative filenames and reject whitespace or Apache metacharacters. 3. Constrain redirect source patterns and destinations to the documented path or URL formats. 4. For rewrite expressions, reject line breaks even when advanced regular expressions are allowed. 5. If raw Apache syntax must be supported, expose it through a clearly named unsafe or trusted-input mode and document that it must never receive untrusted data. 6. Add unit tests covering newline injection, quotation termination, invalid CIDR values, malformed origins, unsupported status codes, and invalid filenames. 7. Validate generated configuration with an isolated Apache syntax check before deployment. ]]>
