T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/_auth.py:11
- Finding
- Environment-Controlled API Base Can Exfiltrate Application Credentials and Access Tokens<![CDATA[ ## Vulnerability Details **File Location**: `scripts/_auth.py:11-13, 76-84`; authenticated requests are also issued through the same configurable base in `scripts/list_figures.py`, `scripts/upload_file.py`, `scripts/create_task.py`, and `scripts/poll_task.py` **Vulnerability Type**: Unvalidated authentication endpoint **Risk Level**: High ### Vulnerable Code ```python CONFIG_DIR = Path(os.environ.get("CHANJING_CONFIG_DIR", Path.home() / ".chanjing")) CONFIG_FILE = CONFIG_DIR / "credentials.json" API_BASE = os.environ.get("CHANJING_API_BASE", "https://open-api.chanjing.cc") ``` ```python url = API_BASE + "/open/v1/access_token" req = urllib.request.Request( url, data=json.dumps({"app_id": app_id, "secret_key": secret_key}).encode("utf-8"), headers={"Content-Type": "application/json"}, method="POST", ) try: with urllib.request.urlopen(req, timeout=30) as resp: body = json.loads(resp.read().decode("utf-8")) ``` ### Technical Analysis `CHANJING_API_BASE` completely controls the destination used to obtain an access token. The value is not checked for an approved hostname, HTTPS, an expected port, embedded credentials, or another safe deployment policy. When a cached token is unavailable or near expiry, `get_token()` sends the long-lived `app_id` and `secret_key` directly to this endpoint. The other scripts similarly attach `access_token` to requests constructed from the same environment-controlled base. Supporting alternate API deployments can be legitimate, but forwarding production credentials to any environment-selected destination exceeds safe minimum privilege. It also permits plaintext transmission if the configured URL uses HTTP. ### Attack Path 1. An attacker influences the environment inherited by the Skill process, its wrapper, job runner, or shell. 2. The attacker sets `CHANJING_API_BASE` to an attacker-controlled HTTP or HTTPS endpoint. 3. The user invokes an authenticated operation such as listing figures, u ...[truncated 936 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Require `https` for all credential-bearing endpoints and reject plaintext HTTP. 2. Allowlist `open-api.chanjing.cc` as the default and expected production hostname. 3. If custom deployments are required, use an explicit trusted-host configuration rather than accepting an unrestricted environment value. 4. Reject URLs containing user information, fragments, unexpected ports, or malformed hostnames. 5. Validate redirect destinations so credentials or bearer tokens cannot be redirected to an untrusted host. 6. Do not reuse production credentials automatically when a custom endpoint is configured. Maintain separate credentials scoped to each approved endpoint. 7. Consider requiring explicit user confirmation before sending secrets to any non-default host. 8. Document the endpoint trust model and fail closed when validation fails. ]]>
