Skip to main content

SKY-D248 · Security in code

Hard-coded localhost URL

Medium

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/api/client.ts · TypeScriptReported on line 1
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

src/api/client.ts · TypeScript
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

  1. Runs with --danger on TypeScript and JavaScript files. It looks at every string literal and template literal of 16 or more characters.
  2. Reports the string when it starts (ignoring case) with http://localhost, http://127.0.0.1, http://0.0.0.0 or the same three with https://. A template such as ` http://127.0.0.1:${port}/health ` counts, because only the start of the text is checked.
  3. Skips files in test folders (test, tests, __tests__) and test file names (*.test.ts, *.spec.js and similar).
  4. Skips a localhost literal that is only the base of new URL(value, 'https://localhost'), directly, as a ternary branch, or through a const whose every use is that base: it parses a relative path and is never contacted.
  5. 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 named process, or code that reassigns process.env, cancels this proof.
  6. Does not look at other private addresses such as http://10.0.0.12 or 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.

src/api/client.ts · TypeScript · the fix, against the flagged file
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, because config is 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 --baseline accept the URLs you already have and report only new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.