T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/cache_doc.py:130
- Finding
- Redirect Destinations Bypass the Intended Network Allowlist## Vulnerability Details **File Location**: `scripts/cache_doc.py`, lines 130-134 **Vulnerability Type**: Redirect validation weakness **Risk Level**: Medium ### Vulnerable Code ```python def fetch_html(url: str) -> str: req = urllib.request.Request(url, headers={'User-Agent': USER_AGENT}) with urllib.request.urlopen(req, timeout=20) as resp: charset = resp.headers.get_content_charset() or 'utf-8' return resp.read().decode(charset, errors='replace') ``` ### Technical Analysis The script validates the user-supplied URL before calling `fetch_html`, restricting its initial scheme and hostname to HTTPS and `kubernetes.io`. However, `urllib.request.urlopen()` follows HTTP redirects automatically, and the final response URL is not passed through `validate_url()`. Consequently, an initially permitted Kubernetes documentation URL could redirect to a different hostname, scheme, explicit port, or direct IP address. This creates a limited server-side request forgery and network-policy bypass condition. Content retrieved from the unapproved destination would also be written beneath a cache path representing official Kubernetes documentation, potentially causing untrusted content to be treated as authoritative. Exploitation depends on an accepted `https://kubernetes.io/docs/...` endpoint returning a redirect to an unapproved destination. The audit found no credential, kubeconfig, environment-variable, local-note, or cluster-state transmission in the request. ### Attack Path 1. An attacker identifies or causes an allowed `https://kubernetes.io/docs/...` endpoint to return an HTTP redirect. 2. The initial URL passes `validate_url()`. 3. `urllib.request.urlopen()` automatically follows the redirect. 4. The redirect target is accessed without reapplying the hostname, scheme, port, direct-IP, path, or query restrictions. 5. The response from the unapproved destination is parsed and stored under the trusted ...[truncated 741 chars]
- Remediation
- ## Remediation Suggestions - Disable automatic redirect handling and process redirects explicitly. - Apply `validate_url()` to every redirect destination before following it. - Resolve relative `Location` headers against the current validated URL and then validate the resulting absolute URL. - Reject redirects to non-HTTPS schemes, hosts other than `kubernetes.io`, explicit ports, direct IP addresses, user-information components, or paths outside `/docs`. - After opening the response, validate `resp.geturl()` as a defense-in-depth control before reading or caching content. - Set a small maximum redirect count and reject redirect loops. - Record the final validated source URL in cache metadata so the stored provenance is accurate.
