Skip to content

Commit

Permalink
Added checks for valid URLs and response objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
macrini committed Oct 17, 2020
1 parent 97035a1 commit d62c50f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
13 changes: 6 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 26 additions & 8 deletions src/UniwebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,34 @@ public function __construct(?array $credentials = null)
*/
public function getInstanceUrl(): string
{
if (!($url = $this->credentials['homepage'] ?? false)) {
$url = trim($this->credentials['homepage'] ?? '');

if (!$url) {
throw new Exception("Invalid empty homepage URL in credentials");
}

// Add a trailing slash of needed
if ($url[strlen($url) - 1] != '/') {
$url .= '/';
$parts = parse_url($url);
$host = $parts['host'] ?? '';
$path = $parts['path'] ?? '';

// If there is no host, there might be a missing '//'
if (!$host && $path) {
$parts = parse_url('//' . $url);
$host = $parts['host'] ?? '';
$path = $parts['path'] ?? '';
}

if (!$host) {
throw new Exception("Invalid homepage URL");
}

// Path must end with a '/' iff it's not empty
if ($path = trim($path, '/')) {
$path .= '/';
}

return $url;
// Only allow for the secure HTTPS protocol
return 'https://' . $host . '/' . $path;
}

public function getClientName(): string
Expand All @@ -68,7 +86,7 @@ public function getClientName(): string
throw new Exception("Invalid empty client name in credentials");
}

return $clientName;
return trim($clientName);
}

public function getClientSecret(): string
Expand All @@ -77,7 +95,7 @@ public function getClientSecret(): string
throw new Exception("Invalid empty client secret in credentials");
}

return $clientSecret;
return trim($clientSecret);
}

/**
Expand Down Expand Up @@ -371,7 +389,7 @@ public function getAccessToken()

$result = json_decode($result);

if (property_exists($result, 'error')) {
if (is_object($result) && property_exists($result, 'error')) {
throw new Exception('Error: ' . $result->error);
} elseif (!$result || !property_exists($result, 'expires_in')) {
throw new Exception('Unable to obtain access token');
Expand Down

0 comments on commit d62c50f

Please sign in to comment.