Skip to content
Open
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
27 changes: 22 additions & 5 deletions src/Altinn.FileAnalyzers/MimeType/MimeTypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Altinn.FileAnalyzers.MimeType;

/// <summary>
/// Validates that the file is of the allowed content type
/// Validates that the file is of the allowed content and uploaded file extension is the same as filename extension.
/// </summary>
public class MimeTypeValidator : IFileValidator
{
Expand All @@ -16,7 +16,7 @@ public class MimeTypeValidator : IFileValidator
public string Id { get; private set; } = "mimeTypeValidator";

/// <summary>
/// Validates that the file is of the allowed content type.
/// Validates that the file is of the allowed content type and uploaded file extension is the same as filename extension.
/// </summary>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously. Suppressed because of the interface.
public async Task<(bool Success, IEnumerable<ValidationIssue> Errors)> Validate(DataType dataType, IEnumerable<FileAnalysisResult> fileAnalysisResults)
Expand All @@ -25,6 +25,25 @@ public class MimeTypeValidator : IFileValidator
List<ValidationIssue> errors = new();

var fileMimeTypeResult = fileAnalysisResults.FirstOrDefault(x => x.MimeType != null);
if (fileMimeTypeResult == null) return (true, errors);

// Verify that uploaded file extension is the same as filename extension

foreach (var fileExtension in fileMimeTypeResult.Extensions)
{
if (fileMimeTypeResult.Filename != null && !fileMimeTypeResult.Filename.EndsWith(fileExtension))
{
ValidationIssue error = new()
{
Source = "File",
Code = "Uploaded file extension is not the same as filename extension", // TODO - Add correct code
Severity = ValidationIssueSeverity.Error,
Description = $"The {fileMimeTypeResult.Filename} filename does not appear to have the same extension as uploaded file. File extension on uploaded file is .{fileExtension}"
};

errors.Add(error);
}
}
Comment on lines +32 to +46

Check notice

Code scanning / CodeQL

Missed opportunity to use Where

This foreach loop [implicitly filters its target sequence](1) - consider filtering the sequence explicitly using '.Where(...)'.

// Verify that file mime type is an allowed content-type
if (!dataType.AllowedContentTypes.Contains(fileMimeTypeResult?.MimeType, StringComparer.InvariantCultureIgnoreCase) && !dataType.AllowedContentTypes.Contains("application/octet-stream"))
Expand All @@ -38,10 +57,8 @@ public class MimeTypeValidator : IFileValidator
};

errors.Add(error);

return (false, errors);
}

return (true, errors);
return errors.Any() ? (false, errors) : (true, errors);
}
}