Skip to content
Merged
Changes from 1 commit
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
43 changes: 40 additions & 3 deletions web/app/api/subrouter/exchange/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,47 @@ export async function POST(request: Request): Promise<Response> {
if (!resolved.ok) return resolved.response;

try {
const tenant = await resolved.value.client.exchangeTeam(
resolved.value.accessToken,
resolved.value.team,
// Keep the trusted exchange request in this route. Older cached server
// bundles delegated to a legacy client which omitted both the control
// credential and scoped capabilities.
const controlToken =
process.env.SUBROUTER_STACK_TENANT_DELETE_TOKEN?.trim();
const hostedUrl = process.env.SUBROUTER_HOSTED_URL?.trim().replace(
/\/+$/,
"",
);
if (!controlToken || !hostedUrl) {
return Response.json(
{ error: "service_unavailable" },
{ status: 503 },
);
}
const capabilities = [
...(resolved.value.team.manageAccounts ? ["manage_accounts"] : []),
...(resolved.value.team.use ? ["use"] : []),
];
const upstream = await fetch(`${hostedUrl}/_subrouter/auth/stack`, {
method: "POST",
headers: {
authorization: `Bearer ${resolved.value.accessToken}`,
"content-type": "application/json",
"x-subrouter-stack-control-token": controlToken,
},
body: JSON.stringify({
capabilities,
teamId: resolved.value.team.teamId,
teamName: resolved.value.team.teamName,
}),
cache: "no-store",
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add the hosted-exchange regression test before merging.

The supplied PR contains no behavior-level regression test for the production login failure. Add a first commit that fails when the route omits the control header or scoped capabilities. Add the route fix in the second commit. Cover the authorization header, control header, capability derivation, no-store behavior, and upstream failure mapping.

As per coding guidelines, “Regression fixes should use two commits: the first adds only the failing test, and the second adds the fix.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/app/api/subrouter/exchange/route.ts` around lines 15 - 47, Add a
behavior-level regression test for the exchange route before changing
implementation, with the initial commit containing only the failing test; then
place the route fix in a second commit. The test should verify the authorization
and control headers, capability derivation from resolved team permissions,
no-store fetch behavior, and mapping of upstream failures. Anchor the test
around the route handler and its existing upstream fetch path.

Source: Coding guidelines

const body = await upstream.text();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "## locate target"
fd -a 'route.ts$' . | sed 's#^\./##' | rg 'web/app/api/subrouter/exchange/route.ts|route\.ts$' | sort

echo "## target file excerpt"
cat -n web/app/api/subrouter/exchange/route.ts | sed -n '1,120p'

echo "## timeout/abort patterns in web route API"
rg -n "fetch\\(|AbortSignal|timeout|AbortController|signal|\\{[\\s\\S]*fetch\\(" web/app/api -g 'route.ts' -g '*.ts' | head -200

echo "## candidate timeout helpers"
rg -n "TIMEOUT|TIME|timeout|AbortSignal|AbortController|setTimeout\\(" web app packages lib server 2>/dev/null | head -300 || true

Repository: manaflow-ai/cmux

Length of output: 38212


Apply the existing subrouter auth timeout to this upstream request.

web/app/api/subrouter/exchange/route.ts:34 calls fetch(...) without a cancellation-aware deadline, and web/app/api/subrouter/exchange/route.ts:48 can also wait indefinitely while reading the hosted response body. Use SUBROUTER_STACK_AUTH_TIMEOUT_MS with a bounded AbortSignal and return the safe exchange error when it is aborted.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/app/api/subrouter/exchange/route.ts` around lines 34 - 48, Update the
upstream request in the exchange route around fetch and response-body reading to
use SUBROUTER_STACK_AUTH_TIMEOUT_MS with a cancellation-aware AbortSignal
covering both operations. Detect an abort caused by this timeout and return the
existing safe exchange error, while preserving current handling for non-timeout
failures.

Source: Coding guidelines

if (!upstream.ok) {
return new Response(body, {
status: upstream.status,
headers: { "content-type": "text/plain; charset=utf-8" },
});
}
const tenant: unknown = JSON.parse(body);
return Response.json(tenant, {
headers: { "cache-control": "no-store" },
});
Expand Down