fix(api): await params in Agent Bridge DNS route (Next.js 16) (#7271) (#7492)

This commit is contained in:
Ronaldo Davi
2026-07-18 11:35:26 -03:00
committed by GitHub
parent 28879375b7
commit 0730eee9f2
2 changed files with 39 additions and 2 deletions

View File

@@ -13,10 +13,10 @@ import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
import { createErrorResponse } from "@/lib/api/errorResponse";
import { ALL_TARGETS } from "@/mitm/targets/index";
type Params = { params: { id: string } };
type Params = { params: Promise<{ id: string }> };
export async function POST(request: Request, { params }: Params): Promise<Response> {
const { id } = params;
const { id } = await params;
let body: unknown;
try {

View File

@@ -0,0 +1,37 @@
import test from "node:test";
import assert from "node:assert/strict";
import { POST } from "../../src/app/api/tools/agent-bridge/agents/[id]/dns/route.ts";
/**
* Regression for #7271: Agent Bridge "Start DNS" returned HTTP 404.
*
* Next.js 16 makes a dynamic route's `params` a Promise. The handler destructured
* `id` directly from the (un-awaited) Promise, so `id` was `undefined` and the
* agent lookup always missed — producing `404 Unknown agent: undefined` for every
* request, including valid agents.
*
* This test drives the handler with the Next.js 16 params shape (a Promise) and an
* unknown agent id. It never reaches the DNS side-effects (the 404 fires before
* `addDNSEntry`), so it is a pure, side-effect-free reproduction: the 404 message
* must echo the *resolved* id — proving `params` was awaited — not `undefined`.
*/
test("POST /agents/[id]/dns awaits params — 404 echoes the real id, not 'undefined'", async () => {
const agentId = "definitely-not-a-real-agent-7271";
const request = new Request("http://localhost/api/tools/agent-bridge/agents/x/dns", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ enabled: false }),
});
const res = await POST(request, { params: Promise.resolve({ id: agentId }) });
const body = await res.json();
assert.equal(res.status, 404);
// Before the fix, `id` was `undefined`, so the message read "Unknown agent: undefined".
assert.equal(body.error.message, `Unknown agent: ${agentId}`);
assert.ok(
!body.error.message.includes("undefined"),
"params must be awaited — message should not contain 'undefined'"
);
});