Dev.to AI 🤖 Ai 👁 0 📖 4 min read

A Redaction Bug in TokenCap's Companion Bridge, and the Fail-Closed Fix

TokenCap is local-first: it builds context packs from your repository and never uploads them. The one place repository text does leave the tool on its way to a third party is the Chrome companion, where you choose to inj

TokenCap is local-first: it builds context packs from your repository and never uploads them. The one place repository text does leave the tool on its way to a third party is the Chrome companion, where you choose to inject a pack into a browser chat. That path has to be redacted every time. For a while it was not always redacted, and this post explains why and what changed.

How the companion gets context

tokencap serve starts a loopback HTTP bridge on 127.0.0.1 (port 4545 by default). The companion calls it with a pairing token in the X-TokenCap-Token header, from an allowlisted origin. For a task, it posts to /generate:

tokencap serve --token my-token

curl -s -X POST http://127.0.0.1:4545/generate \
  -H "X-TokenCap-Token: my-token" \
  -H "Content-Type: application/json" \
  -d '{"projectPath": "/home/me/app", "query": "cookie fallback"}'

The bridge builds the task pack in memory, then puts three parts together: a project primer (from the README, the package.json description and .tokencap/agent/agent.json), a short focus brief, and the task code. A final redaction pass runs over the joined text before it is returned. The file contents in the pack were already redacted when they were read. The final pass is what covers the primer and anything else assembled around them.

The bug

The final pass used to look like this:

try { text = redact(text, result.contextPath, { root, audit: false }).content; }
catch { /* keep text */ }

Browser packs are built with write: false, so no context file is written and result.contextPath is undefined. Inside redact(), the path is made relative to the project root with path.relative(root, filepath). Node throws on an undefined argument:

TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined

The catch kept the text. So in task mode the final pass threw on every request and the text went out unredacted. A secret in the README overview, or any other part of the primer, could reach a browser chat.

Quick mode, which sends only the primer, passed a fixed label and was not hit by the path error. But its catch also kept the text on any other failure.

The fix

Two changes in src/bridge/generate.js.

First, the final pass gets a fixed label, "bridge-task", instead of a path that may not exist. The primer in quick mode keeps its "primer" label.

Second, both passes now fail closed. If redact() throws for any reason, the bridge returns an error and no context:

500 {"error": "redaction_failed", "detail": "Secret redaction failed; context withheld."}

No context is a worse experience than a context pack, and a much better one than an unredacted pack in a chat log. Both cases have regression tests in test/fix-bridge-capture.test.js: one that checks primer secrets are redacted in a task-mode pack, and one that checks a redaction failure withholds the context in both task and quick mode.

A related fix: agent.json and tech-stack.md no longer contain raw .env values. External services are listed by public URL or by variable name.

Honest errors for build failures

The same function had a smaller problem. Any exception while building the pack was answered with:

409 {"error": "project_not_indexed", ...}

The companion then told you to run tokencap make. If the project was already indexed and the build failed for another reason, running make again changed nothing and hid the real error.

The bridge now checks whether the project is indexed before choosing the answer:

409 {"error": "project_not_indexed", "detail": "Run 'tokencap make' in this project first."}

500 {"error": "context_build_failed", "detail": "<the error message>",
     "hint": "Run 'tokencap make --rebuild'; if it persists, report this error."}

The 409 is also returned up front when a request names a relative projectPath or a project that is not indexed. Bridge endpoints act only on indexed projects named by absolute path, so they no longer create .tokencap state in arbitrary directories.

What to do if you used the companion before this fix

  • Update TokenCap with tokencap update.
  • Look at what your primer is built from: the README overview, the package.json description, and .tokencap/agent/agent.json. If any of them held a secret, treat it as exposed to the chats you injected into and rotate it.
  • Run tokencap analyze security scan to check the generated files under .tokencap/.

Limits

Redaction is pattern-based. Failing closed means a crash in the redactor can no longer leak text, but a secret in a format no pattern knows is still passed through, as it would be anywhere else in TokenCap. A request can also turn redaction off on purpose with "redactSecrets": false; the default is on. And the pairing token and origin allowlist stop a random web page from reading your projects over the port. They are not a defence against malware already running as your user.

More: tokencap.vansharora.app

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.