Skip to content

Commit

Permalink
feat(subdomain): support inlined DNSLink names
Browse files Browse the repository at this point in the history
This correctly detects subdomains with DNSLink name inlined into a
single DNS label.

More details: ipfs/in-web-browsers#169

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Jan 18, 2021
1 parent c25fce8 commit 39b4706
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ function isIpns (input, pattern, protocolMatch = defaultProtocolMatch, hashMatch
if (isCID(ipnsId)) return true
// Check if it looks like FQDN
try {
if (!ipnsId.includes('.')) {
// name without tld, assuming its inlined into a single DNS label
// (https://github.com/ipfs/in-web-browsers/issues/169)
// en-wikipedia--on--ipfs-org → en.wikipedia-on-ipfs.org
ipnsId = ipnsId.replace(/--/g, '@').replace(/-/g, '.').replace(/@/g, '-')
}
// URL implementation in web browsers forces lowercase of the hostname
const { hostname } = new URL(`http://${ipnsId}`) // eslint-disable-line no-new
// Check if potential FQDN has an explicit TLD
Expand Down
18 changes: 15 additions & 3 deletions test/test-subdomain.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ describe('ipfs subdomain', () => {
done()
})

it('isIPFS.ipnsSubdomain should not match if *.ipns is not a fqdn with tld', (done) => {
const actual = isIPFS.ipnsSubdomain('http://no-fqdn-with-tld.ipns.dweb.link')
it('isIPFS.ipnsSubdomain should not match if *.ipns is not a valid hostname', (done) => {
const actual = isIPFS.ipnsSubdomain('http://invalid-hostname-.ipns.dweb.link')
expect(actual).to.equal(false)
done()
})

it('isIPFS.ipnsSubdomain should match if *.ipns is FQDN inlined into a single DNS label', (done) => {
const actual = isIPFS.ipnsSubdomain('https://en-wikipedia--on--ipfs-org.ipns.dweb.link')
expect(actual).to.equal(true)
done()
})

it('isIPFS.subdomain should match an ipfs subdomain', (done) => {
const actual = isIPFS.subdomain('http://bafybeie5gq4jxvzmsym6hjlwxej4rwdoxt7wadqvmmwbqi7r27fclha2va.ipfs.dweb.link')
expect(actual).to.equal(true)
Expand Down Expand Up @@ -148,11 +154,17 @@ describe('ipfs subdomain', () => {
})

it('isIPFS.subdomain should not match if *.ipns is not libp2pkey nor fqdn', (done) => {
const actual = isIPFS.subdomain('http://not-a-cid-or-dnslink.ipns.dweb.link')
const actual = isIPFS.subdomain('http://not-a-cid-or-valid-hostname-.ipns.dweb.link')
expect(actual).to.equal(false)
done()
})

it('isIPFS.subdomain should match if *.ipns looks like an inlined DNSLink name', (done) => {
const actual = isIPFS.subdomain('http://en-wikipedia--on--ipfs-org.ipns.dweb.link')
expect(actual).to.equal(true)
done()
})

it('isIPFS.subdomain should not match a Uint8Array data type', (done) => {
const actual = isIPFS.subdomain(uint8ArrayFromString('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'base58btc'))
expect(actual).to.equal(false)
Expand Down

0 comments on commit 39b4706

Please sign in to comment.