T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:43- Finding
API Key Persisted in a Plaintext File Without Explicit Permission Hardening
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py:43-64
Vulnerability Type: Plaintext sensitive-data storage with insufficient file-permission controls
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 supplied API key is written directly to
.envin the process's current working directory. The implementation does not use a platform secret manager, encrypt the credential, select a dedicated protected configuration directory, or explicitly restrict the file to owner-only access.Path.write_text()creates files using permissions derived from the ambient process umask. Consequently, deployments with a permissive umask may create a file readable by other local users or processes. If.envalready exists with unsafe permissions, updating it does not correct those permissions. Storing the file in the working directory also increases the possibility of accidental inclusion in source-control commits, build artifacts, backups, or shared workspace snapshots.The Skill documentation explicitly directs the agent to request and persist the user's API key, so this storag ...[truncated 1429 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer a platform-provided secret manager, operating-system credential store, or runtime-only environment variable instead of filesystem persistence.
- If persistence is essential, store the credential in a dedicated per-user configuration directory rather than the current working directory.
- Create the secret file atomically with mode
0600, and explicitly verify and repair permissions on existing files before reading or updating them. - Avoid following symbolic links when opening the destination, and use an atomic temporary-file-plus-rename procedure to prevent link and partial-write attacks.
- Add
.envto.gitignoreand exclude it from packaging, logging, diagnostics, backups, and workspace synchronization. - Do not print, return, or interpolate the API key into exceptions or logs.
- Document credential rotation and revocation procedures, and advise users to rotate any key that may already have been stored in an exposed workspace.
- Consider making persistence opt-in and clearly disclose the storage location before writing the credential.
