Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scan files uploaded to the bulk endpoint #354

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
106 changes: 66 additions & 40 deletions lib/AvirWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,7 @@ function ($data) use ($scanner) {
function () use ($scanner, $path) {
$status = $scanner->completeAsyncScan();
if ($status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
//prevent from going to trashbin
if ($this->trashEnabled) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->pauseTrash();
}

$owner = $this->getOwner($path);
$this->unlink($path);

if ($this->trashEnabled) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->resumeTrash();
}

$this->logger->warning(
'Infected file deleted. ' . $status->getDetails()
. ' Account: ' . $owner . ' Path: ' . $path,
['app' => 'files_antivirus']
);

$activity = $this->activityManager->generateEvent();
$activity->setApp(Application::APP_NAME)
->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()])
->setMessage(Provider::MESSAGE_FILE_DELETED)
->setObject('', 0, $path)
->setAffectedUser($owner)
->setType(Provider::TYPE_VIRUS_DETECTED);
$this->activityManager->publish($activity);

$this->logger->error('Infected file deleted. ' . $status->getDetails() .
' File: ' . $path . ' Account: ' . $owner, ['app' => 'files_antivirus']);

throw new InvalidContentException(
$this->l10n->t(
'Virus %s is detected in the file. Upload cannot be completed.',
$status->getDetails()
)
);
$this->handleInfected($path, $status);
}
}
);
Expand All @@ -169,4 +130,69 @@ private function isWritingMode($mode) {
);
return in_array($cleanMode, $this->writingModes);
}

/**
* Synchronously scan data that is written to a file by the bulk upload endpoint
*
* @return false|float|int
* @throws InvalidContentException
*/
public function file_put_contents($path, $data) {
if ($this->shouldWrap($path)) {
$scanner = $this->scannerFactory->getScanner($this->mountPoint . $path);
$scanner->initScanner();
$status = $scanner->scanString($data);
if ($status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
$this->handleInfected($path, $status);
}
}

return parent::file_put_contents($path, $data);
}

/**
* @throws InvalidContentException
*/
private function handleInfected(string $path, Status $status): void {
//prevent from going to trashbin
if ($this->trashEnabled) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->pauseTrash();
}

$owner = $this->getOwner($path);
$this->unlink($path);

if ($this->trashEnabled) {
/** @var ITrashManager $trashManager */
$trashManager = \OC::$server->query(ITrashManager::class);
$trashManager->resumeTrash();
}

$this->logger->warning(
'Infected file deleted. ' . $status->getDetails()
. ' Account: ' . $owner . ' Path: ' . $path,
['app' => 'files_antivirus']
);

$activity = $this->activityManager->generateEvent();
$activity->setApp(Application::APP_NAME)
->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()])
->setMessage(Provider::MESSAGE_FILE_DELETED)
->setObject('', 0, $path)
->setAffectedUser($owner)
->setType(Provider::TYPE_VIRUS_DETECTED);
$this->activityManager->publish($activity);

$this->logger->error('Infected file deleted. ' . $status->getDetails() .
' File: ' . $path . ' Account: ' . $owner, ['app' => 'files_antivirus']);

throw new InvalidContentException(
$this->l10n->t(
'Virus %s is detected in the file. Upload cannot be completed.',
$status->getDetails()
)
);
}
}
Loading