T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:46- Finding
API Key Persisted in a Plaintext File Without Restrictive Permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 46–68
Vulnerability Type: Plaintext storage of sensitive credentials with insufficient permission hardening
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-supplied API key to a plaintext
.envfile in the process working directory.Path.write_text()does not explicitly enforce owner-only permissions.For a newly created file, its permissions are determined by the process umask and may permit access by other local users. If
.envalready exists with overly broad permissions, rewriting it does not correct those permissions. The credential also remains on disk after the Skill finishes, increasing its exposure window.The audit confirmed that this key is subsequently used as the
XBY-APIKEYauthentication header for requests tohttps://mcp.xiaobenyang.com/api. Disclosure may therefore allow unauthorized use of the associated API account.Attack Path
- A user provides an
XBY_APIKEYcredential as required by the Skill. set_api_key()invokes `sav ...[truncated 1063 chars]
- A user provides an
- Remediation
View remediation
Remediation Suggestions
- Prefer session-only credential handling or an operating-system secret manager instead of persistent plaintext storage.
- If file persistence is necessary, store the credential in a dedicated application configuration directory rather than the current working directory.
- Create the secret file atomically with owner-only mode
0600; do not rely on the process umask. - When updating an existing file, explicitly 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.
- Avoid retaining the key longer than required and provide a secure credential-removal mechanism.
- Document where the key is stored, how it is protected, and which remote service receives it.
- Add automated tests confirming that newly created and updated secret files are inaccessible to group and other users.
