T09 · Insecure Skill Coding Practices
Error
- Location
- baidu_ocr.py:18
- Finding
- Hardcoded Baidu API Credentials Exposed in Distributed Source Code## Vulnerability Details **File Location**: `baidu_ocr.py:18-31` **Additional Exposure**: `ACTIVATE_GUIDE.md:5` exposes the same API key and states that it is valid. **Vulnerability Type**: Hardcoded cloud service credentials **Risk Level**: High ### Vulnerable Code ```python # 百度 OCR 配置 API_KEY = "4LceeJ8wBDSqa3SqDHmgXuk1" SECRET_KEY = "nIulIWxqaUtY5XyfexSvP4OL8ZBk0krR" # 获取 access_token def get_access_token(): """获取百度 API 的 access_token""" url = "https://aip.baidubce.com/oauth/2.0/token" params = { "grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY } ``` ### Technical Analysis The source code embeds a live-looking Baidu API key and secret key directly in the distributed Python file. The credentials are supplied to Baidu's OAuth client-credentials endpoint to obtain an access token. This contradicts the environment-based configuration declared by the Skill: - `SKILL.md` declares `BAIDU_API_KEY` and `BAIDU_SECRET_KEY` as required environment variables. - `metadata.json` also declares those environment variables. - The implementation never reads them and instead always uses the embedded credentials. Secrets stored in source code cannot be protected through normal runtime access controls. Anyone who can download, inspect, fork, cache, or otherwise access the Skill package can recover and reuse both values. Removing the credentials from a later release would not invalidate copies already distributed. The image upload itself is necessary for the declared cloud OCR functionality: the selected image is Base64-encoded and transmitted over HTTPS to the documented Baidu OCR service. Base64 is transport encoding rather than encryption, but no covert destination or unrelated data collection was identified. The confirmed vulnerability is the credential exposure, not the expected OCR upload. ### Attack Path ...[truncated 1524 chars]
- Remediation
- ## Remediation Suggestions 1. **Immediately revoke and rotate the exposed credentials** - Treat both embedded values as compromised. - Revoke or regenerate the API key and secret through the Baidu AI console. - Review recent API usage, billing activity, enabled services, and authentication logs for unauthorized activity. 2. **Remove all credential values from the project** - Delete the constants from `baidu_ocr.py`. - Remove the API key from `ACTIVATE_GUIDE.md`. - Purge secrets from repository history and previously published artifacts where possible. 3. **Use the declared environment variables** ```python import os API_KEY = os.environ.get("BAIDU_API_KEY") SECRET_KEY = os.environ.get("BAIDU_SECRET_KEY") if not API_KEY or not SECRET_KEY: raise RuntimeError( "BAIDU_API_KEY and BAIDU_SECRET_KEY must be configured" ) ``` The program should fail closed when either variable is absent and must not fall back to bundled credentials. 4. **Protect credentials during operation** - Never print API keys, secret keys, or access tokens. - Restrict access to environment/configuration files containing credentials. - Where supported by the provider, avoid placing access tokens in URLs because query strings may be retained in logs; use an authorization header instead. 5. **Reduce cloud privileges** - Create a dedicated application credential for this Skill. - Enable only the OCR operations actually required. - Apply provider-side quotas, billing alerts, and usage monitoring. - Do not reuse the credential for unrelated Baidu AI services. 6. **Add secret-detection controls** - Run secret scanning in pre-commit hooks and CI. - Block publication when API credentials or token-like values are detected. - Document credential rotation and incident-response procedures.
