fix(ip-validation): implement strict IPv4 format validation #2429
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