Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\Files\NotFoundException;
use OCP\IAvatarManager;
Expand Down Expand Up @@ -147,7 +148,7 @@ public function getAvatar($userId, $size) {
* @NoAdminRequired
*
* @param string $path
* @return DataResponse
* @return DataResponse|JSONResponse
*/
public function postAvatar($path) {
$userId = $this->userSession->getUser()->getUID();
Expand All @@ -172,7 +173,22 @@ public function postAvatar($path) {
$headers
);
}
$content = $node->getContent();

if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
return new JSONResponse(
['data' => ['message' => $this->l->t('The selected file is not an image.')]],
Http::STATUS_BAD_REQUEST
);
}

try {
$content = $node->getContent();
} catch (\OCP\Files\NotPermittedException $e) {
return new JSONResponse(
['data' => ['message' => $this->l->t('The selected file cannot be read.')]],
Http::STATUS_BAD_REQUEST
);
}
} elseif (!is_null($files)) {
if (
$files['error'][0] === 0 &&
Expand Down
8 changes: 4 additions & 4 deletions settings/js/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ $(document).ready(function () {
msg = data.jqXHR.responseJSON.data.message;
}
avatarResponseHandler({
data: {
message: t('settings', 'An error occurred: {message}', { message: msg })
data: {
message: msg
}
});
}
Expand All @@ -304,7 +304,7 @@ $(document).ready(function () {
url: OC.generateUrl('/avatar/'),
data: { path: path }
}).done(avatarResponseHandler)
.fail(function(jqXHR, status){
.fail(function(jqXHR) {
var msg = jqXHR.statusText + ' (' + jqXHR.status + ')';
if (!_.isUndefined(jqXHR.responseJSON) &&
!_.isUndefined(jqXHR.responseJSON.data) &&
Expand All @@ -314,7 +314,7 @@ $(document).ready(function () {
}
avatarResponseHandler({
data: {
message: t('settings', 'An error occurred: {message}', { message: msg })
message: msg
}
});
});
Expand Down
50 changes: 46 additions & 4 deletions tests/Core/Controller/AvatarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function is_uploaded_file($filename) {
use OCP\AppFramework\Http;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\IUser;
use OCP\Files\NotPermittedException;
use OCP\IAvatar;
use Punic\Exception;
use OCP\IUser;
use Test\Traits\UserTrait;

/**
Expand Down Expand Up @@ -314,7 +314,13 @@ public function testPostAvatarFromFile() {
//Mock node API call
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));

$file->expects($this->once())
->method('getContent')
->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$file->expects($this->once())
->method('getMimeType')
->willReturn('image/jpeg');
$this->container['UserFolder']->method('get')->willReturn($file);

//Create request return
Expand All @@ -341,6 +347,36 @@ public function testPostAvatarFromNoFile() {
$this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
}

public function testPostAvatarInvalidType() {
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->expects($this->never())
->method('getContent');
$file->expects($this->exactly(2))
->method('getMimeType')
->willReturn('text/plain');
$this->container['UserFolder']->method('get')->willReturn($file);

$expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
}

public function testPostAvatarNotPermittedException() {
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->expects($this->once())
->method('getContent')
->willThrowException(new NotPermittedException());
$file->expects($this->once())
->method('getMimeType')
->willReturn('image/jpeg');
$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->container['UserFolder']->method('get')->willReturn($file);

$expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
}

/**
* Test what happens if the upload of the avatar fails
*/
Expand All @@ -350,7 +386,13 @@ public function testPostAvatarException() {
->will($this->throwException(new \Exception("foo")));
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
$file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));

$file->expects($this->once())
->method('getContent')
->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$file->expects($this->once())
->method('getMimeType')
->willReturn('image/jpeg');
$this->container['UserFolder']->method('get')->willReturn($file);

$this->container['Logger']->expects($this->once())
Expand Down