Skip to content

Commit

Permalink
fix: isHostname reports malformed IPv6 addresses as valid (#946)
Browse files Browse the repository at this point in the history
* fix: isHostname reports malformed IPv6 addresses as valid
* test: isHostname() malformed ipv6 tests
  • Loading branch information
S410 authored Nov 30, 2020
1 parent b0aa032 commit cec022d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion add-on/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,19 @@ exports.safeURL = safeURL
exports.guiURLString = guiURLString

// ensure value is a valid URL.hostname (FQDN || ipv4 || ipv6 WITH brackets)
exports.isHostname = x => isFQDN(x) || isIP.v4(x) || (!isIP.v6(x) && isIP.v6(x.replace(/[[\]]+/g, '')))
exports.isHostname = x => {
if (isFQDN(x) || isIP.v4(x)) {
return true
}

const match = x.match(/^\[(.*)\]$/)

if (match == null) {
return false
}

return isIP.v6(match[1])
}

// convert JS array to multiline textarea
function hostArrayCleanup (array) {
Expand Down
9 changes: 9 additions & 0 deletions test/functional/lib/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ describe('isHostname()', function () {
it('should return false for invalid URL.hostname (ipv6 without brackets)', () => {
expect(isHostname('fe80::bb67:770c:8a97:1')).to.equal(false)
})
it('should return false for ipv6 with a missing bracket', () => {
expect(
isHostname('[fe80::bb67:770c:8a97:1') ||
isHostname('fe80::bb67:770c:8a97:1]')
).to.equal(false)
})
it('should return false for ipv6 with malformed brackets', () => {
expect(isHostname('[fe80::bb67:770c:8a97]:1]')).to.equal(false)
})
})

describe('hostTextToArray()', function () {
Expand Down

0 comments on commit cec022d

Please sign in to comment.