T09 · Insecure Skill Coding Practices
- Location
scripts/aisa_client.py:54- Finding
AIsa API Key Disclosure to Arbitrary HTTPS Destinations
- Content
View full analysis
str: if path_or_url.startswith("https://"): return path_or_url if not path_or_url.startswith("/"): path_or_url = "/" + path_or_url return API_BASE + path_or_url def post_json(url: str, payload: object) -> object: body = json.dumps(payload).encode("utf-8") request = urllib.request.Request( url, data=body, method="POST", headers={ "Authorization": f"Bearer {api_key()}", "Content-Type": "application/json", }, ) ``` The affected function is reached directly by the `data` command: ```python def command_data(args: argparse.Namespace) -> None: payload = load_json(args.payload) result = post_json(resolve_url(args.path), payload) write_output(args.out, json.dumps(result, ensure_ascii=False, indent=2)) ``` ### Technical Analysis The `data` command accepts a caller-controlled API path. The `resolve_url()` function treats any string beginning with `https://` as an acceptable absolute destination, without verifying that its parsed hostname is exactly `api.aisa.one`. The resulting URL is passed to `post_json()`, which unconditionally adds the value of `AISA_API_KEY` to the `Authorization` header. Consequently, the script forwards the credential to any HTTPS server selected by the caller. A string-prefix check is not an adequate origin validation mechanism. The destination must be parsed and validated by scheme, normalized hostname, port, and redirect behavior. `urllib.request.urlopen()` also follows HTTP redirects by default, so authorization forwarding and redirect handling should be explicitly constrained. ### Attack Path 1. An attacker persuades the user or agent to invo ...[truncated 1046 chars]- Remediation
View remediation
str: if "://" in path or path.startswith("//"): raise ValueError("Only relative AIsa API paths are allowed") if not path.startswith("/"): path = "/" + path return API_BASE + path ``` 2. If absolute URLs are required, parse and enforce an exact destination allowlist: - Scheme must be `https`. - Hostname must be exactly `api.aisa.one`. - Port must be absent or explicitly allowed. - URL user information must be rejected. - Do not use suffix or substring hostname matching. 3. Disable automatic redirects or validate every redirect target before following it. Never forward the `Authorization` header across origins. 4. Keep credential attachment destination-aware. Add the bearer token only after confirming the final request origin is the trusted AIsa API origin. 5. Add tests covering attacker-controlled hosts, deceptive hostnames, user-information syntax, alternate ports, protocol-relative URLs, and cross-origin redirects. 6. Rotate any API key that may previously have been used with an untrusted absolute URL. ]]>
