Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Google.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace LeadThread\GoogleShortener;
4
5
use Exception;
6
use LeadThread\GoogleShortener\Exceptions\GoogleAuthException;
7
use LeadThread\GoogleShortener\Exceptions\GoogleErrorException;
8
use LeadThread\GoogleShortener\Exceptions\GoogleRateLimitException;
9
use GuzzleHttp\Client;
10
11
class Google
12
{
13
    const V1 = 'v1';
14
15
    protected $token;
16
    protected $host;
17
    protected $version;
18
    protected $client;
19
20
    /**
21
     * Creates a Calendly instance that can register and unregister webhooks with the API
22
     * @param string $token   The API token to use
23
     * @param string $version The API version to use
24
     * @param string $host    The Host URL
25
     * @param string $client  The Client instance that will handle the http request
26
     */
27 24
    public function __construct($token, $version = self::V1, $host = "www.googleapis.com", Client $client = null){
28 24
        $this->client = $client;
29 24
        $this->token = $token;
30 24
        $this->version = $version;
31 24
        $this->host = $host;
32 24
    }
33
34 12
    public function shorten($url, $encode = true)
35
    {
36 12
        if (empty($url)) {
37 3
            throw new GoogleErrorException("The URL is empty!");
38
        }
39
40 9
        $url = $this->fixUrl($url, $encode);
41
42 9
        $data = $this->exec($url);
43
            
44 6
        return $data['id'];
45
    }
46
47
    /**
48
     * Returns the response data or throws an Exception if it was unsuccessful
49
     * @param  string $raw The data from the response
50
     * @return array
51
     */
52 9
    protected function handleResponse($raw){
53 9
        $data = json_decode($raw,true);
54
55 9
        if(!empty($data["error"])){
56 3
            $reason = $data["error"]["errors"][0]["reason"];
57 3
            $msg    = $data["error"]["errors"][0]["message"];
58
59
            switch ($reason) {
60 3
                case 'keyInvalid':
61 3
                    throw new GoogleAuthException;
62
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
63
64
                default:
65
                    throw new GoogleErrorException("Reason: {$reason}. Message: {$msg}.");
66
                    break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
67
            }
68
        }
69
70 6
        return $data;
71
    }
72
73
    /**
74
     * Returns a corrected URL
75
     * @param  string  $url    The URL to modify
76
     * @param  boolean $encode Whether or not to encode the URL
77
     * @return string          The corrected URL
78
     */
79 15
    protected function fixUrl($url, $encode){
80 15
        if(strpos($url, "http") !== 0){
81 12
            $url = "http://".$url;
82 12
        }
83
84 15
        if($encode){
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
85
            // Google does not support an encoded url
86 9
        }
87
88 15
        return $url;
89
    }
90
91
    /**
92
     * Builds the request URL to the Google API for a specified action
93
     * @param  string $action The long URL
94
     * @param  string $action The API action
95
     * @return string         The URL
96
     */
97 12
    protected function buildRequestUrl($action = "url"){
98 12
        return "https://{$this->host}/urlshortener/{$this->version}/{$action}?key={$this->token}";
99
    }
100
101
    /**
102
     * Returns the Client instance
103
     * @return Client
104
     */
105 9
    protected function getRequest(){
106 9
        $client = $this->client;
107 9
        if(!$client instanceof Client){
108
            $client = new Client();
109
        }
110 9
        return $client;
111
    }
112
113
    /**
114
     * Executes a CURL request to the Google API
115
     * @param  string $url    The URL to shorten
116
     * @return mixed          The response data
117
     */ 
118 9
    protected function exec($url)
119
    {
120 9
        $client = $this->getRequest();
121
        try{
122 9
            $response = $client->request('POST',$this->buildRequestUrl(),[
123
                'json' => [
124
                    'longUrl' => $url
125 9
                ]
126 9
            ]);
127 9
        } catch (\GuzzleHttp\Exception\ClientException $e){
128
            $response = $e->getResponse();
129
        }
130 9
        return $this->handleResponse($response->getBody());
131
    }
132
}
133