Skip to content

net: introduce removal methods for net.BlockList

Hello! This is my 1st PR and I'm not a C++ developer so bear with me :)

High level changes

Currently, to "remove" a rule from net.BlockList, one must create a new BlockList and add every other existing rule to it. This PR introduces removeAddress(), removeRange(), and removeSubnet() methods to lib/internal/blocklist.js so users can easily modify rule sets.

Adding redundant addresses, ranges, or subnets no longer creates multiple rules in the BlockList. However, this behavior doesn't apply to a subnet that overlaps another subnet or a subnet that's equivalent to a range. The modification to rule-adding behavior eases rule removal discussed in the next section.

Low level changes

To achieve the functionality mentioned, this PR adds SocketAddressRange and SocketAddressMask classes to src/node_sockaddr*. These classes have their own hashing implementations so their instances can be uniquely mapped to rules. SocketAddressRangeRule and SocketAddressMaskRule now utilize these classes, respectively.

SocketAddressBlocklist now has separate mappings for address, range, and subnet rules. This way, SocketAddressBlocklist can uniquely identify an address, range, or subnet and its corresponding rule. This makes rule removal easier but also compels a change to rule-adding behavior where redundant addresses, ranges, or subnets do not create additional rules when compared to rules in the same category.

I updated the documentation and added test cases for rule removal and checking redundant addition of rules.

Some open questions

  1. Is the general approach here ok?
  2. Any missing test/edge cases?
  3. Could/should SocketAddressRange and SocketAddressMask be exposed in the net API? If so, would it make sense to do this in a separate PR?
  4. Any idioms or best practices I'm missing?

Merge request reports

Loading