Skip to content

Commit

Permalink
28 reduce scans on every page load (#29)
Browse files Browse the repository at this point in the history
* checkout the svn into a subdirectory

* don't connect on every single page view

we should only connect, when the first actual scan is requested. not on every page load.

---------

Co-authored-by: PT-ATA No One <[email protected]>
  • Loading branch information
unglaublicherdude and ata-no-one authored Oct 18, 2024
1 parent bc4aa38 commit 160a1a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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

0 comments on commit 160a1a4

Please sign in to comment.