validations: adds restriction for mx with cname

This commit is contained in:
Akshay Nair committed 2021-09-06 16:55:16 +05:30
1 parent 1e77efae8b
commit 905f26a0c8
2 files changed
+20 -18

No files matched your search

+5 -1
View File
@@ -53,7 +53,9 @@ describe('validateDomainData', () => {
{ ...defaultDomain, record: { CNAME: 'https://foobar.com' } }, { ...defaultDomain, record: { CNAME: 'https://foobar.com' } },
{ ...defaultDomain, record: { URL: 'foobar.com' } }, { ...defaultDomain, record: { URL: 'foobar.com' } },
{ ...defaultDomain, record: { CNAME: 'foobar.com', A: ['11.22.22.33'] } }, { ...defaultDomain, record: { CNAME: 'foobar.com', A: ['11.22.22.33'] } },
{ ...defaultDomain, record: { CNAME: 'foobar.com', MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
...INVALID_NAMES.map(name => ({ ...defaultDomain, name })).slice(0, 1), ...INVALID_NAMES.map(name => ({ ...defaultDomain, name })).slice(0, 1),
{ ...defaultDomain, record: { TXT: ['foobar wow nice!!!'] } },
]; ];
const validCases = [ const validCases = [
@@ -69,7 +71,9 @@ describe('validateDomainData', () => {
{ ...defaultDomain, record: { CNAME: 'aa.sd' } }, { ...defaultDomain, record: { CNAME: 'aa.sd' } },
{ ...defaultDomain, record: { URL: 'https://foobar.com' } }, { ...defaultDomain, record: { URL: 'https://foobar.com' } },
{ ...defaultDomain, record: { URL: 'http://foobar.com/foobar/' } }, { ...defaultDomain, record: { URL: 'http://foobar.com/foobar/' } },
{ ...defaultDomain, record: { CNAME: 'foobar.com', MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } }, { ...defaultDomain, record: { MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
{ ...defaultDomain, record: { TXT: 'foobar wow nice!!!' } },
{ ...defaultDomain, record: { A: ['1.1.1.1'], MX: ['mx1.example.com'] } },
]; ];
it('should return false for invalid data', () => { it('should return false for invalid data', () => {
+15 -17
View File
@@ -3,35 +3,32 @@ const { VALID_RECORD_TYPES } = require('./constants');
const { or, and, validate, between, testRegex, withLengthEq, withLengthGte } = require('./helpers'); const { or, and, validate, between, testRegex, withLengthEq, withLengthGte } = require('./helpers');
const INVALID_NAMES = require('./invalid-domains.json'); const INVALID_NAMES = require('./invalid-domains.json');
const isValidURL = testRegex(/^https?:\/\//ig); const isValidURL = and([R.is(String), testRegex(/^https?:\/\//ig)]);
const isValidDomain = testRegex(/^(([a-z0-9\-]+)\.)+[a-z]+$/ig) const isValidDomain = and([R.is(String), testRegex(/^(([a-z0-9\-]+)\.)+[a-z]+$/ig)]);
// TODO: Add priority to records
const allowMXRecord = R.compose( const allowMXRecord = R.compose(
R.ifElse(R.includes('MX'), withLengthEq(2), withLengthEq(1)), R.ifElse(R.includes('MX'), withLengthEq(2), withLengthEq(1)),
R.keys, R.keys,
); );
const validateCnameRecord = key => and([ const validateCnameRecord = type => and([
R.propSatisfies(R.is(String), key), R.propIs(String, type),
allowMXRecord, R.compose(withLengthEq(1), R.keys), // CNAME cannot be used with any other record
R.propSatisfies(withLengthGte(4), key), R.propSatisfies(withLengthGte(4), type),
R.propSatisfies(isValidDomain, key), R.propSatisfies(isValidDomain, type),
//R.propSatisfies(R.complement(isValidURL), key),
]); ]);
const validateARecord = key => and([ const validateARecord = type => and([
R.propIs(Array, type),
allowMXRecord, allowMXRecord,
R.propSatisfies(withLengthGte(1), key), R.propSatisfies(withLengthGte(1), type),
R.propIs(Array, key),
]); ]);
const validateMXRecord = key => and([ const validateMXRecord = type => and([
R.propSatisfies(withLengthGte(1), key), R.propIs(Array, type),
R.propSatisfies(R.all(isValidDomain), key), R.propSatisfies(withLengthGte(1), type),
R.propIs(Array, key), R.propSatisfies(R.all(isValidDomain), type),
]); ]);
const validateDomainData = validate({ const validateDomainData = validate({
@@ -69,6 +66,7 @@ const validateDomainData = validate({
[R.has('A'), validateARecord('A')], [R.has('A'), validateARecord('A')],
[R.has('URL'), R.propSatisfies(isValidURL, 'URL')], [R.has('URL'), R.propSatisfies(isValidURL, 'URL')],
[R.has('MX'), validateMXRecord('MX')], [R.has('MX'), validateMXRecord('MX')],
[R.has('TXT'), R.propSatisfies(R.is(String), 'TXT')],
[R.T, R.T], [R.T, R.T],
]), ]),
]), ]),