Skip to content

Commit 93cc505

Browse files
committed
Fix edge cases in rule parser and test
- Fix test mock targeting console.warn instead of console.log in buildLegacyRules empty portsInput test - Guard against whitespace-only portsInput by trimming before fallback - Validate regex syntax in ~-prefixed rules to fail early instead of crashing HAProxy at startup
1 parent 4bbffa9 commit 93cc505

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

setup/lib/rules.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export function buildRules(rulesInput) {
1313
*/
1414
export function convertRule(rule) {
1515
if (rule.startsWith('~')) {
16-
return rule.slice(1);
16+
const regex = rule.slice(1);
17+
try { new RegExp(regex); } catch (e) {
18+
throw new Error(`Invalid regex in rule "${rule}": ${e.message}`);
19+
}
20+
return regex;
1721
}
1822
return `^${wildcardToRegex(rule)}$`;
1923
}
@@ -67,7 +71,7 @@ export function buildLegacyRules({ domainsInput, portsInput, defaultPort, protoc
6771
`::warning::Deprecated: allowed_${protocol.toLowerCase()}_domains / ${protocol.toLowerCase()}_ports inputs are deprecated. ` +
6872
`Use allowed_${protocol.toLowerCase()}_rules instead.`
6973
);
70-
return convertLegacyDomains(domainsInput, portsInput || String(defaultPort));
74+
return convertLegacyDomains(domainsInput, portsInput?.trim() || String(defaultPort));
7175
}
7276

7377
/**

setup/lib/rules.test.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ describe("convertRule", () => {
9292
"^custom\\.regex:443$",
9393
);
9494
});
95+
96+
it("rejects invalid regex (~ prefix)", () => {
97+
assert.throws(() => convertRule("~^(unclosed"), /Invalid regex/);
98+
});
9599
});
96100

97101
// ---------------------------------------------------------------------------
@@ -228,8 +232,8 @@ describe("buildLegacyRules", () => {
228232
});
229233

230234
it("falls back to defaultPort when portsInput is empty", () => {
231-
const origWarn = console.warn;
232-
console.warn = () => {};
235+
const origLog = console.log;
236+
console.log = () => {};
233237
try {
234238
const result = buildLegacyRules({
235239
domainsInput: "example.com",
@@ -239,7 +243,7 @@ describe("buildLegacyRules", () => {
239243
});
240244
assert.deepEqual(result, ["^example\\.com:443$"]);
241245
} finally {
242-
console.warn = origWarn;
246+
console.log = origLog;
243247
}
244248
});
245249
});

0 commit comments

Comments
 (0)