Skip to content

Require a token on the control plane - #114

Merged
fylorn merged 1 commit into
mainfrom
feat/control-token
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/control-token

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

First step of Windows support: the control plane gets a credential, on both platforms.

Why

The socket's 0700 mode is a guarantee the filesystem makes, and it only holds on this platform. Windows has no equivalent — the control plane can only land on a loopback TCP port there, any local process can connect to one, and unlike a unix socket it cannot be asked who is on the other end (SO_PEERCRED).

So the credential is a second door. Both platforms walk through it, because a defence that only runs on one platform is a defence nobody exercises day to day.

Where the token comes from

  • TW_CONTROL_TOKEN, when a parent process passes one. The desktop app spawns core itself and never adopts a running one, so it generates the token and hands it over through the environment — never through argv (Windows shows every process's command line to every other process of the same user) and never through a file.
  • control.token beside the configuration otherwise, which means someone is running twcore serve by hand. Generated once and read back after that, so a curl user does not have to re-copy it on every restart. Created 0600 rather than chmod'ed afterwards, so there is no window where the key to this door is world-readable.

An empty token — empty variable, empty file — is treated as absent. An empty credential is not a loose door, it is an open one: the comparison would succeed for every request that also brings nothing.

Where the guard goes

On the serving boundary, not inside router(). That route table is what a dozen integration tests drive directly; welding the door into it would only teach every one of those tests to open it first. The door is tested on its own instead.

Verified

Unit tests cover the comparison (constant time, a prefix is not a match), the file (written once, read back, 0600, empty file replaced, surrounding whitespace trimmed), Debug not printing the secret, and the layer's status codes.

Ran against a real twcore serve on an isolated THINKWATCH_HOME:

no credential 401 with control.unauthorized
wrong credential 401
right credential 200
lowercase bearer scheme 200
/events (SSE) without credential 401
restart same token, old one still works
TW_CONTROL_TOKEN set env token accepted, file token rejected, file untouched
token occurrences in the log 0

Note for the desktop side

This is a breaking change to the control-plane contract. Lite pins core by tag, so nothing breaks until it moves to the release that carries this, and that move is where it starts sending Authorization: Bearer.

🤖 Generated with Claude Code

@fylorn
fylorn force-pushed the feat/control-token branch 4 times, most recently from 87c7908 to f74e99d Compare September 23, 2026 05:03
The socket's `0700` mode is a guarantee the filesystem makes, and it only
holds on this platform. Windows has no equivalent: the control plane can
only land on a loopback TCP port there, any local process can connect to
one, and unlike a unix socket it cannot be asked who is on the other end.

So the credential is a second door, and both platforms walk through it.
A defence that only runs on one platform is a defence nobody exercises.

The token comes from `TW_CONTROL_TOKEN` when the parent process passes one
-- the desktop app spawns core itself, so it generates the token and hands
it over through the environment, never through argv (Windows shows every
process's command line to every other process of the same user) and never
through a file. Without that variable someone is running `twcore serve` by
hand, and the token is generated once into `control.token` beside the
configuration, created `0600` rather than chmod'ed afterwards, so there is
no window where the key to this door is world-readable.

The guard goes on at the serving boundary, not inside `router()`. That
route table is what a dozen integration tests drive directly; welding the
door into it would only teach every one of those tests to open it.

The smoke script now passes the token, and asserts that a request without
one is refused. That assertion is the only thing that can prove the door
is real: the middleware's own tests say it refuses, but whether it is
actually mounted on the route table that faces the socket takes the real
binary to answer -- and the shape of getting that wrong is every test
still passing while the control plane stands open.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 7bdc01f into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the feat/control-token branch September 23, 2026 05:22
fylorn added a commit that referenced this pull request Sep 23, 2026
… break

Windows has no SIGTERM, and the desktop app has two things it must do:
restart core after a configuration change, and stop core before installing
an update and *wait for it to be gone* -- without that wait the new one
collides with the lock the old one has not released yet. On that platform
the only thing left is a hard kill, which means SQLite's WAL gets no
chance to settle and in-flight requests are cut mid-response.

So it is a request rather than a signal, and it is slightly better than
one: it is acknowledged. The desktop app knows it was received instead of
firing and guessing.

Both platforms use it. A path that only runs on one platform is a path
nobody exercises.

The handler answers before the switch is thrown. Throwing it immediately
risks the process being gone before the response is written, and what the
client sees then is a reset connection -- indistinguishable from core
crashing, on the one exit it asked for itself.

The switch stores a request that arrives before anyone is waiting
(`notify_one`, not `notify_waiters`). During the first milliseconds of
startup the control plane can be answering before the main loop reaches
its select, and dropping that request leaves a process that never exits.

The protocol version goes to 12, which #114 should have done: a request
without `Authorization: Bearer` now gets 401 for everything, `/status`
included. A client written against 11 never sees the bump -- it hits the
401 first -- but that line is this protocol's change history, and "when
did a credential become required" is something a reader has to be able to
find.

The smoke script now asks core to exit and waits for the process to be
gone. That is the only end-to-end check of the chain: an HTTP request
throws the switch, the main loop's select wakes, the process leaves. Unit
tests can show the switch flips; they cannot show it is wired to the main
loop -- and the shape of getting that wrong is a 202 and a gateway that
keeps running.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn added a commit that referenced this pull request Sep 23, 2026
… break

Windows has no SIGTERM, and the desktop app has two things it must do:
restart core after a configuration change, and stop core before installing
an update and *wait for it to be gone* -- without that wait the new one
collides with the lock the old one has not released yet. On that platform
the only thing left is a hard kill, which means SQLite's WAL gets no
chance to settle and in-flight requests are cut mid-response.

So it is a request rather than a signal, and it is slightly better than
one: it is acknowledged. The desktop app knows it was received instead of
firing and guessing.

Both platforms use it. A path that only runs on one platform is a path
nobody exercises.

The handler answers before the switch is thrown. Throwing it immediately
risks the process being gone before the response is written, and what the
client sees then is a reset connection -- indistinguishable from core
crashing, on the one exit it asked for itself.

The switch stores a request that arrives before anyone is waiting
(`notify_one`, not `notify_waiters`). During the first milliseconds of
startup the control plane can be answering before the main loop reaches
its select, and dropping that request leaves a process that never exits.

The protocol version goes to 12, which #114 should have done: a request
without `Authorization: Bearer` now gets 401 for everything, `/status`
included. A client written against 11 never sees the bump -- it hits the
401 first -- but that line is this protocol's change history, and "when
did a credential become required" is something a reader has to be able to
find.

The smoke script now asks core to exit and waits for the process to be
gone. That is the only end-to-end check of the chain: an HTTP request
throws the switch, the main loop's select wakes, the process leaves. Unit
tests can show the switch flips; they cannot show it is wired to the main
loop -- and the shape of getting that wrong is a 202 and a gateway that
keeps running.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn added a commit that referenced this pull request Sep 23, 2026
… break

Windows has no SIGTERM, and the desktop app has two things it must do:
restart core after a configuration change, and stop core before installing
an update and *wait for it to be gone* -- without that wait the new one
collides with the lock the old one has not released yet. On that platform
the only thing left is a hard kill, which means SQLite's WAL gets no
chance to settle and in-flight requests are cut mid-response.

So it is a request rather than a signal, and it is slightly better than
one: it is acknowledged. The desktop app knows it was received instead of
firing and guessing.

Both platforms use it. A path that only runs on one platform is a path
nobody exercises.

The handler answers before the switch is thrown. Throwing it immediately
risks the process being gone before the response is written, and what the
client sees then is a reset connection -- indistinguishable from core
crashing, on the one exit it asked for itself.

The switch stores a request that arrives before anyone is waiting
(`notify_one`, not `notify_waiters`). During the first milliseconds of
startup the control plane can be answering before the main loop reaches
its select, and dropping that request leaves a process that never exits.

The protocol version goes to 12, which #114 should have done: a request
without `Authorization: Bearer` now gets 401 for everything, `/status`
included. A client written against 11 never sees the bump -- it hits the
401 first -- but that line is this protocol's change history, and "when
did a credential become required" is something a reader has to be able to
find.

The smoke script now asks core to exit and waits for the process to be
gone. That is the only end-to-end check of the chain: an HTTP request
throws the switch, the main loop's select wakes, the process leaves. Unit
tests can show the switch flips; they cannot show it is wired to the main
loop -- and the shape of getting that wrong is a 202 and a gateway that
keeps running.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn added a commit that referenced this pull request Sep 23, 2026
… break (#121)

Windows has no SIGTERM, and the desktop app has two things it must do:
restart core after a configuration change, and stop core before installing
an update and *wait for it to be gone* -- without that wait the new one
collides with the lock the old one has not released yet. On that platform
the only thing left is a hard kill, which means SQLite's WAL gets no
chance to settle and in-flight requests are cut mid-response.

So it is a request rather than a signal, and it is slightly better than
one: it is acknowledged. The desktop app knows it was received instead of
firing and guessing.

Both platforms use it. A path that only runs on one platform is a path
nobody exercises.

The handler answers before the switch is thrown. Throwing it immediately
risks the process being gone before the response is written, and what the
client sees then is a reset connection -- indistinguishable from core
crashing, on the one exit it asked for itself.

The switch stores a request that arrives before anyone is waiting
(`notify_one`, not `notify_waiters`). During the first milliseconds of
startup the control plane can be answering before the main loop reaches
its select, and dropping that request leaves a process that never exits.

The protocol version goes to 12, which #114 should have done: a request
without `Authorization: Bearer` now gets 401 for everything, `/status`
included. A client written against 11 never sees the bump -- it hits the
401 first -- but that line is this protocol's change history, and "when
did a credential become required" is something a reader has to be able to
find.

The smoke script now asks core to exit and waits for the process to be
gone. That is the only end-to-end check of the chain: an HTTP request
throws the switch, the main loop's select wakes, the process leaves. Unit
tests can show the switch flips; they cannot show it is wired to the main
loop -- and the shape of getting that wrong is a 202 and a gateway that
keeps running.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant