Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ if [ "$DO_HOOKS" = 1 ]; then
mkdir -p "$CLAUDE_DIR/hooks"
cp "$ROOT"/hooks/inband-*.sh "$CLAUDE_DIR/hooks/"
chmod +x "$CLAUDE_DIR"/hooks/inband-*.sh
rm -f "$CLAUDE_DIR"/hooks/agent-bridge-*.sh
say "copied hooks to $CLAUDE_DIR/hooks/"

SETTINGS="$CLAUDE_DIR/settings.json"
Expand All @@ -173,6 +174,19 @@ if [ "$DO_HOOKS" = 1 ]; then
const [file, hooksDir] = process.argv.slice(1);
const s = JSON.parse(fs.readFileSync(file, "utf8"));
s.hooks ??= {};
// Drop the hooks of the project before its rename to InBand.
let removed = 0;
for (const event of Object.keys(s.hooks)) {
if (!Array.isArray(s.hooks[event])) continue;
s.hooks[event] = s.hooks[event].filter((group) => {
if (!Array.isArray(group?.hooks)) return true;
const before = group.hooks.length;
group.hooks = group.hooks.filter((hook) => !String(hook.command ?? "").includes("/agent-bridge-"));
removed += before - group.hooks.length;
return group.hooks.length > 0;
});
}
if (removed > 0) console.log(` removed ${removed} agent-bridge hook(s) from settings.json`);
const want = [
["SessionStart", "inband-name.sh", null, undefined],
["SessionEnd", "inband-disconnect.sh", null, 5],
Expand Down Expand Up @@ -212,7 +226,13 @@ if [ "$DO_HOOKS" = 1 ]; then
["Stop", "Checking the inband mailbox"],
]) {
s.hooks[event] ??= [];
if (JSON.stringify(s.hooks[event]).includes("codex-hook.ts")) continue;
// An existing entry can point to an old checkout, for example the pre-rename agent-bridge path.
const existing = s.hooks[event].flatMap((group) => group?.hooks ?? [])
.filter((hook) => String(hook.command ?? "").includes("codex-hook.ts"));
if (existing.length > 0) {
for (const hook of existing) hook.command = command;
continue;
}
s.hooks[event].push({ hooks: [{ type: "command", command, statusMessage, timeout: 5 }] });
added++;
}
Expand All @@ -231,7 +251,7 @@ if [ "$DO_HOOKS" = 1 ]; then
fi
mkdir -p "$target_dir"
local target="$target_dir/lead.md"
if [ -f "$target" ] && ! cmp -s "$ROOT/commands/$client/lead.md" "$target"; then
if [ -f "$target" ] && ! cmp -s "$ROOT/commands/$client/lead.md" "$target" && ! grep -q 'agent-bridge' "$target"; then
say "$client: $target exists and differs, left untouched"
return
fi
Expand All @@ -249,6 +269,12 @@ if [ "$DO_SERVICE" = 1 ]; then
say "no systemctl, start the daemon yourself: bun run src/index.ts"
else
mkdir -p "$UNIT_DIR"
if [ -f "$UNIT_DIR/agent-bridge.service" ]; then
# The pre-rename unit holds the same port.
systemctl --user disable --now agent-bridge >/dev/null 2>&1 || true
rm -f "$UNIT_DIR/agent-bridge.service"
say "stopped and removed the old agent-bridge service"
fi
sed -e "s|%h/.bun/bin/bun|$BUN_BIN|g" \
-e "s|%h/.local/share/mcp-servers/inband|$ROOT|g" \
"$ROOT/inband.service.example" > "$UNIT_DIR/inband.service"
Expand Down
6 changes: 5 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ export function authenticateAuthorizationHeader(runtime: AuthRuntime, authorizat
}

function getHeader(headers: Record<string, string | undefined>, name: string): string | undefined {
return headers[name] ?? headers[name.toLowerCase()] ?? headers[name.toUpperCase()];
const value = headers[name] ?? headers[name.toLowerCase()] ?? headers[name.toUpperCase()];
if (value !== undefined || !name.startsWith("x-inband-")) return value;
// Hooks installed before the rename send x-agent-bridge-* headers with the same signature.
const legacy = name.replace("x-inband-", "x-agent-bridge-");
return headers[legacy] ?? headers[legacy.toLowerCase()];
}

function purgeOldNonces(runtime: AuthRuntime, nowMs: number): void {
Expand Down
13 changes: 13 additions & 0 deletions tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ describe("inband auth", () => {
).toThrow(/replay/i);
});

test("accepts signed requests from hooks that predate the rename", () => {
const request = {
method: "GET",
url: "http://127.0.0.1:7447/claude/hook?agent=claude-api-a1b2&event=SessionStart",
};
const signed = signInbandRequest({ clientId: "claude", token: CLAUDE_TOKEN, ...request, nonce: "legacy-1" });
const legacy = Object.fromEntries(
Object.entries(signed).map(([name, value]) => [name.replace("x-inband-", "x-agent-bridge-"), value]),
);

expect(authenticateSignedRequest(runtime(), { ...request, headers: legacy }).clientId).toBe("claude");
});

test("rejects weak token config and invalid agent patterns", () => {
expect(() =>
buildAuthRuntime({
Expand Down
Loading