🐛 fix: harden config validation against malformed entries

This commit is contained in:
2026-07-01 13:22:55 +07:00
parent 27f9f20729
commit e5c9aca45d
8 changed files with 94 additions and 23 deletions
+2 -2
View File
@@ -6,12 +6,12 @@ import Updater from "./updater";
class App {
public loadConfig(): void {
Logger.log("info", "Loading configuration");
Logger.info("Loading configuration");
Configuration.init();
}
public loadENV(): void {
Logger.log("info", "Loading environment");
Logger.info("Loading environment");
Environment.init();
}
public loadUpdater(): void {
+9
View File
@@ -48,6 +48,15 @@ describe("Configuration", () => {
]);
});
test("getConfig with no key returns all loaded configuration data", async () => {
const { default: Configuration } = await import(`./configuration?t=${Date.now()}-5`);
Configuration.init();
expect(Configuration.getConfig()).toEqual({
UpdaterConfig: [{ token: "example-token", updateInterval: 60, zone: [] }],
});
});
test("getConfig throws for an unknown key", async () => {
const { default: Configuration } = await import(`./configuration?t=${Date.now()}-3`);
Configuration.init();
+9 -2
View File
@@ -59,9 +59,16 @@ function makeCloudflareConfig(overrides: Partial<CloudflareConfig> = {}): Cloudf
};
}
async function freshUpdater() {
interface TestableUpdater {
config: CloudflareConfig[];
start: () => void;
init: () => void;
update: (config: CloudflareConfig) => Promise<void>;
}
async function freshUpdater(): Promise<TestableUpdater> {
const { default: Updater } = await import(`./updater?t=${Date.now()}-${Math.random()}`);
return Updater as any;
return Updater as unknown as TestableUpdater;
}
describe("Updater.init", () => {
+2 -2
View File
@@ -68,8 +68,8 @@ class Updater {
return;
}
IPv4 && Logger.info(`Current IPv4 address: ${IPv4}`);
IPv6 && Logger.info(`Current IPv6 address: ${IPv6}`);
if (IPv4) Logger.info(`Current IPv4 address: ${IPv4}`);
if (IPv6) Logger.info(`Current IPv6 address: ${IPv6}`);
for (const zone of cloudflareConfig.zone) {
const api = new CloudflareAPI(token, zone.id);