Files
OmniRoute/src/app/api/middleware/hooks/route.ts
Chirag Singhal 237bd59173 fix(api): sanitize catch-block error.message in middleware/hooks routes (#6645)
* fix(api): sanitize catch-block error.message in middleware/hooks routes

POST /api/middleware/hooks and PUT /api/middleware/hooks/[name] returned
the raw error?.message in their 500 response bodies (Hard Rule #12),
which could leak internal SQLite error text/paths on a DB failure. Both
now route through sanitizeErrorMessage() from open-sse/utils/error.ts,
matching the pattern already used elsewhere in the codebase.

Regression guard: tests/unit/middleware-hooks-error-sanitization.test.ts

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* test(mutation): register middleware-hooks-error-sanitization in stryker tap.testFiles

The mutation test-coverage gate (check:mutation-test-coverage --strict)
flagged tests/unit/middleware-hooks-error-sanitization.test.ts as
covering open-sse/utils/error.ts but missing from stryker.conf.json's
tap.testFiles list.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(changelog): restore CHANGELOG bullets eaten by release sync

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(changelog): re-restore CHANGELOG bullet after further release sync

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Chirag Singhal <chirag127@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
2026-07-09 17:31:52 -03:00

121 lines
3.8 KiB
TypeScript

import { NextResponse } from "next/server";
import { z } from "zod";
import {
getAllMiddlewareHooks,
createMiddlewareHook,
getMiddlewareHook,
getHookLogs,
} from "@/lib/localDb";
import { registerHook, getAllHooks } from "@/lib/middleware/registry";
import type { HookConfig, CreateHookRequest } from "@/lib/middleware/types";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
const hookScopeSchema = z.union([
z.object({ type: z.literal("global") }),
z.object({ type: z.literal("combo"), comboId: z.string().trim().min(1) }),
]);
const createHookSchema = z.object({
name: z
.string()
.trim()
.min(1, "name is required")
.regex(/^[a-zA-Z0-9_-]+$/, "name must contain only letters, numbers, hyphens, and underscores"),
description: z.string().optional().default(""),
priority: z.number().int().optional().default(200),
scope: hookScopeSchema.optional().default({ type: "global" }),
code: z.string().trim().min(1, "code is required"),
});
/**
* GET /api/middleware/hooks — List all registered hooks
*/
export async function GET(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const url = new URL(request.url);
const hookName = url.searchParams.get("name");
const includeLogs = url.searchParams.get("logs") === "true";
const logLimit = parseInt(url.searchParams.get("logLimit") || "10", 10);
if (hookName) {
const hook = getMiddlewareHook(hookName);
if (!hook) {
return NextResponse.json({ error: "Hook not found" }, { status: 404 });
}
const result: Record<string, unknown> = { hook };
if (includeLogs) {
result.logs = getHookLogs(hookName, logLimit);
}
return NextResponse.json(result);
}
const hooks = getAllMiddlewareHooks();
const registryHooks = getAllHooks();
return NextResponse.json({
hooks,
registryStats: {
dbCount: hooks.length,
registryCount: registryHooks.length,
},
});
} catch (error) {
console.error("[API] GET /api/middleware/hooks error:", error);
return NextResponse.json({ error: "Failed to list hooks" }, { status: 500 });
}
}
/**
* POST /api/middleware/hooks — Register a new hook
*
* Body: { name, description?, priority?, scope?, code }
*/
export async function POST(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const rawBody = await request.json();
const validation = validateBody(createHookSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const body: CreateHookRequest = validation.data;
// Check for duplicate
const existing = getMiddlewareHook(body.name);
if (existing) {
return NextResponse.json({ error: `Hook "${body.name}" already exists` }, { status: 409 });
}
const hookConfig: HookConfig = {
name: body.name,
description: body.description || "",
priority: body.priority ?? 200,
scope: body.scope || { type: "global" },
enabled: true,
code: body.code,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
runCount: 0,
};
// Persist to DB
const saved = createMiddlewareHook(hookConfig);
// Register in runtime registry
registerHook(saved);
return NextResponse.json({ hook: saved }, { status: 201 });
} catch (error: any) {
console.error("[API] POST /api/middleware/hooks error:", error);
return NextResponse.json({ error: sanitizeErrorMessage(error) || "Failed to create hook" }, { status: 500 });
}
}