From 378c4d9710a3507f73a1fb586330303e15f1abaf Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 19:11:32 -0600 Subject: [PATCH] fix(aggregator): validate bind configuration --- pm-stack/aggregator/src/config.ts | 29 ++++++++++-- pm-stack/aggregator/test/config.test.ts | 62 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 pm-stack/aggregator/test/config.test.ts diff --git a/pm-stack/aggregator/src/config.ts b/pm-stack/aggregator/src/config.ts index 0b16a4564b..06854ae7d7 100644 --- a/pm-stack/aggregator/src/config.ts +++ b/pm-stack/aggregator/src/config.ts @@ -5,6 +5,28 @@ import { join } from "node:path"; const s = (v: string | undefined, d: string): string => (v && v.trim() !== "" ? v.trim() : d); +function parseAggregatorPort(value: string | undefined): number { + const normalized = s(value, "4870"); + const port = Number(normalized); + if (!/^\d+$/.test(normalized) || !Number.isSafeInteger(port) || port < 1 || port > 65535) { + throw new Error( + `Invalid PM_AGG_PORT=${JSON.stringify(value)}; expected a decimal integer in range 1-65535`, + ); + } + return port; +} + +function parseAggregatorHosts(value: string | undefined): string[] { + const hosts = s(value, "127.0.0.1") + .split(",") + .map((host) => host.trim()) + .filter(Boolean); + if (hosts.length === 0) { + throw new Error(`Invalid PM_AGG_HOSTS=${JSON.stringify(value)}; expected at least one non-empty host`); + } + return hosts; +} + export interface Config { /** "fixtures" | "live" — the default Source. */ source: "fixtures" | "live"; @@ -78,11 +100,8 @@ export interface Config { export function loadConfig(env: Record = process.env): Config { return { source: env.PM_AGG_SOURCE === "live" ? "live" : "fixtures", - port: Number(s(env.PM_AGG_PORT, "4870")), - hosts: s(env.PM_AGG_HOSTS, "127.0.0.1") - .split(",") - .map((h) => h.trim()) - .filter(Boolean), + port: parseAggregatorPort(env.PM_AGG_PORT), + hosts: parseAggregatorHosts(env.PM_AGG_HOSTS), feedFile: s(env.PM_AGG_FEED_FILE, join(process.cwd(), "fleet-feed.json")), commentsFile: s(env.PM_AGG_COMMENTS_FILE, join(process.cwd(), "comments.json")), roadmapAnnotationsFile: s(env.PM_AGG_ROADMAP_ANNOTATIONS_FILE, join(process.cwd(), "roadmap-annotations.json")), diff --git a/pm-stack/aggregator/test/config.test.ts b/pm-stack/aggregator/test/config.test.ts new file mode 100644 index 0000000000..69f7380c63 --- /dev/null +++ b/pm-stack/aggregator/test/config.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, test } from "bun:test"; +import { loadConfig } from "../src/config.ts"; + +describe("loadConfig PM_AGG_PORT", () => { + test("keeps the existing default for unset and empty values", () => { + expect(loadConfig({}).port).toBe(4870); + expect(loadConfig({ PM_AGG_PORT: "" }).port).toBe(4870); + expect(loadConfig({ PM_AGG_PORT: " " }).port).toBe(4870); + }); + + test("accepts decimal integer ports from 1 through 65535", () => { + expect(loadConfig({ PM_AGG_PORT: "1" }).port).toBe(1); + expect(loadConfig({ PM_AGG_PORT: "4871" }).port).toBe(4871); + expect(loadConfig({ PM_AGG_PORT: "65535" }).port).toBe(65535); + }); + + test("rejects invalid values with the variable, value, and expected range", () => { + for (const value of ["0", "abc", "12.5", "-1", "99999", "0x10", "1e3", "4870oops"]) { + let thrown: unknown; + try { + loadConfig({ PM_AGG_PORT: value }); + } catch (error) { + thrown = error; + } + + expect(thrown).toBeInstanceOf(Error); + expect((thrown as Error).message).toContain("PM_AGG_PORT"); + expect((thrown as Error).message).toContain(JSON.stringify(value)); + expect((thrown as Error).message).toContain("1-65535"); + } + }); +}); + +describe("loadConfig PM_AGG_HOSTS", () => { + test("keeps the existing default for unset and empty values", () => { + expect(loadConfig({}).hosts).toEqual(["127.0.0.1"]); + expect(loadConfig({ PM_AGG_HOSTS: "" }).hosts).toEqual(["127.0.0.1"]); + expect(loadConfig({ PM_AGG_HOSTS: " " }).hosts).toEqual(["127.0.0.1"]); + }); + + test("trims and preserves configured hosts", () => { + expect(loadConfig({ PM_AGG_HOSTS: "localhost, 127.0.0.1" }).hosts).toEqual([ + "localhost", + "127.0.0.1", + ]); + }); + + test("rejects lists that contain no non-empty host", () => { + for (const value of [",", " , , "]) { + let thrown: unknown; + try { + loadConfig({ PM_AGG_HOSTS: value }); + } catch (error) { + thrown = error; + } + + expect(thrown).toBeInstanceOf(Error); + expect((thrown as Error).message).toContain("PM_AGG_HOSTS"); + expect((thrown as Error).message).toContain(JSON.stringify(value)); + } + }); +});