Compare commits

..

1 Commits

Author SHA1 Message Date
diegosouzapw
17c3a693df fix(dashboard): surface auth-required banner for guest-session database settings (#12709)
/api/settings/database and /api/settings/import-json are intentionally
ALWAYS_PROTECTED (GHSA-mghq-58h3-qcqj, GHSA-v7g9-7f55-5g46) and correctly
401 a guest/anonymous session. SystemStorageTab.tsx silently collapsed
that 401 to null, so the entire database-settings section of Settings ->
General just disappeared with zero explanation ("Failed to load
settings").

Extract the fetch/detection logic into systemStorageAuth.tsx (new module,
keeps SystemStorageTab.tsx within its frozen file-size baseline) and
surface an explicit auth-required banner plus a dedicated JSON-import
error message instead of a blank page.
2026-09-10 15:34:48 -03:00
10 changed files with 163 additions and 196 deletions

View File

@@ -1 +0,0 @@
- fix(sse): cap HuggingChat NDJSON body size and bound the read loop with the fetch timeout so a stalled or hostile upstream cannot buffer unbounded memory (#12577)

View File

@@ -0,0 +1 @@
- fix(dashboard): surface an authentication-required banner instead of silently blanking database settings for a guest session (#12709)

View File

@@ -55,14 +55,6 @@ export const SSE_HEARTBEAT_INTERVAL_MS = upstreamTimeouts.sseHeartbeatIntervalMs
// Defaults to FETCH_TIMEOUT_MS. Override with FETCH_BODY_TIMEOUT_MS env var.
export const FETCH_BODY_TIMEOUT_MS = upstreamTimeouts.fetchBodyTimeoutMs;
// Hard byte cap on the HuggingChat NDJSON body accumulated by
// open-sse/executors/huggingchat/jsonlStream.ts. Prevents a stalled/hostile upstream that
// never emits a terminal `finalAnswer` / `status: finished` marker from buffering
// indefinitely (#12577). Sized generously for legitimate long completions while staying
// well below a heap-exhausting size — mirrors the readCappedBuffer/readBodyCapped pattern
// already used by veoaifree-web.ts and context7-fetch.ts.
export const HUGGINGCHAT_MAX_BODY_BYTES = 4 * 1024 * 1024;
// Provider configurations
// OAuth credentials read from env vars with hardcoded fallbacks for backward compatibility.
// Use provider-credentials.json or env vars to override in production.

View File

@@ -538,7 +538,7 @@ export class HuggingChatExecutor extends BaseExecutor {
resolvedModel,
id,
created,
combinedSignal,
signal,
streamCancellationController.signal
);
@@ -626,7 +626,7 @@ export class HuggingChatExecutor extends BaseExecutor {
let fullText: string;
try {
fullText = await readJsonlResponse(upstreamResponse.body, combinedSignal);
fullText = await readJsonlResponse(upstreamResponse.body, signal);
} catch (err) {
if (!(err instanceof HuggingChatStreamError)) throw err;
const message = err instanceof Error ? err.message : String(err);

View File

@@ -1,10 +1,5 @@
// Pure JSONL stream translation (HuggingChat NDJSON -> OpenAI SSE). Verbatim from huggingchat.ts.
import { HUGGINGCHAT_MAX_BODY_BYTES } from "../../config/constants.ts";
const MAX_BODY_EXCEEDED_MESSAGE =
"HuggingChat response exceeded the maximum supported size before completing";
export class HuggingChatStreamError extends Error {
constructor(message: string) {
super(message);
@@ -79,23 +74,15 @@ export async function* streamJsonlToOpenAi(
id: string,
created: number,
signal?: AbortSignal | null,
cancellationSignal?: AbortSignal | null,
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
cancellationSignal?: AbortSignal | null
): AsyncGenerator<string> {
const reader = body.getReader();
const unbindReaderCancellation = bindReaderCancellation(reader, cancellationSignal);
// Also bind the plain `signal` so an already-in-flight `reader.read()` unblocks the
// instant it aborts, instead of only being noticed the next time the loop polls
// `signal?.aborted` (#12577 — a stalled upstream can otherwise leave the read
// suspended forever even once a caller-supplied timeout signal has fired).
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
const decoder = new TextDecoder();
let buffer = "";
let emittedRole = false;
let fullText = "";
let finished = false;
let totalBytes = 0;
let exceededCap = false;
try {
while (true) {
@@ -104,13 +91,6 @@ export async function* streamJsonlToOpenAi(
const { value, done } = await reader.read();
if (done) break;
totalBytes += value.byteLength;
if (totalBytes > maxBytes) {
exceededCap = true;
cancelReader(reader);
break;
}
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
@@ -183,7 +163,7 @@ export async function* streamJsonlToOpenAi(
if (finished) break;
}
if (!finished && !exceededCap && buffer.trim()) {
if (!finished && buffer.trim()) {
const parsed = parseJsonlLine(buffer.trim());
if (parsed.error) {
throw new HuggingChatStreamError(parsed.error);
@@ -210,26 +190,9 @@ export async function* streamJsonlToOpenAi(
}
} finally {
unbindReaderCancellation();
unbindSignalCancellation();
reader.releaseLock();
}
if (exceededCap) {
yield sseChunk({
id,
object: "chat.completion.chunk",
created,
model,
error: {
message: MAX_BODY_EXCEEDED_MESSAGE,
type: "upstream_error",
code: "huggingchat_payload_too_large",
},
});
yield "data: [DONE]\n\n";
return;
}
if (!signal?.aborted && !cancellationSignal?.aborted) {
yield sseChunk({
id,
@@ -246,19 +209,12 @@ export async function* streamJsonlToOpenAi(
export async function readJsonlResponse(
body: ReadableStream<Uint8Array>,
signal?: AbortSignal | null,
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
signal?: AbortSignal | null
): Promise<string> {
const reader = body.getReader();
// Bind the signal so an already-in-flight `reader.read()` unblocks the instant it
// aborts, instead of only being noticed the next time the loop polls `signal?.aborted`
// (#12577 — a stalled upstream can otherwise leave the read suspended forever even
// once a caller-supplied timeout signal has fired).
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
const decoder = new TextDecoder();
let buffer = "";
let fullText = "";
let totalBytes = 0;
try {
while (true) {
@@ -267,12 +223,6 @@ export async function readJsonlResponse(
const { value, done } = await reader.read();
if (done) break;
totalBytes += value.byteLength;
if (totalBytes > maxBytes) {
cancelReader(reader);
throw new HuggingChatStreamError(MAX_BODY_EXCEEDED_MESSAGE);
}
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
@@ -299,7 +249,6 @@ export async function readJsonlResponse(
if (parsed.error) throw new HuggingChatStreamError(parsed.error);
}
} finally {
unbindSignalCancellation();
reader.releaseLock();
}

View File

@@ -4,6 +4,11 @@ import { useState, useEffect, useCallback, useRef } from "react";
import { Card, Button, Badge, ConfirmModal } from "@/shared/components";
import { useLocale, useTranslations } from "next-intl";
import DatabaseBackupRetentionCard from "./DatabaseBackupRetentionCard";
import {
fetchDatabaseSettingsData,
isAuthRequiredResponse,
AuthRequiredBanner,
} from "./systemStorageAuth";
// Whitelist mirrored from src/lib/db/cleanup.ts::RESET_USAGE_HISTORY_PERIODS.
const RESET_USAGE_PERIOD_VALUES = [
@@ -29,16 +34,6 @@ async function fetchStorageHealthData() {
}
}
async function fetchDatabaseSettingsData() {
try {
const res = await fetch("/api/settings/database");
if (res.ok) return await res.json();
} catch (err) {
console.error("Failed to load database settings:", err);
}
return null;
}
export default function SystemStorageTab() {
const [backups, setBackups] = useState([]);
const [backupsLoading, setBackupsLoading] = useState(false);
@@ -108,6 +103,7 @@ export default function SystemStorageTab() {
// Database settings state (tasks 23-26)
const [dbSettings, setDbSettings] = useState<any>(null);
const [dbSettingsLoading, setDbSettingsLoading] = useState(true);
const [dbSettingsAuthRequired, setDbSettingsAuthRequired] = useState(false);
const [dbSettingsSaving, setDbSettingsSaving] = useState(false);
const [dbStatsRefreshing, setDbStatsRefreshing] = useState(false);
@@ -137,8 +133,9 @@ export default function SystemStorageTab() {
applyStorageHealth(await fetchStorageHealthData());
};
const applyDatabaseSettings = useCallback((data) => {
if (data) setDbSettings(data);
const applyDatabaseSettings = useCallback((result: { data: any; authRequired: boolean }) => {
if (result.data) setDbSettings(result.data);
setDbSettingsAuthRequired(result.authRequired);
setDbSettingsLoading(false);
}, []);
@@ -589,6 +586,8 @@ export default function SystemStorageTab() {
});
await loadStorageHealth();
if (backupsExpanded) await loadBackups();
} else if (isAuthRequiredResponse(res.status, data)) {
setImportStatus({ type: "error", message: t("jsonImportAuthRequired") });
} else {
setImportStatus({ type: "error", message: data.error || t("jsonImportFailed") });
}
@@ -1290,6 +1289,7 @@ export default function SystemStorageTab() {
</div>
</div>
{dbSettingsAuthRequired && !dbSettingsLoading && <AuthRequiredBanner t={t} />}
{renderDatabaseStatistics()}
<div className="pt-3 border-t border-border/50 mb-4">

View File

@@ -0,0 +1,57 @@
"use client";
// #12709: database-settings requests are ALWAYS_PROTECTED (routeGuard.ts) — a guest/anonymous
// session correctly gets a 401 AUTH_001 from /api/settings/database and /api/settings/import-json
// (GHSA-mghq-58h3-qcqj, GHSA-v7g9-7f55-5g46). Do NOT loosen that gate; this module only makes the
// client surface the failure instead of silently rendering a blank section.
import Link from "next/link";
export interface DatabaseSettingsFetchResult {
data: unknown;
authRequired: boolean;
}
/**
* True when a response represents the intentional auth-required rejection
* (401, optionally carrying the AUTH_001 error code) rather than some other
* transient failure.
*/
export function isAuthRequiredResponse(status: number, data: unknown): boolean {
if (status !== 401) return false;
const code = (data as { error?: { code?: string } } | null)?.error?.code;
return code === undefined || code === "AUTH_001";
}
export async function fetchDatabaseSettingsData(): Promise<DatabaseSettingsFetchResult> {
try {
const res = await fetch("/api/settings/database");
const body = await res.json().catch(() => null);
if (res.ok) return { data: body, authRequired: false };
return { data: null, authRequired: isAuthRequiredResponse(res.status, body) };
} catch (err) {
console.error("Failed to load database settings:", err);
return { data: null, authRequired: false };
}
}
export function AuthRequiredBanner({ t }: { t: (key: string) => string }) {
return (
<div
role="alert"
className="mb-4 rounded-xl border border-amber-200 dark:border-amber-500/30 bg-amber-50 dark:bg-amber-500/10 px-5 py-4"
>
<h2 className="text-sm font-semibold text-amber-900 dark:text-amber-100">
{t("databaseSettingsAuthRequiredTitle")}
</h2>
<p className="mt-1 text-sm text-amber-900/80 dark:text-amber-200/80">
{t("databaseSettingsAuthRequiredBody")}
</p>
<Link
href="/login"
className="mt-3 inline-flex items-center rounded-lg bg-amber-600 px-3.5 py-2 text-sm font-medium text-white hover:bg-amber-700 dark:bg-amber-500 dark:hover:bg-amber-400"
>
{t("databaseSettingsAuthRequiredCta")}
</Link>
</div>
);
}

View File

@@ -7741,6 +7741,10 @@
"legacyJsonImportSuccess": "Legacy JSON imported successfully!",
"jsonImportFailed": "Failed to import JSON",
"jsonImportError": "Error during JSON import",
"jsonImportAuthRequired": "Authentication required to import a legacy JSON configuration. Please sign in or complete setup first.",
"databaseSettingsAuthRequiredTitle": "Authentication required",
"databaseSettingsAuthRequiredBody": "Database settings and JSON import are only available to an authenticated admin. Sign in or complete setup to view and edit them.",
"databaseSettingsAuthRequiredCta": "Sign in",
"storagePurgeData": "Purge Data",
"storagePurgeDataDesc": "Immediately delete all records without applying retention checks. Use with caution.",
"storageRetentionCleanup": "Retention Settings",

View File

@@ -1,119 +0,0 @@
// Regression test for issue #12577: HuggingChat NDJSON executor buffered the
// upstream body with no byte ceiling and no timeout, so a stalled/hostile
// upstream that never emits a terminal marker (`finalAnswer` / `status:
// finished`) drove unbounded memory growth per in-flight request.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
streamJsonlToOpenAi,
readJsonlResponse,
HuggingChatStreamError,
} from "../../open-sse/executors/huggingchat/jsonlStream.ts";
const REASONABLE_CAP_BYTES = 2 * 1024 * 1024; // 2 MB
const TEST_SAFETY_CEILING_BYTES = REASONABLE_CAP_BYTES * 4; // 8 MB
function makeUnboundedStream(): {
body: ReadableStream<Uint8Array>;
getTotalSent: () => number;
getClosedBySafetyCeiling: () => boolean;
} {
const encoder = new TextEncoder();
const tokenChunk = "a".repeat(32 * 1024); // 32 KB token payload per line
const line = JSON.stringify({ type: "stream", token: tokenChunk }) + "\n";
const lineBytes = encoder.encode(line).byteLength;
let totalSent = 0;
let closedBySafetyCeiling = false;
const body = new ReadableStream<Uint8Array>({
pull(controller) {
if (totalSent >= TEST_SAFETY_CEILING_BYTES) {
closedBySafetyCeiling = true;
controller.close();
return;
}
controller.enqueue(encoder.encode(line));
totalSent += lineBytes;
// Deliberately never emit a finalAnswer/status:finished terminal marker.
},
});
return {
body,
getTotalSent: () => totalSent,
getClosedBySafetyCeiling: () => closedBySafetyCeiling,
};
}
test("streamJsonlToOpenAi aborts once accumulated upstream body exceeds a size cap, instead of buffering forever", async () => {
const { body, getTotalSent, getClosedBySafetyCeiling } = makeUnboundedStream();
const encoder = new TextEncoder();
let sawUpstreamErrorChunk = false;
let bytesReceivedByConsumer = 0;
for await (const chunk of streamJsonlToOpenAi(
body,
"gpt-huggingchat",
"id-1",
0,
undefined,
undefined,
REASONABLE_CAP_BYTES
)) {
bytesReceivedByConsumer += encoder.encode(chunk).byteLength;
if (/upstream_error|too_large|payload.*exceed/i.test(chunk)) {
sawUpstreamErrorChunk = true;
break;
}
}
assert.ok(
sawUpstreamErrorChunk,
`expected streamJsonlToOpenAi to abort with an upstream-error chunk once the ` +
`accumulated body exceeded ~${REASONABLE_CAP_BYTES} bytes, but it kept consuming ` +
`upstream data with no ceiling (sent ${getTotalSent()} bytes before the TEST's own ` +
`safety ceiling stepped in: closedBySafetyCeiling=${getClosedBySafetyCeiling()}, ` +
`bytesReceivedByConsumer=${bytesReceivedByConsumer}). This confirms issue #12577: ` +
`no byte cap is enforced on the read loop.`
);
assert.ok(
getTotalSent() < TEST_SAFETY_CEILING_BYTES,
"expected the cap to trip well before the test's own 8MB safety ceiling"
);
});
test("readJsonlResponse throws a HuggingChatStreamError once accumulated upstream body exceeds a size cap", async () => {
const { body, getClosedBySafetyCeiling } = makeUnboundedStream();
await assert.rejects(
() => readJsonlResponse(body, undefined, REASONABLE_CAP_BYTES),
(err: unknown) => err instanceof HuggingChatStreamError
);
assert.equal(
getClosedBySafetyCeiling(),
false,
"expected the cap to trip well before the test's own 8MB safety ceiling"
);
});
test("streamJsonlToOpenAi terminates the read loop once an idle-timeout signal fires", async () => {
const body = new ReadableStream<Uint8Array>({
pull() {
// Never enqueue and never close: simulates a stalled upstream connection
// that sends nothing at all after headers, relying solely on the caller's
// timeout signal (mirroring huggingchat.ts's combinedSignal) to unblock.
},
});
const idleTimeout = AbortSignal.timeout(50);
const chunks: string[] = [];
for await (const chunk of streamJsonlToOpenAi(body, "gpt-huggingchat", "id-2", 0, idleTimeout)) {
chunks.push(chunk);
}
assert.ok(idleTimeout.aborted, "expected the idle-timeout signal to have fired");
});

View File

@@ -0,0 +1,84 @@
// @vitest-environment jsdom
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import SystemStorageTab from "@/app/(dashboard)/dashboard/settings/components/SystemStorageTab";
vi.mock("next-intl", () => ({
useTranslations: () => (key: string) => key,
useLocale: () => "en",
}));
const roots: Array<{ root: Root; el: HTMLDivElement }> = [];
async function render(): Promise<HTMLDivElement> {
const el = document.createElement("div");
document.body.appendChild(el);
const root = createRoot(el);
await act(async () => {
root.render(<SystemStorageTab />);
});
roots.push({ root, el });
return el;
}
async function flush() {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 10));
});
}
describe("#12709 - SystemStorageTab guest-session 401 on /api/settings/database", () => {
let fetchMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
fetchMock = vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes("/api/settings/database")) {
return new Response(
JSON.stringify({ error: { code: "AUTH_001", message: "Authentication required" } }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
if (url.includes("/api/storage/health")) {
return new Response(
JSON.stringify({
driver: "sqlite",
dbPath: "~/.omniroute/storage.sqlite",
sizeBytes: 0,
retentionDays: { app: 7, call: 7 },
tableMaxRows: { callLogs: 100000, proxyLogs: 100000 },
backupCount: 0,
backupRetention: { maxFiles: 20, days: 0 },
lastBackupAt: null,
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}
return new Response("{}", { status: 200 });
});
(globalThis as any).fetch = fetchMock;
});
afterEach(() => {
for (const { root, el } of roots.splice(0)) {
act(() => root.unmount());
el.remove();
}
vi.restoreAllMocks();
});
it("surfaces an authentication-required message instead of silently hiding Settings", async () => {
const container = await render();
await flush();
await flush();
const dbCall = fetchMock.mock.calls.find((c) => String(c[0]).includes("/api/settings/database"));
expect(dbCall).toBeTruthy();
const text = container.textContent || "";
const mentionsAuth = /auth|sign in|log in|login|401|unauthorized/i.test(text);
expect(mentionsAuth).toBe(true);
});
});