SKY-D248 · Security in code
Hard-coded localhost URL
SKY-D248 reports a string in TypeScript or JavaScript code that starts with a loopback address: http://localhost, http://127.0.0.1 or http://0.0.0.0 (or the https:// forms). Such a URL only works on the developer's own machine, so it usually means a host that should come from configuration was typed into the code.
- Languages
- TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
- Catalog name
- Hardcoded internal URL
Why it matters
A build that ships with http://localhost:8080/api sends real users' requests to their own computer: the feature breaks in production, or a login or payment call goes to whatever happens to listen on that port. It also tells an attacker which internal ports and paths the service uses.
What Skylos flags
const API_BASE_URL = "http://localhost:8080/api"; export async function fetchInvoices(customerId: string): Promise<Response> { const id = encodeURIComponent(customerId); return fetch(`${API_BASE_URL}/customers/${id}/invoices`);}What Skylos leaves alone
const API_BASE_URL = process.env.API_BASE_URL ?? "http://localhost:8080/api"; export async function fetchInvoices(customerId: string): Promise<Response> { const id = encodeURIComponent(customerId); return fetch(`${API_BASE_URL}/customers/${id}/invoices`);}Why Skylos leaves it alone
The localhost URL is only the fallback after ?? when process.env.API_BASE_URL is not set. The real host comes from the environment, so Skylos treats the literal as a development default.
How Skylos decides
- Runs with
--dangeron TypeScript and JavaScript files. It looks at every string literal and template literal of 16 or more characters. - Reports the string when it starts (ignoring case) with
http://localhost,http://127.0.0.1,http://0.0.0.0or the same three withhttps://. A template such as `http://127.0.0.1:${port}/health` counts, because only the start of the text is checked. - Skips files in test folders (
test,tests,__tests__) and test file names (*.test.ts,*.spec.jsand similar). - Skips a localhost literal that is only the base of
new URL(value, 'https://localhost'), directly, as a ternary branch, or through aconstwhose every use is that base: it parses a relative path and is never contacted. - Skips the literal when it is the right-hand side of
??or||and the left-hand side is an environment lookup:process.env.X,process.env["X"],import.meta.env.X,Bun.env.X,Deno.env.get("X")or a??/||chain of them. The lookup must be the real global: a local variable namedprocess, or code that reassignsprocess.env, cancels this proof. - Does not look at other private addresses such as
http://10.0.0.12or internal host names, and does not read Python or other languages.
How to fix it
Read the host from configuration and fail at start-up when it is missing, so a build never silently falls back to a developer address. Keep any localhost default in local tooling (.env.development, test setup) instead of the shipped code.
Removed: const API_BASE_URL = "http://localhost:8080/api";Added: const API_BASE_URL = process.env.API_BASE_URL;Added: if (!API_BASE_URL) {Added: throw new Error("API_BASE_URL is not set");Added: } export async function fetchInvoices(customerId: string): Promise<Response> { const id = encodeURIComponent(customerId); return fetch(`${API_BASE_URL}/customers/${id}/invoices`);}When it can be wrong
- Development-only code outside a test folder, such as a dev-server config, a Storybook or Playwright setup file, is reported.
- A ternary such as
isProd ? "https://api.example.com" : "http://localhost:3000"is reported: only the??/||fallback after an environment lookup is accepted. config.apiUrl ?? "http://localhost:3000"is reported, becauseconfigis not one of the recognised environment objects.- Missed: private network addresses (
http://10.0.0.12,http://192.168.1.5) and internal host names are not checked.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line with a comment on the same line.
TypeScript const DEV_PROXY = "http://localhost:5173"; // skylos: ignore[SKY-D248] vite dev only[tool.skylos] ignore = ["SKY-D248"]turns the rule off for the whole project.skylos baseline .and then--baselineaccept the URLs you already have and report only new ones.