From f9c65f67a947de057950595c328cec8ba26287b7 Mon Sep 17 00:00:00 2001 From: ANIRUDDHA ADAK Date: Tue, 15 Sep 2026 13:17:53 +0530 Subject: [PATCH] fix(electron-utils): block trailing-dot internal hostnames (#396) * fix(electron-utils): block trailing-dot internal hostnames DNS treats localhost. as localhost; strip trailing dots before the hostname gate. * fix(electron-utils): prettier format trailing-dot guard --------- Co-authored-by: Aniruddha Adak --- packages/electron-utils/src/safe-remote-url.ts | 10 ++++++++-- packages/electron-utils/tests/safe-remote-url.test.ts | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/electron-utils/src/safe-remote-url.ts b/packages/electron-utils/src/safe-remote-url.ts index c6e5cbf..a04d6a7 100644 --- a/packages/electron-utils/src/safe-remote-url.ts +++ b/packages/electron-utils/src/safe-remote-url.ts @@ -64,8 +64,14 @@ export async function isSafeRemoteUrl(raw: unknown): Promise { return false } if (url.protocol !== 'http:' && url.protocol !== 'https:') return false - // URL keeps IPv6 literals bracketed; isIP does not accept the brackets - const host = url.hostname.replace(/^\[|\]$/g, '').toLowerCase() + // URL keeps IPv6 literals bracketed; isIP does not accept the brackets. + // Strip a trailing dot: DNS treats "localhost." as "localhost", so the + // hostname gate must too (otherwise the check passes and only the later + // DNS lookup catches it, after a needless external resolution). + const host = url.hostname + .replace(/^\[|\]$/g, '') + .toLowerCase() + .replace(/\.+$/, '') if (host === '') return false if (isIP(host)) return !isBlockedAddress(host) if (host === 'localhost' || BLOCKED_HOST_SUFFIXES.some((s) => host.endsWith(s))) return false diff --git a/packages/electron-utils/tests/safe-remote-url.test.ts b/packages/electron-utils/tests/safe-remote-url.test.ts index 8bdc7b1..0b66bc2 100644 --- a/packages/electron-utils/tests/safe-remote-url.test.ts +++ b/packages/electron-utils/tests/safe-remote-url.test.ts @@ -73,6 +73,15 @@ describe('isSafeRemoteUrl', () => { await expect(isSafeRemoteUrl(url)).resolves.toBe(false) }) + it.each([ + 'http://localhost./x.png', + 'http://LOCALHOST./x.png', + 'http://printer.local./x.png', + 'http://metadata.internal./x.png', + ])('rejects trailing-dot internal hostname %s without DNS', async (url) => { + await expect(isSafeRemoteUrl(url)).resolves.toBe(false) + }) + it('rejects malformed input and non-strings', async () => { await expect(isSafeRemoteUrl('not a url')).resolves.toBe(false) await expect(isSafeRemoteUrl(undefined)).resolves.toBe(false)