-
Notifications
You must be signed in to change notification settings - Fork 446
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
feat: Use CIDR format for connection-manager allow/deny lists #2783
base: main
Are you sure you want to change the base?
Changes from all commits
bc80c72
f5080a6
8a778e3
f6c95cd
cad6894
e21dda3
e95753f
56a3b41
719773d
db47949
d65b996
18bd35c
c8f0230
a871a44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
acul71 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { convertToIpNet } from '@multiformats/multiaddr/convert' | ||
import type { IpNet } from '@chainsafe/netmask' | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
||
/** | ||
* Converts a multiaddr string or object to an IpNet object. | ||
* If the multiaddr doesn't include /ipcidr/32, it will encapsulate with /ipcidr/32. | ||
* | ||
* @param {string | Multiaddr} ma - The multiaddr string or object to convert. | ||
* @returns {IpNet} The converted IpNet object. | ||
* @throws {Error} Throws an error if the multiaddr is not valid. | ||
*/ | ||
export function multiaddrToIpNet (ma: string | Multiaddr): IpNet { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is only used in the If we later find that we need it elsewhere we can move it to the utils package, but right now there's no need to expand the public API. |
||
try { | ||
let parsedMa: Multiaddr | ||
if (typeof ma === 'string') { | ||
parsedMa = multiaddr(ma) | ||
} else { | ||
parsedMa = ma | ||
} | ||
|
||
// Check if /ipcidr is already present, if not encapsulate with /ipcidr/32 | ||
if (!parsedMa.protoNames().includes('ipcidr')) { | ||
parsedMa = parsedMa.encapsulate('/ipcidr/32') | ||
} | ||
|
||
return convertToIpNet(parsedMa) | ||
} catch (error) { | ||
throw new Error(`Can't convert to IpNet, Invalid multiaddr format: ${ma}`) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { multiaddrToIpNet } from '../src/multiaddrToIpNet.js' | ||
|
||
describe('multiaddrToIpNet', () => { | ||
it('should convert a simple multiaddr to an IpNet', () => { | ||
const ma = '/ip4/127.0.0.1' | ||
const ipNet = multiaddrToIpNet(ma) | ||
expect(ipNet.toString()).to.equal('127.0.0.1/32') | ||
}) | ||
|
||
it('should convert a multiaddr with a ipcidr to an IpNet', () => { | ||
const ma = '/ip4/127.0.0.1/ipcidr/32' | ||
const ipNet = multiaddrToIpNet(ma) | ||
expect(ipNet.toString()).to.equal('127.0.0.1/32') | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please can you add some tests for IPv6 addresses |
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can you add some tests that show that incoming connections are denied for multiaddrs in the deny list and allowed for ones in the allow list.
They should show that individual addresses and ranges are handled correctly in both IPv4 and IPv6 formats.
It should be a case of adding more tests like this one and this one.