Skip to content

Commit

Permalink
Backport subdomain validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Nov 16, 2021
1 parent dd21a09 commit dc17b38
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
1.2.2
5 changes: 5 additions & 0 deletions src/Zendesk/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Zendesk\API;

use InvalidArgumentException;

/*
* Dead simple autoloader:
* spl_autoload_register(function($c){@include 'src/'.preg_replace('#\\\|_(?!.+\\\)#','/',$c).'.php';});
Expand Down Expand Up @@ -263,6 +265,9 @@ public function __construct($subdomain, $username, $scheme = "https", $hostname
if (empty($subdomain)) {
$this->apiUrl = "$scheme://$hostname:$port/api/{$this->apiVer}/";
} else {
if (! preg_match('/^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$/', $subdomain)) {
throw new InvalidArgumentException('Invalid Zendesk subdomain.');
}
$this->apiUrl = "$scheme://$subdomain.$hostname:$port/api/{$this->apiVer}/";
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/GroupMembershipsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Zendesk\API\LiveTests;
namespace Zendesk\API\UnitTests;

use Zendesk\API\Client;

Expand Down
20 changes: 20 additions & 0 deletions tests/Zendesk/API/UnitTests/InvalidSubdomainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Zendesk\API\UnitTests;

use Zendesk\API\Client;
use InvalidArgumentException;

/**
* InvalidSubdomain test class
*/
class InvalidSubdomainTest extends BasicTest
{
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidSubdomainThrows()
{
new Client('...', '');
}
}
2 changes: 1 addition & 1 deletion tests/Zendesk/API/UnitTests/OrganizationsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Zendesk\API\LiveTests;
namespace Zendesk\API\UnitTests;

use Zendesk\API\Client;

Expand Down

0 comments on commit dc17b38

Please sign in to comment.