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.

includes/api/ApiMergeHistory.php (4 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
 *
4
 *
5
 * Created on Dec 29, 2015
6
 *
7
 * Copyright © 2015 Geoffrey Mon <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 */
26
27
/**
28
 * API Module to merge page histories
29
 * @ingroup API
30
 */
31
class ApiMergeHistory extends ApiBase {
32
33
	public function execute() {
34
		$this->useTransactionalTimeLimit();
35
36
		$params = $this->extractRequestParams();
37
38
		$this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39
		$this->requireOnlyOneParameter( $params, 'to', 'toid' );
40
41
		// Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
42 View Code Duplication
		if ( isset( $params['from'] ) ) {
43
			$fromTitle = Title::newFromText( $params['from'] );
44
			if ( !$fromTitle || $fromTitle->isExternal() ) {
45
				$this->dieUsageMsg( [ 'invalidtitle', $params['from'] ] );
46
			}
47
		} elseif ( isset( $params['fromid'] ) ) {
48
			$fromTitle = Title::newFromID( $params['fromid'] );
49
			if ( !$fromTitle ) {
50
				$this->dieUsageMsg( [ 'nosuchpageid', $params['fromid'] ] );
51
			}
52
		}
53
54 View Code Duplication
		if ( isset( $params['to'] ) ) {
55
			$toTitle = Title::newFromText( $params['to'] );
56
			if ( !$toTitle || $toTitle->isExternal() ) {
57
				$this->dieUsageMsg( [ 'invalidtitle', $params['to'] ] );
58
			}
59
		} elseif ( isset( $params['toid'] ) ) {
60
			$toTitle = Title::newFromID( $params['toid'] );
61
			if ( !$toTitle ) {
62
				$this->dieUsageMsg( [ 'nosuchpageid', $params['toid'] ] );
63
			}
64
		}
65
66
		$reason = $params['reason'];
67
		$timestamp = $params['timestamp'];
68
69
		// Merge!
70
		$status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
0 ignored issues
show
The variable $fromTitle does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
The variable $toTitle does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
It seems like $fromTitle can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
It seems like $toTitle can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71
		if ( !$status->isOK() ) {
72
			$this->dieStatus( $status );
73
		}
74
75
		$r = [
76
			'from' => $fromTitle->getPrefixedText(),
77
			'to' => $toTitle->getPrefixedText(),
78
			'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
79
			'reason' => $params['reason']
80
		];
81
		$result = $this->getResult();
82
83
		$result->addValue( null, $this->getModuleName(), $r );
84
	}
85
86
	/**
87
	 * @param Title $from
88
	 * @param Title $to
89
	 * @param string $timestamp
90
	 * @param string $reason
91
	 * @return Status
92
	 */
93
	protected function merge( Title $from, Title $to, $timestamp, $reason ) {
94
		$mh = new MergeHistory( $from, $to, $timestamp );
95
96
		return $mh->merge( $this->getUser(), $reason );
97
	}
98
99
	public function mustBePosted() {
100
		return true;
101
	}
102
103
	public function isWriteMode() {
104
		return true;
105
	}
106
107 View Code Duplication
	public function getAllowedParams() {
108
		return [
109
			'from' => null,
110
			'fromid' => [
111
				ApiBase::PARAM_TYPE => 'integer'
112
			],
113
			'to' => null,
114
			'toid' => [
115
				ApiBase::PARAM_TYPE => 'integer'
116
			],
117
			'timestamp' => [
118
				ApiBase::PARAM_TYPE => 'timestamp'
119
			],
120
			'reason' => '',
121
		];
122
	}
123
124
	public function needsToken() {
125
		return 'csrf';
126
	}
127
128
	protected function getExamplesMessages() {
129
		return [
130
			'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
131
			'reason=Reason'
132
			=> 'apihelp-mergehistory-example-merge',
133
			'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
134
			'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
135
			=> 'apihelp-mergehistory-example-merge-timestamp',
136
		];
137
	}
138
139
	public function getHelpUrls() {
140
		return 'https://www.mediawiki.org/wiki/API:Mergehistory';
141
	}
142
}
143