Skip to content

Commit

Permalink
Domains: allow different variations of root domain for supported records
Browse files Browse the repository at this point in the history
Before, we only support an empty name (subdomain) as means of inputting the root
domain. Since some users might by mistake or out of habit input other
variations, this change will treat the following the same as empty
subdomain and add a root domain (for records that it's allowed):
@
@.example.com
@.example.com.
example.com
example.com.

Fixes #3318
  • Loading branch information
Igor Klimer committed Feb 17, 2016
1 parent e48ca87 commit 571a665
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions client/lib/domains/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ const includes = require( 'lodash/collection/includes' ),
mapValues = require( 'lodash/object/mapValues' ),
endsWith = require( 'lodash/string/endsWith' );

function validateAllFields( fieldValues ) {
function validateAllFields( fieldValues, domainName ) {
return mapValues( fieldValues, ( value, fieldName ) => {
const isValid = validateField( {
name: fieldName,
value: value,
type: fieldValues.type,
domainName: domainName
} );

return isValid ? [] : [ 'Invalid' ];
} );
}

function validateField( { name, value, type } ) {
function validateField( { name, value, type, domainName } ) {
switch ( name ) {
case 'name':
return isValidName( value, type );
return isValidName( value, type, domainName );
case 'target':
return isValidDomain( value, type );
case 'data':
Expand All @@ -45,8 +46,8 @@ function isValidDomain( name ) {
return /^([a-z0-9-_]{1,63}\.)*[a-z0-9-]{1,63}\.[a-z]{2,63}$/i.test( name );
}

function isValidName( name, type ) {
if ( isNameEmptyAndItsAllowed( name, type ) ) {
function isValidName( name, type, domainName ) {
if ( isRootDomainAndItsAllowed( name, type, domainName ) ) {
return true;
}

Expand Down Expand Up @@ -87,8 +88,8 @@ function getNormalizedData( record, selectedDomainName ) {
function getNormalizedName( name, type, selectedDomainName ) {
const endsWithDomain = endsWith( name, '.' + selectedDomainName );

if ( isNameEmptyAndItsAllowed( name, type ) ) {
return name;
if ( isRootDomainAndItsAllowed( name, type, selectedDomainName ) ) {
return '';
}

if ( endsWithDomain ) {
Expand All @@ -106,8 +107,15 @@ function isIpRecord( type ) {
return includes( [ 'A', 'AAAA' ], type );
}

function isNameEmptyAndItsAllowed( name, type ) {
return ! name && includes( [ 'MX', 'SRV', 'TXT' ], type );
function isRootDomainAndItsAllowed( name, type, domainName ) {
const rootDomainVariations = [
'@',
domainName,
domainName + '.',
'@.' + domainName,
'@.' + domainName + '.' ];
return ( ! name || includes( rootDomainVariations, name ) ) &&
includes( [ 'MX', 'SRV', 'TXT' ], type );
}

function getFieldWithDot( field ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 ) );
}
} );

Expand Down

0 comments on commit 571a665

Please sign in to comment.