Skip to content

fix(ipv4): reject leading-zero octets, not addresses starting with 0 (#113) - #114

Merged
sagold merged 1 commit into
sagold:mainfrom
patchwright:fix/ipv4-leading-zero-octet
Jul 18, 2026
Merged

fix(ipv4): reject leading-zero octets, not addresses starting with 0 (#113)#114
sagold merged 1 commit into
sagold:mainfrom
patchwright:fix/ipv4-leading-zero-octet

Conversation

@patchwright

Copy link
Copy Markdown
Contributor

Problem

Fixes #113. The ipv4 format validator rejected valid addresses whose first octet is 0, like 0.0.0.0, returning a leading-zero error. Confirmed on main 1cc2a30.

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 with 0. So 0.0.0.0 was wrongly rejected.

Fix

Replace the whole-address check with a per-octet check: data.split(".").some((octet) => octet.length > 1 && octet[0] === "0"). Single-0 octets (0.0.0.0) now pass; octal-like octets (01.2.3.4) still reject. The isValidIPV4 regex is untouched. Two lines changed. ipv6 left unchanged (its leading-zero semantics differ, out of scope for #113).

How to test

npm test -- --grep 113
accepts 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)

…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)

@Adityakumar37 Adityakumar37 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Let's wait for @sagold.

I also wasn't 100% sure about this issue, so I wanted @sagold to review it.

@sagold

sagold commented Jul 18, 2026

Copy link
Copy Markdown
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!

@sagold
sagold merged commit ad2a7e6 into sagold:main Jul 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IPv4 format validator rejects valid address 0.0.0.0

3 participants