Skip to content

Commit

Permalink
file scan for screening and licensing (#1551)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

-spdbt-3146(ook up the file virus scan service with our screening and
licensing app.)
  • Loading branch information
esdd1995 authored Oct 12, 2024
2 parents da5c4a2 + 9bd1896 commit 3a6d77d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Spd.Manager.Licence;
using Spd.Utilities.FileScanning;
using Spd.Utilities.LogonUser;
using Spd.Utilities.Recaptcha;
using Spd.Utilities.Shared.Exceptions;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Security.Principal;

namespace Spd.Presentation.Licensing.Controllers;
[ApiController]
public class LicenceAppDocumentController : SpdLicenceControllerBase
{
private readonly IMediator _mediator;
private readonly IConfiguration _configuration;
private readonly IFileScanProvider _fileScanProvider;
private readonly IPrincipal _currentUser;

public LicenceAppDocumentController(IMediator mediator, IDistributedCache cache,
IDataProtectionProvider dpProvider,
IPrincipal currentUser,
IRecaptchaVerificationService recaptchaVerificationService,
IConfiguration configuration) : base(cache, dpProvider, recaptchaVerificationService, configuration)
IConfiguration configuration,
IFileScanProvider fileScanProvider) : base(cache, dpProvider, recaptchaVerificationService, configuration)
{
_mediator = mediator;
_configuration = configuration;
_fileScanProvider = fileScanProvider;
_currentUser = currentUser;
}

Expand All @@ -42,8 +46,9 @@ public LicenceAppDocumentController(IMediator mediator, IDistributedCache cache,
public async Task<IEnumerable<LicenceAppDocumentResponse>> UploadLicenceAppFiles([FromForm][Required] LicenceAppDocumentUploadRequest fileUploadRequest, [FromRoute] Guid licenceAppId, CancellationToken ct)
{
VerifyFiles(fileUploadRequest.Documents);
await FileVirusScanAsync(fileUploadRequest.Documents, ct);

if (User.HasClaim("Policy", "OnlyBcsc"))
if (_currentUser.GetIdentityProvider() == null) //bcsc identity provider is null
{
var applicantInfo = _currentUser.GetBcscUserIdentityInfo();
return await _mediator.Send(new CreateDocumentInTransientStoreCommand(fileUploadRequest, applicantInfo.Sub, licenceAppId), ct);
Expand All @@ -64,6 +69,7 @@ public async Task<IEnumerable<LicenceAppDocumentResponse>> UploadLicenceAppFiles
public async Task<Guid> UploadFilesToCache([FromForm][Required] LicenceAppDocumentUploadRequest fileUploadRequest, CancellationToken ct)
{
VerifyFiles(fileUploadRequest.Documents);
await FileVirusScanAsync(fileUploadRequest.Documents, ct);

CreateDocumentInCacheCommand command = new(fileUploadRequest);
var newFileInfos = await _mediator.Send(command, ct);
Expand Down Expand Up @@ -104,11 +110,22 @@ public async Task<Guid> UploadLicenceAppFilesAnonymous([FromForm][Required] Lice
{
await VerifyKeyCode();
VerifyFiles(fileUploadRequest.Documents);
await FileVirusScanAsync(fileUploadRequest.Documents, ct);

CreateDocumentInCacheCommand command = new(fileUploadRequest);
var newFileInfos = await _mediator.Send(command, ct);
Guid fileKeyCode = Guid.NewGuid();
await Cache.SetAsync(fileKeyCode.ToString(), newFileInfos, TimeSpan.FromMinutes(30), ct);
return fileKeyCode;
}

protected async Task FileVirusScanAsync(IList<IFormFile> documents, CancellationToken ct)
{
foreach (IFormFile file in documents)
{
var result = await _fileScanProvider.ScanAsync(file.OpenReadStream(), ct);
if (result.Result != ScanResult.Clean)
throw new ApiException(HttpStatusCode.BadRequest, "The uploaded file is not clean.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="..\Spd.Manager.Common\Spd.Manager.Common.csproj" />
<ProjectReference Include="..\Spd.Manager.Licence\Spd.Manager.Licence.csproj" />
<ProjectReference Include="..\Spd.Manager.Payment\Spd.Manager.Payment.csproj" />
<ProjectReference Include="..\Spd.Utilities.FileScanning\Spd.Utilities.FileScanning.csproj" />
<ProjectReference Include="..\Spd.Utilities.Hosting\Spd.Utilities.Hosting.csproj" />
<ProjectReference Include="..\Spd.Utilities.LogonUser\Spd.Utilities.LogonUser.csproj" />
<ProjectReference Include="..\Spd.Utilities.Payment\Spd.Utilities.Payment.csproj" />
Expand Down
15 changes: 13 additions & 2 deletions src/Spd.Presentation.Screening/Controllers/ApplicantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Spd.Manager.Screening;
using Spd.Manager.Shared;
using Spd.Presentation.Screening.Configurations;
using Spd.Utilities.FileScanning;
using Spd.Utilities.LogonUser;
using Spd.Utilities.Recaptcha;
using Spd.Utilities.Shared;
Expand All @@ -23,17 +24,19 @@ public class ApplicantController : SpdControllerBase
private readonly IPrincipal _currentUser;
private readonly IRecaptchaVerificationService _verificationService;
private readonly IConfiguration _configuration;

private readonly IFileScanProvider _fileScanProvider;

public ApplicantController(IMediator mediator,
IPrincipal currentUser,
IRecaptchaVerificationService verificationService,
IConfiguration configuration)
IConfiguration configuration,
IFileScanProvider fileScanProvider)
{
_mediator = mediator;
_currentUser = currentUser;
_verificationService = verificationService;
_configuration = configuration;
_fileScanProvider = fileScanProvider;
}

#region application-invites
Expand Down Expand Up @@ -219,6 +222,7 @@ public async Task<IEnumerable<ApplicantAppFileCreateResponse>> UploadApplicantAp
{
throw new ApiException(HttpStatusCode.BadRequest, $"{file.Name} exceeds maximum supported file size {fileUploadConfig.MaxFileSizeMB} MB.");
}
await FileVirusScanAsync(file, ct);
}
if (fileUploadRequest.FileType != FileTypeCode.ApplicantInformation && fileUploadRequest.Files.Count > 1)
{
Expand Down Expand Up @@ -248,6 +252,13 @@ public async Task<FileStreamResult> DownloadFileTemplate([FromRoute] Guid applic

}
#endregion

protected async Task FileVirusScanAsync(IFormFile document, CancellationToken ct)
{
var result = await _fileScanProvider.ScanAsync(document.OpenReadStream(), ct);
if (result.Result != ScanResult.Clean)
throw new ApiException(HttpStatusCode.BadRequest, "The uploaded file is not clean.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using Microsoft.AspNetCore.Mvc;
using Spd.Manager.Screening;
using Spd.Manager.Shared;
using Spd.Utilities.FileScanning;
using Spd.Utilities.LogonUser;
using Spd.Utilities.Shared;
using Spd.Utilities.Shared.Exceptions;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Globalization;
using System.Net;
using System.Security.Principal;
using System.Text;
using System.Text.Json;
Expand All @@ -26,20 +28,23 @@ public class ApplicationController : SpdControllerBase
private readonly IConfiguration _configuration;
private readonly IPrincipal _currentUser;
private readonly ILogger<ApplicationController> _logger;
private readonly IFileScanProvider _fileScanProvider;

public ApplicationController(IMediator mediator,
IValidator<ApplicationCreateRequest> appCreateRequestValidator,
IValidator<ApplicationCreateRequestFromBulk> appCreateRequestFromBulkValidator,
IConfiguration configuration,
IPrincipal currentUser,
ILogger<ApplicationController> logger)
ILogger<ApplicationController> logger,
IFileScanProvider fileScanProvider)
{
_mediator = mediator;
_appCreateRequestValidator = appCreateRequestValidator;
_appCreateRequestFromBulkValidator = appCreateRequestFromBulkValidator;
_configuration = configuration;
_currentUser = currentUser;
_logger = logger;
_fileScanProvider = fileScanProvider;
}

#region application-invites
Expand Down Expand Up @@ -174,6 +179,7 @@ public async Task<BulkUploadCreateResponse> BulkUpload([FromForm][Required] Bulk
var userId = this.HttpContext.User.GetUserId();
if (userId == null) throw new ApiException(System.Net.HttpStatusCode.Unauthorized);

await FileVirusScanAsync(bulkUploadRequest.File, ct);
//validation file
string fileName = bulkUploadRequest.File.FileName;
string exe = fileName.Split(".").Last();
Expand Down Expand Up @@ -363,7 +369,7 @@ public async Task<ActionResult> IdentityVerify([FromRoute] Guid applicationId, [
/// <returns></returns>
[Route("api/orgs/{orgId}/application")]
[HttpPost]
public async Task<ApplicationCreateResponse> AddApplication([FromForm][Required] CreateApplication createApplication, [FromRoute] Guid orgId)
public async Task<ApplicationCreateResponse> AddApplication([FromForm][Required] CreateApplication createApplication, [FromRoute] Guid orgId, CancellationToken ct)
{

bool isPSSO = false;
Expand All @@ -384,6 +390,7 @@ public async Task<ApplicationCreateResponse> AddApplication([FromForm][Required]
{
if (createApplication.ConsentFormFile == null)
throw new ApiException(System.Net.HttpStatusCode.BadRequest, "The consent file must be supplied.");
await FileVirusScanAsync(createApplication.ConsentFormFile, ct);
}

var userId = this.HttpContext.User.GetUserId();
Expand Down Expand Up @@ -743,6 +750,13 @@ private ClearanceAccessListSortBy GetClearanceSortBy(string? sortby)
};
}
#endregion

protected async Task FileVirusScanAsync(IFormFile document, CancellationToken ct)
{
var result = await _fileScanProvider.ScanAsync(document.OpenReadStream(), ct);
if (result.Result != ScanResult.Clean)
throw new ApiException(HttpStatusCode.BadRequest, "The uploaded file is not clean.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="..\Spd.Manager.Common\Spd.Manager.Common.csproj" />
<ProjectReference Include="..\Spd.Manager.Payment\Spd.Manager.Payment.csproj" />
<ProjectReference Include="..\Spd.Manager.Screening\Spd.Manager.Screening.csproj" />
<ProjectReference Include="..\Spd.Utilities.FileScanning\Spd.Utilities.FileScanning.csproj" />
<ProjectReference Include="..\Spd.Utilities.Hosting\Spd.Utilities.Hosting.csproj" />
<ProjectReference Include="..\Spd.Utilities.LogonUser\Spd.Utilities.LogonUser.csproj" />
<ProjectReference Include="..\Spd.Utilities.Recaptcha\Spd.Utilities.Recaptcha.csproj" />
Expand Down

0 comments on commit 3a6d77d

Please sign in to comment.