T09 · Insecure Skill Coding Practices
Warning
- Location
- greetings.py:17
- Finding
- Broken and Misleading API Credential Handling## Vulnerability Details **File Location**: `greetings.py`, lines 17–18, 26–27, 35–36, 52–53, 89–94, and 123–127 **Vulnerability Type**: Insecure credential handling and undocumented environment dependency **Risk Level**: Medium ### Vulnerable Code The DashScope agents contain a literal placeholder instead of loading a credential securely: ```python "api_key": "TODO_REPLACE_WITH_ENV", "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", ``` The placeholder is placed directly in the HTTP authorization header: ```python req = urllib.request.Request( url, data=data, headers={ 'Content-Type': 'application/json', 'Authorization': f"Bearer {agent['api_key']}" } ) ``` An unrelated environment variable is read only after `main()` has finished, and its value is never used: ```python if __name__ == '__main__': main() import os API_KEY = os.getenv("DEEPSEEK_API_KEY") if not API_KEY: raise ValueError("DEEPSEEK_API_KEY 环境变量未配置") ``` ### Technical Analysis Four DashScope agent definitions use the literal value `TODO_REPLACE_WITH_ENV` as their API key. `call_dashscope()` places that value in an `Authorization: Bearer` header and sends it to the official DashScope HTTPS endpoint. The script later reads `DEEPSEEK_API_KEY`, but this occurs after `main()` and all API calls. The value is not assigned to the agent definitions and is never consumed by `call_dashscope()`. Consequently: 1. DashScope requests are sent with an invalid placeholder credential. 2. Setting `DEEPSEEK_API_KEY` does not authenticate the DashScope requests. 3. Omitting that unrelated variable causes the program to raise an exception after its main processing. 4. The placeholder design may encourage users to insert a real API key directly into source code, exposing it through source distribution, backups, or version-control history. The audited data flow does **not** show envi ...[truncated 1791 chars]
- Remediation
- ## Remediation Suggestions 1. Read the correct DashScope credential from a clearly named environment variable before invoking `main()`: ```python import os DASHSCOPE_API_KEY = os.environ.get("DASHSCOPE_API_KEY") if not DASHSCOPE_API_KEY: raise RuntimeError("DASHSCOPE_API_KEY is not configured") ``` 2. Remove `api_key` values from the static `AGENTS` configuration and pass the credential explicitly to the request function: ```python def call_dashscope(agent: dict, api_key: str) -> str: req = urllib.request.Request( url, data=data, headers={ "Content-Type": "application/json", "Authorization": f"Bearer {api_key}", }, ) ``` 3. Validate credentials before making any network request. Reject empty values and known placeholders such as `TODO_REPLACE_WITH_ENV`. 4. Remove the trailing `DEEPSEEK_API_KEY` block because it is unrelated to DashScope and does not affect authentication. 5. Never instruct users to place API keys directly in source files. Add secret files, if any are introduced, to `.gitignore`, and recommend environment variables or an operating-system secret manager. 6. Document `greetings.py`, its DashScope network destination, the information sent, and the required environment variable in `SKILL.md`. 7. Make greeting-related network access explicitly user-triggered and fail before transmission when configuration is invalid. 8. Avoid exposing authorization headers or credential values in exceptions, logs, or debug output.
