Issues (4122)

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.

libs/virtualrest/SwiftVirtualRESTService.php (2 issues)

Severity

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
 * Virtual HTTP service client for Swift
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 */
22
23
/**
24
 * Example virtual rest service for OpenStack Swift
25
 * @TODO: caching support (APC/memcached)
26
 * @since 1.23
27
 */
28
class SwiftVirtualRESTService extends VirtualRESTService {
29
	/** @var array */
30
	protected $authCreds;
31
	/** @var int UNIX timestamp */
32
	protected $authSessionTimestamp = 0;
33
	/** @var int UNIX timestamp */
34
	protected $authErrorTimestamp = null;
35
	/** @var int */
36
	protected $authCachedStatus = null;
37
	/** @var string */
38
	protected $authCachedReason = null;
39
40
	/**
41
	 * @param array $params Key/value map
42
	 *   - swiftAuthUrl       : Swift authentication server URL
43
	 *   - swiftUser          : Swift user used by MediaWiki (account:username)
44
	 *   - swiftKey           : Swift authentication key for the above user
45
	 *   - swiftAuthTTL       : Swift authentication TTL (seconds)
46
	 */
47
	public function __construct( array $params ) {
48
		// set up defaults and merge them with the given params
49
		$mparams = array_merge( [
50
			'name' => 'swift'
51
		], $params );
52
		parent::__construct( $mparams );
53
	}
54
55
	/**
56
	 * @return int|bool HTTP status on cached failure
57
	 */
58
	protected function needsAuthRequest() {
59
		if ( !$this->authCreds ) {
60
			return true;
61
		}
62
		if ( $this->authErrorTimestamp !== null ) {
63
			if ( ( time() - $this->authErrorTimestamp ) < 60 ) {
64
				return $this->authCachedStatus; // failed last attempt; don't bother
65
			} else { // actually retry this time
66
				$this->authErrorTimestamp = null;
67
			}
68
		}
69
		// Session keys expire after a while, so we renew them periodically
70
		return ( ( time() - $this->authSessionTimestamp ) > $this->params['swiftAuthTTL'] );
71
	}
72
73
	protected function applyAuthResponse( array $req ) {
74
		$this->authSessionTimestamp = 0;
75
		list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $req['response'];
0 ignored issues
show
The assignment to $rbody is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
The assignment to $rerr is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
76
		if ( $rcode >= 200 && $rcode <= 299 ) { // OK
77
			$this->authCreds = [
78
				'auth_token'  => $rhdrs['x-auth-token'],
79
				'storage_url' => $rhdrs['x-storage-url']
80
			];
81
			$this->authSessionTimestamp = time();
82
			return true;
83
		} elseif ( $rcode === 403 ) {
84
			$this->authCachedStatus = 401;
85
			$this->authCachedReason = 'Authorization Required';
86
			$this->authErrorTimestamp = time();
87
			return false;
88
		} else {
89
			$this->authCachedStatus = $rcode;
90
			$this->authCachedReason = $rdesc;
91
			$this->authErrorTimestamp = time();
92
			return null;
93
		}
94
	}
95
96
	public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
97
		$result = [];
98
		$firstReq = reset( $reqs );
99
		if ( $firstReq && count( $reqs ) == 1 && isset( $firstReq['isAuth'] ) ) {
100
			// This was an authentication request for work requests...
101
			$result = $reqs; // no change
102
		} else {
103
			// These are actual work requests...
104
			$needsAuth = $this->needsAuthRequest();
105
			if ( $needsAuth === true ) {
106
				// These are work requests and we don't have any token to use.
107
				// Replace the work requests with an authentication request.
108
				$result = [
109
					$idGeneratorFunc() => [
110
						'method'  => 'GET',
111
						'url'     => $this->params['swiftAuthUrl'] . "/v1.0",
112
						'headers' => [
113
							'x-auth-user' => $this->params['swiftUser'],
114
							'x-auth-key'  => $this->params['swiftKey'] ],
115
						'isAuth'  => true,
116
						'chain'   => $reqs
117
					]
118
				];
119 View Code Duplication
			} elseif ( $needsAuth !== false ) {
120
				// These are work requests and authentication has previously failed.
121
				// It is most efficient to just give failed pseudo responses back for
122
				// the original work requests.
123
				foreach ( $reqs as $key => $req ) {
124
					$req['response'] = [
125
						'code'     => $this->authCachedStatus,
126
						'reason'   => $this->authCachedReason,
127
						'headers'  => [],
128
						'body'     => '',
129
						'error'    => ''
130
					];
131
					$result[$key] = $req;
132
				}
133
			} else {
134
				// These are work requests and we have a token already.
135
				// Go through and mangle each request to include a token.
136
				foreach ( $reqs as $key => $req ) {
137
					// The default encoding treats the URL as a REST style path that uses
138
					// forward slash as a hierarchical delimiter (and never otherwise).
139
					// Subclasses can override this, and should be documented in any case.
140
					$parts = array_map( 'rawurlencode', explode( '/', $req['url'] ) );
141
					$req['url'] = $this->authCreds['storage_url'] . '/' . implode( '/', $parts );
142
					$req['headers']['x-auth-token'] = $this->authCreds['auth_token'];
143
					$result[$key] = $req;
144
					// @TODO: add ETag/Content-Length and such as needed
145
				}
146
			}
147
		}
148
		return $result;
149
	}
150
151
	public function onResponses( array $reqs, Closure $idGeneratorFunc ) {
152
		$firstReq = reset( $reqs );
153
		if ( $firstReq && count( $reqs ) == 1 && isset( $firstReq['isAuth'] ) ) {
154
			$result = [];
155
			// This was an authentication request for work requests...
156
			if ( $this->applyAuthResponse( $firstReq ) ) {
157
				// If it succeeded, we can subsitute the work requests back.
158
				// Call this recursively in order to munge and add headers.
159
				$result = $this->onRequests( $firstReq['chain'], $idGeneratorFunc );
160 View Code Duplication
			} else {
161
				// If it failed, it is most efficient to just give failing
162
				// pseudo-responses back for the actual work requests.
163
				foreach ( $firstReq['chain'] as $key => $req ) {
164
					$req['response'] = [
165
						'code'     => $this->authCachedStatus,
166
						'reason'   => $this->authCachedReason,
167
						'headers'  => [],
168
						'body'     => '',
169
						'error'    => ''
170
					];
171
					$result[$key] = $req;
172
				}
173
			}
174
		} else {
175
			$result = $reqs; // no change
176
		}
177
		return $result;
178
	}
179
}
180