T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:41- Finding
API Credential Persisted in a Plaintext Environment File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 41–63
Vulnerability Type: Plaintext storage of sensitive credentials
Risk Level: MediumVulnerable Code
python def save_api_key_to_env(api_key: str) -> 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 TrueTechnical Analysis
The function persistently writes the user-provided API key to a plaintext
.envfile. It neither creates the file with an explicit owner-only permission mode nor validates or corrects the permissions of an existing file. Consequently, confidentiality depends on the process umask and the existing filesystem permissions.The implementation also follows the
.envpath without checking whether it is a symbolic link and replaces the file non-atomically. A local attacker with suitable access to the working directory could potentially redirect the write or observe the credential during file operations. Because the project does not include a.gitignore, the stored secret may also be accidentally committed or captured by backups and artifact collection.Attack Path
- A user supplies an API key as directed by the Skill documentation.
- The Agent invokes
set_api_key(), which callssave_api_key_to_env(). - The function stores the key as
XBY_APIKEY=<secret>in the working directory’s.envfile. - A local user or process wit ...[truncated 1086 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer session-only secret handling or an operating-system credential manager instead of persistent plaintext storage.
- Obtain explicit user consent before persisting the API key.
- If file persistence is unavoidable, create the secret file with owner-only permissions (
0600) and verify or correct the permissions of existing files before reading or writing them. - Reject symbolic links and ensure the resolved destination is the intended regular file inside the expected project directory.
- Write updates atomically through a securely created temporary file in the same directory, apply restrictive permissions, and then replace the destination.
- Add
.envto.gitignoreand exclude it from packaging, logging, backups, diagnostics, and artifact collection. - Avoid retaining duplicate copies of the credential longer than necessary and never include it in exception messages.
- Document credential rotation procedures so users can promptly revoke and replace an exposed key.
