Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/formats/additionalFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const formats: Record<string, (options: JsonSchemaValidatorParams) => Val
if (typeof data !== "string" || data === "") {
return undefined;
}
if (data && data[0] === "0") {
// leading zeroes should be rejected, as they are treated as octals
if (data && data.split(".").some((octet) => octet.length > 1 && octet[0] === "0")) {
// leading zeroes in an octet should be rejected, as they are treated as octals
return node.createError("format-ipv4-leading-zero-error", { value: data, pointer, schema });
}
if (data.length <= 15 && isValidIPV4.test(data)) {
Expand Down
25 changes: 25 additions & 0 deletions src/tests/issues/issue113.ipv4-leading-zero.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { strict as assert } from "assert";
import { compileSchema } from "../../compileSchema";

// issue#113 — ipv4 format validator rejected valid addresses whose first octet
// is "0" (e.g. 0.0.0.0). The guard meant to reject leading-zero *octets*
// (octal-looking, e.g. 01.2.3.4), not any address starting with "0".
describe("issue#113 - ipv4 format rejects valid 0.0.0.0 (over-broad leading-zero guard)", () => {
it("accepts 0.0.0.0 as a valid ipv4 address", () => {
const node = compileSchema({ $schema: "draft-07", format: "ipv4" });
const { errors } = node.validate("0.0.0.0");
assert.equal(errors?.length ?? 0, 0, "0.0.0.0 should be valid");
});

it("accepts 127.0.0.1 as a valid ipv4 address", () => {
const node = compileSchema({ $schema: "draft-07", format: "ipv4" });
const { errors } = node.validate("127.0.0.1");
assert.equal(errors?.length ?? 0, 0, "127.0.0.1 should be valid");
});

it("rejects 01.2.3.4 (leading zero in an octet, octal-like)", () => {
const node = compileSchema({ $schema: "draft-07", format: "ipv4" });
const { errors } = node.validate("01.2.3.4");
assert.notEqual(errors?.length ?? 0, 0, "01.2.3.4 should be rejected");
});
});
Loading