T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:45- Finding
API Key Persisted in a Plaintext .env File Without Access-Control Hardening
- 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 ``` The Skill instructions explicitly require this persistence behavior: ```markdown - 拿到用户提供的密钥后,调用 `scripts.config.set_api_key(api_key)` 保存,然后继续处理 ``` ### Technical Analysis The application stores the user-supplied `XBY_APIKEY` credential as unencrypted text in a predictable `.env` file in the current working directory. The file is written with `Path.write_text()` without explicitly enforcing owner-only permissions, validating that the destination is a regular file, or rejecting symbolic links. Actual file permissions depend on the process umask and the state of an existing `.env` file. Consequently, the code does not itself guarantee that on ...[truncated 2173 chars]- Remediation
View remediation
