Localhost Is Not a Security Boundary: Protecting a Local Web Interface Without a Login

René Kulik on 22.11.2026

Introduction

One of my side projects is a small Node.js tool that runs in the background and comes with a web interface. I start it in a terminal, open http://127.0.0.1:3000, and use it like any other web app. It has forms that change data, buttons that start jobs, and a few pages that show what the tool has collected.

It has no login. It runs on my machine, for me, and a login screen for a tool only I use felt like ceremony. But “it only runs on localhost” is one of those sentences that sounds like a security model and isn’t one.

In this post, I will show why a local web interface without a login is reachable from any website you visit, and the few lines of code that close that gap.

Why “Only on My Machine” Is Not Enough

The server only listens on my machine, so nobody on the internet can reach it directly. That part is true. The problem is that something on my machine talks to the internet all day: my browser. Every page I open runs code in that browser, and that browser can reach 127.0.0.1 just fine.

There are two ways a web page can use that.

Cross-Site Requests

Any website can send a request to any other address. It cannot read the response unless CORS allows it, but for many attacks that does not matter. A form like this, placed on any page on the internet, works:

<form method="post" action="http://127.0.0.1:3000/items/42/delete">
  <button>Win a prize</button>
</form>

A plain form post is a “simple request” in CORS terms. The browser sends it without asking the server first, and the server executes it. The attacker never sees the response, but the item is gone. This is classic cross-site request forgery, and a login does not even play a role here: there is no session to steal, because the interface trusts everyone.

DNS Rebinding

The second attack is less known and worse, because it lets a foreign page read everything.

The browser’s same-origin policy is based on names, not on IP addresses. A page from http://evil.example:3000 may read responses from http://evil.example:3000, because that is its own origin. Now the attacker controls the DNS for evil.example. On the first lookup, it points to the attacker’s server, which delivers a page with some JavaScript. Shortly after, the DNS answer changes to 127.0.0.1. When the script sends its next request to evil.example:3000, it lands on my local tool, and as far as the browser is concerned, it is a same-origin request. The script can read the response and send it home.

This is not theoretical. DNS rebinding has been used against routers, smart home devices, and development servers for years.

Binding to the Loopback Address

The first step is the easy one. The server should listen on the loopback address only, not on all interfaces:

await server.listen({ host: '127.0.0.1', port: 3000 });

With 0.0.0.0, every device on the same network could reach the interface, including the café Wi-Fi. With 127.0.0.1, only processes on my machine can. That is necessary, but as shown above, it does not help against my own browser.

Checking the Host Header

The answer to DNS rebinding is in the request itself. When the browser sends a request to http://evil.example:3000, the Host header says evil.example:3000, even though the connection ended up on 127.0.0.1. The browser cannot hide that, and a page cannot change it.

So the server only answers requests whose host name is one of its own loopback names:

const LOOPBACK_HOST_NAMES = new Set(['127.0.0.1', 'localhost', '[::1]']);

const readHostName = (host: string | undefined): string | undefined => {
  return URL.parse(`http://${host ?? ''}`)?.hostname;
};

I parse the header with URL.parse instead of splitting it at the colon. That handles the port and IPv6 addresses like [::1]:3000 without any string handling of my own, and it returns null for anything that is not a valid host.

With this check in place, the rebinding attack gets a 403 on every request. The attacker’s script still reaches my server, but my server refuses to talk to a page that calls it by a foreign name.

Letting the Browser Say Where a Request Comes From

The Host check does not help against cross-site forms, though. A form on evil.example that posts to http://127.0.0.1:3000 sends Host: 127.0.0.1:3000, because that is where it posts to.

Here, two other headers help. Modern browsers attach them to every request, and a page cannot set them itself:

For requests that change something, I only accept same-origin and none:

const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
const TRUSTED_FETCH_SITES = new Set(['same-origin', 'none']);

Note that same-site is not on the list. That one is easy to get wrong. A “site” ignores the port, so http://localhost:8080 and http://localhost:3000 are the same site. Any other local dev server I happen to run, including one that serves a page with a malicious dependency in it, would count as same-site. Only same-origin means the request came from my interface itself.

The Origin check is a second line for the same rule: if a browser sends an Origin, it has to match the host the request was sent to, port included.

Both checks only reject requests that carry these headers. A request without them, from curl or a script, does not come from a web page at all, and anything that can run curl on my machine has already won anyway. So those requests pass.

Read requests (GET, HEAD, OPTIONS) skip this check. They must never change anything, which is a good rule for any application, and the Host check already protects what they return.

The Whole Guard

In my tool, this is a Fastify onRequest hook, so it runs before any route:

import type { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify';

const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
const TRUSTED_FETCH_SITES = new Set(['same-origin', 'none']);
const LOOPBACK_HOST_NAMES = new Set(['127.0.0.1', 'localhost', '[::1]']);

const readHostName = (host: string | undefined): string | undefined => {
  return URL.parse(`http://${host ?? ''}`)?.hostname;
};

export const rejectCrossSiteRequests = (
  request: FastifyRequest,
  _reply: FastifyReply,
  done: HookHandlerDoneFunction,
): void => {
  if (!LOOPBACK_HOST_NAMES.has(readHostName(request.headers.host) ?? '')) {
    done(new HttpError(403, 'This interface only answers on this machine.'));

    return;
  }

  if (SAFE_METHODS.has(request.method)) {
    done();

    return;
  }

  const fetchSite = request.headers['sec-fetch-site'];
  const origin = request.headers.origin;
  const isForeignFetch = fetchSite !== undefined && !TRUSTED_FETCH_SITES.has(fetchSite);
  const isForeignOrigin = origin !== undefined && origin !== `http://${request.headers.host ?? ''}`;

  done(isForeignFetch || isForeignOrigin ? new HttpError(403, 'Changes can only be made from the interface.') : undefined);
};
instance.addHook('onRequest', rejectCrossSiteRequests);

HttpError is a small error class of my own that carries a status code, which Fastify picks up for the response.

That is all. No tokens in forms, no session, no extra dependency. The forms in my interface are plain HTML forms, and they keep working, because the browser marks them as same-origin on its own.

What This Does Not Cover

This guard is about one threat: web pages using my browser to reach a local tool. It is not a replacement for a login in every case.

Conclusion

A web server on localhost is reachable by every page your browser opens. Without a login, two checks make it safe for the common case: answer only to your own host names, which stops DNS rebinding, and accept changes only from your own origin, which stops cross-site forms.

If you have a local tool with a web interface, open its server setup and look for two things: the address it listens on, and whether it looks at Host at all. Most development servers do the first one right and skip the second.