Issues (493)

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.

tests/unit/lib/addressbookTest.php (10 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Copyright (c) 2013 Thomas Tanghus ([email protected])
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Contacts;
10
11
use Sabre\VObject\Reader;
12
use Test\TestCase;
13
14
require_once __DIR__ . '/backend/mock.php';
15
16
class AddressBookTest extends TestCase {
17
18
	/**
19
	* @var array
20
	*/
21
	protected $abinfo;
22
	/**
23
	* @var \OCA\Contacts\Addressbook
24
	*/
25
	protected $ab;
26
	/**
27
	* @var \OCA\Contacts\Backend\AbstractBackend
28
	*/
29
	protected $backend;
30
31
	public function setUp() {
32
		parent::setUp();
33
34
		$this->backend = new Backend\Mock('foobar');
35
		$this->abinfo = $this->backend->getAddressBook('foo');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->backend->getAddressBook('foo') can be null. However, the property $abinfo is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
36
		$this->ab = new AddressBook($this->backend, $this->abinfo);
0 ignored issues
show
It seems like $this->abinfo can also be of type null; however, OCA\Contacts\Addressbook::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
38
	}
39
40
	public function tearDown() {
41
		unset($this->backend);
42
		unset($this->ab);
43
44
		parent::tearDown();
45
	}
46
47
	public function testGetDisplayName() {
48
49
		$this->assertEquals('d-name', $this->ab->getDisplayName());
50
51
	}
52
53
	public function testGetPermissions() {
54
55
		$this->assertEquals(\OCP\PERMISSION_ALL, $this->ab->getPermissions());
56
57
	}
58
59
	public function testGetBackend() {
60
61
		$this->assertEquals($this->backend, $this->ab->getBackend());
62
63
	}
64
65
	public function testGetChild() {
66
67
		$contact = $this->ab->getChild('123');
68
		$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact);
69
		$this->assertEquals('Max Mustermann', $contact->getDisplayName());
70
71
	}
72
73
	public function testChildExists() {
74
75
		$this->assertFalse($this->ab->childExists('12'));
76
		$this->assertTrue($this->ab->childExists('123'));
77
78
	}
79
80 View Code Duplication
	public function testAddChild() {
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...
81
82
		$carddata = file_get_contents(__DIR__ . '/../data/test2.vcf');
83
		$vcard = Reader::read($carddata);
84
		$id = $this->ab->addChild($vcard);
0 ignored issues
show
It seems like $vcard defined by \Sabre\VObject\Reader::read($carddata) on line 83 can also be of type object<Sabre\VObject\Component> or object<Sabre\VObject\Property>; however, OCA\Contacts\Addressbook::addChild() does only seem to accept array|object<OCA\Contacts\VObject\VCard>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
		$this->assertNotEquals(false, $id);
86
87
		return $this->ab;
88
	}
89
90
	public function testDeleteChild() {
91
92
		$this->assertTrue($this->ab->deleteChild('123'));
93
		$this->assertEquals(array(), $this->ab->getChildren());
94
95
	}
96
97
	public function testGetChildNotFound() {
98
99
		try {
100
			$contact = $this->ab->getChild('Nowhere');
0 ignored issues
show
$contact is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
101
		} catch(\Exception $e) {
102
			$this->assertEquals('Contact not found', $e->getMessage());
103
			$this->assertEquals(404, $e->getCode());
104
			return;
105
		}
106
107
		$this->fail('Expected Exception 404.');
108
109
	}
110
111
	/**
112
	* @depends testAddChild
113
	 * @param $ab
114
	 */
115
	public function testGetChildren($ab) {
116
117
		$contacts = $ab->getChildren();
118
119
		$this->assertCount(2, $contacts);
120
121
		$this->assertEquals('Max Mustermann', $contacts[0]->getDisplayName());
122
		$this->assertEquals('John Q. Public', $contacts[1]->getDisplayName());
123
124
	}
125
126
	public function testDelete() {
127
128
		$this->assertTrue($this->ab->delete());
129
		$this->assertEquals(array(), $this->backend->addressBooks);
0 ignored issues
show
The property addressBooks does not seem to exist in OCA\Contacts\Backend\AbstractBackend.

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...
130
131
	}
132
133
	public function testGetLastModified() {
134
135
		$this->assertNull($this->ab->lastModified());
136
137
	}
138
139 View Code Duplication
	public function testUpdate() {
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...
140
141
		$this->assertTrue(
142
			$this->ab->update(array('displayname' => 'bar'))
143
		);
144
145
		$this->assertEquals('bar', $this->backend->addressBooks[0]['displayname']);
0 ignored issues
show
The property addressBooks does not seem to exist in OCA\Contacts\Backend\AbstractBackend.

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...
146
		$props = $this->ab->getMetaData();
0 ignored issues
show
$props is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
147
148
		return $this->ab;
149
150
	}
151
152
	/**
153
	* @depends testUpdate
154
	*/
155
	public function testGetMetaData($ab) {
156
157
		$props = $ab->getMetaData();
158
		$this->assertEquals('bar', $props['displayname']);
159
160
	}
161
162
	public function testArrayAccess() {
163
164
		$carddata = file_get_contents(__DIR__ . '/../data/test2.vcf');
165
		$vcard = Reader::read($carddata);
166
167
		$contact = $this->ab['123'];
168
169
		// Test get
170
		$this->assertTrue(isset($this->ab['123']));
171
		$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact);
172
		$this->assertEquals('Max Mustermann', $contact->getDisplayName());
173
174
		// Test unset
175
		unset($this->ab['123']);
176
177
		$this->assertTrue(!isset($this->ab['123']));
178
179
		// Test set
180
		try {
181
			$this->ab[] = $vcard;
182
		} catch(\Exception $e) {
183
			return;
184
		}
185
186
		$this->fail('Expected Exception');
187
188
	}
189
190
	/**
191
	* @depends testAddChild
192
	*/
193
	public function testIterator($ab) {
194
195
		$count = 0;
196
197
		foreach($ab as $contact) {
198
			$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact);
199
			$count += 1;
200
		}
201
202
		$this->assertEquals(2, $count);
203
	}
204
205
	/**
206
	* @depends testAddChild
207
	*/
208
	public function testCountable($ab) {
209
210
		$this->assertEquals(2, count($ab));
211
212
	}
213
214
}
215