Issues (233)

Security Analysis    not enabled

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/API/AuthApi.php (8 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
namespace Wechat\API;
3
4
use Wechat\Utils\Url;
5
use Wechat\Api;
6
7
/**
8
 * 微信Auth相关接口.
9
 *
10
 * @author Tian.
11
 */
12
class AuthApi extends BaseApi
13
{
14
    const API_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
15
16
    protected static $authorizedUser;
17
18
    /**
19
     * 生成outh URL
20
     *
21
     * @param string $to
22
     * @param string $scope
23
     * @param string $state
24
     *
25
     * @return string
26
     */
27
    public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
28
    {
29
        $to !== null || $to = Url::current();
30
31
        $queryStr = [
32
            'appid'         => $this->getAppId(),
33
            'redirect_uri'  => $to,
34
            'response_type' => 'code',
35
            'scope'         => $scope,
36
            'state'         => $state,
37
        ];
38
39
        return self::API_URL . '?' . http_build_query($queryStr) . '#wechat_redirect';
40
    }
41
42
    /**
43
     * 直接跳转
44
     *
45
     * @param string $to
46
     * @param string $scope
47
     * @param string $state
48
     */
49
    public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
50
    {
51
        header('Location:' . $this->url($to, $scope, $state));
52
53
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
54
    }
55
56
    /**
57
     * 获取用户信息
58
     *
59
     * @param string $openId
60
     * @param string $accessToken
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    public function getUser($openId, $accessToken)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $queryStr = [
67
            'access_token' => $accessToken,
68
            'openid'       => $openId,
69
            'lang'         => 'zh_CN',
70
        ];
71
72
        $this->apitype = 'sns';
73
        $this->module  = 'userinfo';
74
        $res           = $this->_get('', $queryStr);
75
76
        return $res;
77
    }
78
79
    /**
80
     * 获取已授权用户
81
     *
82
     * @return array $user
83
     */
84
    public function user()
0 ignored issues
show
user uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
85
    {
86
        if (self::$authorizedUser || !$_GET['state'] || (!$code = $_GET['code']) && $_GET['state']) {
87
            return self::$authorizedUser;
88
        }
89
90
        $permission = $this->getAccessPermission($code);
91
92
        if ($permission['scope'] !== 'snsapi_userinfo') {
93
            $user = ['openid' => $permission['openid']];
94
        } else {
95
            $user = $this->getUser($permission['openid'], $permission['access_token']);
96
        }
97
98
        return $this->authorizedUser = $user;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->authorizedUser = $user; of type array|boolean adds the type boolean to the return on line 98 which is incompatible with the return type documented by Wechat\API\AuthApi::user of type array.
Loading history...
99
    }
100
101
    /**
102
     * 通过授权获取用户
103
     *
104
     * @param null   $to
105
     * @param string $scope
106
     * @param string $state
107
     *
108
     * @return array
109
     */
110
    public function authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
0 ignored issues
show
authorize uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
111
    {
112
        if (!$_GET['state'] && !$code = $_GET['code']) {
113
            $this->redirect($to, $scope, $state);
114
        }
115
116
        return $this->user();
117
    }
118
119
    /**
120
     * 检查 Access Token 是否有效
121
     *
122
     * @param string $accessToken
123
     * @param string $openId
124
     *
125
     * @return boolean
126
     */
127 View Code Duplication
    public function accessTokenIsValid($accessToken, $openId)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $params = [
130
            'openid'       => $openId,
131
            'access_token' => $accessToken,
132
        ];
133
134
        $this->apitype = 'sns';
135
        $this->module  = 'auth';
136
137
        $res = $this->_get('', $params);
138
139
        return $res;
140
    }
141
142
    /**
143
     * 刷新 access_token
144
     *
145
     * @param $refreshToken
146
     *
147
     * @return bool|array
148
     */
149 View Code Duplication
    public function refresh($refreshToken)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $queryStr = [
152
            'appid'         => $this->getAppId(),
153
            'grant_type'    => 'refresh_token',
154
            'refresh_token' => $refreshToken,
155
        ];
156
157
        $this->apitype = 'sns';
158
        $this->module  = 'oauth2';
159
        $res           = $this->_get('refresh_token', $queryStr);
160
161
        return $res;
162
    }
163
164
    /**
165
     * 获取access token
166
     *
167
     * @param string $code
168
     *
169
     * @return string
170
     */
171 View Code Duplication
    public function getAccessPermission($code)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        $queryStr = [
174
            'appid'      => $this->getAppId(),
175
            'secret'     => $this->getAppSecret(),
176
            'code'       => $code,
177
            'grant_type' => 'authorization_code',
178
        ];
179
180
        $this->apitype = 'sns';
181
        $this->module  = 'oauth2';
182
        $res           = $this->_get('access_token', $queryStr);
183
184
        return $res;
185
    }
186
}
187