Skip to content

Commit

Permalink
#294 Check if stream length is greater than 0 to avoid processing zer…
Browse files Browse the repository at this point in the history
…o byte streams (#299)

* Check if stream length is greater than 0 to avoid processing files without content
  • Loading branch information
tjololo authored Sep 5, 2023
1 parent a77266b commit b5f5fd2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Altinn.App.Api/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,19 @@ public async Task<ActionResult> Create(

using Stream fileStream = new MemoryStream();
await streamContent.CopyToAsync(fileStream);

if (fileStream.Length == 0)
{
const string errorMessage = "Invalid data provided. Error: The file is zero bytes.";
var error = new ValidationIssue
{
Code = ValidationIssueCodes.DataElementCodes.ContentTypeNotAllowed,
Severity = ValidationIssueSeverity.Error,
Description = errorMessage
};
_logger.LogError(errorMessage);
return new BadRequestObjectResult(await GetErrorDetails(new List<ValidationIssue> { error }));
}

bool parseSuccess = Request.Headers.TryGetValue("Content-Disposition", out StringValues headerValues);
string filename = parseSuccess ? DataRestrictionValidation.GetFileNameFromHeader(headerValues) : string.Empty;

Expand Down
38 changes: 38 additions & 0 deletions test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ public async Task CreateDataElement_BinaryPdf_AnalyserShouldRunOk()

Assert.Equal(HttpStatusCode.Created, response.StatusCode);
}

[Fact]
public async Task CreateDataElement_ZeroBytes_BinaryPdf_AnalyserShouldReturnBadRequest()
{
OverrideServicesForThisTest = (services) =>
{
services.AddTransient<IFileAnalyser, MimeTypeAnalyserSuccessStub>();
services.AddTransient<IFileValidator, MimeTypeValidatorStub>();
};

// Setup test data
string org = "tdd";
string app = "contributer-restriction";
HttpClient client = GetRootedClient(org, app);

Guid guid = new Guid("0fc98a23-fe31-4ef5-8fb9-dd3f479354cd");
TestData.DeleteInstance(org, app, 1337, guid);
TestData.PrepareInstance(org, app, 1337, guid);

// Setup the request
string token = PrincipalUtil.GetOrgToken("nav", "160694123");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
ByteArrayContent fileContent = await CreateBinaryContent(org, app, "zero.pdf", "application/pdf");
string url = $"/{org}/{app}/instances/1337/{guid}/data?dataType=specificFileType";
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = fileContent
};

// This is where it happens
HttpResponseMessage response = await client.SendAsync(request);

// Cleanup testdata
TestData.DeleteInstanceAndData(org, app, 1337, guid);

Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Assert.Equal("Invalid data provided. Error: The file is zero bytes.",response.Content.ReadAsStringAsync().Result);
}

[Fact]
public async Task CreateDataElement_JpgFakedAsPdf_AnalyserShouldRunAndFail()
Expand Down
Empty file.

0 comments on commit b5f5fd2

Please sign in to comment.