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

#294 Check if stream length is greater than 0 to avoid processing zero byte streams #299

Merged
merged 2 commits into from
Sep 5, 2023
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
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