T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:48- 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 def set_api_key(api_key: str) -> bool: """设置API key并持久化到.env""" if not api_key or not api_key.strip(): return False api_key = api_key.strip() if not save_api_key_to_env(api_key): return False # 更新全局 settings 实例 settings.api_key = api_key return True ``` ### Technical Analysis The Skill explicitly collects an API credential and persists it in a project-local `.env` file as plaintext. The write operation does not enforce restrictive file permissions, use an operating-system credential store, perform an atomic replacement, or verify that the target is a regular file rather than a symbolic link. The key is interpolated directly into an environment-file line. `set_api_key()` removes only leading and trailing whitespace. It does not reject embedded carriage-return or newline characters. Consequently, a crafted value can insert additional entries into `.env`. Those entries can affect later executions when ...[truncated 2272 chars]- Remediation
View remediation
