Skip to content

Commit 8baf58e

Browse files
authored
Fix network setup wizard IP input / e2e tests (#854)
* fix network wizard * fix tests
1 parent 4783638 commit 8baf58e

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

web/src/pages/wizard/components/WizardNetworkConfiguration/WizardNetworkConfiguration.tsx

+30-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './style.scss';
22

33
import { zodResolver } from '@hookform/resolvers/zod';
44
import { useMutation, useQuery } from '@tanstack/react-query';
5+
import ipaddr from 'ipaddr.js';
56
import { useEffect, useMemo, useRef, useState } from 'react';
67
import { SubmitHandler, useForm } from 'react-hook-form';
78
import { z } from 'zod';
@@ -20,7 +21,7 @@ import { QueryKeys } from '../../../../shared/queries';
2021
import { ModifyNetworkRequest } from '../../../../shared/types';
2122
import { titleCase } from '../../../../shared/utils/titleCase';
2223
import { trimObjectStrings } from '../../../../shared/utils/trimObjectStrings.ts';
23-
import { validateIpOrDomainList, validateIPv4 } from '../../../../shared/validators';
24+
import { validateIpOrDomainList } from '../../../../shared/validators';
2425
import { useWizardStore } from '../../hooks/useWizardStore';
2526

2627
type FormInputs = ModifyNetworkRequest['network'];
@@ -91,13 +92,36 @@ export const WizardNetworkConfiguration = () => {
9192
if (!netmaskPresent) {
9293
return false;
9394
}
94-
const ipValid = validateIPv4(value, true);
95-
if (ipValid) {
96-
const host = value.split('.')[3].split('/')[0];
97-
if (host === '0') return false;
95+
const ipValid = ipaddr.isValidCIDR(value);
96+
if (!ipValid) {
97+
return false;
98+
}
99+
const [address] = ipaddr.parseCIDR(value);
100+
if (address.kind() === 'ipv6') {
101+
const networkAddress = ipaddr.IPv6.networkAddressFromCIDR(value);
102+
const broadcastAddress = ipaddr.IPv6.broadcastAddressFromCIDR(value);
103+
if (
104+
(address as ipaddr.IPv6).toNormalizedString() ===
105+
networkAddress.toNormalizedString() ||
106+
(address as ipaddr.IPv6).toNormalizedString() ===
107+
broadcastAddress.toNormalizedString()
108+
) {
109+
return false;
110+
}
111+
} else {
112+
const networkAddress = ipaddr.IPv4.networkAddressFromCIDR(value);
113+
const broadcastAddress = ipaddr.IPv4.broadcastAddressFromCIDR(value);
114+
if (
115+
(address as ipaddr.IPv4).toNormalizedString() ===
116+
networkAddress.toNormalizedString() ||
117+
(address as ipaddr.IPv4).toNormalizedString() ===
118+
broadcastAddress.toNormalizedString()
119+
) {
120+
return false;
121+
}
98122
}
99123
return ipValid;
100-
}),
124+
}, LL.form.error.addressNetmask()),
101125
endpoint: z.string().min(1, LL.form.error.required()),
102126
port: z
103127
.number({

0 commit comments

Comments
 (0)