T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:47- Finding
API Key Persisted in Plaintext Without Restrictive File Permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 47–66
Vulnerability Type: Plaintext sensitive credential 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 True except Exception as e: print(f"保存API key失败: {e}") return FalseTechnical Analysis
The function persists a user-supplied API key in a plaintext
.envfile.Path.write_text()creates or overwrites the file using permissions derived from the process umask, but the implementation does not explicitly enforce owner-only access. It also does not inspect or correct the permissions of an existing.envfile.Consequently, the credential may be readable by other local users or processes when the runtime environment has a permissive umask, shared project directory, or preexisting
.envfile with unsafe permissions. The implementation also follows filesystem links without checking whether.envis a symbolic link, which can make the write target dependent on the surrounding filesystem state.Attack Path
- A user provides an API key as required by the Skill instructions.
- The agent invokes
set_api_key(), which callssave_api_key_to_env(). - The function writes the ...[truncated 1015 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer session-only credential handling or an operating-system credential manager instead of plaintext project files.
- If file persistence is necessary, create the credential file atomically with owner-only mode
0600. - When updating an existing file, verify and correct its permissions before writing.
- Refuse to write through symbolic links and validate that the destination is a regular file owned by the expected user.
- Store the credential outside shared project directories where practical.
- Ensure
.envis excluded from version control, build artifacts, backups, diagnostic bundles, and logs. - Document key revocation and rotation procedures in case the file is exposed.
- Avoid placing the credential in process-wide environment state unless required, because child processes may inherit it.
