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)