Part 5 of 14 · 2 min read
A local server is reachable by any web page unless it checks who is asking
Think of the post office: letters can arrive from anywhere in the world, so before acting on one you check who actually sent it.
In plain words: a local HTTP server feels private but is not, because any web page a browser opens can try to talk to localhost. With a trick called DNS rebinding, a hostile site can even make the browser treat a local server as if it belonged to the hostile site's own address. So validating the Origin header, binding to loopback only, and authenticating every connection are not optional extras. They are what makes a local endpoint actually local.
A worked example. Running the example's server with the origin check switched off, three kinds of caller all succeed at placing a hold: a plain program with no Origin header, a page served from localhost, and a page served from a hostile origin. Switching the origin check on changes only the third row: the hostile page's request is rejected with HTTP 403 and no hold is created, while the first two rows stay exactly the same. That first row is the detail worth noticing. The check never stopped the plain program at all, because programs are not required to send an Origin header. It specifically stops browsers acting on behalf of a hostile page.
The key lines. From code/mini-transport/http_server.py:
# from code/mini-transport/http_server.py
def _origin_ok(self) -> bool:
o = self.headers.get("Origin")
if not outer.check_origin or o is None:
return True
ok = o in outer.allowed_origins or any(o.startswith(a + ":") for a in outer.allowed_origins)
if not ok:
outer.stats["origin_rejected"] += 1
self._plain(403, rpc.error(None, rpc.INVALID_REQUEST, "Forbidden origin").encode())
return ok
Read it as: a missing Origin header passes the check by default, with o is None: return True. That one line is the whole reason the check cannot replace authentication. It was built to catch a browser silently carrying a hostile page's request, not to stop a program that simply chooses not to send the header at all.
Origin validation, DNS rebinding
Origin validation checks the Origin header on an incoming connection against an allow-list, rejecting with 403 when it does not match. DNS rebinding is a trick where a hostile site makes a victim's browser resolve the site's own hostname to a local address, so the browser's same-origin rules no longer protect a local server from it.
Example: the origin check lets every program-sent request through regardless, while blocking only the hostile-page row. That is the direct evidence for why the specification lists authentication as its own separate requirement, rather than trusting origin validation alone to secure the endpoint.