T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:40- Finding
API Key Stored in a Plaintext File Without Permission or Symlink Protections
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 40-68
Vulnerability Type: Plaintext credential storage and unsafe file handling
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 API key is persisted as plaintext in a
.envfile located relative to the process working directory. The implementation does not:- Restrict the resulting file to owner-only permissions.
- Verify that
.envis a regular file rather than a symbolic link. - Use an atomic, exclusive file-creation operation.
- Resolve the file under a trusted, application-specific configuration directory.
- Use an operating-system credential store.
Consequently, the effective confidentiality of the API key depends on the process umask, directory permissions, and integrity of the current working directory. If an attacker can inspect files in that directory, the API key can be recovered directly. If an attacker can prepare
.envas a symbolic link, invokingset_api_key()may overwrite another file writable by the running process.Attack Path
Credential disclosure path:
1 ...[truncated 1331 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer an operating-system credential manager, secret-management service, or environment-only configuration rather than persistent plaintext storage.
- If file storage is unavoidable, use a fixed application-specific configuration directory owned by the current user rather than the current working directory.
- Reject an existing path if it is a symbolic link or is not a regular file.
- Create the file atomically with owner-only permissions, such as mode
0600on POSIX systems. - Write through a securely created temporary file in the same directory, apply restrictive permissions, and atomically replace the destination.
- Verify ownership and permissions before reading an existing credential file.
- Add
.envto source-control ignore rules and document that it contains sensitive credentials. - Avoid including API-key values in exceptions, logs, or diagnostic output.
