Repository navigation
Require a token on the control plane - #114
Merged
Merged
Conversation
fylorn
force-pushed
the
feat/control-token
branch
4 times, most recently
from
September 23, 2026 05:03
87c7908 to
f74e99d
Compare
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
force-pushed
the
feat/control-token
branch
from
September 23, 2026 05:11
f74e99d to
650bc70
Compare
This was referenced Sep 23, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First step of Windows support: the control plane gets a credential, on both platforms.
Why
The socket's
0700mode 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.tokenbeside the configuration otherwise, which means someone is runningtwcore serveby hand. Generated once and read back after that, so a curl user does not have to re-copy it on every restart. Created0600rather 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),Debugnot printing the secret, and the layer's status codes.Ran against a real
twcore serveon an isolatedTHINKWATCH_HOME:401withcontrol.unauthorized401200bearerscheme200/events(SSE) without credential401TW_CONTROL_TOKENsetNote 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