T09 · Insecure Skill Coding Practices
- Location
scripts/config.py:41- Finding
API Key Persisted Without Restrictive File Permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/config.py, lines 41–60
Vulnerability Type: Plaintext credential storage with uncontrolled file permissions
Risk Level: MediumVulnerable Code
python def save_api_key_to_env(api_key: str) -> bool: """Store API key in the .env file.""" 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"Failed to save API key: {e}") return FalseTechnical Analysis
The function stores the user-provided API key as plaintext in
.envusingPath.write_text(). It does not create the file with owner-only permissions, correct the permissions of an existing file, or use a protected credential store.The resulting access permissions depend on the operating system, process umask, and any permissions already assigned to
.env. In a shared or improperly configured environment, another local account or process may be able to read the credential. Updating an existing.envalso rewrites the file without first validating that it is a regular file owned by the expected user, and the update is not atomic.Sending the API key to the documented HTTPS API in
scripts/call_api.pyis necessary for the declared authenticated proxy functionality and is not, by itself, confirmed exfiltration. The vulnerability is specifically ...[truncated 1199 chars]- Remediation
View remediation
Remediation Suggestions
- Prefer storing the credential in an operating-system credential manager or secret-management service rather than in a project-local plaintext file.
- If persistence is unnecessary, retain the key only for the current process or session.
- If
.envpersistence is required on POSIX systems, create and maintain it with owner-only mode0600. - Validate that an existing
.envis a regular file owned by the expected user and reject symbolic links or unexpected file types before writing. - Use an atomic update: create a protected temporary file in the same directory, write and flush its contents, set restrictive permissions, and atomically replace
.env. - Add
.envto version-control ignore rules and clearly document that it contains a secret. - Never include the credential in application logs, exception messages, or returned tool data.
- Support key rotation and revocation so a disclosed credential can be invalidated promptly.
- Document that search queries and requested URLs are forwarded to the third-party API, warning users not to submit secrets or private signed URLs.
