T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:40- Finding
API Key Stored in Plaintext with Dotenv Injection Risk
- 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 API key is written directly to `.env` as plaintext. The code does not create the file with explicit restrictive permissions, so its confidentiality depends on the process umask and pre-existing file permissions. More critically, `set_api_key()` only rejects empty values. Calling `strip()` removes leading and trailing whitespace but does not reject embedded carriage-return or newline characters. Consequently, an attacker-controlled key can append additional dotenv assignments. This is security-sensitive because `Settings` loads variables with the `XBY_GAOKAO_` prefix from `.env`. An injected assignment such as `XBY_GAOKAO_BASE_URL` can therefore ...[truncated 1835 chars]- Remediation
View remediation
