Skip to content

Commit 4171f49

Browse files
committed
fix: handle data URLs gracefully
1 parent 40a04ad commit 4171f49

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/create-entity-finder-api.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ const IP_REGEX = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
44
const ROOT_DOMAIN_REGEX = /[^.]+\.([^.]+|(gov|com|co|ne)\.\w{2})$/i
55

66
function getDomainFromOriginOrURL(originOrURL) {
7+
if (typeof originOrURL !== 'string') return null
8+
if (originOrURL.length > 10000 || originOrURL.startsWith('data:')) return null
79
if (DOMAIN_IN_URL_REGEX.test(originOrURL)) return originOrURL.match(DOMAIN_IN_URL_REGEX)[1]
810
if (DOMAIN_CHARACTERS.test(originOrURL)) return originOrURL.match(DOMAIN_CHARACTERS)[0]
911
throw new Error(`Unable to find domain in "${originOrURL}"`)
1012
}
1113

1214
function getRootDomain(originOrURL) {
1315
const domain = getDomainFromOriginOrURL(originOrURL)
16+
if (!domain) return null
1417
if (IP_REGEX.test(domain)) return domain
1518
const match = domain.match(ROOT_DOMAIN_REGEX)
1619
return (match && match[0]) || domain
@@ -19,6 +22,7 @@ function getRootDomain(originOrURL) {
1922
function getEntityInDataset(entityByDomain, entityByRootDomain, originOrURL) {
2023
const domain = getDomainFromOriginOrURL(originOrURL)
2124
const rootDomain = getRootDomain(domain)
25+
if (!domain || !rootDomain) return undefined
2226
if (entityByDomain.has(domain)) return entityByDomain.get(domain)
2327
if (entityByRootDomain.has(rootDomain)) return entityByRootDomain.get(rootDomain)
2428
return undefined

lib/index.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ describe('getRootDomain', () => {
3939
expect(getRootDomain('*.hulce.photography')).toEqual('hulce.photography')
4040
})
4141

42+
it('runs on *massive* inputs', () => {
43+
const massiveInput = '123456789'.repeat(100e3)
44+
expect(getRootDomain(massiveInput)).toEqual(null)
45+
})
46+
4247
it('throws on invalid inputs', () => {
4348
expect(() => getRootDomain('this is not a domain')).toThrow()
4449
expect(() => getRootDomain('neither-is-this')).toThrow()
@@ -112,6 +117,11 @@ Object {
112117
expect(getEntity('http://fbcdn-photos-e-a.akamaihd.net/1234.jpg').name).toEqual('Facebook')
113118
expect(getEntity('http://unknown.akamaihd.net/1234.jpg').name).toEqual('Akamai')
114119
})
120+
121+
it('runs on *massive* inputs', () => {
122+
const massiveInput = '123456789'.repeat(100e3)
123+
expect(getEntity(massiveInput)).toEqual(undefined)
124+
})
115125
})
116126

117127
describe('build state', () => {

0 commit comments

Comments
 (0)