fix(ipv4): reject leading-zero octets, not addresses starting with 0 (#113) - #114
Merged
Merged
Conversation
…agold#113) The ipv4 guard rejected any address starting with 0 (e.g. 0.0.0.0) instead of addresses with a leading-zero octet (e.g. 01.2.3.4). Per-octet check; ipv6 unchanged (out of scope). Assisted-by: Claude (code generation, reviewed and tested locally)
Owner
|
Sorry for the slow response. I was expecting the spec test suite to test this case, but tests are ignoring this ip. Your fix is aligned to all other tests. Will merge and publish. Thank you so much for your contribution! |
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.
Problem
Fixes #113. The
ipv4format validator rejected valid addresses whose first octet is0, like0.0.0.0, returning a leading-zero error. Confirmed on main1cc2a30.Root cause
The guard in
src/formats/additionalFormats.ts(ipv4) checked whether the whole address starts with"0"(if (data && data[0] === "0")), but the comment intent is to reject leading-zero OCTETS (octal-looking, e.g.01.2.3.4), not any address starting with0. So0.0.0.0was wrongly rejected.Fix
Replace the whole-address check with a per-octet check:
data.split(".").some((octet) => octet.length > 1 && octet[0] === "0"). Single-0octets (0.0.0.0) now pass; octal-like octets (01.2.3.4) still reject. TheisValidIPV4regex is untouched. Two lines changed.ipv6left unchanged (its leading-zero semantics differ, out of scope for #113).How to test
npm test -- --grep 113accepts 0.0.0.0 as a valid ipv4 address
accepts 127.0.0.1 as a valid ipv4 address
rejects 01.2.3.4 (leading zero in an octet, octal-like)
3 passing. Full suite 8528 passing, 0 failing. The new test fails on main (bites) and passes with this change.
Backward compatibility
No breaking changes. Addresses wrongly rejected (
0.0.0.0) now validate; octal-like addresses (01.2.3.4) still reject.Assisted-by: Claude (code generation, reviewed and tested locally)