T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/lnbits_cli.py:10
- Finding
- Unrestricted LNbits Base URL Can Expose the Wallet API Key## Vulnerability Details **File Location**: `scripts/lnbits_cli.py`, lines 10-26 **Vulnerability Type**: Unvalidated security-sensitive endpoint configuration **Risk Level**: High ### Vulnerable Code ```python BASE_URL = os.getenv("LNBITS_BASE_URL", "https://legend.lnbits.com").rstrip("/") API_KEY = os.getenv("LNBITS_API_KEY") # --- Helpers --- def error(msg, code=1): print(json.dumps({"error": msg})) sys.exit(code) def request(method, endpoint, data=None): if not API_KEY: error("LNBITS_API_KEY environment variable is not set.") url = f"{BASE_URL}/api/v1{endpoint}" headers = { "X-Api-Key": API_KEY, "Content-Type": "application/json" } body = json.dumps(data).encode("utf-8") if data else None ``` ### Technical Analysis `LNBITS_BASE_URL` is accepted directly from the environment and used to construct authenticated requests without validating its scheme, hostname, port, or other URL components. Every authenticated request places the wallet API key in the `X-Api-Key` header. If the variable is set to an attacker-controlled HTTPS server, that server receives the API key. If it is set to an HTTP endpoint, the key and associated wallet traffic may be transmitted without transport encryption and intercepted by a network attacker. Environment-based configuration is legitimate, but a credential-bearing client should validate the destination before releasing an administrator-level wallet credential. ### Attack Path 1. An attacker influences the process environment, deployment configuration, `.env` file, shell profile, or agent configuration containing `LNBITS_BASE_URL`. 2. The attacker changes the value to an attacker-controlled URL or an unencrypted HTTP endpoint. 3. A user or agent invokes `balance`, `invoice`, `decode`, or `pay`. 4. The `request` function constructs a URL under the configured endpoint. 5. The CLI sends `LNBITS_API_KEY` in the `X-Api ...[truncated 588 chars]
- Remediation
- ## Remediation Suggestions - Parse `LNBITS_BASE_URL` with `urllib.parse.urlsplit`. - Require the `https` scheme, except for an explicitly enabled local-development mode restricted to loopback addresses. - Reject embedded usernames, passwords, fragments, and unexpected URL components. - Maintain an explicit allowlist of trusted LNbits hostnames where deployment requirements permit it. - If arbitrary self-hosted instances must be supported, require an explicit trust-on-first-use or administrator approval step before sending credentials to a new host. - Prevent redirects from forwarding `X-Api-Key` to a different origin. Use a redirect handler that rejects cross-origin redirects for authenticated requests. - Keep TLS certificate validation enabled and do not introduce an unverified SSL context. - Store the API key in an approved secret manager with tightly restricted access, and rotate it immediately if endpoint redirection or disclosure is suspected.
