Skip to content

brain: listen on the box alone, and refuse to start without GATEWAY_TOKEN (#477) - #490

Merged
teaspoonai merged 1 commit into
mainfrom
fix/brain-port-477
Dec 1, 2026
Merged

teaspoonai merged 1 commit into
mainfrom
fix/brain-port-477

Conversation

@teaspoonai

Copy link
Copy Markdown
Contributor

Fixes #477. The brain's port listens on the device alone, and the brain refuses to start without GATEWAY_TOKEN.

Decisions (the owner's, 2026-11-30)

  • Listen on the device, not a firewall rule. Every client of the brain is on the device: the room microphone, the OpenClaw plugin, Caddy, the Discord bridge and teaport. A NemoClaw sandbox is the one exception, and reaches the brain over the docker bridge.
    • The brain listens where BRAIN_LISTEN says: 127.0.0.1 by default.
    • install.sh writes 127.0.0.1,172.18.0.1 on a NemoClaw install.
    • An address that isn't loopback is bound with IP_FREEBIND, so the bridge can come up after the brain, with no start order and no retry.
    • A firewall rule would be extra state that docker, ufw or an admin could flush, which would open the port again without anyone noticing.
  • Refuse to start without a token. The brain exits 78 (EX_CONFIG), and the unit's RestartPreventExitStatus=78 keeps that to one failure instead of a restart loop. TEAPORT_ALLOW_NO_AUTH=1 lets a developer's brain or a test run open on purpose.
    • "No token, but loopback only" isn't safe: the agent's Chromium browses the web on the device, and a page there can open a WebSocket to 127.0.0.1:7861.

What changed

  • gateway_server.py
    • refuse_without_token() runs at startup.
    • listen_addresses() parses IP literals and refuses names.
    • listen_sockets() makes one bound socket per address and closes them all if any bind fails.
    • serve() hands those sockets to uvicorn (Server.run(sockets=)).
    • --host now defaults to BRAIN_LISTEN and takes a comma-separated list.
  • settings_access.gateway_token_ok() is the one check for /talk, /talk/status and the config page on the brain's port. A configured token must match. With no token configured, nothing passes unless the brain is open on purpose. Note that token_matches("", "") is True, so this case is decided before the compare.
  • config_ui._authorize uses gateway_token_ok: with no token, the config page on the port refuses the device itself too.
  • config_schema.toml:
    • new rows BRAIN_LISTEN and TEAPORT_ALLOW_NO_AUTH, both installer tier, so the config page can't change them;
    • GATEWAY_TOKEN's help text says the brain won't start without it.
  • install.sh
    • brain_listen writes BRAIN_LISTEN on every full run, so a device that changes agents follows.
    • A repair run that can't see the sandbox (run as root or another account, so detect_agent looks in another home) only seeds the bridge address on a device with a teaport-sandbox-recover unit. It doesn't strip the address.
    • --only brain writes no brain.env, so brain_listen_seed seeds the bridge address on a NemoClaw device before the new brain starts. That device is identified by its teaport-sandbox-recover unit.
    • agent_nemoclaw uses the same BRAIN_BRIDGE_ADDR.
  • The Discord bridge sends the token. It never did: bridge.env had no token, and index.js added no ?token=. Every installed device has a token, so the brain already refused the bridge with 1008.
    • The new bridge_env row BRIDGE_GATEWAY_TOKEN is written by phase_bridge from resolve_gateway_token.
    • index.js reads it through env.js and appends it in brainUrl(). It's never logged: the log lines show BRAIN_URL.
  • teaport doctor
    • It fails on a missing GATEWAY_TOKEN, where it used to warn. It only warns when the brain is open on purpose.
    • It names a brain that refused its configuration (exit 78), and --fix doesn't restart that brain, since a restart would refuse again.
    • It warns when the port answers on any address but loopback or the bridge, and when a NemoClaw device's BRAIN_LISTEN lacks the bridge.
  • teaport-brain.service.in: RestartPreventExitStatus=78.
  • Docs
    • security: a new section, "The brain's port";
    • troubleshooting: a new entry, "The brain does not start without its token";
    • configuration, architecture and cli updated;
    • the settings reference regenerated;
    • citation ranges shifted by --fix-sources, with the same hashes.

Who this affects

  • Any device install.sh set up, and factory devices: no change. Every installer run since the first release writes a token, and the factory seal checks it.
  • A device whose brain.env lost its token (a hand edit, a restored backup): the brain no longer starts, so the device is silent until the installer runs again. teaport doctor and the new troubleshooting entry say so.
  • A developer running teaport-brain without a token: set one, or TEAPORT_ALLOW_NO_AUTH=1.
  • Anyone who reached http://<device>:7861 from another machine: that no longer connects. The config page and pairing links on that port already refused other machines. An SSH tunnel still works.

Not in this PR

  • The deb-only update path (teaport-update) doesn't re-render the unit or seed BRAIN_LISTEN. It isn't turned on yet. install.sh --only brain does both.

Tests

  • New brain/tests/test_brain_port.py:
    • the listen addresses, and the default;
    • a free bind of an address no interface holds (TEST-NET-1), and the plain bind it replaces failing;
    • no socket left open after a failed bind;
    • uvicorn serving on the bound sockets;
    • the refusal (exit 78) and the opt-out;
    • gateway_token_ok with empty tokens;
    • /talk closing 1008 without a token;
    • the unit's RestartPreventExitStatus;
    • install.sh's brain_listen, and --only brain's seed order;
    • doctor's listen check, against a fake ss.
  • Updated:
    • test_settings_access: no token opens nothing, even on the device; open on purpose, the device only;
    • test_config_ui: doctor fails with no token;
    • test_install_brain_only: the seed step, its order, NemoClaw vs. not;
    • test_sip_fake_gateway and test_call_experience: the brain they start has no token, so it runs open on purpose.
  • Independent review: an agent with only the diff. It found the two SIP tests starting a tokenless brain (blocking), the repair-run case, the Discord bridge, a false doctor warning on an unreadable brain.env, stale comments, and two IPv6 forms that would loop on a bind error. All are fixed here. A second pass on the fixes was clean, apart from a nit: a box moved from NemoClaw to host OpenClaw kept the bridge address. That is fixed too: only a run with no agent seen seeds it.
  • Mutation-checked: dropping the free bind, letting an empty token pass, and dropping the startup refusal each fail a test.
  • Full brain suite (brain/tests/test_suite.py): 163 passed, 7 skipped (appliance-only). A first run caught three more tests that assumed an open /talk/status or the old doctor warning; they are updated here.
  • shellcheck: no new findings on cli/teaport and install.sh. Docs lint: 0 errors.

🤖 Generated with Claude Code

…OKEN (#477)

The brain's port (7861) listened on 0.0.0.0, and an empty GATEWAY_TOKEN
opened /talk, /talk/status and the config page to anyone who reached it.

- The brain listens where BRAIN_LISTEN says: 127.0.0.1. A NemoClaw install
  adds the docker bridge's 172.18.0.1, bound with IP_FREEBIND, so the bridge
  may come up after the brain. --host takes a comma-separated list. A name,
  a scoped or an IPv4-mapped address is refused at the start.
- No GATEWAY_TOKEN: the brain exits 78 (EX_CONFIG), and the unit's
  RestartPreventExitStatus=78 makes that one failure, not a loop.
  TEAPORT_ALLOW_NO_AUTH=1 runs a developer's brain open on purpose. A page
  in the agent's browser reaches 127.0.0.1 too, so loopback alone is no
  excuse for no token.
- settings_access.gateway_token_ok is the one check for /talk, /talk/status
  and the config page on the port. token_matches("", "") is True, so the
  empty case is decided before the compare.
- install.sh writes BRAIN_LISTEN on every full run. A repair run that can't
  see the sandbox seeds the bridge address on a NemoClaw box instead, and
  --only brain seeds it there too, before the new brain starts.
- The Discord bridge sends the token now (BRIDGE_GATEWAY_TOKEN in
  bridge.env, from install.sh). It never did, so the brain refused it on
  every installed box.
- teaport doctor: no token fails; a brain that refused its configuration
  is named, and --fix doesn't restart it; a port that answers beyond the
  box, or a NemoClaw BRAIN_LISTEN without the bridge, warns.
- Docs: security "The brain's port", troubleshooting "The brain does not
  start without its token", configuration, architecture and cli; settings
  reference regenerated; citations moved.
- Tests: test_brain_port.py (new); test_settings_access, test_config_ui,
  test_install_brain_only updated; the two SIP tests that start a brain
  without a token run it open on purpose; test_local_audio and
  test_wake_gate poll /talk/status with the token, and test_tts_pacing
  expects doctor's failure for a missing one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@teaspoonai
teaspoonai merged commit 0cb5574 into main Dec 1, 2026
8 checks passed
teaspoonai added a commit that referenced this pull request Dec 1, 2026
…#490

The install-agent (#480) and brain-port (#490) changes moved install.sh's lines;
every cited block is the same text at its new lines (hashes unchanged). The
installer reference's environment-variable item cites TEAPORT_INSTALL_AGENT too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
teaspoonai added a commit that referenced this pull request Dec 1, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	cli/teaport
#	docs/src/content/docs/getting-started/installation.md
#	docs/src/content/docs/guides/configuration.md
#	docs/src/content/docs/guides/discord-bridge.md
#	docs/src/content/docs/guides/faq.md
#	docs/src/content/docs/guides/local-audio.md
#	docs/src/content/docs/guides/phone-calls.md
#	docs/src/content/docs/guides/troubleshooting.md
#	docs/src/content/docs/guides/updating.md
#	docs/src/content/docs/guides/wifi-setup.md
#	docs/src/content/docs/index.mdx
#	docs/src/content/docs/reference/architecture.md
#	docs/src/content/docs/reference/cli.md
#	docs/src/content/docs/reference/installer.md
#	docs/src/content/docs/reference/licenses.md
#	docs/src/content/docs/reference/security.md
#	docs/src/content/docs/reference/settings.md
teaspoonai added a commit that referenced this pull request Dec 1, 2026
The paragraphs match main's; #490 changed the code they cite (cmd_doctor's
gateway token and listen checks, config_ui's on-box rule, the schema's help), and
main re-cited them. Same ranges here, mapped onto this branch's files.

Co-Authored-By: Claude Opus 5.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.

brain: fail closed without GATEWAY_TOKEN and stop exposing /talk port to the LAN

1 participant