diff --git a/src/Altinn.App.Api/Controllers/DataController.cs b/src/Altinn.App.Api/Controllers/DataController.cs index 268637804..4f08e29f2 100644 --- a/src/Altinn.App.Api/Controllers/DataController.cs +++ b/src/Altinn.App.Api/Controllers/DataController.cs @@ -163,7 +163,19 @@ public async Task 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 { error })); + } + bool parseSuccess = Request.Headers.TryGetValue("Content-Disposition", out StringValues headerValues); string filename = parseSuccess ? DataRestrictionValidation.GetFileNameFromHeader(headerValues) : string.Empty; diff --git a/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs b/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs index 0a72fb397..cb73343c7 100644 --- a/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs +++ b/test/Altinn.App.Api.Tests/Controllers/DataControllerTests.cs @@ -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(); + services.AddTransient(); + }; + + // 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() diff --git a/test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/_testdata_/zero.pdf b/test/Altinn.App.Api.Tests/Data/apps/tdd/contributer-restriction/_testdata_/zero.pdf new file mode 100644 index 000000000..e69de29bb