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

28 reduce scans on every page load #29

Merged
merged 2 commits into from
Oct 18, 2024
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: 1 addition & 1 deletion .devcontainer/configureWordPress.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ docker exec -it --user www-data gdata-antivirus-app-1 bash -c "wp plugin install
docker exec -it --user www-data gdata-antivirus-app-1 bash -c "wp plugin activate plugin-check"
docker exec -it --user www-data gdata-antivirus-app-1 bash -c "wp plugin activate gdata-antivirus"

svn co https://plugins.svn.wordpress.org/gdata-antivirus/ svn
svn co https://plugins.svn.wordpress.org/gdata-antivirus/ svn/gdata-antivirus
32 changes: 23 additions & 9 deletions Vaas/ScanClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ScanClient {
private VaasOptions $vaas_options;
private IGdataAntivirusFileSystem $file_system;
private AdminNotices $admin_notices;
private bool $connected = false;

public function __construct(
LoggerInterface $logger,
Expand All @@ -32,13 +33,6 @@ public function __construct(
$this->file_system = $file_system;
$this->admin_notices = $admin_notices;

try {
$this->Connect();
} catch (\Exception $e) {
$this->admin_notices->add_notice($e->getMessage());
$this->logger->error("VaaS connection failed. Please verify if the VaaS-Url is correct.");
return;
}
$plugin_upload_scan_enabled = (bool) get_option('gdatacyberdefenseag_antivirus_options_on_demand_scan_plugin_upload_scan_enabled', true);
$media_upload_scan_enabled = (bool) get_option('gdatacyberdefenseag_antivirus_options_on_demand_scan_media_upload_scan_enabled', true);
// We don't need to add the filters if both plugin and media upload scan are disabled.
Expand All @@ -60,7 +54,15 @@ public function __construct(
}
}

public function reconnect() {
$this->connected = false;
$this->connect();
}

public function connect() {
if ($this->connected === true) {
return;
}
$options = $this->vaas_options->get_options();
$this->vaas = new Vaas($options['vaas_url'], $this->logger, new VaasParameters(false, false));
if (! $this->vaas_options->credentials_configured()) {
Expand All @@ -82,6 +84,7 @@ public function connect() {
);
$this->vaas->connect($client_credentials_grant_authenticator->getToken());
}
$this->connected = true;
}

public function scan_post( $data, $postdata, $unsanitized_postarr ) {
Expand All @@ -102,11 +105,12 @@ public function scan_post( $data, $postdata, $unsanitized_postarr ) {
$post_content = wp_unslash($postdata['post_content']);
$stream = $this->file_system->get_resource_stream_from_string($post_content);

$this->connect();
try {
$verdict = $this->vaas->ForStream($stream);
} catch (VaasInvalidStateException $e) {
try {
$this->connect();
$this->reconnect();
$verdict = $this->vaas->ForStream($stream);
} catch (\Exception $e) {
$this->admin_notices->add_notice(esc_html__('virus scan failed', 'gdata-antivirus'));
Expand Down Expand Up @@ -154,11 +158,12 @@ public function scan_comment( $commentdata ) {

$commend_content = wp_unslash($commentdata['comment_content']);
$stream = $this->file_system->get_resource_stream_from_string($commend_content);
$this->connect();
try {
$verdict = $this->vaas->ForStream($stream);
} catch (VaasInvalidStateException $e) {
try {
$this->connect();
$this->reconnect();
$verdict = $this->vaas->ForStream($stream);
} catch (\Exception $e) {
$this->admin_notices->add_notice(esc_html__('virus scan failed', 'gdata-antivirus'));
Expand Down Expand Up @@ -219,8 +224,17 @@ public function scan_single_upload( $file ) {
}

public function scan_file( $file_path ): Verdict {
$this->connect();
try {
$verdict = $this->vaas->ForFile($file_path)->Verdict;
} catch (VaasInvalidStateException $e) {
try {
$this->reconnect();
$verdict = $this->vaas->ForFile($file_path)->Verdict;
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
return Verdict::UNKNOWN;
}
} catch (\Exception $e) {
$this->logger->debug($e->getMessage());
return Verdict::UNKNOWN;
Expand Down