Skip to content

Commit

Permalink
Fix issue ipfs#564: Modify filtering logic and update related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acul71 committed Aug 8, 2024
1 parent 777d868 commit e751c08
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/block-brokers/src/trustless-gateway/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export function filterNonHTTPMultiaddrs (multiaddrs: Multiaddr[], allowInsecure:

return isPrivateIp(ma.toOptions().host) === false
}

// When allowInsecure is false and allowLocal is true, allow multiaddrs with "127.0.0.1", "localhost", or any subdomain ending with ".localhost"
if (!allowInsecure && allowLocal) {
if (ma.toOptions().host === '127.0.0.1' || ma.toOptions().host === 'localhost' || ma.toOptions().host.endsWith('.localhost'))
return true
}

return false
})
Expand Down
24 changes: 24 additions & 0 deletions packages/block-brokers/test/trustless-gateway-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ describe('trustless-gateway-block-broker-utils', () => {

expect(filtered.length).to.deep.equal(0)
})

it('filterNonHTTPMultiaddrs allows 127.0.0.1 when allowInsecure=false', async function () {
const localMaddr = uriToMultiaddr('http://127.0.0.1')

const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)

expect(filtered.length).to.deep.equal(1)
})

it('filterNonHTTPMultiaddrs allows localhost when allowInsecure=false', async function () {
const localMaddr = uriToMultiaddr('http://localhost')

const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)

expect(filtered.length).to.deep.equal(1)
})

it('filterNonHTTPMultiaddrs allows *.localhost when allowInsecure=false', async function () {
const localMaddr = uriToMultiaddr('http://example.localhost')

const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)

expect(filtered.length).to.deep.equal(1)
})
})

0 comments on commit e751c08

Please sign in to comment.