T09 · Insecure Skill Coding Practices
- Location
scripts/search_images.py:316- Finding
Hardcoded Pexels API Credential Exposed in Source Code
- Content
View full analysis
Vulnerability Details
File Location:
scripts/search_images.py, lines 316–319
Vulnerability Type: Hardcoded API credential
Risk Level: MediumVulnerable Code
python # 方法 1: 使用 Pexels API(已配置 API key) pexels_api_key = "D6nVsjiUzdW7BctVWb3Hf8KdmkZ3qF0oZNPAu4labGgxbYsFHegeFNNO" if pexels_api_key and pexels_api_key != "your_api_key_here": result = search_pexels_images(query, pexels_api_key, count)The credential is subsequently transmitted to the official Pexels endpoint in
scripts/search_images.py, lines 124–134:python url = f"https://api.pexels.com/v1/search?query={encoded_query}&per_page={count}" headers = { 'Authorization': api_key, 'User-Agent': 'Weather Outfit Advisor / 1.0' } req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req, timeout=10) as response: data = json.loads(response.read().decode('utf-8'))Technical Analysis
A live-looking Pexels API credential is embedded directly in distributed source code. Anyone who can download, inspect, or execute the Skill can recover the credential without authentication.
Sending an authorization credential to the intended Pexels HTTPS endpoint is required for authenticated image searches. However, distributing a shared credential in the package is not required and violates least-secret-exposure practices. The key should be supplied at runtime through an environment variable or secret-management facility.
The image-search query is user-controlled and sent to Pexels as a URL query parameter. This transmission is consistent with the declared image-search functionality and does not, by itself, demonstrate malicious exfiltration. The confirmed vulnerability is the public exposure and automatic use of the embedded credential.
Attack Path
- An attacker obtains the Skill package or accesses its source repository.
- The attacker opens
scripts/search_images.pyand extracts the credential from line 316. - The attacker sup ...[truncated 1352 chars]
- Remediation
View remediation
Remediation Suggestions
-
Revoke and rotate the exposed credential immediately
- Treat the current key as compromised.
- Generate a replacement through the Pexels account console.
- Review API usage logs for unexpected requests or quota consumption.
-
Remove credentials from source code
- Load the key from an environment variable:
python import os pexels_api_key = os.environ.get("PEXELS_API_KEY") if pexels_api_key: result = search_pexels_images(query, pexels_api_key, count) else: result = search_bing_images(query, count)-
Fail safely when no credential is configured
- Use the existing manual-search fallback.
- Return a clear configuration error without printing or logging secret values.
- Do not ship a default shared production credential.
-
Use an appropriate secret-management facility
- For hosted deployments, inject the credential through the platform's secret store.
- Restrict access to only the process that performs Pexels requests.
- Avoid storing the key in tracked configuration files, command-line arguments, or generated output.
-
Clean repository history and distributed artifacts
- Remove the credential from prior Git revisions with an appropriate history-rewriting tool.
- Replace previously published packages where feasible.
- Rotation remains mandatory because history rewriting does not invalidate copied keys.
-
Add preventive controls
- Enable secret scanning in version control and CI.
- Add tests that reject key-like literals in committed source files.
- Apply provider-side quota limits and usage alerts.
- Document runtime configuration consistently;
SKILL.mdmentions environment-variable configuration, but the implementation currently does not use it.
-
