T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:42- Finding
API Key Persisted in a Plaintext Working-Directory File
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 42–62
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 as plaintext in
.env. The path is relative to the process's current working directory rather than a dedicated, access-controlled configuration directory. The implementation does not explicitly create the file with restrictive permissions or verify the permissions of an existing file.Consequently, the credential can be placed in an unrelated project directory, included accidentally in version control, exposed through backups, or made readable to other local users depending on the process umask and existing file permissions. Assigning the key to
os.environalso makes it available to code running in the same process and potentially to subsequently spawned child processes.Attack Path
- A user supplies an API key as required by the Skill workflow.
set_api_key()invokessave_api_key_to_env().- The function resolves `Path(".env") ...[truncated 870 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer session-only credential handling or an operating-system credential manager instead of plaintext persistence.
- Obtain explicit user consent before storing the key persistently.
- If file storage is required, use a deterministic, Skill-specific user configuration directory rather than the current working directory.
- Create the credential file atomically with owner-only permissions such as
0600, and reject or repair files with broader permissions. - Add
.envto source-control ignore rules and document that credential files must not be committed, copied into artifacts, or included in logs. - Avoid placing the key in the process environment unless required. Ensure that child processes do not inherit it unnecessarily.
- Support credential rotation and advise users to revoke any key that may already have been exposed.
