-
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
Open
acul71
wants to merge
14
commits into
libp2p:main
Choose a base branch
from
acul71:issue-1510
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−12
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bc80c72
Use standard IP cidr format for allow/deny lists (no test)
acul71 f5080a6
make one line of code
acul71 8a778e3
Fix lint issue
acul71 f6c95cd
nit: instead of ma => we should indicate that it's an ipNet
acul71 cad6894
fix: allow both specific IP addresses and ipcidr
acul71 e21dda3
Refactor allow/deny list parsing with encapsulate() and improved erro…
acul71 e95753f
chore: add @chainsafe/netmask package for IpNet support
acul71 56a3b41
chore: add @chainsafe/netmask package for IpNet support
acul71 719773d
feat(utils): add multiaddrToIpNet function to convert multiaddr to Ip…
acul71 db47949
fix: update allow list to use multiaddrToIpNet for conversion
acul71 d65b996
test: add test for parsing allow list as IpNet objects and fix prunin…
acul71 18bd35c
refactor: simplify allow/deny list parsing with utility function
acul71 c8f0230
test: add test for correct parsing and storage of allow/deny lists as…
acul71 a871a44
Merge branch 'main' of https://github.com/libp2p/js-libp2p into issue…
acul71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
acul71 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
acul71 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.