T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:43- Finding
API Key Persisted in a Plaintext Environment File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 43–62
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 True except Exception as e: print(f"保存API key失败: {e}") return FalseTechnical Analysis
The function permanently writes the user's API key to a plaintext
.envfile. It relies on the platform's default file-creation permissions and does not explicitly enforce owner-only access. If the file already exists with permissive permissions, those permissions remain unchanged.The code also uses a predictable relative path and does not reject symbolic links or perform an atomic, securely permissioned write. In an environment where another local principal can control the working directory, a malicious
.envsymlink could potentially redirect the credential write to another writable location.Attack Path
- The user supplies an API key as required by the Skill.
- The Skill passes the key to
set_api_key(), which invokessave_api_key_to_env(). - The key is written verbatim as
XBY_APIKEY=<secret>in the working directory's.envfile. - A local user or process w ...[truncated 765 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer an operating-system credential store or secret-management service instead of persisting credentials in a project file.
- If persistence is unnecessary, retain the key only in process memory or require it through a session-scoped environment variable.
- If file persistence is unavoidable, create the secret file atomically with owner-only permissions such as
0600. - Verify that the destination is a regular file owned by the expected user, and reject symbolic links before reading or writing it.
- Correct permissions on existing
.envfiles rather than retaining potentially permissive modes. - Add
.envto.gitignoreand relevant packaging exclusions, and document that it must not be committed, archived, or included in support bundles. - Support credential rotation and revoke any key that may already have been exposed.
