fix(web): harden apiFetch and session cookie cleanup

- apiFetch forces credentials same-origin and redirect error after the
  caller init, so a caller cannot widen either and a future redirect is
  refused like in SSEStream.
- clearToken reuses the cookie deletion clearSession uses, including
  SameSite=Lax and Secure over HTTPS.
- loadOlderHistory catches the ForbiddenError and shows the server
  message instead of leaving an unhandled rejection.
This commit is contained in:
BaaRai
2026-12-03 00:52:13 +01:00
parent c86cee8853
commit d597c156d7
3 changed files with 16 additions and 3 deletions
+5
View File
@@ -2,6 +2,8 @@
import * as React from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { t } from "@/i18n";
@@ -150,6 +152,9 @@ export default function LogsPage() {
});
}
setHasMore(data.has_more ?? false);
} catch (reason) {
// A 403 ForbiddenError carries the server message; a network failure carries its own.
toast.error((reason as Error)?.message ?? String(reason));
} finally {
setLoadingHistory(false);
}
+2
View File
@@ -50,6 +50,8 @@ export async function apiFetch(path: string, init?: RequestInit): Promise<Respon
const token = getToken();
const r = await fetch(`/api${path}`, {
...init,
credentials: "same-origin",
redirect: "error",
headers: {
...(token ? { Authorization: `Bearer ${token}` } : {}),
...(init?.headers as Record<string, string> | undefined),
+9 -3
View File
@@ -10,13 +10,19 @@ export interface CurrentUser {
role: string;
}
// clearTokenCookie expires the route-guard cookie with the same attributes
// setToken uses, so every path that ends a session leaves no cookie behind.
function clearTokenCookie(): void {
const secure = window.location.protocol === "https:" ? "; Secure" : "";
document.cookie = `${TOKEN_KEY}=; path=/; max-age=0; SameSite=Lax${secure}`;
}
// clearSession ends the session after the API answered 401: it drops the
// stored token and the route-guard cookie and sends the browser to /login.
export function clearSession(): void {
if (typeof window === "undefined") return;
localStorage.removeItem(TOKEN_KEY);
const secure = window.location.protocol === "https:" ? "; Secure" : "";
document.cookie = `${TOKEN_KEY}=; path=/; max-age=0; SameSite=Lax${secure}`;
clearTokenCookie();
window.location.href = "/login";
}
@@ -39,7 +45,7 @@ export const auth = {
clearToken(): void {
localStorage.removeItem(TOKEN_KEY);
document.cookie = `${TOKEN_KEY}=; path=/; max-age=0`;
clearTokenCookie();
},
// Parse the current user from the JWT payload's sub field; display only, no signature verification.