From 7a07a16d0e1193590a12e2075ef80c84e7238c20 Mon Sep 17 00:00:00 2001
From: Jaleel Carter <Jaleel.Carter@acquia.com>
Date: Thu, 14 Jul 2016 13:29:03 -0400
Subject: [PATCH] 2299657-45

---
 link.install             | 14 ++++++++++++++
 link.module              | 16 ++++++++++------
 tests/link.validate.test | 14 ++++++++------
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/link.install b/link.install
index 14e745d..e1820f7 100644
--- a/link.install
+++ b/link.install
@@ -115,3 +115,17 @@ function link_update_7001() {
     }
   }
 }
+
+/**
+ * Removes unused link_extra_domains variable.
+ */
+function link_update_7002() {
+  variable_del('link_extra_domains');
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function link_uninstall() {
+  variable_del('link_allowed_domains');
+}
diff --git a/link.module b/link.module
index 1e9d088..aece66d 100644
--- a/link.module
+++ b/link.module
@@ -10,7 +10,6 @@ define('LINK_INTERNAL', 'internal');
 define('LINK_FRONT', 'front');
 define('LINK_EMAIL', 'email');
 define('LINK_NEWS', 'news');
-define('LINK_DOMAINS', 'aero|arpa|asia|biz|build|com|cat|ceo|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|post|pro|tel|travel|mobi|local|xxx');
 
 define('LINK_TARGET_DEFAULT', 'default');
 define('LINK_TARGET_NEW_WINDOW', '_blank');
@@ -1261,7 +1260,7 @@ function link_validate_url($text, $langcode = NULL) {
  *
  * @param string $text
  *   Url to be checked.
- * 
+ *
  * @return mixed
  *   Returns boolean FALSE if the URL is not valid. On success, returns one of
  *   the LINK_(linktype) constants.
@@ -1383,11 +1382,16 @@ function link_url_type($text) {
 }
 
 /**
- * Returns the list of allowed domains, including domains added by admins via variable_set/$config.
+ * Returns the list of allowed domains.
+ *
+ * If the variable link_allowed_domains is set, restrict allowed domains to the
+ * strings in that array. If the variable link_allowed_domains is not set, allow
+ * all domains between 2 and 63 characters in length.
+ * See https://tools.ietf.org/html/rfc1034.
  */
 function _link_domains() {
-  $link_extra_domains = variable_get('link_extra_domains', array());
-  return empty($link_extra_domains) ? LINK_DOMAINS : LINK_DOMAINS . '|' . implode('|', $link_extra_domains);
+  $link_allowed_domains = variable_get('link_allowed_domains', array());
+  return empty($link_allowed_domains) ? '[a-z][a-z0-9-]{1,62}' : implode('|', $link_allowed_domains);
 }
 
 /**
@@ -1439,7 +1443,7 @@ function link_field_settings_form() {
 
 /**
  * Additional callback to adapt the property info of link fields.
- * 
+ *
  * @see entity_metadata_field_entity_property_info()
  */
 function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
diff --git a/tests/link.validate.test b/tests/link.validate.test
index a9ac116..4cb66a4 100644
--- a/tests/link.validate.test
+++ b/tests/link.validate.test
@@ -368,7 +368,7 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
       'group' => 'Link',
     );
   }
-  
+
   function setUp() {
     parent::setUp('link');
   }
@@ -429,11 +429,10 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
   function testValidateInternalLinks() {
     $tempfile = drupal_tempnam('public://files', 'test');
     $links = array(
-      'rss.xml',
       file_uri_target($tempfile),
       drupal_realpath($tempfile),
     );
-    
+
     foreach ($links as $link) {
       $type = link_url_type($link);
       $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
@@ -482,8 +481,8 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
       //$valid2 = valid_url($link, TRUE);
       //$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
     }
-    // Test if we can make a tld valid:
-    variable_set('link_extra_domains', array('frog'));
+    // Test if we can make a tld allowable:
+    variable_set('link_allowed_domains', array('frog'));
     $valid = link_validate_url('http://www.example.frog');
     $this->assertEqual(LINK_EXTERNAL, $valid, "Testing that http://www.example.frog is a valid external link if we've added 'frog' to the list of valid domains.");
   }
@@ -495,7 +494,6 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
       'http://4827.0.0.2/',
       '//www.example.com/',
       'http://www.testß.com/', // ß not allowed in domain names!
-      'http://www.example.frog/', // Bad TLD
       //'http://www.-fudge.com/', // domains can't have sections starting with a dash.
       'http://example.com/index.php?page=this\that',
       'example@example.com',
@@ -504,5 +502,9 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
       $valid = link_validate_url($link);
       $this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
     }
+    // Test if we can make a tld disallowed:
+    variable_set('link_allowed_domains', array('toad'));
+    $valid = link_validate_url('http://www.example.frog');
+    $this->assertEqual(FALSE, $valid, "Testing that http://www.example.frog is an invalid external link if we've not added 'frog' to the list of valid domains.");
   }
 }
-- 
2.7.4 (Apple Git-66)

