T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:44- Finding
API Key Stored Persistently in a Plaintext .env File Without Permission Hardening
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 44–61
Vulnerability Type: Plaintext sensitive-data storage
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 a user-supplied API key in plaintext to a relative
.envfile in the process working directory. It does not explicitly restrict file permissions, verify that the destination is private, reject symbolic links, or perform an atomic secure write.The project instructions require users to provide this key and direct the agent to persist it through
scripts.config.set_api_key(). Consequently, sensitive-data persistence is part of the expected workflow rather than an exceptional operation.Security exposure depends on the runtime environment and its default file-creation mask. If the resulting file is readable by other local users or processes, stored in a shared directory, included in a backup, or accidentally committed to source control, the API key can be disclosed. Use of a relative path also means the destination changes according to the caller's working directory.
No evidence was found that the project deliberately exfiltrates the key to an unrelated destination. The key is used as the
XBY-APIKEYheader when c ...[truncated 1571 chars]- Remediation
View remediation
Remediation Suggestions
- Prefer nonpersistent, session-scoped credential delivery through the process environment or a dedicated secret-management service.
- If persistence is necessary, obtain explicit user consent and store the key in an operating-system credential vault rather than a project
.envfile. - Resolve the destination to a dedicated per-user configuration directory rather than relying on the current working directory.
- On POSIX systems, securely create a new credential file with mode
0600and verify ownership and permissions before reading or updating it. - Reject symbolic links and non-regular files. Use safe file-opening primitives such as
os.open()withO_NOFOLLOW, where supported. - Write updates atomically through a securely created temporary file in the same private directory, apply restrictive permissions, and then replace the destination.
- Add
.envto.gitignoreand ensure packaging, logging, backup, and diagnostic processes exclude credential files. - Avoid placing the key in exception messages or logs, and document a credential rotation procedure for suspected disclosure.
- Validate the final storage permissions and fail closed if a private destination cannot be guaranteed.
