-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathUploadValidation.cs
More file actions
109 lines (91 loc) · 3.88 KB
/
Copy pathUploadValidation.cs
File metadata and controls
109 lines (91 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2026 Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using Microsoft.AspNetCore.Http;
using System.Net;
namespace Moryx.Media.Endpoints;
public static class UploadValidation
{
private const int Megabyte = 1048576;
// To add further signatures, see the File Signatures Database (https://www.filesignatures.net/)
private static readonly Dictionary<string, List<byte[]>> _signatures = new()
{
{ ".gif", [[0x47, 0x49, 0x46, 0x38]] },
{ ".png", [[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]] },
{ ".pdf", [[0x25, 0x50, 0x44, 0x46]] },
{ ".jpeg", [
[0xFF, 0xD8, 0xFF, 0xE0],
[0xFF, 0xD8, 0xFF, 0xE2],
[0xFF, 0xD8, 0xFF, 0xE3]
]
},
{ ".jpg", [
[0xFF, 0xD8, 0xFF, 0xE0],
[0xFF, 0xD8, 0xFF, 0xE1],
[0xFF, 0xD8, 0xFF, 0xE8]
]
}
};
// TODO: Add anti virus scan (or a notification to add one on the system)
public static bool ValidateFormFile(IFormFile formFile,
IMediaServer mediaServer, out string errorMessage)
{
// Don't trust the file name sent by the client.
var trustedFileName = WebUtility.HtmlEncode(formFile.FileName);
errorMessage = $"{trustedFileName} file size of 0 MB is to small!";
if (formFile.Length == 0)
return false;
var megabyteSizeLimit = mediaServer.FileSizeLimitInMb();
errorMessage = $"{trustedFileName} exceeds {megabyteSizeLimit:N1} MB.";
if (formFile.Length > megabyteSizeLimit * Megabyte)
return false;
try
{
using var readStream = formFile.OpenReadStream();
using var reader = new StreamReader(readStream, detectEncodingFromByteOrderMarks: true);
// Check if the file's only content was a BOM
errorMessage = $"{trustedFileName} file size of 0 MB is to small!";
if (reader.EndOfStream)
return false;
var fileTypes = mediaServer.GetSupportedFileTypes();
errorMessage = $"{trustedFileName} file type isn't permitted.";
if (!fileTypes.Contains(Path.GetExtension(formFile.FileName)))
return false;
errorMessage = $"{trustedFileName} file signature doesn't match the file's extension.";
if (!IsValidFileExtensionAndSignature(formFile.FileName, readStream, fileTypes))
return false;
}
catch
{
errorMessage = $"{trustedFileName} validation failed.";
return false;
}
errorMessage = string.Empty;
return true;
}
private static bool IsValidFileExtensionAndSignature(string fileName, Stream data, string[] permittedExtensions)
{
if (string.IsNullOrEmpty(fileName) || data == null || data.Length == 0)
return false;
var extension = Path.GetExtension(fileName).ToLowerInvariant();
if (string.IsNullOrEmpty(extension) || !permittedExtensions.Contains(extension))
return false;
data.Position = 0;
using var reader = new BinaryReader(data);
if (extension.Equals(".txt") || extension.Equals(".csv") || extension.Equals(".prn"))
{
// Limits characters to ASCII encoding.
for (var i = 0; i < data.Length; i++)
if (reader.ReadByte() > sbyte.MaxValue)
return false;
return true;
}
if (!_signatures.ContainsKey(extension))
{
return true;
}
// Test the input content's file signature
var signatures = _signatures.GetValueOrDefault(extension);
var headerBytes = reader.ReadBytes(signatures.Max(m => m.Length));
return signatures.Any(signature => headerBytes.Take(signature.Length).SequenceEqual(signature));
}
}