-
Notifications
You must be signed in to change notification settings - Fork 10
IPMatcher: Switch node logic out in favour of pytricia #547
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
base: main
Are you sure you want to change the base?
Changes from all commits
1c6d6f3
e968e77
64d263e
d873fdf
9ddede9
09e98c6
c4dfc10
828d265
66eb7b2
eb5e75f
b1e95a7
fc2fb7a
4c89db1
848fbdf
0d09bbb
64679b8
3cecdb2
3844902
27812b6
944fe15
f3e248e
4cfae74
061ef75
9244f8b
dd2ad34
7e31023
52e2d88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,43 @@ | ||
| """ | ||
| Based on https://github.com/demskie/netparser | ||
| MIT License - Copyright (c) 2019 alex | ||
| """ | ||
| import ipaddress | ||
|
|
||
| from .shared import parse_base_network, sort_networks, summarize_sorted_networks | ||
| from .sort import binary_search_for_insertion_index | ||
| import pytricia | ||
|
|
||
|
|
||
| def preparse(network: str) -> str: | ||
| # Remove the brackets around IPv6 addresses if they are there. | ||
| network = network.strip("[]") | ||
| try: | ||
| ip = ipaddress.IPv6Address(network) | ||
| if ip.ipv4_mapped: | ||
| return str(ip.ipv4_mapped) | ||
| except ValueError: | ||
|
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. Empty except ValueError in preparse silently swallows parsing errors for IPv6 addresses Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
| pass | ||
| return network | ||
|
|
||
|
|
||
| class IPMatcher: | ||
| def __init__(self, networks=None): | ||
| self.sorted = [] | ||
| self.trie = pytricia.PyTricia(128) | ||
| if networks is not None: | ||
| subnets = [] | ||
| for s in networks: | ||
| net = parse_base_network(s, False) | ||
| if net and net.is_valid(): | ||
| subnets.append(net) | ||
| sort_networks(subnets) | ||
| self.sorted = summarize_sorted_networks(subnets) | ||
| self._add(s) | ||
| # We freeze in constructor ensuring that after initialization the IPMatcher is always frozen. | ||
| self.trie.freeze() | ||
|
|
||
| def has(self, network): | ||
| """ | ||
| Checks if the given IP address is in the list of networks. | ||
| """ | ||
| net = parse_base_network(network, False) | ||
| if not net or not net.is_valid(): | ||
| try: | ||
| return self.trie.get(preparse(network)) is not None | ||
| except ValueError: | ||
| return False | ||
| idx = binary_search_for_insertion_index(net, self.sorted) | ||
| if idx < 0: | ||
| return False | ||
| if idx < len(self.sorted) and self.sorted[idx].contains(net): | ||
| return True | ||
| if idx - 1 >= 0 and self.sorted[idx - 1].contains(net): | ||
| return True | ||
| return False | ||
|
|
||
| def add(self, network): | ||
| net = parse_base_network(network, False) | ||
| if not net or not net.is_valid(): | ||
| return self | ||
| idx = binary_search_for_insertion_index(net, self.sorted) | ||
| if idx < len(self.sorted) and self.sorted[idx].compare(net) == 0: | ||
| return self | ||
| self.sorted.insert(idx, net) | ||
|
|
||
| def _add(self, network): | ||
| try: | ||
| self.trie[preparse(network)] = True | ||
| except ValueError: | ||
|
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. The _add method swallows ValueError and SystemError exceptions (bypassing error handling) which can hide failures when adding networks. Details🔧 How do I fix it? More info - Comment |
||
| pass | ||
| except SystemError: | ||
| pass | ||
| return self | ||
|
|
||
| def is_empty(self): | ||
| return len(self.sorted) == 0 | ||
| return len(self.trie) == 0 | ||
This file was deleted.
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
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.
Function parameter 'network' is reassigned with network = network.strip("[]") in preparse; reassigning input parameters reduces clarity.
Details
✨ AI Reasoning
1) The new function preparse(network) strips surrounding brackets and may convert IPv6 mapped addresses to IPv4; it reassigns the parameter 'network' via network = network.strip("[]").
2) Reassigning the function parameter can obscure the original input value and make debugging or future changes harder to follow.
3) This change was introduced in this diff (preparse was added) and thus constitutes a new instance of argument reassignment, so it violates the rule.
🔧 How do I fix it?
Create new local variables instead of reassigning parameters. Use different variable names to clearly distinguish between input and modified values.
More info - Comment
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.