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
5 changes: 5 additions & 0 deletions crates/defguard_core/src/grpc/enrollment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ impl EnrollmentServer {
None,
true,
);
if device.name.is_empty() {
return Err(Status::invalid_argument(
"Cannot add a new device with no name. You may be trying to add a new user device as a network device. Defguard CLI supports only network devices.",
));
}
let device = device.save(&mut *transaction).await.map_err(|err| {
error!(
"Failed to save device {}, pubkey {} for user {}({:?}): {err}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ export const MethodStep = () => {
// biome-ignore lint/correctness/useExhaustiveDependencies: migration, checkMeLater
useEffect(() => {
if (networks) {
const options: SelectOption<number>[] = networks.map((n) => ({
key: n.id,
value: n.id,
label: n.name,
}));
const options: SelectOption<number>[] = networks
.filter((n) => n.location_mfa_mode === 'disabled')
.map((n) => ({
key: n.id,
value: n.id,
label: n.name,
}));
setState({
networks,
networkOptions: options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export const WizardNetworkConfiguration = () => {
.string()
.trim()
.min(1, LL.form.error.required())
.refine((val) => validateIpOrDomain(val), LL.form.error.endpoint()),
.refine(
(val) => validateIpOrDomain(val, false, true),
LL.form.error.endpoint(),
),
port: z
.number({
invalid_type_error: LL.form.error.invalid(),
Expand Down
18 changes: 12 additions & 6 deletions web/src/shared/validators.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ipaddr from 'ipaddr.js';
import { z } from 'zod';

import { patternValidDomain, patternValidWireguardKey } from './patterns';

export const validateWireguardPublicKey = (props: {
Expand All @@ -24,11 +23,13 @@ export const validateIpOrDomain = (
allowMask = false,
allowIPv6 = false,
): boolean => {
return (
(allowIPv6 && validateIPv6(val, allowMask)) ||
validateIPv4(val, allowMask) ||
patternValidDomain.test(val)
);
const hasLetter = /\p{L}/u.test(val);
const hasColon = /:/.test(val);
if (!hasLetter || hasColon) {
return (allowIPv6 && validateIPv6(val, allowMask)) || validateIPv4(val, allowMask);
} else {
return patternValidDomain.test(val);
}
};

// Returns false when invalid
Expand All @@ -41,6 +42,7 @@ export const validateIpList = (
.replace(' ', '')
.split(splitWith)
.every((el) => {
if (!el.includes('/') && allowMasks) return false;
return validateIPv4(el, allowMasks) || validateIPv6(el, allowMasks);
});
};
Expand Down Expand Up @@ -76,6 +78,10 @@ export const validateIPv4 = (ip: string, allowMask = false): boolean => {
return ipaddr.IPv4.isValidCIDR(ip);
}
}
const ipv4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/;
if (!ipv4Pattern.test(ip)) {
return false;
}
return ipaddr.IPv4.isValid(ip);
};

Expand Down
Loading