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: Adjust filtering logic for secure contexts; improve tests #579

Merged
merged 5 commits into from
Aug 12, 2024
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
8 changes: 8 additions & 0 deletions packages/block-brokers/src/trustless-gateway/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ 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) {
const { host } = ma.toOptions()
if (host === '127.0.0.1' || host === 'localhost' || 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)
})
})
Loading