hardening, add some checks for whitespace-only strings

This commit is contained in:
Arthur Schiwon 2015-12-04 11:13:39 +01:00
parent f9081303b1
commit 0c1c029571
2 changed files with 25 additions and 22 deletions

View File

@ -66,6 +66,7 @@ class Comment implements IComment {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$id = trim($id);
if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) {
$this->data['id'] = $id; $this->data['id'] = $id;
return $this; return $this;
@ -95,7 +96,7 @@ class Comment implements IComment {
if(!is_string($parentId)) { if(!is_string($parentId)) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$this->data['parentId'] = $parentId; $this->data['parentId'] = trim($parentId);
return $this; return $this;
} }
@ -121,7 +122,7 @@ class Comment implements IComment {
if(!is_string($id)) { if(!is_string($id)) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$this->data['topmostParentId'] = $id; $this->data['topmostParentId'] = trim($id);
return $this; return $this;
} }
@ -171,7 +172,7 @@ class Comment implements IComment {
if(!is_string($message)) { if(!is_string($message)) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$this->data['message'] = $message; $this->data['message'] = trim($message);
return $this; return $this;
} }
@ -193,10 +194,10 @@ class Comment implements IComment {
* @since 9.0.0 * @since 9.0.0
*/ */
public function setVerb($verb) { public function setVerb($verb) {
if(!is_string($verb)) { if(!is_string($verb) || empty(trim($verb))) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('Non-empty String expected.');
} }
$this->data['verb'] = $verb; $this->data['verb'] = trim($verb);
return $this; return $this;
} }
@ -230,13 +231,13 @@ class Comment implements IComment {
*/ */
public function setActor($actorType, $actorId) { public function setActor($actorType, $actorId) {
if( if(
!is_string($actorType) || empty($actorType) !is_string($actorType) || empty(trim($actorType))
|| !is_string($actorId) || empty($actorId) || !is_string($actorId) || empty(trim($actorId))
) { ) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$this->data['actorType'] = $actorType; $this->data['actorType'] = trim($actorType);
$this->data['actorId'] = $actorId; $this->data['actorId'] = trim($actorId);
return $this; return $this;
} }
@ -316,13 +317,13 @@ class Comment implements IComment {
*/ */
public function setObject($objectType, $objectId) { public function setObject($objectType, $objectId) {
if( if(
!is_string($objectType) || empty($objectType) !is_string($objectType) || empty(trim($objectType))
|| !is_string($objectId) || empty($objectId) || !is_string($objectId) || empty(trim($objectId))
) { ) {
throw new \InvalidArgumentException('String expected.'); throw new \InvalidArgumentException('String expected.');
} }
$this->data['objectType'] = $objectType; $this->data['objectType'] = trim($objectType);
$this->data['objectId'] = $objectId; $this->data['objectId'] = trim($objectId);
return $this; return $this;
} }

View File

@ -60,24 +60,24 @@ class Test_Comments_Comment extends TestCase
public function simpleSetterProvider() { public function simpleSetterProvider() {
return [ return [
['Id'], ['Id', true],
['ParentId'], ['ParentId', true],
['Message'], ['Message', true],
['Verb'], ['Verb', true],
['ChildrenCount'], ['Verb', ''],
['ChildrenCount', true],
]; ];
} }
/** /**
* @dataProvider simpleSetterProvider * @dataProvider simpleSetterProvider
*/ */
public function testSimpleSetterInvalidInput($field) { public function testSimpleSetterInvalidInput($field, $input) {
$comment = new \OC\Comments\Comment(); $comment = new \OC\Comments\Comment();
$setter = 'set' . $field; $setter = 'set' . $field;
$this->setExpectedException('InvalidArgumentException'); $this->setExpectedException('InvalidArgumentException');
// we have no field that is supposed to accept a Bool $comment->$setter($input);
$comment->$setter(true);
} }
public function roleSetterProvider() { public function roleSetterProvider() {
@ -85,9 +85,11 @@ class Test_Comments_Comment extends TestCase
['Actor', true, true], ['Actor', true, true],
['Actor', 'user', true], ['Actor', 'user', true],
['Actor', true, 'alice'], ['Actor', true, 'alice'],
['Actor', ' ', ' '],
['Object', true, true], ['Object', true, true],
['Object', 'file', true], ['Object', 'file', true],
['Object', true, 'file64'], ['Object', true, 'file64'],
['Object', ' ', ' '],
]; ];
} }