Issues (65)

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/OpenAuth.php (5 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 OpenOauth;
3
4
use OpenOauth\Core\Core;
5
use OpenOauth\Core\Http\Http;
6
use OpenOauth\Core\CacheDriver\BaseDriver as CacheBaseDriver;
7
use OpenOauth\Core\DatabaseDriver\BaseDriver as DatabaseBaseDriver;
8
9
/**
10
 * 微信Auth相关接口.
11
 *
12
 * @author Tian.
13
 */
14
class OpenAuth extends Core
15
{
16
    const API_URL       = 'https://open.weixin.qq.com/connect/oauth2/authorize';
17
    const CODE_GET_USER = 'https://api.weixin.qq.com/sns/oauth2/component/access_token';
18
    const API_USER_INFO = 'https://api.weixin.qq.com/sns/userinfo';
19
20
    protected $authorizedUser;
21
    private   $authorized_app_id;
22
23
    /**
24
     * AuthApi constructor.
25
     *
26
     * @param $authorized_app_id
27
     */
28
    public function __construct($authorized_app_id)
29
    {
30
        parent::__construct();
31
32
        $this->authorized_app_id = $authorized_app_id;
33
    }
34
35
    /**
36
     * 获取当前URL
37
     *
38
     * @return string
39
     */
40 View Code Duplication
    public static function current()
0 ignored issues
show
current uses the super-global variable $_SERVER 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...
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...
41
    {
42
        $protocol = (!empty($_SERVER['HTTPS'])
43
            && $_SERVER['HTTPS'] !== 'off'
44
            || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
45
46
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
47
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
48
        } else {
49
            $host = $_SERVER['HTTP_HOST'];
50
        }
51
52
        return $protocol . $host . $_SERVER['REQUEST_URI'];
53
    }
54
55
    /**
56
     * 生成outh URL
57
     *
58
     * @param string $to
59
     * @param string $scope
60
     * @param string $state
61
     *
62
     * @return string
63
     */
64
    public function url($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
65
    {
66
        $to !== null || $to = $this->current();
67
68
        $queryStr = [
69
            'appid'           => $this->authorized_app_id,
70
            'redirect_uri'    => $to,
71
            'response_type'   => 'code',
72
            'scope'           => $scope,
73
            'state'           => $state,
74
            'component_appid' => $this->configs->component_app_id,
75
        ];
76
77
        return self::API_URL . '?' . http_build_query($queryStr) . '#wechat_redirect';
78
    }
79
80
    /**
81
     * 直接跳转
82
     *
83
     * @param string $to
84
     * @param string $scope
85
     * @param string $state
86
     */
87
    public function redirect($to = null, $scope = 'snsapi_userinfo', $state = 'STATE')
88
    {
89
        header('Location:' . $this->url($to, $scope, $state));
90
91
        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...
92
    }
93
94
    /**
95
     * 获取已授权用户
96
     *
97
     * @return array $user
98
     */
99
    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...
100
    {
101
        if ($this->authorizedUser || !isset($_GET['state']) || (!$code = isset($_GET['code']) ? $_GET['code'] : false) && isset($_GET['state'])) {
102
            return $this->authorizedUser;
103
        }
104
105
        $user = $this->getUser($code);
106
107
        return $this->authorizedUser = $user;
108
    }
109
110
    /**
111
     * 通过授权获取用户
112
     *
113
     * @param null   $to
114
     * @param string $scope
115
     * @param string $state
116
     *
117
     * @return array
118
     */
119
    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...
120
    {
121
        if (!isset($_GET['state']) && (!$code = isset($_GET['code']) ? $_GET['code'] : false)) {
122
            $this->redirect($to, $scope, $state);
123
        }
124
125
        return $this->user();
126
    }
127
128
    /**
129
     * 获取用户信息
130
     *
131
     * @param string $code
132
     *
133
     * @return array
134
     */
135
    public function getUser($code)
136
    {
137
138
        $queryStr = [
139
            'appid'                  => $this->authorized_app_id,
140
            'grant_type'             => 'authorization_code',
141
            'component_appid'        => $this->configs->component_app_id,
142
            'code'                   => $code,
143
            'component_access_token' => $this->getComponentAccessToken(),
144
        ];
145
146
        $query_data = http_build_query($queryStr);
147
148
        $res = Http::_get(self::CODE_GET_USER . '?' . $query_data);
149
150
        return $res;
151
    }
152
153
    /**
154
     * 获取大授权 用户信息
155
     *
156
     * @param        $access_token
157
     * @param        $openid
158
     * @param string $lang
159
     *
160
     * @return array
161
     */
162
    public function getUserInfo($access_token, $openid, $lang = 'zh_CN')
163
    {
164
        $queryStr = [
165
            'access_token' => $access_token,
166
            'openid'       => $openid,
167
            'lang'         => $lang,
168
        ];
169
170
        $query_data = http_build_query($queryStr);
171
172
        $res = Http::_get(self::API_USER_INFO . '?' . $query_data);
173
174
        return $res;
175
    }
176
}
177