mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
fix(monitoring): sign the GlitchTip relay in and strip Prisma call arguments
The browser error relay forwarded anything posted to it into the operator's GlitchTip without a session, limited only per address, so anyone could fill the incident list. Separately, a Prisma client error prints the failed call into its message and stack, with the arguments object (a reading's value, a note's text) when no source map is at hand, and the server sent that to GlitchTip and the logs after a credential scrub that does not look for it. The relay now needs a signed-in session and is limited per account; errors on pages before sign-in go unreported. The central redaction cuts a Prisma call's printed arguments, keeping the call, a reason line that names a field, and the stack frames, and the GlitchTip sender applies the redaction to every event at the egress boundary, whichever caller built it.
This commit is contained in:
1 parent
f2a9b0fc0e
commit
a20897b305
5 files changed
+121
-19
No files matched your search
@@ -6,6 +6,7 @@ vi.mock("@/lib/db", () => ({ prisma: {} }));
|
||||
vi.mock("@/lib/api-handler", () => ({
|
||||
apiHandler: <T extends (...args: unknown[]) => Promise<Response>>(h: T): T =>
|
||||
h,
|
||||
requireAuth: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/rate-limit", () => ({
|
||||
@@ -29,6 +30,7 @@ import { POST } from "../route";
|
||||
import { checkRateLimit } from "@/lib/rate-limit";
|
||||
import { getGlitchtipSettings } from "@/lib/monitoring-settings";
|
||||
import { sendGlitchtipEvent } from "@/lib/monitoring/glitchtip";
|
||||
import { requireAuth } from "@/lib/api-handler";
|
||||
|
||||
function jsonReq(body: unknown, contentType = "application/json"): NextRequest {
|
||||
return new NextRequest("http://localhost/api/monitoring/glitchtip", {
|
||||
@@ -40,6 +42,9 @@ function jsonReq(body: unknown, contentType = "application/json"): NextRequest {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(requireAuth).mockResolvedValue({
|
||||
user: { id: "user-1" },
|
||||
} as Awaited<ReturnType<typeof requireAuth>>);
|
||||
vi.mocked(checkRateLimit).mockResolvedValue({
|
||||
allowed: true,
|
||||
remaining: 19,
|
||||
@@ -58,6 +63,20 @@ beforeEach(() => {
|
||||
});
|
||||
|
||||
describe("POST /api/monitoring/glitchtip", () => {
|
||||
it("relays nothing for a caller without a session", async () => {
|
||||
vi.mocked(requireAuth).mockRejectedValue(new Error("Unauthorized"));
|
||||
await expect(POST(jsonReq({ message: "injected" }))).rejects.toThrow();
|
||||
expect(sendGlitchtipEvent).not.toHaveBeenCalled();
|
||||
expect(checkRateLimit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("limits the relay per account", async () => {
|
||||
await POST(jsonReq({ message: "boom" }));
|
||||
expect(vi.mocked(checkRateLimit).mock.calls[0]![0]).toBe(
|
||||
"glitchtip:user:user-1",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns 429 when rate limit is exceeded", async () => {
|
||||
vi.mocked(checkRateLimit).mockResolvedValue({
|
||||
allowed: false,
|
||||
@@ -169,20 +188,9 @@ describe("POST /api/monitoring/glitchtip", () => {
|
||||
expect(res.status).toBe(502);
|
||||
});
|
||||
|
||||
it("rate-limits per IP — uses x-forwarded-for as the bucket key", async () => {
|
||||
const r = new NextRequest("http://localhost/api/monitoring/glitchtip", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-forwarded-for": "10.0.0.42",
|
||||
},
|
||||
body: JSON.stringify({ message: "boom" }),
|
||||
});
|
||||
await POST(r);
|
||||
it("keeps the documented 20 per minute window", async () => {
|
||||
await POST(jsonReq({ message: "boom" }));
|
||||
const callArgs = vi.mocked(checkRateLimit).mock.calls[0];
|
||||
// First arg is the bucket key — must include the resolved client IP.
|
||||
expect(callArgs?.[0]).toContain("10.0.0.42");
|
||||
// 20 requests / 60 000 ms — guard the documented sliding window.
|
||||
expect(callArgs?.[1]).toBe(20);
|
||||
expect(callArgs?.[2]).toBe(60_000);
|
||||
});
|
||||
|
||||
@@ -4,13 +4,12 @@ import {
|
||||
apiError,
|
||||
apiSuccess,
|
||||
apiValidationError,
|
||||
getClientIp,
|
||||
sanitiseZodIssues,
|
||||
} from "@/lib/api-response";
|
||||
import { checkRateLimit } from "@/lib/rate-limit";
|
||||
import { getGlitchtipSettings } from "@/lib/monitoring-settings";
|
||||
import { sendGlitchtipEvent } from "@/lib/monitoring/glitchtip";
|
||||
import { apiHandler } from "@/lib/api-handler";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate, getEvent } from "@/lib/logging/context";
|
||||
|
||||
const clientErrorSchema = z
|
||||
@@ -24,11 +23,17 @@ const clientErrorSchema = z
|
||||
})
|
||||
.strict();
|
||||
|
||||
/**
|
||||
* The browser's error reporter relays here into the operator's GlitchTip.
|
||||
* It needs a signed-in session: the relay used to be anonymous, so anyone
|
||||
* could write events into the incident list, limited only per address. A
|
||||
* page that fails before sign-in goes unreported, which is the price.
|
||||
*/
|
||||
export const POST = apiHandler(async (request: NextRequest) => {
|
||||
annotate({ action: { name: "monitoring.glitchtip.report" } });
|
||||
|
||||
const ip = getClientIp(request);
|
||||
const rl = await checkRateLimit(`glitchtip:${ip}`, 20, 60 * 1000);
|
||||
const { user } = await requireAuth();
|
||||
const rl = await checkRateLimit(`glitchtip:user:${user.id}`, 20, 60 * 1000);
|
||||
if (!rl.allowed) return apiError("Rate limit exceeded", 429);
|
||||
|
||||
const settings = await getGlitchtipSettings();
|
||||
|
||||
Reference in new issue
Block a user