Blog ·

How the local connection is protected

Blocknotik's interface is a web page served by the program on 127.0.0.1. Other computers can't reach that address — but every website open in your browser can try. A page on some random site can tell your browser to send requests to http://127.0.0.1:41873/…. If the local program answered them, that site could read or change your notes.

This is a well-known class of problems with local servers, so Blocknotik was built with it in mind from the first line. Here's what's in place.

1. Listen only on 127.0.0.1

The server binds to the loopback address, not to all network interfaces. Nobody else on your Wi-Fi or office network can connect, no firewall rules needed. The port is picked at random at every start unless you set one.

2. A session key that other sites don't have

At start, the program generates a random 256-bit key and opens the browser at /auth?token=…. That address puts the key into a cookie (HttpOnly, SameSite=Strict) and redirects to the main page, so the key disappears from the address bar.

The main page is only served when that cookie is present. It contains the key in a <meta> tag, and the page's script sends it with every API request in a custom header, X-Blocknotik-Token.

The important part: the API ignores the cookie and only accepts the header. A foreign site can make your browser send cookies along with a request, but it can't add custom headers to a request to another origin without that origin's explicit permission (CORS) — and Blocknotik never grants it. So a forged request from another site arrives without the key and gets 401.

3. Check who's asking

Every request passes three more checks:

Requests that change something must also be JSON or a file upload. A plain HTML form on another site can only send text/plain or form-encoded bodies, so it's rejected before any of the above even matters.

4. Paths can't leave the folder

Every note name that arrives from the page is checked before it gets near the file system: it must be a relative path ending in .md, without .., without hidden folders, without characters or names that Windows treats specially. After joining it with the notes folder, the program resolves symbolic links and checks again that the result is still inside. Zip archives on import go through the same cleaning, so an archive with ../../something in it can't write outside the folder.

5. Nothing executes from notes

Markdown is turned into HTML by the program, with every bit of text escaped. HTML written in a note is shown as text. Links are only made for http, https and mailto addresses; javascript: and data: links are dropped. Images are only loaded from the notes folder.

On top of that, every response carries a strict Content-Security-Policy: scripts only from the program itself, no inline scripts, no connections anywhere else, no embedding in frames. Even if something slipped through the renderer, the browser would refuse to run it or to send data elsewhere.

What it doesn't protect against

Programs running under your own user account can read your notes directly from disk, and they could read the session key from the program's console output. That's the normal boundary of any desktop application: protection against other users and other websites, not against software you've already let in.

Tested, not just described

The test suite sends requests without the key, with a wrong key, with only the cookie, with a foreign Host, a foreign Origin, a text/plain body, and with note names like ../x.md and .trash/x.md, and checks that each one is refused. The browser run-through of the Linux build repeats the main ones against the real program. The code is in server.go and store.go in the source archive.

← All posts