diff --git a/php/examples/VaasExample/GetVerdictByFile.php b/php/examples/VaasExample/GetVerdictByFile.php index 9e9759fa..7231fa18 100644 --- a/php/examples/VaasExample/GetVerdictByFile.php +++ b/php/examples/VaasExample/GetVerdictByFile.php @@ -2,7 +2,6 @@ namespace VaasExamples; -use Monolog\Logger; use VaasSdk\ClientCredentialsGrantAuthenticator; use VaasSdk\Vaas; @@ -22,4 +21,4 @@ $scanPath = getenv("SCAN_PATH"); $vaasVerdict = $vaas->ForFile($scanPath); -fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); +fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n"); diff --git a/php/examples/VaasExample/GetVerdictByHash.php b/php/examples/VaasExample/GetVerdictByHash.php index 1ae4e432..cc56de68 100644 --- a/php/examples/VaasExample/GetVerdictByHash.php +++ b/php/examples/VaasExample/GetVerdictByHash.php @@ -10,7 +10,8 @@ $authenticator = new ClientCredentialsGrantAuthenticator( getenv("CLIENT_ID"), getenv("CLIENT_SECRET"), - getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"); + getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token" +); $vaas = new Vaas( getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de" ); @@ -18,7 +19,7 @@ // EICAR $vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"); -fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); +fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n"); // SOMEFILE $vaasVerdict = $vaas->ForSha256("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8"); -fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); +fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n"); diff --git a/php/examples/VaasExample/GetVerdictByUrl.php b/php/examples/VaasExample/GetVerdictByUrl.php index ba7c6eda..be835efe 100644 --- a/php/examples/VaasExample/GetVerdictByUrl.php +++ b/php/examples/VaasExample/GetVerdictByUrl.php @@ -10,7 +10,8 @@ $authenticator = new ClientCredentialsGrantAuthenticator( getenv("CLIENT_ID"), getenv("CLIENT_SECRET"), - getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"); + getenv("TOKEN_URL") ?? "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token" +); $vaas = new Vaas( getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de" ); @@ -18,7 +19,7 @@ // EICAR $vaasVerdict = $vaas->ForUrl("https://secure.eicar.org/eicar.com"); -fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); +fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n"); // SOMEFILE $vaasVerdict = $vaas->ForUrl("https://www.gdatasoftware.com/oem/verdict-as-a-service"); -fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n"); +fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n"); diff --git a/php/src/vaas/Message/AuthRequest.php b/php/src/vaas/Message/AuthRequest.php index 4cc55335..04059cda 100644 --- a/php/src/vaas/Message/AuthRequest.php +++ b/php/src/vaas/Message/AuthRequest.php @@ -2,17 +2,14 @@ namespace VaasSdk\Message; -use Ramsey\Uuid\Rfc4122\UuidV4; - -class AuthRequest +class AuthRequest extends BaseMessage { - public Kind $kind; public string $token; public string $session_id; public function __construct(string $token, string $sessionId = "") { - $this->kind = new Kind(Kind::AUTH_REQUEST); + $this->kind = Kind::AuthRequest; $this->token = $token; $this->session_id = $sessionId; } diff --git a/php/src/vaas/Message/AuthResponse.php b/php/src/vaas/Message/AuthResponse.php index 531dff3e..b704f7db 100644 --- a/php/src/vaas/Message/AuthResponse.php +++ b/php/src/vaas/Message/AuthResponse.php @@ -2,10 +2,14 @@ namespace VaasSdk\Message; -class AuthResponse +class AuthResponse extends BaseMessage { - public Kind $kind; public bool $success; public ?string $session_id; public string $text; + + public function __construct() + { + $this->kind = Kind::AuthResponse; + } } diff --git a/php/src/vaas/Message/BaseMessage.php b/php/src/vaas/Message/BaseMessage.php new file mode 100644 index 00000000..1b8cf852 --- /dev/null +++ b/php/src/vaas/Message/BaseMessage.php @@ -0,0 +1,8 @@ +kind = $kind; + $this->guid = $uuid != null ? $uuid : UuidV4::getFactory()->uuid4()->toString(); + $this->session_id = $SessionId; + } +} diff --git a/php/src/vaas/Message/Error.php b/php/src/vaas/Message/Error.php index c94c33be..8d244e71 100644 --- a/php/src/vaas/Message/Error.php +++ b/php/src/vaas/Message/Error.php @@ -2,7 +2,7 @@ namespace VaasSdk\Message; -class Error +class Error extends BaseMessage { public string $type; @@ -12,7 +12,10 @@ class Error public ?ProblemDetails $problem_details; - public Kind $kind; + public function __construct() + { + $this->kind = Kind::Error; + } /** * @return string diff --git a/php/src/vaas/Message/Kind.php b/php/src/vaas/Message/Kind.php index 65cb55ce..6f49a0b9 100644 --- a/php/src/vaas/Message/Kind.php +++ b/php/src/vaas/Message/Kind.php @@ -2,57 +2,13 @@ namespace VaasSdk\Message; -use JsonSerializable; -use VaasSdk\Exceptions\UnknownKindException; - -class Kind implements JsonSerializable +enum Kind: string { - public const AUTH_REQUEST = "AuthRequest"; - public const AUTH_RESPONSE = "AuthResponse"; - public const VERDICT_REQUEST = "VerdictRequest"; - public const VERDICT_RESPONSE = "VerdictResponse"; - public const VERDICT_REQUEST_FOR_URL = "VerdictRequestForUrl"; - public const VERDICT_REQUEST_FOR_STREAM = "VerdictRequestForStream"; - public const ERROR = "Error"; - - private string $_kindString = ""; - - public function __construct(string $type) - { - switch ($type) { - case self::AUTH_REQUEST: - $this->_kindString = self::AUTH_REQUEST; - break; - case self::AUTH_RESPONSE: - $this->_kindString = self::AUTH_RESPONSE; - break; - case self::VERDICT_REQUEST: - $this->_kindString = self::VERDICT_REQUEST; - break; - case self::VERDICT_RESPONSE: - $this->_kindString = self::VERDICT_RESPONSE; - break; - case self::VERDICT_REQUEST_FOR_URL: - $this->_kindString = self::VERDICT_REQUEST_FOR_URL; - break; - case self::VERDICT_REQUEST_FOR_STREAM: - $this->_kindString = self::VERDICT_REQUEST_FOR_STREAM; - break; - case self::ERROR: - $this->_kindString = self::ERROR; - break; - default: - throw new UnknownKindException(); - } - } - - public function __toString() - { - return $this->_kindString; - } - - public function jsonSerialize(): string - { - return $this->_kindString; - } + case AuthRequest = "AuthRequest"; + case AuthResponse = "AuthResponse"; + case VerdictRequest = "VerdictRequest"; + case VerdictResponse = "VerdictResponse"; + case VerdictRequestForUrl = "VerdictRequestForUrl"; + case VerdictRequestForStream = "VerdictRequestForStream"; + case Error = "Error"; } diff --git a/php/src/vaas/Message/Verdict.php b/php/src/vaas/Message/Verdict.php index 0db7ea64..024d59d4 100644 --- a/php/src/vaas/Message/Verdict.php +++ b/php/src/vaas/Message/Verdict.php @@ -2,46 +2,10 @@ namespace VaasSdk\Message; -use VaasSdk\Exceptions\UnkownVerdictException; -use JsonSerializable; - -class Verdict implements JsonSerializable +enum Verdict: string { - public const MALICIOUS = "Malicious"; - public const CLEAN = "Clean"; - public const UNKNOWN = "Unknown"; - public const PUP = "Pup"; - - private string $_verdictString = ""; - - public function __construct(string $type) - { - switch ($type) { - case self::MALICIOUS: - $this->_verdictString = Verdict::MALICIOUS; - break; - case self::CLEAN: - $this->_verdictString = Verdict::CLEAN; - break; - case self::UNKNOWN: - $this->_verdictString = Verdict::UNKNOWN; - break; - case self::PUP: - $this->_verdictString = Verdict::PUP; - break; - - default: - throw new UnkownVerdictException(); - } - } - - public function __toString() - { - return $this->_verdictString; - } - - public function jsonSerialize(): string - { - return $this->_verdictString; - } + case MALICIOUS = "Malicious"; + case CLEAN = "Clean"; + case UNKNOWN = "Unknown"; + case PUP = "Pup"; } diff --git a/php/src/vaas/Message/VerdictRequest.php b/php/src/vaas/Message/VerdictRequest.php index 55091648..6f09fc3a 100644 --- a/php/src/vaas/Message/VerdictRequest.php +++ b/php/src/vaas/Message/VerdictRequest.php @@ -2,20 +2,13 @@ namespace VaasSdk\Message; -use Ramsey\Uuid\Rfc4122\UuidV4; - -class VerdictRequest +class VerdictRequest extends BaseVerdictRequest { public string $sha256; - public string $guid; - public Kind $kind; - public string $session_id; public function __construct(string $sha256, string $uuid = null, string $SessionId) { - $this->kind = new Kind(Kind::VERDICT_REQUEST); - $this->guid = $uuid != null ? $uuid : UuidV4::getFactory()->uuid4()->toString(); + parent::__construct(Kind::VerdictRequest, $uuid, $SessionId); $this->sha256 = $sha256; - $this->session_id = $SessionId; } } diff --git a/php/src/vaas/Message/VerdictRequestForStream.php b/php/src/vaas/Message/VerdictRequestForStream.php index 96fbb1e9..4312f9ae 100644 --- a/php/src/vaas/Message/VerdictRequestForStream.php +++ b/php/src/vaas/Message/VerdictRequestForStream.php @@ -4,16 +4,10 @@ use Ramsey\Uuid\Rfc4122\UuidV4; -class VerdictRequestForStream +class VerdictRequestForStream extends BaseVerdictRequest { - public string $guid; - public Kind $kind; - public string $session_id; - public function __construct(string $SessionId, string $uuid = null) { - $this->kind = new Kind(Kind::VERDICT_REQUEST_FOR_STREAM); - $this->guid = $uuid != null ? $uuid : UuidV4::getFactory()->uuid4()->toString(); - $this->session_id = $SessionId; + parent::__construct(Kind::VerdictRequestForStream, $uuid, $SessionId); } } diff --git a/php/src/vaas/Message/VerdictRequestForUrl.php b/php/src/vaas/Message/VerdictRequestForUrl.php index 4c494737..c4def769 100644 --- a/php/src/vaas/Message/VerdictRequestForUrl.php +++ b/php/src/vaas/Message/VerdictRequestForUrl.php @@ -4,18 +4,13 @@ use Ramsey\Uuid\Rfc4122\UuidV4; -class VerdictRequestForUrl +class VerdictRequestForUrl extends BaseVerdictRequest { public string $url; - public string $guid; - public Kind $kind; - public string $session_id; public function __construct(string $url, string $uuid = null, string $SessionId) { - $this->kind = new Kind(Kind::VERDICT_REQUEST_FOR_URL); - $this->guid = $uuid != null ? $uuid : UuidV4::getFactory()->uuid4()->toString(); + parent::__construct(Kind::VerdictRequestForUrl, $uuid, $SessionId); $this->url = $url; - $this->session_id = $SessionId; } } diff --git a/php/src/vaas/Message/VerdictResponse.php b/php/src/vaas/Message/VerdictResponse.php index 956829f0..75b5cea7 100644 --- a/php/src/vaas/Message/VerdictResponse.php +++ b/php/src/vaas/Message/VerdictResponse.php @@ -2,7 +2,7 @@ namespace VaasSdk\Message; -class VerdictResponse +class VerdictResponse extends BaseMessage { public Verdict $verdict; public ?string $url; diff --git a/php/src/vaas/Vaas.php b/php/src/vaas/Vaas.php index bb00593e..f7698f2c 100644 --- a/php/src/vaas/Vaas.php +++ b/php/src/vaas/Vaas.php @@ -27,6 +27,8 @@ use VaasSdk\Message\VerdictRequestForUrl; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; +use VaasSdk\Message\BaseMessage; +use VaasSdk\Message\BaseVerdictRequest; use VaasSdk\Message\VaasVerdict; use WebSocket\BadOpcodeException; @@ -78,6 +80,8 @@ public function Connect( * Gets verdict by hashstring * * @param string $hashString the hash to get the verdict for + * @param bool $useCache enables verdict cache on the server + * @param bool $useShed enables hash-lookup on the server * @param string $uuid unique identifier * * @throws Exceptions\InvalidSha256Exception @@ -85,20 +89,41 @@ public function Connect( * * @return VaasVerdict the verdict */ - public function ForSha256(string $hashString, string $uuid = null): VaasVerdict + public function ForSha256WithFlags(string $hashString, bool $useCache = true, bool $useShed = true, string $uuid = null): VaasVerdict { if ($this->_logger != null) - $this->_logger->debug("ForSha256", ["Sha256" => $hashString]); + $this->_logger->debug("ForSha256WithFlags", ["Sha256" => $hashString]); $sha256 = Sha256::TryFromString($hashString); - return new VaasVerdict($this->_verdictResponseForSha256($sha256, $uuid)); + return new VaasVerdict($this->_verdictResponseForSha256($sha256, $useCache, $useShed, $uuid)); + } + + /** + * Gets verdict by hashstring + * + * @param string $hashString the hash to get the verdict for + * @param string $uuid unique identifier + * + * @throws Exceptions\InvalidSha256Exception + * @throws Exceptions\TimeoutException + * + * @return VaasVerdict the verdict + */ + public function ForSha256(string $hashString, string $uuid = null): VaasVerdict + { + if ($this->_logger != null) + $this->_logger->debug("ForSha256", ["Sha256" => $hashString]); + + return $this->ForSha256WithFlags($hashString, true, true, $uuid); } /** * Gets verdict by url * - * @param string $url url to get the verdict for + * @param string|null $url url to get the verdict for + * @param bool $useCache enables verdict cache on the server + * @param bool $useShed enables hash-lookup on the server * @param string|null $uuid unique identifier * * @return VaasVerdict the verdict @@ -106,21 +131,41 @@ public function ForSha256(string $hashString, string $uuid = null): VaasVerdict * @throws TimeoutException * @throws InvalidArgumentException */ - public function ForUrl(?string $url, string $uuid = null): VaasVerdict + public function ForUrlWithFlags(?string $url, bool $useCache = true, bool $useShed = true, string $uuid = null): VaasVerdict { - if ($this->_logger != null) $this->_logger->debug("ForUrl", ["URL:" => $url]); + if ($this->_logger != null) $this->_logger->debug("ForUrlWithFlags", ["URL:" => $url]); if (!filter_var($url, FILTER_VALIDATE_URL)) { throw new \InvalidArgumentException("Url is not valid"); } - return new VaasVerdict($this->_verdictResponseForUrl($url, $uuid)); + return new VaasVerdict($this->_verdictResponseForUrl($url, $useCache, $useShed, $uuid)); + } + + /** + * Gets verdict by url + * + * @param string|null $url url to get the verdict for + * @param string|null $uuid unique identifier + * + * @return VaasVerdict the verdict + * + * @throws TimeoutException + * @throws InvalidArgumentException + */ + public function ForUrl(?string $url, string $uuid = null): VaasVerdict + { + if ($this->_logger != null) $this->_logger->debug("ForUrl", ["URL:" => $url]); + + return $this->ForUrlWithFlags($url, true, true, $uuid); } /** * Gets verdict by file * * @param string $path the path to get the verdict for + * @param bool $useCache enables verdict cache on the server + * @param bool $useShed enables hash-lookup on the server * @param bool $upload should the file be uploaded if initial verdict is unknown * @param string $uuid unique identifier * @@ -131,16 +176,16 @@ public function ForUrl(?string $url, string $uuid = null): VaasVerdict * * @return VaasVerdict the verdict */ - public function ForFile(string $path, bool $upload = true, string $uuid = null): VaasVerdict + public function ForFileWithFlags(string $path, bool $useCache = true, bool $useShed = true, $upload = true, string $uuid = null): VaasVerdict { if ($this->_logger != null) - $this->_logger->debug("ForFile", ["File" => $path]); + $this->_logger->debug("ForFileWithFlags", ["File" => $path]); $sha256 = Sha256::TryFromFile($path); if ($this->_logger != null) $this->_logger->debug("Calculated Hash", ["Sha256" => $sha256]); - $verdictResponse = $this->_verdictResponseForSha256($sha256, $uuid); + $verdictResponse = $this->_verdictResponseForSha256($sha256, $useCache, $useShed, $uuid); if ($verdictResponse->verdict == Verdict::UNKNOWN && $upload === true) { if ($this->_logger != null) $this->_logger->debug("UploadToken", ["UploadToken" => $verdictResponse->upload_token]); @@ -160,6 +205,35 @@ public function ForFile(string $path, bool $upload = true, string $uuid = null): } /** + * Gets verdict by file + * + * @param string $path the path to get the verdict for + * @param bool $upload should the file be uploaded if initial verdict is unknown + * @param string $uuid unique identifier + * + * @throws Exceptions\TimeoutException + * @throws Exceptions\FileDoesNotExistException + * @throws Exceptions\InvalidSha256Exception + * @throws Exceptions\UploadFailedException + * + * @return VaasVerdict the verdict + */ + public function ForFile(string $path, $upload = true, string $uuid = null): VaasVerdict + { + if ($this->_logger != null) + $this->_logger->debug("ForFile", ["File" => $path]); + + return $this->ForFileWithFlags($path, true, true, $upload, $uuid); + } + + /** + * Gets verdict by stream + * + * @param string $path the path to get the verdict for + * @param bool $upload should the file be uploaded if initial verdict is unknown + * @param string $uuid unique identifier + * + * @throws JsonMapper_Exception * @throws VaasClientException * @throws TimeoutException @@ -169,13 +243,13 @@ public function ForFile(string $path, bool $upload = true, string $uuid = null): * @throws GuzzleException * @throws UploadFailedException */ - public function ForStream(Stream $stream, string $uuid = null): VaasVerdict + public function ForStreamWithFlags(Stream $stream, bool $useCache = true, bool $useShed = true, string $uuid = null): VaasVerdict { if ($uuid == null) { $uuid = UuidV4::getFactory()->uuid4()->toString(); } - $verdictResponse = $this->_verdictResponseForStream($uuid); + $verdictResponse = $this->_verdictResponseForStream($useCache, $useShed, $uuid); if ($verdictResponse->verdict != Verdict::UNKNOWN) { throw new VaasServerException("Server returned verdict without receiving content."); @@ -194,6 +268,27 @@ public function ForStream(Stream $stream, string $uuid = null): VaasVerdict return new VaasVerdict($verdictResponse); } + /** + * Gets verdict by stream + * + * @param string $path the path to get the verdict for + * @param bool $upload should the file be uploaded if initial verdict is unknown + * @param string $uuid unique identifier + * + * @throws JsonMapper_Exception + * @throws VaasClientException + * @throws TimeoutException + * @throws VaasServerException + * @throws BadOpcodeException + * @throws VaasInvalidStateException + * @throws GuzzleException + * @throws UploadFailedException + */ + public function ForStream(Stream $stream, string $uuid = null): VaasVerdict + { + return $this->ForStreamWithFlags($stream, true, true, $uuid); + } + /** * @return AuthResponse * @throws VaasConnectionClosedException @@ -228,11 +323,14 @@ private function _waitForAuthResponse(): AuthResponse if ($result != null) { if ($this->_logger != null) $this->_logger->debug("Result", json_decode($result, true)); - - $resultObject = json_decode($result); - if ($resultObject->kind == Kind::AUTH_RESPONSE) { + $genericObject = \json_decode($result); + $resultObject = (new JsonMapper())->map( + $genericObject, + new BaseMessage() + ); + if ($resultObject->kind == Kind::AuthResponse) { $authResponse = (new JsonMapper())->map( - $resultObject, + $genericObject, new AuthResponse() ); if ($this->_logger != null) @@ -242,10 +340,10 @@ private function _waitForAuthResponse(): AuthResponse } return $authResponse; } - if ($resultObject->kind == Kind::ERROR) { + if ($resultObject->kind == Kind::Error) { try { $errorResponse = (new JsonMapper())->map( - $resultObject, + $genericObject, new Error() ); } catch (JsonMapper_Exception $e) { @@ -294,7 +392,11 @@ private function _waitForVerdict(string $guid): VerdictResponse if ($this->_logger != null) $this->_logger->debug("Result", json_decode($result, true)); $resultObject = json_decode($result); - if ($resultObject->kind == Kind::ERROR) { + $baseMessage = (new JsonMapper())->map( + $resultObject, + new BaseMessage() + ); + if ($baseMessage->kind == Kind::Error) { try { $errorResponse = (new JsonMapper())->map( $resultObject, @@ -306,17 +408,20 @@ private function _waitForVerdict(string $guid): VerdictResponse } $this->_handleWebSocketErrorResponse($errorResponse); } - if (!isset($resultObject->guid) || !isset($resultObject->kind)) { + if ($baseMessage->kind != Kind::VerdictResponse) { continue; } - if ($resultObject->kind != Kind::VERDICT_RESPONSE) { + + $verdictResponse = (new JsonMapper())->map( + $resultObject, + new VerdictResponse() + ); + if (!isset($verdictResponse->guid) || !isset($verdictResponse->kind)) { continue; } - if ($resultObject->guid == $guid) { - return (new JsonMapper())->map( - $resultObject, - new VerdictResponse() - ); + + if ($verdictResponse->guid == $guid) { + return $verdictResponse; } } sleep(1); @@ -346,7 +451,7 @@ private function _handleWebSocketErrorResponse(Error $errorResponse): void * * @return VerdictResponse */ - private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null): VerdictResponse + private function _verdictResponseForSha256(Sha256 $sha256, bool $useCache, bool $useShed, string $uuid = null): VerdictResponse { if ($this->_logger != null) $this->_logger->debug("_verdictResponseForSha256"); @@ -357,6 +462,8 @@ private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null): $websocket = $this->_vaasConnection->GetAuthenticatedWebsocket(); $request = new VerdictRequest(strtolower($sha256), $uuid, $this->_vaasConnection->SessionId); + $request->UseCache = $useCache; + $request->UseShed = $useShed; $websocket->send(json_encode($request)); if ($this->_logger != null) @@ -370,7 +477,7 @@ private function _verdictResponseForSha256(Sha256 $sha256, string $uuid = null): * * @return VerdictResponse */ - private function _verdictResponseForUrl(string $url, string $uuid = null): VerdictResponse + private function _verdictResponseForUrl(string $url, bool $useCache, bool $useShed, string $uuid = null): VerdictResponse { if ($this->_logger != null) $this->_logger->debug("_verdictResponseForUrl"); @@ -381,6 +488,8 @@ private function _verdictResponseForUrl(string $url, string $uuid = null): Verdi $websocket = $this->_vaasConnection->GetAuthenticatedWebsocket(); $request = new VerdictRequestForUrl($url, $uuid, $this->_vaasConnection->SessionId); + $request->UseCache = $useCache; + $request->UseShed = $useShed; $websocket->send(json_encode($request)); if ($this->_logger != null) @@ -397,7 +506,7 @@ private function _verdictResponseForUrl(string $url, string $uuid = null): Verdi * @throws BadOpcodeException * @throws VaasInvalidStateException */ - private function _verdictResponseForStream(string $uuid = null): VerdictResponse + private function _verdictResponseForStream(bool $useCache, bool $useShed, string $uuid = null): VerdictResponse { if ($this->_logger != null) $this->_logger->debug("_verdictResponseForStream"); @@ -408,6 +517,8 @@ private function _verdictResponseForStream(string $uuid = null): VerdictResponse $websocket = $this->_vaasConnection->GetAuthenticatedWebsocket(); $request = new VerdictRequestForStream($this->_vaasConnection->SessionId, $uuid); + $request->UseCache = $useCache; + $request->UseShed = $useShed; $websocket->send(json_encode($request)); if ($this->_logger != null) diff --git a/php/tests/vaas/VaasTest.php b/php/tests/vaas/VaasTest.php index b7f3e5c1..d2cec3f2 100644 --- a/php/tests/vaas/VaasTest.php +++ b/php/tests/vaas/VaasTest.php @@ -143,6 +143,19 @@ public function testForSha256MaliciousSha256_GetsMaliciousResponse(): void $this->assertEqualsIgnoringCase(self::MALICIOUS_HASH, $verdict->Sha256); } + public function testForSha256MaliciousSha256WithFlag_BothFlagsFalse_GetsMaliciousResponse(): void + { + $uuid = $this->getUuid(); + + $vaas = new Vaas($_ENV["VAAS_URL"], $this->_getDebugLogger()); + $vaas->Connect($this->getClientCredentialsGrantAuthenticator()->getToken()); + $verdict = $vaas->ForSha256WithFlags(self::MALICIOUS_HASH, false, false, $uuid); + + $this->assertEquals(Verdict::MALICIOUS, $verdict->Verdict); + $this->assertEquals($uuid, $verdict->Guid); + $this->assertEqualsIgnoringCase(self::MALICIOUS_HASH, $verdict->Sha256); + } + public function testForMultipleMaliciousSha256_GetsMaliciousResponses(): void { $vaas = new Vaas($_ENV["VAAS_URL"], $this->_getDebugLogger()); @@ -268,6 +281,27 @@ public function testForMultipleUnknownFiles_GetsUnknownResponses(): void $this->assertEqualsIgnoringCase($unknownHash3, $verdict3->Sha256); } + public function testForFileWithFlagsCleanFile_GetsCleanResponse(): void + { + $uuid = $this->getUuid(); + + $cleanFile = pack("nvc*", 0x65, 0x0a, 0x67, 0x0a, 0x65, 0x0a, 0x62, 0x0a); + $tmp = tmpfile(); + fwrite($tmp, $cleanFile); + fseek($tmp, 0); + $sha256 = Sha256::TryFromFile(stream_get_meta_data($tmp)['uri']); + + $vaas = new Vaas($_ENV["VAAS_URL"], $this->_getDebugLogger()); + $vaas->Connect($this->getClientCredentialsGrantAuthenticator()->getToken()); + $verdict = $vaas->ForFileWithFlags(stream_get_meta_data($tmp)['uri'], false, false, true, $uuid); + + $this->assertEquals(Verdict::CLEAN, $verdict->Verdict); + $this->assertEquals($uuid, $verdict->Guid); + $this->assertEqualsIgnoringCase($sha256, $verdict->Sha256); + + fclose($tmp); + } + public function testForFileCleanFile_GetsCleanResponse(): void { $uuid = $this->getUuid(); @@ -349,6 +383,18 @@ public function testForEmptyFile_GetsCleanResponse(): void fclose($tmp); } + public function testForUrlWithFlagsMaliciousUrl_GetsMaliciousResponse(): void + { + $uuid = $this->getUuid(); + + $vaas = new Vaas($_ENV["VAAS_URL"], $this->_getDebugLogger()); + $vaas->Connect($this->getClientCredentialsGrantAuthenticator()->getToken()); + $verdict = $vaas->ForUrlWithFlags(self::MALICIOUS_URL, false, false, $uuid); + + $this->assertEquals(Verdict::MALICIOUS, $verdict->Verdict); + $this->assertEquals($uuid, $verdict->Guid); + } + public function testForUrlMaliciousUrl_GetsMaliciousResponse(): void { $uuid = $this->getUuid(); @@ -430,6 +476,20 @@ public function testForUrl_WithStatus4xx_ThrowsVaasClientException() $this->_getDebugLogger()->info("Verdict for URL " . $invalidUrl . " is " . $verdict->Verdict); } + public function testForStreamWithFlags_WithEicarString_ReturnsMalicious() + { + $vaas = new Vaas($_ENV["VAAS_URL"], $this->_getDebugLogger()); + $vaas->Connect($this->getClientCredentialsGrantAuthenticator()->getToken()); + $eicar = "X5O!P%@AP[4\\PZX54(P^)7CC)7}\$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!\$H+H*"; + $stream = fopen(sprintf('data://text/plain,%s', $eicar), 'r'); + rewind($stream); + $eicarStream = new Stream($stream); + + $verdict = $vaas->ForStreamWithFlags($eicarStream, false, false); + + $this->assertEquals(Verdict::MALICIOUS, $verdict->Verdict); + } + /** * @throws JsonMapper_Exception * @throws VaasClientException