Skip to content

Commit df737bc

Browse files
Neograph734helmo
authored andcommitted
Issue #922252 by Neograph734, anarcat: Prevent clients from hijacking each others subdomains
1 parent e296847 commit df737bc

File tree

3 files changed

+78
-15
lines changed

3 files changed

+78
-15
lines changed

alias/hosting_alias.module

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function hosting_alias_form_site_node_form_alter(&$form, &$form_state) {
7272
function hosting_alias_site_form_validate($form, &$form_state) {
7373
$aliases = $form_state['values']['aliases'] = array_filter($form_state['values']['aliases']);
7474
foreach ($aliases as $key => $alias) {
75-
hosting_alias_validate_alias($form_state['node'], $alias, $key);
75+
hosting_alias_validate_alias($form_state['values'], $alias, $key);
7676
}
7777
}
7878

@@ -365,8 +365,7 @@ function hosting_alias_node_revision_delete($node) {
365365
function hosting_alias_validate_alias($site, $alias, $key) {
366366
if ($alias = strtolower(trim($alias))) {
367367
$alias = strtolower(trim($alias));
368-
$params = isset($site->nid) ? array('nid' => $site->nid) : array();
369-
if (!hosting_domain_allowed($alias, $params) || $alias == $site->title) {
368+
if (!hosting_domain_allowed($alias, (array) $site) || $alias == $site->title) {
370369
form_set_error("aliases][$key", t('The domain name @alias is already in use', array('@alias' => $alias)));
371370
}
372371
if (!_hosting_valid_fqdn_wildcard($alias)) {
@@ -524,6 +523,19 @@ function hosting_alias_automatic_aliases($url) {
524523
* generated aliases to ensure that this url has not been used before
525524
*/
526525
function hosting_alias_allow_domain($url, $params = array()) {
526+
$domains_to_check = array();
527+
528+
// Convert url to root domain.
529+
$url_array = explode('.', $url);
530+
531+
while ($url_array) {
532+
$domains_to_check[] = implode('.', $url_array);
533+
array_shift($url_array);
534+
}
535+
536+
// Get the client node from the client name.
537+
$client = hosting_get_client($params['client']);
538+
527539
$query = db_select('node', 'n')
528540
->fields('n', array('nid'))
529541
->condition('n.type', 'site');
@@ -532,12 +544,30 @@ function hosting_alias_allow_domain($url, $params = array()) {
532544
$query->condition('h.status', HOSTING_SITE_DELETED, '<>');
533545

534546
$query->leftJoin('hosting_site_alias', 'a', 'n.vid = a.vid');
535-
$query->condition('a.alias', $url);
547+
548+
// Check for either ...
549+
$or = db_or();
550+
// Exact matches ...
551+
$or->condition('a.alias', $url);
552+
553+
// Or root domain matches of other clients
554+
$and = db_and();
555+
$or2 = db_or();
556+
foreach ($domains_to_check as $domain) {
557+
$or2->condition('a.alias', '%' . db_like($domain), 'LIKE');
558+
}
559+
$and->condition($or2);
560+
561+
$and->condition('h.client', $client->nid, '<>');
562+
563+
$or->condition($and);
564+
565+
$query->condition($or);
536566

537567
// For existing sites, don't match the site's current aliases.
538568
if (isset($params['nid'])) {
539569
$query->condition('n.nid', $params['nid'], '<>');
540570
}
541-
return !$query->countQuery()->execute()->fetchField();
542571

572+
return !$query->countQuery()->execute()->fetchField();
543573
}

site/hosting_site.form.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ function hosting_site_validate($node, &$form) {
392392

393393
// TODO: maybe we should allow creation of sites that conflict with HOSTING_SITE_DISABLED (which would then need to be renamed before being re-enabled)
394394
if (!hosting_domain_allowed($url, (array) $node)) {
395-
form_set_error('title', t("The domain name you have specified is already in use."));
395+
form_set_error('title', t("The domain name you have specified is already in use, or does not belang to you."));
396396
}
397397

398398
// If the quota module is loaded and this is a new node, check

site/hosting_site.module

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -661,18 +661,51 @@ function hosting_site_status_codes($type = NULL) {
661661
* @see hosting_domain_allowed()
662662
*/
663663
function hosting_site_allow_domain($url, $params = array()) {
664-
$query = "SELECT COUNT(n.nid) FROM {node} n
665-
JOIN {hosting_site} h ON n.nid = h.nid
666-
WHERE type = 'site' AND n.title = :title AND h.status <> :status";
667-
$args[':title'] = $url;
668-
$args[':status'] = HOSTING_SITE_DELETED;
664+
$domains_to_check = array();
669665

666+
// Convert url to root domain.
667+
$url_array = explode('.', $url);
668+
669+
while ($url_array) {
670+
$domains_to_check[] = implode('.', $url_array);
671+
array_shift($url_array);
672+
}
673+
674+
// Get the client node from the client name.
675+
$client = hosting_get_client($params['client']);
676+
677+
$query = db_select('node', 'n')
678+
->fields('n', array('nid'))
679+
->condition('n.type', 'site');
680+
681+
$query->leftJoin('hosting_site', 'h', 'h.nid = n.nid');
682+
$query->condition('h.status', HOSTING_SITE_DELETED, '<>');
683+
684+
// Check for either ...
685+
$or = db_or();
686+
// Exact matches ...
687+
$or->condition('n.title', $url);
688+
689+
// Or root domain matches of other clients
690+
$and = db_and();
691+
$or2 = db_or();
692+
foreach ($domains_to_check as $domain) {
693+
$or2->condition('n.title', '%' . db_like($domain), 'LIKE');
694+
}
695+
$and->condition($or2);
696+
697+
$and->condition('h.client', $client->nid, '<>');
698+
699+
$or->condition($and);
700+
701+
$query->condition($or);
702+
703+
// For existing sites, don't match the site's current url.
670704
if (isset($params['nid'])) {
671-
$query .= " AND n.nid <> :nid";
672-
$args[':nid'] = $params['nid'];
705+
$query->condition('n.nid', $params['nid'], '<>');
673706
}
674-
$result = !db_query($query, $args)->fetchField();
675-
return $result;
707+
708+
return !$query->countQuery()->execute()->fetchField();
676709
}
677710

678711
/**

0 commit comments

Comments
 (0)