T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:45- Finding
API Key Persisted in an Unprotected 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") os.environ["XBY_APIKEY"] = api_key return True except Exception as e: print(f"保存API key失败: {e}") return False ``` The Skill instructions explicitly direct the agent to collect and save the credential: ```markdown - First use: if `.env` does not contain `XBY_APIKEY`, the agent must ask the user for the API key. - After receiving the key, call `scripts.config.set_api_key(api_key)` to save it. ``` ### Technical Analysis The Skill solicits an API credential and persists it as plaintext in a `.env` file relative to the process's current working directory. The implementation uses `Path.write_text()` without explicitly creating the file with owner-only permissions such as mode `0600`. The relative path also means the credential may be written outside the intended project directory when the Skill is launched from another working directory. Before writing, the implementation does not verify that `.env` is a regular file owned by the expected user, nor does it reject symbolic links. It reads and rewrites the d ...[truncated 1854 chars]- Remediation
View remediation
