T09 · Insecure Skill Coding Practices
- Location
scripts/base.py:13- Finding
Unvalidated API Base URL Can Expose the Access Key to an Attacker-Controlled Endpoint
- Content
View full analysis
Vulnerability Details
File Location:
scripts/base.py, lines 13-37
Vulnerability Type: Unvalidated external endpoint and potential plaintext credential transmission
Risk Level: MediumVulnerable Code
python ACCESS_KEY = os.environ.get('QIBOOK_ACCESS_KEY') BASE_URL = os.environ.get('QIBOOK_BASE_URL') # ... url = f"{BASE_URL}/skill/entData/combinedQuery" headers = {'access_key': ACCESS_KEY} try: response = requests.get(url, headers=headers, params=filtered, verify=True, timeout=30)Technical Analysis
The application obtains
BASE_URLdirectly from theQIBOOK_BASE_URLenvironment variable and uses it to construct the destination of an authenticated HTTP request. It does not parse or validate the URL's scheme, hostname, port, or resolved network destination before attaching the access key.Consequently, a malicious or incorrectly configured value can direct the request to an attacker-controlled server. For example, setting the variable to an attacker-operated HTTPS URL causes the script to transmit the
QIBOOK_ACCESS_KEYvalue in the customaccess_keyheader. Setting it to anhttp://URL additionally transmits the credential without transport encryption. Theverify=Trueargument only validates TLS certificates when HTTPS is used; it neither requires HTTPS nor establishes that the destination is an approved QiBook host.Because the destination is configurable, the request functionality can also act as a limited server-side request primitive. Its reach is constrained to the fixed
/skill/entData/combinedQuerypath and a GET request, but it may still contact internal or loopback HTTP services if the runtime environment permits such connections.requests.get()follows redirects by default. The implementation does not independently validate redirect destinations, so hardening should also account for redirects rather than validating only the initial URL.Attack Path
- A ...[truncated 1506 chars]
- Remediation
View remediation
Remediation Suggestions
- Remove arbitrary runtime endpoint selection where possible and use a fixed official API origin in code or trusted deployment configuration.
- Parse the configured URL with
urllib.parse.urlparseand require:- The
httpsscheme. - An exact approved hostname or a narrowly defined hostname allowlist.
- No embedded username or password.
- Only approved ports.
- No unexpected path, query string, or fragment in the configured origin.
- The
- Reject loopback, link-local, private, multicast, and otherwise non-public resolved IP addresses unless they are explicitly required and trusted.
- Disable automatic redirects with
allow_redirects=False, or validate every redirect destination against the same scheme and hostname policy before following it. - Attach the access-key header only after destination validation succeeds.
- Apply least privilege, expiration, rotation, rate limits, and usage monitoring to the API key. Rotate the key if it may previously have been used with an untrusted base URL.
- Fail closed with a clear configuration error when the URL does not meet the policy.
- Add tests covering HTTP URLs, attacker-controlled domains, embedded credentials, unusual ports, loopback and private addresses, and redirects to unapproved destinations.
