T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:45- Finding
API Key Persisted in a Plaintext Environment File
- Content
View full analysis
bool: """将API key保存到.env文件""" try: env_path = Path(".env") lines = [] if env_path.exists(): lines = env_path.read_text(encoding="utf-8").splitlines() found = False new_lines = [] for line in lines: if line.startswith("XBY_APIKEY="): new_lines.append(f"XBY_APIKEY={api_key}") found = True else: new_lines.append(line) if not found: new_lines.append(f"XBY_APIKEY={api_key}") env_path.write_text("\n".join(new_lines) + "\n", encoding="utf-8") ``` ### Technical Analysis The `save_api_key_to_env` function persistently writes the user-supplied API key to a `.env` file in plaintext. The code does not explicitly create or update the file with restrictive permissions. Consequently, the effective access controls depend on the process umask and any pre-existing file permissions. The project instructions explicitly direct the agent to collect the API key and invoke `set_api_key`, which calls this function. Therefore, credential persistence is part of the normal execution path rather than an unreachable utility. Although exploitation requires access to the local filesystem, project workspace, backups, or exported artifacts, any process or user able to read the `.env` file can recover the credential without further decoding or privilege escalation. ### Attack Path 1. A user supplies an `XBY_APIKEY` credential as required by the Skill. 2. The agent calls `scripts.config.set_api_key(api_key)`. 3. `set_api_key` invokes `save_api_key_to_env`. 4. The function stores the complete credential as `XBY_APIKEY=` in the working directory's `.env` file. 5. A local use ...[truncated 1116 chars]- Remediation
View remediation
