T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:40- Finding
API Key Persisted in a Plaintext Environment File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 40-59
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 stores a user-supplied API key directly in a plaintext
.envfile.Path.write_text()does not explicitly enforce owner-only permissions, so the resulting access permissions depend on the process umask or permissions of an existing file. The file is also rewritten directly rather than through an atomic, permission-controlled replacement.Any local account or process with permission to read the project directory may recover the credential. The file may also be exposed through backups, workspace synchronization, artifact collection, or accidental source-control commits.
Attack Path
- The Skill asks the user to provide an API key.
- The Agent invokes
scripts.config.set_api_key(). set_api_key()invokessave_api_key_to_env().- The function writes
XBY_APIKEY=<secret>to.envas plaintext. - A local process, another user with directory access, a ...[truncated 505 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer a platform-managed secret store, operating-system keychain, or runtime-only environment injection instead of filesystem persistence.
- If file persistence is unavoidable, create the secret file with owner-only permissions such as
0600. - Write updates atomically to a permission-controlled temporary file and then replace the destination.
- Ensure
.envis excluded from source control, build artifacts, diagnostic bundles, and backups where possible. - Document credential rotation procedures and recommend immediate rotation if the workspace is exposed.
- Avoid retaining the key longer than required and never include it in logs or error messages.
