Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(security): cap the SSRF_ALLOWED_HOSTS warning and name rejects

Yudhi Armyndharis7e54a1d04f72
diff --git a/src/common/security/ssrf-allowed-hosts.spec.ts b/src/common/security/ssrf-allowed-hosts.spec.ts
index 2553b1871..b0781b145 100644
--- a/src/common/security/ssrf-allowed-hosts.spec.ts
+++ b/src/common/security/ssrf-allowed-hosts.spec.ts
@@ -49,6 +49,40 @@ describe('SSRF_ALLOWED_HOSTS entries the guard can never match', () => {
expect(lines[0]).not.toMatch(/token|user|n8n|corp|hooks|minio/);
});
+ it('gives a value the URL parser refuses its own reason, apart from one it rewrites', () => {
+ process.env.SSRF_ALLOWED_HOSTS = '[::1]:8080, fe80::1%eth0, exa mple.com, 127.1';
+
+ warnUnusableAllowedHosts();
+
+ const [line] = warnings();
+ expect(line).toContain('entry 1 (a value the URL parser rejects)');
+ expect(line).toContain('entry 2 (a value the URL parser rejects)');
+ expect(line).toContain('entry 3 (a value the URL parser rejects)');
+ expect(line).toContain('entry 4 (a spelling the URL parser rewrites)');
+ });
+
+ it('lists at most 20 unusable entries in one line and counts the rest', () => {
+ process.env.SSRF_ALLOWED_HOSTS = Array.from({ length: 30000 }, (_, i) => `*.many${i}`).join(',');
+
+ warnUnusableAllowedHosts();
+
+ const lines = warnings();
+ expect(lines).toHaveLength(1);
+ expect(lines[0]).toContain('entry 20 (a wildcard)');
+ expect(lines[0]).not.toContain('entry 21 ');
+ expect(lines[0]).toContain('29980 more are not listed (the list shows at most 20)');
+ expect(lines[0].length).toBeLessThan(2000);
+ });
+
+ it('adds no summary when every unusable entry fits', () => {
+ process.env.SSRF_ALLOWED_HOSTS = Array.from({ length: 20 }, (_, i) => `*.few${i}`).join(',');
+
+ warnUnusableAllowedHosts();
+
+ expect(warnings()[0]).toContain('entry 20 (a wildcard)');
+ expect(warnings()[0]).not.toContain('not listed');
+ });
+
// The URL parser rewrites these hosts, and the guard compares the rewritten hostname, so the spelling written in
// the entry never equals it; the same address written as the parser prints it does.
it('is right that a non-canonical IP address entry never matches, and that the canonical one does', async () => {
diff --git a/src/common/security/ssrf-guard.ts b/src/common/security/ssrf-guard.ts
index fd3bedf71..db6ae1de0 100644
--- a/src/common/security/ssrf-guard.ts
+++ b/src/common/security/ssrf-guard.ts
@@ -82,12 +82,12 @@ function allowedHostEntries(): string[] {
);
}
-/** What the URL parser makes of an entry used as a host, brackets stripped; undefined when it is no host at all. */
-function parsedHostname(entry: string): string | undefined {
+/** What the URL parser makes of an entry used as a host, brackets stripped; null when the parser rejects it. */
+function parsedHostname(entry: string): string | null {
try {
return new URL(`http://${entry.includes(':') ? `[${entry}]` : entry}/`).hostname.replace(/^\[|\]$/g, '');
} catch {
- return undefined;
+ return null;
}
}
@@ -101,27 +101,39 @@ function unusableAllowedHostReason(entry: string): string | undefined {
if (entry.includes('*')) return 'a wildcard';
if (/[/?#]/.test(entry)) return 'a path';
if (/^[^:]+:\d*$/.test(entry)) return 'a port';
- return parsedHostname(entry) === entry ? undefined : 'a spelling the URL parser rewrites';
+ const parsed = parsedHostname(entry);
+ if (parsed === null) return 'a value the URL parser rejects';
+ return parsed === entry ? undefined : 'a spelling the URL parser rewrites';
}
let warnedUnusableHosts = '';
+/** The most entries one warning lists, so a long list of unusable entries cannot flood the log. */
+const MAX_LISTED_UNUSABLE_HOSTS = 20;
+
/**
* An entry is compared with the hostname of the URL, so one the check above names never matches, and the refusal it
- * was meant to lift only says the destination is not allowed. Name such entries once, by position and reason.
+ * was meant to lift only says the destination is not allowed. Name such entries once, by position and reason, at most
+ * MAX_LISTED_UNUSABLE_HOSTS of them, and count the rest.
*/
export function warnUnusableAllowedHosts(entries: readonly string[] = allowedHostEntries()): void {
- const problems = entries.flatMap((entry, index) => {
+ const listed: string[] = [];
+ let unusable = 0;
+ entries.forEach((entry, index) => {
const reason = entry ? unusableAllowedHostReason(entry) : undefined;
- return reason ? [`entry ${index + 1} (${reason})`] : [];
+ if (!reason) return;
+ unusable++;
+ if (listed.length < MAX_LISTED_UNUSABLE_HOSTS) listed.push(`entry ${index + 1} (${reason})`);
});
- const key = problems.join(', ');
+ const more = unusable - listed.length;
+ const key = more > 0 ? `${listed.join(', ')}; ${more} more are not listed` : listed.join(', ');
if (key === warnedUnusableHosts) return;
warnedUnusableHosts = key;
- if (problems.length > 0) {
+ if (unusable > 0) {
logger.warn(
'SSRF_ALLOWED_HOSTS entries must be a bare host name or an IP address written as the URL parser prints it ' +
- `(127.0.0.1, ::1); these never match and are ignored: ${key}`,
+ `(127.0.0.1, ::1); these never match and are ignored: ${key}` +
+ (more > 0 ? ` (the list shows at most ${MAX_LISTED_UNUSABLE_HOSTS})` : ''),
);
}
}