T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:43- Finding
API Key Persisted in a Plaintext Environment File Without Permission 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 ``` ### Technical Analysis The Skill persists a user-provided API key directly into a plaintext `.env` file. It does not explicitly restrict the file to the owner, verify that the path is not a symbolic link, or use an atomic and securely created temporary file. For a newly created file, its permissions are determined by the process umask. In environments with permissive defaults, other local users or processes may be able to read it. If `.env` already exists, the code preserves potentially insecure permissions. The relative path also causes the credential to be written into the current working directory rather than a controlled application configuration directory. The read-modify-write operation is not atomic. Moreover, following an existing `.env` symbolic link could cause the API key to be written to another file selected by a local attacker who can prepare the working directory. ### Attack Path 1. A user activates the Skill without an existing API key. 2. The Skill requests the A ...[truncated 1018 chars]- Remediation
View remediation
