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/utils/AvroValidator.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
/**
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation; either version 2 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License along
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 * http://www.gnu.org/copyleft/gpl.html
17
 *
18
 * @file
19
 */
20
21
/**
22
 * Generate error strings for data that doesn't match the specified
23
 * Avro schema. This is very similar to AvroSchema::is_valid_datum(),
24
 * but returns error messages instead of a boolean.
25
 *
26
 * @since 1.26
27
 * @author Erik Bernhardson <[email protected]>
28
 * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
29
 */
30
class AvroValidator {
31
	/**
32
	 * @param AvroSchema $schema The rules to conform to.
33
	 * @param mixed $datum The value to validate against $schema.
34
	 * @return string|string[] An error or list of errors in the
35
	 *  provided $datum. When no errors exist the empty array is
36
	 *  returned.
37
	 */
38
	public static function getErrors( AvroSchema $schema, $datum ) {
39
		switch ( $schema->type ) {
0 ignored issues
show
The property type does not seem to exist in AvroSchema.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
40
		case AvroSchema::NULL_TYPE:
41
			if ( !is_null( $datum ) ) {
42
				return self::wrongType( 'null', $datum );
43
			}
44
			return [];
45
		case AvroSchema::BOOLEAN_TYPE:
46
			if ( !is_bool( $datum ) ) {
47
				return self::wrongType( 'boolean', $datum );
48
			}
49
			return [];
50
		case AvroSchema::STRING_TYPE:
51
		case AvroSchema::BYTES_TYPE:
52
			if ( !is_string( $datum ) ) {
53
				return self::wrongType( 'string', $datum );
54
			}
55
			return [];
56 View Code Duplication
		case AvroSchema::INT_TYPE:
57
			if ( !is_int( $datum ) ) {
58
				return self::wrongType( 'integer', $datum );
59
			}
60
			if ( AvroSchema::INT_MIN_VALUE > $datum
61
				|| $datum > AvroSchema::INT_MAX_VALUE
62
			) {
63
				return self::outOfRange(
64
					AvroSchema::INT_MIN_VALUE,
65
					AvroSchema::INT_MAX_VALUE,
66
					$datum
67
				);
68
			}
69
			return [];
70 View Code Duplication
		case AvroSchema::LONG_TYPE:
71
			if ( !is_int( $datum ) ) {
72
				return self::wrongType( 'integer', $datum );
73
			}
74
			if ( AvroSchema::LONG_MIN_VALUE > $datum
75
				|| $datum > AvroSchema::LONG_MAX_VALUE
76
			) {
77
				return self::outOfRange(
78
					AvroSchema::LONG_MIN_VALUE,
79
					AvroSchema::LONG_MAX_VALUE,
80
					$datum
81
				);
82
			}
83
			return [];
84
		case AvroSchema::FLOAT_TYPE:
85
		case AvroSchema::DOUBLE_TYPE:
86
			if ( !is_float( $datum ) && !is_int( $datum ) ) {
87
				return self::wrongType( 'float or integer', $datum );
88
			}
89
			return [];
90
		case AvroSchema::ARRAY_SCHEMA:
91
			if ( !is_array( $datum ) ) {
92
				return self::wrongType( 'array', $datum );
93
			}
94
			$errors = [];
95
			foreach ( $datum as $d ) {
96
				$result = self::getErrors( $schema->items(), $d );
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method items() does only exist in the following sub-classes of AvroSchema: AvroArraySchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
97
				if ( $result ) {
98
					$errors[] = $result;
99
				}
100
			}
101
			return $errors;
102
		case AvroSchema::MAP_SCHEMA:
103
			if ( !is_array( $datum ) ) {
104
				return self::wrongType( 'array', $datum );
105
			}
106
			$errors = [];
107
			foreach ( $datum as $k => $v ) {
108
				if ( !is_string( $k ) ) {
109
					$errors[] = self::wrongType( 'string key', $k );
110
				}
111
				$result = self::getErrors( $schema->values(), $v );
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method values() does only exist in the following sub-classes of AvroSchema: AvroMapSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
112
				if ( $result ) {
113
					$errors[$k] = $result;
114
				}
115
			}
116
			return $errors;
117
		case AvroSchema::UNION_SCHEMA:
118
			$errors = [];
119
			foreach ( $schema->schemas() as $schema ) {
120
				$result = self::getErrors( $schema, $datum );
121
				if ( !$result ) {
122
					return [];
123
				}
124
				$errors[] = $result;
125
			}
126
			if ( $errors ) {
127
				return [ "Expected any one of these to be true", $errors ];
128
			}
129
			return "No schemas provided to union";
130
		case AvroSchema::ENUM_SCHEMA:
131
			if ( !in_array( $datum, $schema->symbols() ) ) {
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method symbols() does only exist in the following sub-classes of AvroSchema: AvroEnumSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
132
				$symbols = implode( ', ', $schema->symbols );
0 ignored issues
show
The property symbols does not seem to exist in AvroSchema.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
				return "Expected one of $symbols but recieved $datum";
134
			}
135
			return [];
136
		case AvroSchema::FIXED_SCHEMA:
137
			if ( !is_string( $datum ) ) {
138
				return self::wrongType( 'string', $datum );
139
			}
140
			$len = strlen( $datum );
141
			if ( $len !== $schema->size() ) {
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method size() does only exist in the following sub-classes of AvroSchema: AvroFixedSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
142
				return "Expected string of length {$schema->size()}, "
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method size() does only exist in the following sub-classes of AvroSchema: AvroFixedSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
143
					. "but recieved one of length $len";
144
			}
145
			return [];
146
		case AvroSchema::RECORD_SCHEMA:
147
		case AvroSchema::ERROR_SCHEMA:
148
		case AvroSchema::REQUEST_SCHEMA:
149
			if ( !is_array( $datum ) ) {
150
				return self::wrongType( 'array', $datum );
151
			}
152
			$errors = [];
153
			foreach ( $schema->fields() as $field ) {
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class AvroSchema as the method fields() does only exist in the following sub-classes of AvroSchema: AvroRecordSchema. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
154
				$name = $field->name();
155
				if ( !array_key_exists( $name, $datum ) ) {
156
					$errors[$name] = 'Missing expected field';
157
					continue;
158
				}
159
				$result = self::getErrors( $field->type(), $datum[$name] );
160
				if ( $result ) {
161
					$errors[$name] = $result;
162
				}
163
			}
164
			return $errors;
165
		default:
166
			return "Unknown avro schema type: {$schema->type}";
167
		}
168
	}
169
170
	public static function typeOf( $datum ) {
171
		return is_object( $datum ) ? get_class( $datum ) : gettype( $datum );
172
	}
173
174
	public static function wrongType( $expected, $datum ) {
175
		return "Expected $expected, but recieved " . self::typeOf( $datum );
176
	}
177
178
	public static function outOfRange( $min, $max, $datum ) {
179
		return "Expected value between $min and $max, but recieved $datum";
180
	}
181
}
182