T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:45- Finding
Plaintext API Key Persistence with Unsafe File Handling
- 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") os.environ["XBY_APIKEY"] = api_key return True except Exception as e: print(f"保存API key失败: {e}") return False ``` The persistence behavior is explicitly required by `SKILL.md:16-18`, which directs the Agent to collect the key and call `scripts.config.set_api_key(api_key)`. ### Technical Analysis The Skill stores the user-supplied API key as plaintext in a relative `.env` file. It does not: - Set restrictive permissions such as `0600`. - Verify the owner or current permissions of an existing file. - Reject symbolic links. - Use a dedicated, trusted configuration directory. - Write the updated file atomically. - Use an operating-system credential store. Because the path is resolved relative to the process working directory, the destination may not be controlled exclusively by the Skill. An attacker with access to that directory could pre-create `.env` with permissive permissions or make it a symbolic link to another writable file. The API key is also copied into the process environment. This may expose it to child processes or diagnostic tools, depending on th ...[truncated 1364 chars]- Remediation
View remediation
