How to fix: Client Script Hardcodes a Localhost API URL
A client-side script assigns an API base URL / endpoint variable to a literal http://localhost or 127.0.0.1 address.
Why it matters
In production, every API call built from this base URL fails outright (or, worse, silently targets whatever happens to be listening on the visitor's own machine at that port), and it signals the deploy pipeline never swapped in environment-specific configuration.
This is a common artifact of AI-scaffolded or quickly-prototyped apps: the assistant generates a working local dev config, and it ships unchanged because there was no environment-variable indirection for the API base URL.
How to fix it
- 1Read the API base URL from a build-time environment variable, never a literal string.
- 2Fail the production build if the environment variable resolves to localhost/127.0.0.1.
- 3Add an end-to-end smoke test against the deployed environment that would catch a broken API base URL.
Code examples
Use an environment variable instead
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
if (!apiUrl) throw new Error('NEXT_PUBLIC_API_URL is not set');References
Detected automatically by VulnRadar. The check id is cs-hardcoded-localhost-api-url, stable across every scan so you can filter or diff on it.
More client-side checks
- HighCSP Allows 'unsafe-inline' Scripts
- MediumCSP Allows 'unsafe-eval'
- HighpostMessage Without Origin Validation
- HighSensitive Data Stored in localStorage
- HighDOM XSS via location.hash Assignment
- Highdocument.write() in Page Scripts
- Higheval() in Client-Side JavaScript
- MediumExternal Script Without Subresource Integrity
Questions
- How severe is Client Script Hardcodes a Localhost API URL?
- VulnRadar rates this medium severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id cs-hardcoded-localhost-api-url, which stays the same between runs so you can filter, diff, or gate CI on it.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.