From f744f3206fd1eabbee7226da1e3f3671c0bb4e18 Mon Sep 17 00:00:00 2001 From: Radford Smith Date: Tue, 1 Dec 2015 14:48:36 +0100 Subject: [PATCH 1/3] Domains: DNS: Fix bug with CNAME display after adding domain --- client/lib/domains/dns/reducer.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/client/lib/domains/dns/reducer.js b/client/lib/domains/dns/reducer.js index 09b41e66bc003b..1bfe7d5b5bb187 100644 --- a/client/lib/domains/dns/reducer.js +++ b/client/lib/domains/dns/reducer.js @@ -3,6 +3,7 @@ */ import filter from 'lodash/collection/filter'; import React from 'react/addons'; +import escapeRegExp from 'lodash/string/escapeRegExp'; /** * Internal dependencies @@ -18,11 +19,14 @@ function updateDomainState( state, domainName, dns ) { } function addDns( state, domainName, record ) { - const command = { - [ domainName ]: { records: { $push: [ record ] } } - }; + const domainSuffix = new RegExp( '\\.' + escapeRegExp( domainName ) + '\\.$' ), + newRecord = Object.assign( {}, record, { + name: record.name.replace( domainSuffix, '' ) + } ); - return React.addons.update( state, command ); + return React.addons.update( state, { + [ domainName ]: { records: { $push: [ newRecord ] } } + } ); } function deleteDns( state, domainName, record ) { From 9b345b6c4b311990d4218784c164dffad9cda0ae Mon Sep 17 00:00:00 2001 From: Radford Smith Date: Tue, 1 Dec 2015 14:54:54 +0100 Subject: [PATCH 2/3] Domains: DNS: Only allow selected domain in CNAME "name" field --- client/lib/domains/dns/index.js | 25 +++++++++++++------ .../domain-management/dns/dns-add-new.jsx | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client/lib/domains/dns/index.js b/client/lib/domains/dns/index.js index 12e8d1244e4e67..f8603067f650fe 100644 --- a/client/lib/domains/dns/index.js +++ b/client/lib/domains/dns/index.js @@ -2,25 +2,28 @@ * External dependencies */ const includes = require( 'lodash/collection/includes' ), - mapValues = require( 'lodash/object/mapValues' ); + mapValues = require( 'lodash/object/mapValues' ), + endsWith = require( 'lodash/string/endsWith' ); -function validateAllFields( fieldValues ) { +function validateAllFields( fieldValues, selectedDomainName ) { return mapValues( fieldValues, ( value, fieldName ) => { const isValid = validateField( { name: fieldName, value: value, - type: fieldValues.type + type: fieldValues.type, + selectedDomainName } ); return isValid ? [] : [ 'Invalid' ]; } ); } -function validateField( { name, value, type } ) { +function validateField( { name, value, type, selectedDomainName } ) { switch ( name ) { case 'name': + return isValidName( value, type, selectedDomainName ); case 'target': - return isValidName( value ); + return isValidDomainName( value ); case 'data': return isValidData( value, type ); case 'protocol': @@ -36,10 +39,18 @@ function validateField( { name, value, type } ) { } } -function isValidName( name ) { +function isValidDomainName( name ) { return /^([\da-z-]+\.)+[\da-z-]+$/i.test( name ); } +function isValidName( name, type, selectedDomainName ) { + if ( type === 'CNAME' && ! endsWith( name, selectedDomainName ) ) { + return false; + } + + return isValidDomainName( name ); +} + function isValidData( data, type ) { switch ( type ) { case 'A': @@ -48,7 +59,7 @@ function isValidData( data, type ) { return data.match( /^[a-f0-9\:]+$/i ); case 'CNAME': case 'MX': - return isValidName( data ); + return isValidDomainName( data ); case 'TXT': return data.length < 256; } diff --git a/client/my-sites/upgrades/domain-management/dns/dns-add-new.jsx b/client/my-sites/upgrades/domain-management/dns/dns-add-new.jsx index 3cbcb214921720..f1ee1a027cbca0 100644 --- a/client/my-sites/upgrades/domain-management/dns/dns-add-new.jsx +++ b/client/my-sites/upgrades/domain-management/dns/dns-add-new.jsx @@ -62,7 +62,7 @@ const DnsAddNew = React.createClass( { initialFields: this.getInitialFields(), onNewState: this.setFormState, validatorFunction: ( fieldValues, onComplete ) => { - onComplete( null, validateAllFields( fieldValues ) ); + onComplete( null, validateAllFields( fieldValues, this.props.selectedDomainName ) ); } } ); From 9a52c6a1182fbd17ab0f5da833d3d2f38bef5938 Mon Sep 17 00:00:00 2001 From: Radford Smith Date: Wed, 2 Dec 2015 16:01:40 +0100 Subject: [PATCH 3/3] Domains: DNS: Don't allow the root domain to be a CNAME record --- client/lib/domains/dns/index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/client/lib/domains/dns/index.js b/client/lib/domains/dns/index.js index f8603067f650fe..832a7b076e7e9b 100644 --- a/client/lib/domains/dns/index.js +++ b/client/lib/domains/dns/index.js @@ -44,11 +44,22 @@ function isValidDomainName( name ) { } function isValidName( name, type, selectedDomainName ) { - if ( type === 'CNAME' && ! endsWith( name, selectedDomainName ) ) { - return false; + switch ( type ) { + case 'CNAME': + return ( + isValidCname( name, selectedDomainName ) && + isValidDomainName( name ) + ); + default: + return isValidDomainName( name ); } +} - return isValidDomainName( name ); +function isValidCname( name, selectedDomainName ) { + return ( + name !== selectedDomainName && + endsWith( name, selectedDomainName ) + ); } function isValidData( data, type ) {