Issues (5)

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/Guzzle/MiddlewareFactory.php (1 issue)

Labels
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
namespace Mediawiki\Api\Guzzle;
4
5
use GuzzleHttp\Exception\ConnectException;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Middleware;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Psr7\Response;
10
use Psr\Log\LoggerAwareInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
14
/**
15
 * @access private
16
 *
17
 * @author Addshore
18
 */
19
class MiddlewareFactory implements LoggerAwareInterface {
20
21
	/**
22
	 * @var LoggerInterface
23
	 */
24
	private $logger;
25
26 10
	public function __construct() {
27 10
		$this->logger = new NullLogger();
28 10
	}
29
30
	/**
31
	 * @param LoggerInterface $logger The new Logger object.
32
	 */
33
	public function setLogger( LoggerInterface $logger ) {
34
		$this->logger = $logger;
35
	}
36
37
	/**
38
	 * @private
39
	 *
40
	 * @param bool $delay default to true, can be false to speed up tests
41
	 *
42
	 * @return callable
43
	 */
44 10
	public function retry( $delay = true ) {
45 10
		if ( $delay ) {
46 10
			return Middleware::retry( $this->newRetryDecider(), $this->getRetryDelay() );
47
		} else {
48
			return Middleware::retry( $this->newRetryDecider() );
49
		}
50
	}
51
52
	/**
53
	 * Returns a method that takes the number of retries and returns the number of miliseconds
54
	 * to wait
55
	 *
56
	 * @return callable
57
	 */
58 10
	private function getRetryDelay() {
59
		return function ( $numberOfRetries, Response $response = null ) {
60
			// The $response argument is only passed as of Guzzle 6.2.2.
61 10
			if ( $response !== null ) {
62
				// Retry-After may be a number of seconds or an absolute date (RFC 7231,
63
				// section 7.1.3).
64 7
				$retryAfter = $response->getHeaderLine( 'Retry-After' );
65
66 7
				if ( is_numeric( $retryAfter ) ) {
67 1
					return 1000 * $retryAfter;
68
				}
69
70 6
				if ( $retryAfter ) {
71 2
					$seconds = strtotime( $retryAfter ) - time();
72 2
					return 1000 * max( 1, $seconds );
73
				}
74
			}
75
76 7
			return 1000 * $numberOfRetries;
77 10
		};
78
	}
79
80
	/**
81
	 * @return callable
82
	 */
83
	private function newRetryDecider() {
84 10
		return function (
85
			$retries,
86
			Request $request,
87
			Response $response = null,
88
			RequestException $exception = null
89
		) {
90
			// Don't retry if we have run out of retries
91 10
			if ( $retries >= 5 ) {
92 1
				return false;
93
			}
94
95 10
			$shouldRetry = false;
96
97
			// Retry connection exceptions
98 10
			if ( $exception instanceof ConnectException ) {
99 3
				$shouldRetry = true;
100
			}
101
102 10
			if ( $response ) {
103 9
				$data = json_decode( $response->getBody(), true );
104
105
				// Retry on server errors
106 9
				if ( $response->getStatusCode() >= 500 ) {
107 2
					$shouldRetry = true;
108
				}
109
110 9
				foreach ( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) {
111
					if (
112
						// Retry if the API explicitly tells us to:
113
						// https://www.mediawiki.org/wiki/Manual:Maxlag_parameter
114 5
						$response->getHeaderLine( 'Retry-After' )
115
						||
116
						// Retry if we have a response with an API error worth retrying
117 2
						in_array(
118 2
							$mediawikiApiErrorHeader,
119
							[
120 2
								'ratelimited',
121
								'maxlag',
122
								'readonly',
123
								'internal_api_error_DBQueryError',
124
							]
125
						)
126
						||
127
						// Or if we have been stopped from saving as an 'anti-abuse measure'
128
						// Note: this tries to match "actionthrottledtext" i18n messagae for mediawiki
129
						(
130 2
							$mediawikiApiErrorHeader == 'failed-save' &&
131 5
							strstr( $data['error']['info'], 'anti-abuse measure' )
132
						)
133
					) {
134 5
						$shouldRetry = true;
135
					}
136
137
				}
138
			}
139
140
			// Log if we are retrying
141 10
			if ( $shouldRetry ) {
142 10
				$this->logger->warning(
143 10
					sprintf(
144 10
						'Retrying %s %s %s/5, %s',
145 10
						$request->getMethod(),
146 10
						$request->getUri(),
147 10
						$retries + 1,
148 10
						$response ? 'status code: ' . $response->getStatusCode() :
149 10
							$exception->getMessage()
0 ignored issues
show
It seems like $exception is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
150
					)
151
				);
152
			}
153
154 10
			return $shouldRetry;
155 10
		};
156
	}
157
158
}
159