From 746abc2dcbd5216f76392e9c9ef45201749631f5 Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Fri, 20 Nov 2026 14:36:01 +0000 Subject: [PATCH] fix(settings): cap the stored body capture limit at the config value --- src/services/settings.ts | 19 +++++++++++++++--- tests/settings-ceiling.test.ts | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 tests/settings-ceiling.test.ts diff --git a/src/services/settings.ts b/src/services/settings.ts index ef43ed8..ec6b33f 100644 --- a/src/services/settings.ts +++ b/src/services/settings.ts @@ -1,4 +1,9 @@ -import { applyConfigOverrides, type DmsApiConfig, getConfig } from "@/config"; +import { + applyConfigOverrides, + type DmsApiConfig, + getBaseConfig, + getConfig, +} from "@/config"; import type { ApiSettingsModel } from "@/db"; import type { ApiSettings } from "@/db/tables/api_settings.table"; import { isRouteScope, type RouteScope } from "./scope"; @@ -13,6 +18,9 @@ import { isRouteScope, type RouteScope } from "./scope"; * call site — capture middleware, prune crons, aggregations — picks them up * without knowing about the DB. The scope is cached here for synchronous * access from the read endpoints. + * + * The body capture limit set in `antelope.config.ts` is a ceiling: a stored + * setting above it is capped to it. */ export interface ApiSettingsPayload { @@ -54,7 +62,10 @@ function overridesFrom(doc: SettingsDocument): Partial { overrides.requestSlownessThreshold = doc.requestSlownessThreshold; } if (doc.requestLogMaxBodySize != null) { - overrides.requestLogMaxBodySize = doc.requestLogMaxBodySize; + overrides.requestLogMaxBodySize = Math.min( + doc.requestLogMaxBodySize, + getBaseConfig().requestLogMaxBodySize, + ); } if (doc.requestLogCaptureHeaders != null) { overrides.requestLogCaptureHeaders = doc.requestLogCaptureHeaders; @@ -78,7 +89,9 @@ function applyDocument(doc: SettingsDocument | undefined): void { } /** Load persisted settings and apply them; called once at module start. */ -export async function loadSettings(model: ApiSettingsModel): Promise { +export async function loadSettings( + model: Pick, +): Promise { applyDocument(await model.getSingleton()); } diff --git a/tests/settings-ceiling.test.ts b/tests/settings-ceiling.test.ts new file mode 100644 index 0000000..6c6f607 --- /dev/null +++ b/tests/settings-ceiling.test.ts @@ -0,0 +1,36 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { applyConfigOverrides, getConfig, setConfig } from "@/config"; +import type { ApiSettingsModel } from "@/db"; +import { ApiSettings } from "@/db/tables/api_settings.table"; +import { loadSettings } from "@/services/settings"; + +function modelStoring( + requestLogMaxBodySize: number, +): Pick { + return { + getSingleton: async () => + Object.assign(new ApiSettings(), { + scope: "own", + requestLogMaxBodySize, + }), + }; +} + +describe("a stored settings override", () => { + afterEach(() => { + applyConfigOverrides({}); + setConfig(); + }); + + it("is capped to the body capture limit of the config", async () => { + setConfig({ requestLogMaxBodySize: 1_000 }); + await loadSettings(modelStoring(1_000_000)); + expect(getConfig().requestLogMaxBodySize).toBe(1_000); + }); + + it("still lowers the limit under the config ceiling", async () => { + setConfig({ requestLogMaxBodySize: 1_000 }); + await loadSettings(modelStoring(200)); + expect(getConfig().requestLogMaxBodySize).toBe(200); + }); +});