Issues (2473)

Branch: master

Security Analysis    no vulnerabilities found

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.

engine/classes/ElggAnnotation.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
 * Elgg Annotations
4
 *
5
 * Annotations allow you to attach bits of information to entities. They are
6
 * essentially the same as metadata, but with additional helper functions for
7
 * performing calculations.
8
 *
9
 * @note Internal: Annotations are stored in the annotations table.
10
 *
11
 * @package    Elgg.Core
12
 * @subpackage DataModel.Annotations
13
 */
14
class ElggAnnotation extends \ElggExtender {
15
16
	/**
17
	 * (non-PHPdoc)
18
	 *
19
	 * @see \ElggData::initializeAttributes()
20
	 *
21
	 * @return void
22
	 */
23
	protected function initializeAttributes() {
24
		parent::initializeAttributes();
25
26
		$this->attributes['type'] = 'annotation';
27
	}
28
29
	/**
30
	 * Construct a new annotation object
31
	 *
32
	 * Plugin developers will probably never use the constructor.
33
	 * See \ElggEntity for its API for adding annotations.
34
	 *
35
	 * @param \stdClass $row Database row as \stdClass object
36
	 */
37 View Code Duplication
	public function __construct($row = null) {
0 ignored issues
show
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...
38
		$this->initializeAttributes();
39
40
		if (!empty($row)) {
41
			// Create from db row
42
			if ($row instanceof \stdClass) {
43
				$annotation = $row;
44
45
				$objarray = (array) $annotation;
46
				foreach ($objarray as $key => $value) {
47
					$this->attributes[$key] = $value;
48
				}
49
			} else {
50
				// get an \ElggAnnotation object and copy its attributes
51
				elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use elgg_get_annotation_from_id()', 1.9);
52
				$annotation = elgg_get_annotation_from_id($row);
53
				$this->attributes = $annotation->attributes;
54
			}
55
		}
56
	}
57
58
	/**
59
	 * Save this instance
60
	 *
61
	 * @return int an object id
62
	 *
63
	 * @throws IOException
64
	 */
65 View Code Duplication
	public function save() {
0 ignored issues
show
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...
66
		if ($this->id > 0) {
67
			return update_annotation($this->id, $this->name, $this->value, $this->value_type,
0 ignored issues
show
Bug Compatibility introduced by
The expression update_annotation($this-...uid, $this->access_id); of type integer|boolean adds the type integer to the return on line 67 which is incompatible with the return type declared by the abstract method ElggData::save of type boolean.
Loading history...
68
				$this->owner_guid, $this->access_id);
69
		} else {
70
			$this->id = create_annotation($this->entity_guid, $this->name, $this->value,
71
				$this->value_type, $this->owner_guid, $this->access_id);
72
73
			if (!$this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
74
				throw new \IOException("Unable to save new " . get_class());
75
			}
76
			return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->id; (integer) is incompatible with the return type declared by the abstract method ElggData::save of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
77
		}
78
	}
79
80
	/**
81
	 * Delete the annotation.
82
	 *
83
	 * @return bool
84
	 */
85
	public function delete() {
86
		$result = _elgg_delete_metastring_based_object_by_id($this->id, 'annotation');
87
		if ($result) {
88
			elgg_delete_river(array('annotation_id' => $this->id));
89
		}
90
91
		return $result;
92
	}
93
94
	/**
95
	 * Disable the annotation.
96
	 *
97
	 * @return bool
98
	 * @since 1.8
99
	 */
100
	public function disable() {
101
		return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'annotations');
102
	}
103
104
	/**
105
	 * Enable the annotation.
106
	 *
107
	 * @return bool
108
	 * @since 1.8
109
	 */
110
	public function enable() {
111
		return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'annotations');
112
	}
113
114
	/**
115
	 * Determines whether or not the user can edit this annotation
116
	 *
117
	 * @param int $user_guid The GUID of the user (defaults to currently logged in user)
118
	 *
119
	 * @return bool
120
	 * @see elgg_set_ignore_access()
121
	 */
122
	public function canEdit($user_guid = 0) {
123
124 View Code Duplication
		if ($user_guid) {
125
			$user = get_user($user_guid);
126
			if (!$user) {
127
				return false;
128
			}
129
		} else {
130
			$user = _elgg_services()->session->getLoggedInUser();
131
			$user_guid = _elgg_services()->session->getLoggedInUserGuid();
132
		}
133
134
		$result = false;
135
		if ($user) {
136
			// If the owner of annotation is the specified user, they can edit.
137
			if ($this->getOwnerGUID() == $user_guid) {
138
				$result = true;
139
			}
140
141
			// If the user can edit the entity this is attached to, they can edit.
142
			$entity = $this->getEntity();
143
			if ($result == false && $entity->canEdit($user->getGUID())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
144
				$result = true;
145
			}
146
		}
147
148
		// Trigger plugin hook - note that $user may be null
149
		$params = array('entity' => $entity, 'user' => $user, 'annotation' => $this);
0 ignored issues
show
The variable $entity 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...
150
		return _elgg_services()->hooks->trigger('permissions_check', 'annotation', $params, $result);
151
	}
152
153
	// SYSTEM LOG INTERFACE
154
155
	/**
156
	 * For a given ID, return the object associated with it.
157
	 * This is used by the river functionality primarily.
158
	 * This is useful for checking access permissions etc on objects.
159
	 *
160
	 * @param int $id An annotation ID.
161
	 *
162
	 * @return \ElggAnnotation
163
	 */
164
	public function getObjectFromID($id) {
165
		return elgg_get_annotation_from_id($id);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return elgg_get_annotation_from_id($id); (ElggExtender) is incompatible with the return type declared by the interface Loggable::getObjectFromID of type ElggEntity.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
166
	}
167
}
168