T09 · Insecure Skill Coding Practices
- Location
scripts/lnbits_cli.py:10- Finding
Unrestricted LNbits Base URL Can Expose the Wallet API Key
- Content
View full analysis
Vulnerability Details
File Location:
scripts/lnbits_cli.py, lines 10-26
Vulnerability Type: Unvalidated security-sensitive endpoint configuration
Risk Level: HighVulnerable 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 NoneTechnical Analysis
LNBITS_BASE_URLis 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 theX-Api-Keyheader.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
- An attacker influences the process environment, deployment configuration,
.envfile, shell profile, or agent configuration containingLNBITS_BASE_URL. - The attacker changes the value to an attacker-controlled URL or an unencrypted HTTP endpoint.
- A user or agent invokes
balance,invoice,decode, orpay. - The
requestfunction constructs a URL under the configured endpoint. - The CLI sends
LNBITS_API_KEYin the `X-Api ...[truncated 588 chars]
- An attacker influences the process environment, deployment configuration,
- Remediation
View remediation
Remediation Suggestions
- Parse
LNBITS_BASE_URLwithurllib.parse.urlsplit. - Require the
httpsscheme, 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-Keyto 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.
- Parse
