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

Dotnet implementation of #390 #392

Merged
merged 2 commits into from
Mar 12, 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
15 changes: 15 additions & 0 deletions dotnet/Vaas/src/Vaas/Messages/Detection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace Vaas.Messages;

public class Detection
{
[JsonPropertyName("engine")]
public int? Engine { get; init; }

[JsonPropertyName("fileName")]
public string FileName { get; init; }

Check warning on line 11 in dotnet/Vaas/src/Vaas/Messages/Detection.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'FileName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 11 in dotnet/Vaas/src/Vaas/Messages/Detection.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'FileName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("virus")]
public string Virus { get; init; }

Check warning on line 14 in dotnet/Vaas/src/Vaas/Messages/Detection.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'Virus' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 14 in dotnet/Vaas/src/Vaas/Messages/Detection.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'Virus' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
12 changes: 12 additions & 0 deletions dotnet/Vaas/src/Vaas/Messages/LibMagic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace Vaas.Messages;

public class LibMagic
{
[JsonPropertyName("fileType")]
public string FileType { get; init; }

Check warning on line 8 in dotnet/Vaas/src/Vaas/Messages/LibMagic.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'FileType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in dotnet/Vaas/src/Vaas/Messages/LibMagic.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'FileType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonPropertyName("mimeType")]
public string MimeType { get; init; }

Check warning on line 11 in dotnet/Vaas/src/Vaas/Messages/LibMagic.cs

View workflow job for this annotation

GitHub Actions / Build & Test C# SDK (8.0.x)

Non-nullable property 'MimeType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
8 changes: 7 additions & 1 deletion dotnet/Vaas/src/Vaas/Messages/VerdictResponse.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using CommunityToolkit.Diagnostics;
Expand Down Expand Up @@ -32,9 +33,14 @@ public VerdictResponse(string sha256, Verdict verdict)

[JsonPropertyName("upload_token")]
public string? UploadToken { get; init; }

[JsonPropertyName("detections")]
public List<Detection>? Detections { get; init; }

[JsonPropertyName("libMagic")]
public LibMagic? LibMagic { get; init; }

[MemberNotNullWhen(true, nameof(Sha256), nameof(Guid))]
public bool IsValid => !string.IsNullOrWhiteSpace(Sha256)
&& !string.IsNullOrWhiteSpace(Guid);

}
6 changes: 6 additions & 0 deletions dotnet/Vaas/src/Vaas/VaasVerdict.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace Vaas.Messages;

public class VaasVerdict
Expand All @@ -6,8 +8,12 @@ public VaasVerdict(VerdictResponse verdictResponse)
{
Sha256 = verdictResponse.Sha256 ?? "";
Verdict = verdictResponse.Verdict;
Detections = verdictResponse.Detections;
LibMagic = verdictResponse.LibMagic;
}

public string Sha256 { get; init; }
public Verdict Verdict { get; init; }
public List<Detection>? Detections { get; init; }
public LibMagic? LibMagic { get; init; }
}
33 changes: 33 additions & 0 deletions dotnet/Vaas/test/Vaas.Test/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ private async Task<Vaas> AuthenticateWithCredentials()
{ "VerdictAsAService:Credentials:GrantType", "ClientCredentials" },
{ "VerdictAsAService:Credentials:ClientId", AuthenticationEnvironment.ClientId },
{ "VerdictAsAService:Credentials:ClientSecret", AuthenticationEnvironment.ClientSecret },
{ "VerdictAsAService:UseCache", "false" }
});
ServiceCollectionTools.Output(_output, services);
var provider = services.BuildServiceProvider();
Expand Down Expand Up @@ -304,4 +305,36 @@ public async Task Connect_WithResourceOwnerPasswordGrantAuthenticator()
var vaas = provider.GetRequiredService<IVaas>();
await vaas.Connect(CancellationToken.None);
}

[Fact]
public async Task ForStream_WithEicarUrl_ReturnsMaliciousWithDetectionsAndMimeType()
{
var vaas = await AuthenticateWithCredentials();
var url = new Uri("https://secure.eicar.org/eicar.com.txt");
var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None);
var targetStream = await response.Content.ReadAsStreamAsync();

var verdict = await vaas.ForStreamAsync(targetStream, CancellationToken.None);

Assert.Equal(Verdict.Malicious, verdict.Verdict);
Assert.NotNull(verdict.LibMagic);
Assert.NotNull(verdict.Detections);
Assert.Equal("text/plain", verdict.LibMagic.MimeType);
Assert.Contains(verdict.Detections, detection => detection.Virus == "EICAR_TEST_FILE");
}

[Fact]
public async Task ForUrl_WithEicarUrl_ReturnsMaliciousWithDetectionAndMimeType()
{
var vaas = await AuthenticateWithCredentials();
var uri = new Uri("https://secure.eicar.org/eicar.com");

var verdict = await vaas.ForUrlAsync(uri, CancellationToken.None);

Assert.Equal(Verdict.Malicious, verdict.Verdict);
Assert.NotNull(verdict.LibMagic);
Assert.NotNull(verdict.Detections);
Assert.Equal("text/plain", verdict.LibMagic.MimeType);
Assert.Contains(verdict.Detections, detection => detection.Virus == "EICAR_TEST_FILE");
}
}
Loading