T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/mx_stock_simulator.py:50
- Finding
- Configurable API destination can expose the API key and account requests<![CDATA[ ## Vulnerability Details **File Location**: `scripts/mx_stock_simulator.py`, lines 13-14 and 50-58 **Vulnerability Type**: Unvalidated credential-bearing request destination **Risk Level**: High ### Vulnerable Code ```python MX_APIKEY = os.environ.get('MX_APIKEY') MX_API_URL = os.environ.get('MX_API_URL', 'https://mkapi2.dfcfs.com/finskillshub') ``` ```python def make_request(endpoint, payload): """发送POST请求到API""" url = f"{MX_API_URL}{endpoint}" headers = { 'apikey': MX_APIKEY, 'Content-Type': 'application/json' } try: response = requests.post(url, headers=headers, json=payload, timeout=30) ``` ### Technical Analysis `MX_API_URL` is accepted directly from the environment without validating its scheme, hostname, port, or embedded credentials. Every request then transmits `MX_APIKEY` in an HTTP header to the configured destination. Consequently, an attacker who can influence the process environment or deployment configuration can replace the legitimate API endpoint with an attacker-controlled URL. The code does not require HTTPS and does not restrict the destination to the documented `mkapi2.dfcfs.com` host. In addition, `requests` follows redirects by default, and the code does not explicitly validate the final destination. ### Attack Path 1. The attacker obtains control over the Skill's environment configuration, launch script, container configuration, or another source that sets `MX_API_URL`. 2. The attacker sets `MX_API_URL` to an HTTP or HTTPS server under their control. 3. A user invokes a balance, holdings, order, cancellation, buy, or sell operation. 4. `make_request()` constructs the URL from the attacker-controlled base value. 5. The program sends the `apikey` header and operation payload to the attacker's server. 6. The attacker captures the API key and request data and may use the credential against the legitimate service, su ...[truncated 547 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions - Do not allow arbitrary origins for credential-bearing requests. - Parse the configured URL with `urllib.parse.urlparse` and require: - The `https` scheme. - An explicitly approved hostname such as `mkapi2.dfcfs.com`. - An approved port. - No username or password embedded in the URL. - Prefer a fixed service origin unless alternate endpoints are operationally required. - Disable automatic redirects with `allow_redirects=False`, or validate every redirect target before following it. - Never forward the API key when a redirect changes the origin. - Fail closed when URL validation fails and avoid including the credential in error messages. - Consider using a narrowly scoped, short-lived API credential where supported. ]]>
