Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/services/settings.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -54,7 +62,10 @@ function overridesFrom(doc: SettingsDocument): Partial<DmsApiConfig> {
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;
Expand All @@ -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<void> {
export async function loadSettings(
model: Pick<ApiSettingsModel, "getSingleton">,
): Promise<void> {
applyDocument(await model.getSingleton());
}

Expand Down
36 changes: 36 additions & 0 deletions tests/settings-ceiling.test.ts
Original file line number Diff line number Diff line change
@@ -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<ApiSettingsModel, "getSingleton"> {
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);
});
});
Loading