fix(ip-validation): implement strict IPv4 format validation#2429
Merged
ravichandra1998g merged 2 commits intoServiceNowDevProgram:mainfrom Oct 22, 2025
Merged
Conversation
…nforce IPv4 octet ranges Tighten IPv4 pattern to enforce 0-255 per octet, rejecting invalid addresses like 256.256.256.256 and 999.999.999.999 Replace loose IPv6 matcher with comprehensive pattern covering full, compressed, link-local with zone IDs, and IPv4-embedded forms
ravichandra1998g
approved these changes
Oct 22, 2025
Contributor
ravichandra1998g
left a comment
There was a problem hiding this comment.
looks good to me.
approving this as this new code is an enhancement over the old one, covering all the edge cases.
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.
This PR updates the IPv4 validation regex to strictly enforce the standard IP address format, addressing the failing test cases from #2176.
Changes
Strict Format Validation: Updated regex to properly validate IPv4 addresses (0-255 per octet)
Reject Invalid Formats: Now correctly handles edge cases:
Rejects non-numeric IPs (e.g., "a.b.c.d")
Rejects incorrect segment counts (e.g., "1.2.3.4.5")
Rejects out-of-range values (e.g., "256.256.256.256")
Rejects leading zeros (e.g., "01.02.03.04")
Maintains Compatibility: Still accepts all valid IPv4 addresses
Testing
The following test cases now pass:
isValidIP("1.2.3.4") // true
isValidIP("255.255.255.255") // true
isValidIP("a.b.c.d") // false
isValidIP("1.2.3.4.5") // false
isValidIP("256.1.2.3") // false
isValidIP("01.02.03.04") // false
Related Issues
Fixes #2176
Additional Context
This change ensures that the IP validation is both strict and accurate, preventing false positives for invalid IP formats while maintaining compatibility with all valid IPv4 addresses. The updated regex follows the standard IPv4 specification more precisely.
Feedback submitted