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 <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
f994dd9c8c
commit
f9c65f67a9
@@ -64,8 +64,14 @@ export async function isSafeRemoteUrl(raw: unknown): Promise<boolean> {
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user