mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-16 04:03:02 +03:00
* fix(api): route error responses through sanitizeErrorMessage (Hard Rule #12) 9 API routes returned raw String(error)/error.message directly in HTTP 500 bodies, leaking SQLite paths, SQL text and internal messages. Route all through sanitizeErrorMessage() per Hard Rule #12: - settings/compression (GET+PUT), settings/compression/mcp-accessibility (GET+PUT) - cache/entries (GET+POST), db/health (GET+POST), db-backups/exportAll - assess, combos/test, settings/notion, settings/obsidian Test: tests/unit/rule12-error-sanitization-sweep.test.ts asserts sanitized 500 bodies contain no absolute paths / stack tails. * test(stryker): register rule12 error-sanitization sweep in tap.testFiles The new tests/unit/rule12-error-sanitization-sweep.test.ts covers a mutated module, so it must be listed in stryker.conf.json tap.testFiles for the mutation-coverage gate (check-mutation-test-coverage.mjs --strict) to pass. Co-authored-by: Chirag Singhal <chirag127@users.noreply.github.com> --------- Co-authored-by: Chirag Singhal <chirag127@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com> Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { isAuthenticated } from "@/shared/utils/apiAuth";
|
|
import {
|
|
clearObsidianToken,
|
|
getObsidianConfig,
|
|
getObsidianBaseUrl,
|
|
getObsidianVaultPath,
|
|
setObsidianToken,
|
|
setObsidianBaseUrl,
|
|
setObsidianVaultPath,
|
|
} from "@/lib/db/obsidian";
|
|
import { createObsidianClient } from "@/lib/obsidian/api";
|
|
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
|
|
|
|
const setTokenSchema = z.object({
|
|
token: z.string().min(1).max(5000),
|
|
baseUrl: z.string().url().optional(),
|
|
}).strict();
|
|
|
|
export async function GET(request: NextRequest) {
|
|
if (!(await isAuthenticated(request))) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const config = getObsidianConfig();
|
|
return NextResponse.json({
|
|
connected: config.connected,
|
|
hasToken: config.token !== null,
|
|
baseUrl: config.baseUrl,
|
|
vaultPath: config.vaultPath,
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json({ error: sanitizeErrorMessage(error) }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
if (!(await isAuthenticated(request))) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
let rawBody: unknown;
|
|
try {
|
|
rawBody = await request.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const parsed = setTokenSchema.safeParse(rawBody);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "Missing or invalid token", details: parsed.error.issues },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
let urlToUse = parsed.data.baseUrl;
|
|
|
|
if (!urlToUse) {
|
|
urlToUse = getObsidianBaseUrl();
|
|
}
|
|
|
|
if (urlToUse && /:27124(?:\/|$)/.test(urlToUse)) {
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
"URL uses port 27124, which is the MCP endpoint (HTTPS, self-signed cert). " +
|
|
"The Obsidian Local REST API uses plain HTTP on port 27123. " +
|
|
"Please use http://<ip>:27123 instead.",
|
|
connected: false,
|
|
},
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const client = createObsidianClient(parsed.data.token, urlToUse);
|
|
const result = await client.checkStatus();
|
|
const authResult = result as Record<string, unknown>;
|
|
if (authResult?.authenticated === false) {
|
|
return NextResponse.json(
|
|
{ error: "Token validation failed: invalid token", connected: false },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
setObsidianToken(parsed.data.token);
|
|
if (parsed.data.baseUrl) {
|
|
setObsidianBaseUrl(parsed.data.baseUrl);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
connected: true,
|
|
message: "Obsidian API token saved and validated",
|
|
});
|
|
} catch (error) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
return NextResponse.json({ error: sanitizeErrorMessage(msg), connected: false }, { status: 400 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
if (!(await isAuthenticated(request))) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
clearObsidianToken();
|
|
return NextResponse.json({
|
|
connected: false,
|
|
message: "Obsidian integration disconnected",
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json({ error: sanitizeErrorMessage(error) }, { status: 500 });
|
|
}
|
|
}
|