Skip to content

Commit e5533fe

Browse files
authored
fix: check target tooptip (#971)
* fix: host address validation - Change the validation error to say "host address" instead of "hostname" * chore: fix code style issues - throw of exception caught locally - redundant variable initializer - redundant if-statement - RegExp: redundant character escape
1 parent c7494ca commit e5533fe

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

‎src/schemas/general/HostNameTarget.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { z } from 'zod';
22

3-
import { validateHostname } from 'validation';
3+
import { validateHostAddress } from 'validation';
44

55
import { TargetSchema } from './Target';
66

77
export const HostNameTargetSchema = TargetSchema.and(z.string().superRefine(validate));
88

99
function validate(target: string, ctx: z.RefinementCtx) {
10-
const message = validateHostname(target);
10+
const message = validateHostAddress(target);
1111

1212
if (message) {
1313
return ctx.addIssue({

‎src/validation.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
validateDomain,
3-
validateHostname,
3+
validateHostAddress,
44
validateHostPort,
55
validateHttpTarget,
66
validateLabelName,
@@ -80,12 +80,12 @@ describe('http', () => {
8080

8181
describe('PING', () => {
8282
it('should reject hostnames without domains', async () => {
83-
expect(validateHostname('grafana')).toBe('Target must be a valid hostname');
83+
expect(validateHostAddress('grafana')).toBe('Target must be a valid host address');
8484
});
8585
it('should reject ping targets with invalid hostnames', async () => {
8686
const testcases: string[] = ['x.', '.y', 'x=y.org'];
8787
testcases.forEach((testcase: string) => {
88-
expect(validateHostname(testcase)).toBe('Target must be a valid hostname');
88+
expect(validateHostAddress(testcase)).toBe('Target must be a valid host address');
8989
});
9090
});
9191

@@ -100,7 +100,7 @@ describe('PING', () => {
100100
'224.0.0.0',
101101
];
102102
testcases.forEach((testcase: string) => {
103-
expect(validateHostname(testcase)).toBe(undefined);
103+
expect(validateHostAddress(testcase)).toBe(undefined);
104104
});
105105
});
106106

@@ -115,7 +115,7 @@ describe('PING', () => {
115115
'ff00::', // multicast address
116116
];
117117
testcases.forEach((testcase: string) => {
118-
expect(validateHostname(testcase)).toBe(undefined);
118+
expect(validateHostAddress(testcase)).toBe(undefined);
119119
});
120120
});
121121
});

‎src/validation.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import validUrl from 'valid-url';
55
import { Label } from 'types';
66

77
export function validateHttpTarget(target: string) {
8-
let message = 'Target must be a valid web URL';
8+
const message = 'Target must be a valid web URL';
99

1010
try {
1111
const httpEncoded = encodeURI(target);
1212
const isValidUrl = Boolean(validUrl.isWebUri(httpEncoded));
1313

1414
if (!isValidUrl) {
15-
throw new Error(message);
15+
return message;
1616
}
1717
} catch {
1818
return message;
@@ -22,8 +22,7 @@ export function validateHttpTarget(target: string) {
2222
const parsedUrl = new URL(target);
2323

2424
if (!parsedUrl.protocol) {
25-
message = 'Target must have a valid protocol';
26-
throw new Error(message);
25+
return 'Target must have a valid protocol';
2726
}
2827

2928
// isWebUri will allow some invalid hostnames, so we need addional validation
@@ -33,7 +32,7 @@ export function validateHttpTarget(target: string) {
3332
}
3433

3534
const hostname = parsedUrl.hostname;
36-
if (validateHostname(hostname)) {
35+
if (validateHostAddress(hostname)) {
3736
return 'Target must have a valid hostname';
3837
}
3938
} catch {
@@ -44,7 +43,7 @@ export function validateHttpTarget(target: string) {
4443
}
4544

4645
function isIpV6FromUrl(target: string) {
47-
let isIpV6 = true;
46+
let isIpV6;
4847
try {
4948
const address = Address6.fromURL(target);
5049
isIpV6 = Boolean(address.address);
@@ -165,10 +164,8 @@ export function validateDomain(target: string): string | undefined {
165164

166165
const filteredElements = rawElements.filter((element, index) => {
167166
const isLast = index === rawElements.length - 1;
168-
if (isLast && element === '') {
169-
return false;
170-
}
171-
return true;
167+
168+
return !(isLast && element === '');
172169
});
173170

174171
const errors = filteredElements
@@ -196,7 +193,7 @@ function isCharacterLetter(character: string): boolean {
196193
}
197194

198195
function isValidDomainCharacter(character: string): boolean {
199-
const regex = new RegExp(/[-A-Za-z0-9\.]/);
196+
const regex = new RegExp(/[-A-Za-z0-9.]/);
200197
return Boolean(!character.match(regex)?.length);
201198
}
202199

@@ -235,7 +232,7 @@ function validateDomainElement(element: string, isLast: boolean): string | undef
235232
return undefined;
236233
}
237234

238-
export function validateHostname(target: string): string | undefined {
235+
export function validateHostAddress(target: string): string | undefined {
239236
const ipv4 = isIpV4(target);
240237
const ipv6 = isIpV6(target);
241238
const pc = punycode.toASCII(target);
@@ -245,14 +242,14 @@ export function validateHostname(target: string): string | undefined {
245242
'i'
246243
);
247244
if (!pc.match(re) && !ipv4 && !ipv6) {
248-
return 'Target must be a valid hostname';
245+
return 'Target must be a valid host address';
249246
}
250247

251248
return undefined;
252249
}
253250

254251
export function validateHostPort(target: string): string | undefined {
255-
const re = new RegExp(/^(?:\[([0-9a-f:.]+)\]|([^:]+)):(\d+)$/, 'i');
252+
const re = new RegExp(/^(?:\[([0-9a-f:.]+)]|([^:]+)):(\d+)$/, 'i');
256253
const match = target.match(re);
257254

258255
if (match === null) {
@@ -273,5 +270,5 @@ export function validateHostPort(target: string): string | undefined {
273270
return 'Port must be greater than 0';
274271
}
275272

276-
return validateHostname(host);
273+
return validateHostAddress(host);
277274
}

0 commit comments

Comments
 (0)