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
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IAppConfig;
use OCP\ICertificateManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Util;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -142,6 +143,7 @@ function (string $mountPoint, IStorage $storage) {
'block_unscannable' => $appConfig->getAvBlockUnscannable(),
'userManager' => $userManager,
'block_unreachable' => $appConfig->getAvBlockUnreachable(),
'request' => $container->get(IRequest::class),
]);
},
1
Expand Down
28 changes: 26 additions & 2 deletions lib/AvirWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\InvalidContentException;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

Expand All @@ -36,6 +37,7 @@ class AvirWrapper extends Wrapper {
private bool $blockUnscannable = false;
private IUserManager $userManager;
private string $blockUnReachable = 'yes';
private IRequest $request;

/**
* @param array $parameters
Expand All @@ -52,6 +54,7 @@ public function __construct($parameters) {
$this->blockUnscannable = $parameters['block_unscannable'];
$this->userManager = $parameters['userManager'];
$this->blockUnReachable = $parameters['block_unreachable'];
$this->request = $parameters['request'];

/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $parameters['eventDispatcher'];
Expand Down Expand Up @@ -95,9 +98,30 @@ private function shouldWrap(string $path): bool {
);
}

/**
* Try to extract actual path for .ocTransferId.part files (because the name is hashed).
*/
private function getPathForScanner(string $path): ?string {
$defaultReturnValue = null;
if ($this->mountPoint !== null) {
$defaultReturnValue = $this->mountPoint . $path;
}

if (!preg_match('/\.ocTransferId\d+\.part$/i', $path)) {
return $defaultReturnValue;
}

$davFilesPrefix = '/dav/files';
if (!str_starts_with($this->request->getPathInfo(), $davFilesPrefix)) {
return $defaultReturnValue;
}

return substr($this->request->getPathInfo(), strlen($davFilesPrefix));
}

private function wrapSteam(string $path, $stream) {
try {
$scanner = $this->scannerFactory->getScanner($this->mountPoint ? $this->mountPoint . $path : null);
$scanner = $this->scannerFactory->getScanner($this->getPathForScanner($path));
$scanner->initScanner();
return CallbackReadDataWrapper::wrap(
$stream,
Expand Down Expand Up @@ -149,7 +173,7 @@ private function isWritingMode($mode) {
*/
public function file_put_contents(string $path, mixed $data): int|float|false {
if ($this->shouldWrap($path)) {
$scanner = $this->scannerFactory->getScanner($this->mountPoint . $path);
$scanner = $this->scannerFactory->getScanner($this->getPathForScanner($path));
$scanner->initScanner();
$status = $scanner->scanString($data);
if ($status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
Expand Down
3 changes: 0 additions & 3 deletions lib/Scanner/ICAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ public function initScanner() {
}

$path = '/' . trim($this->path, '/');
if (str_contains($path, '.ocTransferId') && str_ends_with($path, '.part')) {
[$path] = explode('.ocTransferId', $path, 2);
}
$remote = $this->request ? $this->request->getRemoteAddress() : null;
$encodedPath = implode('/', array_map('rawurlencode', explode('/', $path)));

Expand Down
7 changes: 5 additions & 2 deletions tests/AvirWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Files_Antivirus\Scanner\ScannerFactory;
use OCA\Files_Antivirus\StatusFactory;
use OCP\Activity\IManager;
use OCP\IRequest;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -80,7 +81,8 @@ protected function setUp(): void {
'mount_point' => '/' . self::UID . '/files/',
'block_unscannable' => false,
'userManager' => $this->createMock(IUserManager::class),
'block_unreachable' => 'yes'
'block_unreachable' => 'yes',
'request' => $this->createMock(IRequest::class),
]);

$this->config->expects($this->any())
Expand Down Expand Up @@ -144,6 +146,7 @@ public function testWrapStreamWithNullMountPoint(): void {
'block_unscannable' => false,
'userManager' => $this->createMock(IUserManager::class),
'block_unreachable' => 'no',
'request' => $this->createMock(IRequest::class),
]);

$scanner = $this->createMock(IScanner::class);
Expand Down Expand Up @@ -180,7 +183,7 @@ public function testHandleConnectionErrorIsTriggered(): void {
->method('error')
->with($this->stringContains('Simulated failure'));

$wrapper = new class([ 'storage' => $this->storage, 'scannerFactory' => $scannerFactory, 'l10n' => $this->l10n, 'logger' => $logger, 'activityManager' => $this->createMock(\OCP\Activity\IManager::class), 'isHomeStorage' => false, 'eventDispatcher' => $this->createMock(\OCP\EventDispatcher\IEventDispatcher::class), 'trashEnabled' => false, 'mount_point' => '/', 'block_unscannable' => false, 'userManager' => $this->createMock(IUserManager::class), 'block_unreachable' => 'yes', ]) extends \OCA\Files_Antivirus\AvirWrapper {
$wrapper = new class([ 'storage' => $this->storage, 'scannerFactory' => $scannerFactory, 'l10n' => $this->l10n, 'logger' => $logger, 'activityManager' => $this->createMock(\OCP\Activity\IManager::class), 'isHomeStorage' => false, 'eventDispatcher' => $this->createMock(\OCP\EventDispatcher\IEventDispatcher::class), 'trashEnabled' => false, 'mount_point' => '/', 'block_unscannable' => false, 'userManager' => $this->createMock(IUserManager::class), 'block_unreachable' => 'yes', 'request' => $this->createMock(IRequest::class) ]) extends \OCA\Files_Antivirus\AvirWrapper {
public bool $connectionErrorCalled = false;
protected function handleConnectionError(string $path): void {
$this->connectionErrorCalled = true;
Expand Down
Loading