T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:48- Finding
API Key Persisted in a Plaintext Working-Directory File Without Permission or Symlink Protections
- Content
View full analysis
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 False ``` ### Technical Analysis The function stores the API key as plaintext in `.env`, resolved relative to the process's current working directory. It does not select a fixed, application-owned configuration directory or enforce restrictive file permissions such as `0600`. When `.env` is newly created, its effective permissions depend on the process umask. If the file already exists, its existing permissions are retained without validation or correction. As a result, the key can be exposed to other local users or processes where directory or file permissions allow access. The implementation also performs an ordinary path-based read followed by `Path.write_text()`. It does not reject symbolic links, verify file ownership, or use an atomic secure-file creation mechanism. A locally accessible attacker who can prepare the working directory could create `.env` as a symbolic link to another file writable by the Skill's process. The function would follow that link and rewrite the ...[truncated 2424 chars]- Remediation
View remediation
