Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isHostname reports malformed IPv6 addresses as valid #946

Merged
merged 2 commits into from Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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