Skip to content

Commit

Permalink
OCPBUGS-16077: fix base domain name validation (#2268)
Browse files Browse the repository at this point in the history
* Using same regex for base domain and dns domain that BE

* Add unit tests for validate base domain name
  • Loading branch information
ammont82 authored Aug 22, 2023
1 parent 69f45b7 commit f2bb903
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test, describe, expect } from 'vitest';
import {
baseDomainValidationSchema,
dnsNameValidationSchema,
hostPrefixValidationSchema,
ipBlockValidationSchema,
Expand Down Expand Up @@ -317,4 +318,51 @@ describe('validationSchemas', () => {

expect(counter).toBe(invalid.length);
});

test('baseDomainNameValidationSchema', async () => {
const valid = [
'a.com',
'co',
'1c',
'1-c',
'1--c',
'aaa',
'abc.def',
'a-aa.com',
'a--aa.com',
'aa.com.com.com.com',
'red.cat--rahul.com',
];
const invalid = [
'a',
'-',
'a-',
'-aaa.com.',
'aaa-.com',
'a.c',
'aaa.c',
'DNSnamescancontainonlyalphabeticalcharactersa-znumericcharacters0-9theminussign-andtheperiod',
'DNSnamescancontainonlyalphabeticalcharactersa-znumericcharacters0-9theminussign-andtheperiod.com',
];

await Promise.all(
valid.map((value) =>
baseDomainValidationSchema
.validate(value)
.catch(() => expect(value).toBe(`was rejected but is valid`)),
),
);

let counter = 0;
await Promise.all(
invalid.map((value) =>
baseDomainValidationSchema.validate(value).then(
() => expect(value).toBe('should be rejected since it is invalid'),
() => counter++,
),
),
);

expect(counter).toBe(invalid.length);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const CLUSTER_NAME_VALID_CHARS_REGEX = /^[a-z0-9-]*$/;
const SSH_PUBLIC_KEY_REGEX =
/^(ssh-rsa|ssh-ed25519|ecdsa-[-a-z0-9]*) AAAA[0-9A-Za-z+/]+[=]{0,3}( .+)?$/;
const DNS_NAME_REGEX = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/;
const BASE_DOMAIN_REGEX = /^([a-z0-9]+(-[a-z0-9]+)*)$/;
const DNS_NAME_REGEX_OCM = /^([a-z0-9]+(-[a-z0-9]+)*[.])+[a-z]{2,}$/;
const BASE_DOMAIN_REGEX = /^[a-z\d][\-]*[a-z\d]+$/;
const DNS_NAME_REGEX_OCM = /^([a-z\d]([\-]*[a-z\d]+)*\.)+[a-z\d]+[\-]*[a-z\d]+$/;

const PROXY_DNS_REGEX =
/(^\.?([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*$)/;
Expand Down

0 comments on commit f2bb903

Please sign in to comment.