Skip to content

Commit 75e2635

Browse files
authored
fix: avoid loading localhost referrer favicons
1 parent 92a272b commit 75e2635

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎src/ui/scripts/components/presentations/PresentationIconList.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createElement as h } from 'react'
33

44
import Favicon from '../Favicon.js'
55

6+
import faviconUrl from '../../../../utils/faviconUrl.js'
67
import enhanceUrl from '../../enhancers/enhanceUrl.js'
78
import sumByProp from '../../utils/sumByProp.js'
89

@@ -95,7 +96,7 @@ const PresentationIconList = (props) => {
9596

9697
return h(UrlRow, {
9798
...commonProps,
98-
faviconUrl: new URL('/favicon.ico', item.url).href,
99+
faviconUrl: faviconUrl(item.url),
99100
url: item.url,
100101
})
101102
}),

‎src/utils/faviconUrl.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const localhostRegex = /(^|\.)localhost$/i
2+
const loopbackRegex = /^127(?:\.\d{1,3}){3}$/
3+
4+
const isLocalHostname = (hostname) => {
5+
return localhostRegex.test(hostname) === true || loopbackRegex.test(hostname) === true || hostname === '[::1]'
6+
}
7+
8+
export default (url) => {
9+
if (url == null || isLocalHostname(url.hostname) === true) return
10+
11+
return new URL('/favicon.ico', url).href
12+
}

‎test/utils/faviconUrl.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import test from 'ava'
2+
3+
import faviconUrl from '../../src/utils/faviconUrl.js'
4+
5+
test('return favicon URL for public hostnames', (t) => {
6+
const result = faviconUrl(new URL('https://example.com/path'))
7+
8+
t.is(result, 'https://example.com/favicon.ico')
9+
})
10+
11+
test('return nothing for localhost hostname', (t) => {
12+
const result = faviconUrl(new URL('http://localhost:5173/path'))
13+
14+
t.is(result, undefined)
15+
})
16+
17+
test('return nothing for localhost subdomains', (t) => {
18+
const result = faviconUrl(new URL('http://app.localhost:5173/path'))
19+
20+
t.is(result, undefined)
21+
})
22+
23+
test('return nothing for IPv4 loopback hostname', (t) => {
24+
const result = faviconUrl(new URL('http://127.0.0.1:5173/path'))
25+
26+
t.is(result, undefined)
27+
})
28+
29+
test('return nothing for IPv6 loopback hostname', (t) => {
30+
const result = faviconUrl(new URL('http://[::1]:5173/path'))
31+
32+
t.is(result, undefined)
33+
})

0 commit comments

Comments
 (0)