Skip to content

Commit ac4bdb8

Browse files
acul71SgtPooki
andauthored
fix: Adjust filtering logic for secure contexts; improve tests (#579)
* Fix issue #564: Modify filtering logic and update related tests * chore: fix linting issues * Update packages/block-brokers/src/trustless-gateway/utils.ts Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> * refactor: simplify conditional logic in filterNonHTTPMultiaddrs --------- Co-authored-by: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com>
1 parent 777d868 commit ac4bdb8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/block-brokers/src/trustless-gateway/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export function filterNonHTTPMultiaddrs (multiaddrs: Multiaddr[], allowInsecure:
2121
return isPrivateIp(ma.toOptions().host) === false
2222
}
2323

24+
// When allowInsecure is false and allowLocal is true, allow multiaddrs with "127.0.0.1", "localhost", or any subdomain ending with ".localhost"
25+
if (!allowInsecure && allowLocal) {
26+
const { host } = ma.toOptions()
27+
if (host === '127.0.0.1' || host === 'localhost' || host.endsWith('.localhost')) {
28+
return true
29+
}
30+
}
31+
2432
return false
2533
})
2634
}

packages/block-brokers/test/trustless-gateway-utils.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,28 @@ describe('trustless-gateway-block-broker-utils', () => {
2727

2828
expect(filtered.length).to.deep.equal(0)
2929
})
30+
31+
it('filterNonHTTPMultiaddrs allows 127.0.0.1 when allowInsecure=false', async function () {
32+
const localMaddr = uriToMultiaddr('http://127.0.0.1')
33+
34+
const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)
35+
36+
expect(filtered.length).to.deep.equal(1)
37+
})
38+
39+
it('filterNonHTTPMultiaddrs allows localhost when allowInsecure=false', async function () {
40+
const localMaddr = uriToMultiaddr('http://localhost')
41+
42+
const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)
43+
44+
expect(filtered.length).to.deep.equal(1)
45+
})
46+
47+
it('filterNonHTTPMultiaddrs allows *.localhost when allowInsecure=false', async function () {
48+
const localMaddr = uriToMultiaddr('http://example.localhost')
49+
50+
const filtered = filterNonHTTPMultiaddrs([localMaddr], false, true)
51+
52+
expect(filtered.length).to.deep.equal(1)
53+
})
3054
})

0 commit comments

Comments
 (0)