Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport subdomain validation #477

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you ❤️


use Zendesk\API\Client;

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

namespace Zendesk\API\UnitTests;

use Zendesk\API\Client;
use InvalidArgumentException;

/**
* InvalidSubdomain test class
*/
class InvalidSubdomainTest extends BasicTest
{
public function testInvalidSubdomainThrows()
{
$this->setExpectedException('InvalidArgumentException');

new Client('...', '');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we would need a more specific test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to add one. What test would you like to see?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're introducing a regex, could you please provide a test that passes the validation and one that throws InvalidArgumentException?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


public function testValidSubdomain()
{
new Client('zendesk.example.com', 'example');
$this->assertTrue(true);
}
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file does not exist in current master, and a similar issue has already been fixed, can you please try to merge master in here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is for the v1 branch. It doesn't make sense to merge the current master.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification.


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

use Zendesk\API\Client;

Expand Down