Skip to content
GitLab
    • Explore Projects Groups Topics Snippets
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Register
  • Sign in
  • F file-upload-bundle
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 3
    • Issues 3
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • Deployments
    • Deployments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • Repository
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
Collapse sidebar

En prévision de l'arrivée de la forge institutionnelle INRAE, nous vous invitons à créer vos nouveaux projets sur la forge MIA.

  • Pôle IS
  • Bundles Symfony 2Bundles Symfony 2
  • file-upload-bundle
  • Merge requests
  • !1
An error occurred while fetching the assigned milestone of the selected merge_request.

Add antivirus analyse (xenelop quahog)

  • Review changes

  • Download
  • Patches
  • Plain diff
Merged Guillaume Perréal requested to merge antivirus into symfony3 7 years ago
  • Overview 9
  • Commits 6
  • Pipelines 0
  • Changes 4
All threads resolved!
Compare
  • version 7
    11a29fd3
    7 years ago

  • version 6
    2a94ce03
    7 years ago

  • version 5
    38e33e0f
    7 years ago

  • version 4
    016a9968
    7 years ago

  • version 3
    360f3b08
    7 years ago

  • version 2
    360f3b08
    7 years ago

  • version 1
    c47b7717
    7 years ago

  • symfony3 (base)

and
  • latest version
    c3ed8cbc
    6 commits, 7 years ago

  • version 7
    11a29fd3
    5 commits, 7 years ago

  • version 6
    2a94ce03
    7 commits, 7 years ago

  • version 5
    38e33e0f
    3 commits, 7 years ago

  • version 4
    016a9968
    2 commits, 7 years ago

  • version 3
    360f3b08
    2 commits, 7 years ago

  • version 2
    360f3b08
    2 commits, 7 years ago

  • version 1
    c47b7717
    1 commit, 7 years ago

4 files
+ 97
− 54

    Preferences

    File browser
    Compare changes
Listener/VirusScannerListener.php
+ 30
− 14
  • View file @ 11a29fd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -6,9 +6,10 @@
@@ -6,9 +6,10 @@
namespace Irstea\FileUploadBundle\Listener;
namespace Irstea\FileUploadBundle\Listener;
use CL\Tissue\Adapter\AdapterInterface;
use Xenolope\Quahog\Client;
use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Exception\RejectedFileException;
use Irstea\FileUploadBundle\Exception\RejectedFileException;
 
use Xenolope\Quahog\Exception\ConnectionException;
/**
/**
* Description of AntivirusListener.
* Description of AntivirusListener.
@@ -16,42 +17,57 @@ use Irstea\FileUploadBundle\Exception\RejectedFileException;
@@ -16,42 +17,57 @@ use Irstea\FileUploadBundle\Exception\RejectedFileException;
class VirusScannerListener
class VirusScannerListener
{
{
/**
/**
* @var AdapterInterface
* @var Client
*/
*/
private $scanner;
private $client;
 
/**
/**
* @param AdapterInterface $scanner
* @param Client $client
*/
*/
public function __construct(AdapterInterface $scanner)
public function __construct(Client $client)
{
{
$this->scanner = $scanner;
$this->client = $client;
}
}
 
/**
/**
* @param FileUploadCompleteEvent $event
* @param FileUploadCompleteEvent $event
 
* @throws ConnectionException
*/
*/
public function onFileUploadCompleted(FileUploadCompleteEvent $event)
public function onFileUploadCompleted(FileUploadCompleteEvent $event)
{
{
 
$client = $this->getClient();
 
$file = $event->getUploadedFile();
$file = $event->getUploadedFile();
$path = $file->getLocalPath();
$path = $file->getLocalPath();
$result = $this->scanner->scan([$path]);
$result = $client->scanFile($path);
 
$hasVirus = $result['status'] === Client::RESULT_FOUND;
$meta = $file->getMetadata();
$meta = $file->getMetadata();
 
$meta['virus_scanner'] = ['has_virus' => $hasVirus];
$meta['virus_scanner'] = ['has_virus' => $result->hasVirus()];
if ($hasVirus) {
$desc = $result['reason'];
if ($result->hasVirus()) {
$meta['virus_scanner']['detected'] = $result['reason'];
if (null !== $desc = $result->getDetections()[0]->getDescription()) {
$meta['virus_scanner']['detected'] = $desc;
}
}
}
$file->setMetadata($meta);
$file->setMetadata($meta);
if ($result->hasVirus()) {
if ($hasVirus) {
throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !');
throw new RejectedFileException($file, $desc ? sprintf('Found the %s virus !', $desc) : 'Virus found !');
}
}
}
}
 
 
/**
 
* @return Client
 
* @throws ConnectionException
 
*/
 
private function getClient(): Client
 
{
 
// Check clamd server's state
 
$this->client->ping();
 
return $this->client;
 
}
}
}
Resources/config/services.yml
+ 22
− 6
  • View file @ 11a29fd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -10,6 +10,10 @@ parameters:
@@ -10,6 +10,10 @@ parameters:
irstea_file_upload.max_chunk_size: 0
irstea_file_upload.max_chunk_size: 0
 
irstea_file_upload.virus_scanner.clamav_socket_path: unix:///var/run/clamav/clamd.ctl
 
irstea_file_upload.virus_scanner.client_timeout: 30
 
irstea_file_upload.virus_scanner.client_mode: !php/const:PHP_NORMAL_READ
 
services:
services:
# Le gestionnaire de fichiers
# Le gestionnaire de fichiers
@@ -70,12 +74,24 @@ services:
@@ -70,12 +74,24 @@ services:
- "%irstea_file_upload.filesystem.name%"
- "%irstea_file_upload.filesystem.name%"
# Scanner anti-virus
# Scanner anti-virus
# irstea_file_upload.virus_scanner:
irstea_file_upload.virus_scanner.socket:
# class: Irstea\FileUploadBundle\Listener\VirusScannerListener
class: Socket\Raw\Socket
# arguments:
arguments:
# - @cl_tissue.scanner
- "%irstea_file_upload.virus_scanner.clamav_socket_path"
# tags:
# - { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted }
irstea_file_upload.virus_scanner.client:
 
class: Xenolope\Quahog\Client
 
arguments:
 
- "@irstea_file_upload.virus_scanner.socket"
 
- "%irstea_file_upload.virus_scanner.client_timeout"
 
- "%irstea_file_upload.virus_scanner.client_mode"
 
 
irstea_file_upload.virus_scanner:
 
class: Irstea\FileUploadBundle\Listener\VirusScannerListener
 
arguments:
 
- "@irstea_file_upload.virus_scanner.client"
 
tags:
 
- { name: kernel.event_listener, event: file_upload.complete, method: onFileUploadCompleted }
# Extension Twig
# Extension Twig
irstea_file_upload.twig_extension:
irstea_file_upload.twig_extension:
Tests/Listener/VirusScannerListenerTest.php
+ 43
− 33
  • View file @ 11a29fd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -6,12 +6,13 @@
@@ -6,12 +6,13 @@
namespace Irstea\FileUploadBundle\Tests\Listener;
namespace Irstea\FileUploadBundle\Tests\Listener;
use CL\Tissue\Model\Detection;
use CL\Tissue\Model\ScanResult;
use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Listener\VirusScannerListener;
use Irstea\FileUploadBundle\Listener\VirusScannerListener;
 
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_TestCase;
 
use Xenolope\Quahog\Client;
 
use Xenolope\Quahog\Exception\ConnectionException;
/**
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16.
* Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16.
@@ -26,7 +27,7 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
@@ -26,7 +27,7 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
/**
/**
* @var PHPUnit_Framework_MockObject_MockObject
* @var PHPUnit_Framework_MockObject_MockObject
*/
*/
protected $scanner;
protected $client;
/**
/**
* @var FileUploadCompleteEvent
* @var FileUploadCompleteEvent
@@ -39,23 +40,31 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
@@ -39,23 +40,31 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
protected $file;
protected $file;
/**
/**
* {@inheritdoc}
* {@inheritDoc}
*/
*/
protected function setUp()
protected function setUp()
{
{
self::markTestSkipped('No CL\Tissue');
$this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
 
 
$this->file = $this->getMock(UploadedFileInterface::class);
 
$this->file->expects($this->any())->method('getLocalPath')->willReturn('foopath');
 
$this->file->expects($this->any())->method('getMetadata')->willReturn([]);
 
 
$this->event = new FileUploadCompleteEvent($this->file);
 
 
$this->listener = new VirusScannerListener($this->client);
}
}
public function testOnFileUploadCompletedResultOk()
public function testOnFileUploadCompletedResultOk()
{
{
$result = new ScanResult(
$result = [
['foopath'],
'filename' => 'footpath',
['foopath'],
'reason' => null,
[]
'status' => Client::RESULT_OK,
);
];
$this->scanner->expects(static::once())->method('scan')->with(['foopath'])->willReturn($result);
$this->file->expects(static::once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
$this->client->expects($this->once())->method('scanFile')->with('foopath')->willReturn($result);
 
$this->file->expects($this->once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
$this->listener->onFileUploadCompleted($this->event);
$this->listener->onFileUploadCompleted($this->event);
}
}
@@ -65,36 +74,37 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
@@ -65,36 +74,37 @@ class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
*/
*/
public function testOnFileUploadCompletedVirusFound()
public function testOnFileUploadCompletedVirusFound()
{
{
$result = new ScanResult(
$result = [
['foopath'],
'filename' => 'foopath',
['foopath'],
'reason' => 'Terrible virus',
[new Detection('foopath', Detection::TYPE_VIRUS, 'BAR')]
'status' => Client::RESULT_FOUND,
);
];
$this->scanner->expects(static::once())->method('scan')->with(['foopath'])->willReturn($result);
$this->file->expects(static::once())->method('setMetadata')->with(
$this->client->expects($this->once())
['virus_scanner' => ['has_virus' => true, 'detected' => 'BAR']]
->method('scanFile')
);
->with('foopath')
 
->willReturn($result);
 
 
$this->file->expects($this->once())
 
->method('setMetadata')
 
->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]);
$this->listener->onFileUploadCompleted($this->event);
$this->listener->onFileUploadCompleted($this->event);
}
}
/**
/**
* @expectedException \Irstea\FileUploadBundle\Exception\RejectedFileException
* @expectedException Xenolope\Quahog\Exception\ConnectionException
*/
*/
public function testOnFileUploadCompletedVirusFoundNoDescription()
public function testGetClientFailed()
{
{
$result = new ScanResult(
$this->client->expects($this->once())->method('ping')->willThrowException(new ConnectionException());
['foopath'],
$this->listener->onFileUploadCompleted($this->event);
['foopath'],
}
[new Detection('foopath', Detection::TYPE_VIRUS)]
);
$this->scanner->expects(static::once())->method('scan')->with(['foopath'])->willReturn($result);
$this->file->expects(static::once())->method('setMetadata')->with(
['virus_scanner' => ['has_virus' => true]]
);
 
public function testGetClientSucceed()
 
{
 
$this->client->expects($this->once())->method('ping')->willReturn(true);
$this->listener->onFileUploadCompleted($this->event);
$this->listener->onFileUploadCompleted($this->event);
}
}
 
}
}
composer.json
+ 2
− 1
  • View file @ 11a29fd3

  • Edit in single-file editor

  • Open in Web IDE


@@ -33,7 +33,8 @@
@@ -33,7 +33,8 @@
"ramsey/uuid": "~2.8",
"ramsey/uuid": "~2.8",
"blueimp/jquery-file-upload": "~9.9",
"blueimp/jquery-file-upload": "~9.9",
"white-october/pagerfanta-bundle": "~1.0",
"white-october/pagerfanta-bundle": "~1.0",
"willdurand/js-translation-bundle": "~2.2"
"willdurand/js-translation-bundle": "~2.2",
 
"xenolope/quahog": "^2.1"
},
},
"require-dev": {
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8"
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 Participants
Reference:
Source branch: antivirus

Menu

Explore Projects Groups Topics Snippets