T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/hik_open_device_alarm_capability_management.py:103
- Finding
- Unrestricted Base URL Can Receive OAuth Credentials and Bearer Tokens## Vulnerability Details **File Location**: `scripts/hik_open_device_alarm_capability_management.py:103-114, 182-198, 245-262, 359` **Vulnerability Type**: Arbitrary credential destination and insecure transport configuration **Risk Level**: High ### Vulnerable Code ```python def normalize_base_url(base_url: str) -> str: normalized = base_url.strip() if not normalized: raise ApiError("base URL must not be empty") return normalized.rstrip("/") def resolve_base_url(explicit_base_url: str | None) -> str: if explicit_base_url: return normalize_base_url(explicit_base_url) env_base_url = os.getenv(BASE_URL_ENV_VAR) if env_base_url: return normalize_base_url(env_base_url) return DEFAULT_BASE_URL ``` ```python def fetch_access_token( base_url: str, client_id: str, client_secret: str, timeout: float, ) -> dict[str, Any]: status, payload = http_json_request( method="POST", url=base_url.rstrip("/") + TOKEN_PATH, headers=None, timeout=timeout, form_body={ "client_id": client_id, "client_secret": client_secret, "grant_type": "client_credentials", "scope": "app", }, ) ``` ```python def api_request( base_url: str, timeout: float, cache_file: Path, explicit_token: str | None, spec: RequestSpec, force_refresh: bool = False, ) -> dict[str, Any]: if force_refresh and cache_file.exists(): cache_file.unlink() token, refreshed = resolve_access_token(base_url, timeout, cache_file, explicit_token) headers = {"Authorization": f"Bearer {token}"} status, payload = http_json_request( method=spec.method, url=spec.build_url(base_url), headers=headers, timeout=timeout, json_body=spec.json_body, ) ``` ```pyt ...[truncated 3000 chars]
- Remediation
- ## Remediation Suggestions 1. Require `https` for all credential-bearing requests and reject plain HTTP. 2. Allowlist `api2.hik-cloud.com` as the production destination. 3. If custom environments are required, require an explicit development-mode opt-in and maintain a configurable allowlist of trusted hosts. 4. Parse the URL with `urllib.parse.urlsplit()` and reject: - Schemes other than HTTPS. - Missing or unapproved hostnames. - Embedded usernames or passwords. - Fragments. - Unexpected ports unless explicitly authorized. 5. Do not permit production client credentials or bearer tokens to be forwarded to arbitrary custom endpoints. 6. Consider using separate test credentials when a non-production endpoint is selected. 7. Add tests proving that HTTP URLs, attacker-controlled hosts, user-information URLs, and malformed authority components are rejected. 8. Clearly warn operators that command-line access tokens may be exposed through process listings; prefer environment injection or protected credential storage.
