T09 · Insecure Skill Coding Practices
- Location
scripts/pi_query.py:21- Finding
Unrestricted API Endpoint Override Can Disclose the AppKey
- Content
View full analysis
Vulnerability Details
File Location:
scripts/pi_query.py, lines 21 and 135–137
Vulnerability Type: Unvalidated API endpoint override and credential exposure
Risk Level: MediumVulnerable Code
python API_BASE_URL = os.environ.get("JIKE_API_BASE_URL", "https://api.jikeapi.cn").rstrip("/")python url = f"{API_BASE_URL}{API_PATH_MAP[command]}?{urllib.parse.urlencode({**params, 'appkey': appkey})}" try: with urllib.request.urlopen(url, timeout=15) as response: return json.loads(response.read().decode("utf-8"))Technical Analysis
The script permits the
JIKE_API_BASE_URLenvironment variable to replace the trusted API origin without validating its scheme or hostname. It then places the AppKey in the URL query string and sends the request to the configured origin.An attacker who can influence the process environment can set this variable to an attacker-controlled server. The server would receive the AppKey in the
appkeyquery parameter. The override also accepts a plaintext HTTP URL, allowing network observers or intermediaries to inspect the credential and query data.Passing credentials in a query string creates additional exposure because complete URLs may be recorded in proxy, web-server, monitoring, or diagnostic logs.
Attack Path
- The victim configures a valid
JIKE_PI_QUERY_KEYorJIKE_APPKEY. - An attacker, compromised launcher, CI configuration, wrapper script, or otherwise untrusted execution environment sets
JIKE_API_BASE_URLto an attacker-controlled URL, such ashttps://attacker.example. - The victim or agent invokes either supported Pi query command.
- The script constructs a URL under the attacker-controlled origin and appends the AppKey as the
appkeyquery parameter. urllib.request.urlopen()sends the request to that origin.- The attacker obtains the AppKey from the incoming request URL or associated server logs.
- The attacker can reuse the credential against the ...[truncated 683 chars]
- The victim configures a valid
- Remediation
View remediation
Remediation Suggestions
- Remove the runtime API-origin override from production builds and use the fixed trusted endpoint
https://api.jikeapi.cn. - If endpoint configurability is required for controlled testing:
- Parse the configured URL before use.
- Require the
httpsscheme. - Reject embedded credentials, fragments, unexpected ports, and malformed hosts.
- Enforce an explicit hostname allowlist, preferably only
api.jikeapi.cn. - Keep test endpoint overrides behind a separate development-only option.
- Send the AppKey in an API authorization header rather than the query string if the remote API supports it.
- If query-string authentication is mandated by the API, ensure that application, proxy, monitoring, and server logs redact the
appkeyparameter. - Treat process environment configuration as security-sensitive and prevent untrusted users, wrappers, or job definitions from modifying it.
- Add automated tests confirming that plaintext HTTP and non-allowlisted hosts are rejected before any credential-bearing request is sent.
- Rotate the AppKey if the script has previously run with an untrusted or unexpected
JIKE_API_BASE_URL.
- Remove the runtime API-origin override from production builds and use the fixed trusted endpoint
