T09 · Insecure Skill Coding Practices
- Location
scripts/imagegen.py:91- Finding
Unrestricted API Endpoint Can Receive the Bearer API Key
- Content
View full analysis
Vulnerability Details
File Location:
scripts/imagegen.py, lines 91–99 and 130–143
Vulnerability Type: Unvalidated custom API endpoint causing credential disclosure
Risk Level: HighVulnerable Code
python def _resolve_base_url(self, cli_url): if cli_url: return cli_url.rstrip("/") env_url = os.environ.get("AI_HIVE_BASE_URL") if env_url: return env_url.rstrip("/") file_config = self._read_config_file() if file_config.get("base_url"): return file_config["base_url"].rstrip("/") return DEFAULT_BASE_URLpython def __init__(self, config): self.config = config self.base = config.base_url self.headers = { "Authorization": f"Bearer {config.api_key}", "Content-Type": "application/json", } def _url(self, path): return f"{self.base}/openapi/v1/{path}" def _request(self, method, url, **kwargs): self.config.log(f"{method} {url}") try: resp = requests.request( method, url, headers=self.headers, timeout=DEFAULT_TIMEOUT, **kwargs )Technical Analysis
The API base URL can be supplied through the
--base-urlcommand-line option, theAI_HIVE_BASE_URLenvironment variable, or the local configuration file. The value is accepted without validating its scheme or hostname.AiHiveClientsubsequently attaches the AI-HIVE bearer token to every request made to the resulting endpoint. Consequently, an attacker-controlled URL can receive the completeAuthorization: Bearer ...header. The implementation also permits plaintext HTTP endpoints, which can expose the token to network interception.This violates endpoint trust-boundary validation and credential-scoping principles. A production credential intended for AI-HIVE must not automatically be transmitted to arbitrary hosts selected through untrusted runtime configuration.
Attack Path
...[truncated 1291 chars]
- Remediation
View remediation
Remediation Suggestions
- Restrict authenticated requests to the expected AI-HIVE origin by default:
- Require the
httpsscheme. - Require the exact approved hostname, such as
ai-hive.iclip.cn. - Reject embedded credentials, unexpected ports, fragments, and ambiguous hostnames.
- Require the
- Remove arbitrary
--base-urlandAI_HIVE_BASE_URLoverrides from normal production use if they are unnecessary. - If custom endpoints are required for development:
- Require an explicit unsafe-development flag.
- Display the resolved destination and request confirmation.
- Use a separate development credential rather than the production AI-HIVE key.
- Never attach an AI-HIVE credential to a host outside the approved allowlist.
- Reject plaintext HTTP endpoints because bearer credentials would otherwise be exposed in transit.
- Parse URLs with
urllib.parse.urlparserather than relying on string operations, and compare normalized scheme, hostname, and port against an allowlist. - Add automated tests covering malicious hosts, HTTP URLs, user-info syntax, subdomain confusion, unexpected ports, and environment/configuration poisoning.
- Document credential rotation procedures and advise users to revoke and replace any key that may have been sent to an untrusted endpoint.
A hardened validation pattern could resemble:
python from urllib.parse import urlparse ALLOWED_API_ORIGINS = { ("https", "ai-hive.iclip.cn", 443), } def validate_base_url(value): parsed = urlparse(value) port = parsed.port or 443 if parsed.username or parsed.password: raise SystemExit("Credentials are not permitted in the API URL") if parsed.fragment: raise SystemExit("Fragments are not permitted in the API URL") if (parsed.scheme, parsed.hostname, port) not in ALLOWED_API_ORIGINS: raise SystemExit("Untrusted API endpoint") return value.rstrip("/")- Restrict authenticated requests to the expected AI-HIVE origin by default:
