T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:45- Finding
API Key Persisted in a Plaintext Environment File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 45-67
Vulnerability Type: Plaintext sensitive-data 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 FalseThe persistence requirement is also specified in
SKILL.md, lines 15-17 and 25-27, which directs the agent to collect a key from the user and callscripts.config.set_api_key()to save it.Technical Analysis
The supplied API credential is written directly to
.envas plaintext. The code relies on ambient process permissions and does not explicitly enforce owner-only access, protect against symbolic-link replacement, use an operating-system credential store, or ensure that.envis excluded from source control.The file path is relative to the process working directory. Consequently, the secret may be written into a shared, backed-up, or version-controlled directory. Existing files are also rewritten without checking whether
.envis a symbolic link.Attack Path
- The Skill asks the user to provide an Xiaobenyang API key.
set_api_key()passes the key tosave_api_key_to_env().- The function wri ...[truncated 1030 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer session-only credential handling or an operating-system credential manager instead of writing the key to the project directory.
- If persistence is essential, obtain explicit user consent and explain the storage location and risks.
- Create the credential file atomically with owner-only permissions such as
0600; verify and repair permissions on existing files. - Reject symbolic links and non-regular files before reading or replacing
.env. - Store credentials in a user-specific configuration directory rather than the current working directory.
- Add
.envto.gitignoreand provide automated secret-scanning checks for commits. - Avoid copying the credential into additional process state unless required, and provide a documented mechanism to delete or rotate it.
- Never include the key in logs, exceptions, telemetry, or API response data.
