mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
fix(admin-api): bypass browser cache locks for live requests
Use no-store on both client requests and admin responses. Personalized live data must not be reused, and one stalled same-URL cache entry must not block later log requests behind the browser cache lock.
This commit is contained in:
1 parent
6cdcedde92
commit
82206b066a
3 files changed
+27
-1
No files matched your search
@@ -15,6 +15,10 @@ export function useAuthState() {
|
||||
export async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(path, {
|
||||
credentials: "same-origin",
|
||||
// Live data, never from the browser cache. This also keeps requests out of
|
||||
// its per-URL cache lock: otherwise one request that never answers makes
|
||||
// every later request for the same URL wait up to 20 s behind it.
|
||||
cache: "no-store",
|
||||
...init,
|
||||
// JSON in/out: h3's readBody only parses JSON bodies when the request
|
||||
// declares application/json, and the browser defaults string bodies to
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { defineEventHandler, getMethod, getRouterParam, setResponseStatus } from "h3";
|
||||
import {
|
||||
defineEventHandler,
|
||||
getMethod,
|
||||
getRouterParam,
|
||||
setResponseHeader,
|
||||
setResponseStatus,
|
||||
} from "h3";
|
||||
import {
|
||||
adminApiMe,
|
||||
adminApiGroupsGet,
|
||||
@@ -27,6 +33,8 @@ import {
|
||||
export default defineEventHandler((event) => {
|
||||
const method = getMethod(event);
|
||||
const seg = (getRouterParam(event, "slug") ?? "").split("/").filter(Boolean);
|
||||
// Per-user, live admin data: no browser or proxy may store or reuse it.
|
||||
setResponseHeader(event, "Cache-Control", "no-store");
|
||||
|
||||
if (seg[0] === "me" && method === "GET") return adminApiMe(event);
|
||||
if (seg[0] === "groups" && seg.length === 1) {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import handler from "../server/routes/admin/api/[...slug]";
|
||||
import { makeEvent, responseHeader, responseStatus } from "./helpers";
|
||||
|
||||
describe("admin API route", () => {
|
||||
it("never lets a response be cached", async () => {
|
||||
const event = makeEvent("/admin/api/nope", { env: { GITHUB_WEBHOOK_SECRET: "s" } as never });
|
||||
event.context.params = { slug: "nope" };
|
||||
const res = await handler(event);
|
||||
expect(responseStatus(event)).toBe(404);
|
||||
expect(res).toEqual({ error: "Not found" });
|
||||
expect(responseHeader(event, "cache-control")).toBe("no-store");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user