T09 · Insecure Skill Coding Practices
- Location
scripts/mx_stock_simulator.py:11- Finding
API Credential Can Be Disclosed to an Untrusted Configurable Endpoint
- Content
View full analysis
Vulnerability Details
File Location:
scripts/mx_stock_simulator.py, lines 11-25
Vulnerability Type: Unrestricted credential-bearing outbound request
Risk Level: HighVulnerable Code
python MX_APIKEY = os.environ.get('MX_APIKEY') MX_API_URL = os.environ.get('MX_API_URL', 'https://mkapi2.dfcfs.com/finskillshub') OUTPUT_DIR = '/root/.openclaw/workspace/mx_data/output' os.makedirs(OUTPUT_DIR, exist_ok=True) def make_request(endpoint: str, body: Dict[str, Any], output_prefix: str) -> None: """发送POST请求并保存结果""" full_url = f"{MX_API_URL}{endpoint}" headers = { 'apikey': MX_APIKEY, 'Content-Type': 'application/json' } try: response = requests.post(full_url, headers=headers, json=body)Technical Analysis
The application reads
MX_API_URLdirectly from the environment and sends the secretMX_APIKEYto the resulting URL. It does not parse or validate the URL, enforce HTTPS, restrict the destination hostname or port, reject embedded credentials, or explicitly disable redirects.Environment variables are configuration inputs rather than inherently trusted values. If an attacker, deployment configuration, wrapper process, or compromised environment can modify
MX_API_URL, the credential-bearing request can be redirected to an attacker-controlled HTTP or HTTPS server. Plain HTTP values would additionally expose the credential to network interception.Because the API key is placed in a custom
apikeyheader, redirect behavior also needs explicit control. The implementation does not verify the destination of any redirect before a subsequent request is made.Attack Path
- An attacker gains the ability to influence the process environment or deployment configuration.
- The attacker sets
MX_API_URLto an endpoint under their control, such ashttps://attacker.example. - A user invokes any implemented query for positions, fu ...[truncated 1027 chars]
- Remediation
View remediation
Remediation Suggestions
- Parse the base URL with
urllib.parse.urlparse. - Require the
httpsscheme and reject plaintext HTTP. - Allowlist the documented hostname,
mkapi2.dfcfs.com, unless additional trusted hosts are explicitly required. - Reject embedded usernames, passwords, fragments, unexpected ports, malformed paths, and ambiguous hostnames.
- Disable automatic redirects with
allow_redirects=False, or validate every redirect destination before resending credentials. - Construct URLs using a safe URL-joining mechanism rather than direct string concatenation.
- Fail closed when the API key is missing or the endpoint fails validation.
- Use a narrowly scoped, revocable API credential and rotate any credential potentially exposed through an untrusted configuration.
Example validation policy:
python from urllib.parse import urlparse ALLOWED_API_HOSTS = {"mkapi2.dfcfs.com"} parsed = urlparse(MX_API_URL) if ( parsed.scheme != "https" or parsed.hostname not in ALLOWED_API_HOSTS or parsed.username is not None or parsed.password is not None or parsed.port not in (None, 443) ): raise ValueError("Untrusted API URL") response = requests.post( full_url, headers=headers, json=body, allow_redirects=False, timeout=(5, 30), )- Parse the base URL with
