feat(socket): Add CIDR support for ip whitelisting - #30
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends SM JSON API’s connection whitelist to support CIDR ranges (in addition to exact IP matches) when deciding whether to accept incoming socket connections.
Changes:
- Bumps plugin version to
1.2.1. - Replaces the inline whitelist check on connect with a new
IsIPWhitelisted()helper. - Adds CIDR parsing/matching helpers (
IsIPInCIDR,IPv4ToInt) to support entries like172.17.0.0/16.
Suppressed comments (3)
addons/sourcemod/scripting/SMJSONAPI.sp:592
- Invalid/negative CIDR prefix lengths should not silently widen the whitelist. Currently iPrefixLen <= 0 returns true (which would also allow entries like "/-1" to match all), and values > 32 are clamped. Consider treating out-of-range values as invalid (false), and only treating exactly /0 as match-all.
if (iPrefixLen <= 0)
return true;
if (iPrefixLen > 32)
iPrefixLen = 32;
addons/sourcemod/scripting/SMJSONAPI.sp:610
- IPv4ToInt() currently doesn't validate octet ranges and uses 0 as an "invalid" sentinel, which makes valid CIDRs like "0.0.0.0/8" impossible to match. Validate each octet is 0..255 and return a distinct invalid value (e.g. -1) on parse failure.
int IPv4ToInt(const char[] ip)
{
char octets[4][4];
if (ExplodeString(ip, ".", octets, 4, 4) != 4)
return 0;
addons/sourcemod/scripting/SMJSONAPI.sp:598
- If IPv4ToInt() is updated to return -1 on failure, this check should be updated accordingly; otherwise CIDRs involving 0.0.0.0 ranges will continue to be treated as invalid and never match.
if (iClientIp == 0 || iRangeIp == 0)
return false;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.