Stale Fallback
Hardcoded localhost URLs, including as the fallback for an env var
warningAI Quality
stale-fallbackWhy this matters
AI tools hardcode localhost URLs to make the code run on the first try. As a fallback, the URL ships to production and quietly takes over the day an env var goes missing: requests go to a port nobody is listening on.
Example
lib/api.tsconst API_URL = process.env.API_URL || "http://localhost:3000";
export async function fetchUser(id: string) {
const res = await fetch(`${API_URL}/api/users/${id}`);
return res.json();
}lib/api.tsconst API_URL = process.env.API_URL;
if (!API_URL) throw new Error("API_URL is not set");
export async function fetchUser(id: string) {
const res = await fetch(`${API_URL}/api/users/${id}`);
return res.json();
}How to fix
Read the URL from an environment variable and fail at startup when it is missing. Keep localhost in .env.local, not in the code. Config files and tests are skipped.
Reference
- In the output
- Findings carry
"ruleId": "stale-fallback"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line stale-fallback- Silence a whole file
// prodlint-disable stale-fallbackat the top of the file- How it works
- Matches text patterns. Source on GitHub