-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadPolicy.cs
More file actions
128 lines (111 loc) · 4.17 KB
/
Copy pathUploadPolicy.cs
File metadata and controls
128 lines (111 loc) · 4.17 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.IO;
using SmartFileKit.Domain;
using SmartFileKit.Validation;
namespace SmartFileKit.Security
{
/// <summary>
/// Implements reusable, fluent security policies for validating file uploads.
/// </summary>
public class UploadPolicy
{
private FileSize? _maxSize;
private readonly List<string> _allowedExtensions = new List<string>();
private readonly List<string> _blockedExtensions = new List<string>();
private readonly List<FileCategory> _allowedCategories = new List<FileCategory>();
private readonly List<FileCategory> _blockedCategories = new List<FileCategory>();
private bool _enforceSignature = true;
private UploadPolicy() { }
/// <summary>
/// Creates a new instance of the <see cref="UploadPolicy"/> configuration builder.
/// </summary>
public static UploadPolicy Create() => new UploadPolicy();
/// <summary>
/// Restricts uploads to files below the maximum size limit.
/// </summary>
public UploadPolicy MaxSize(FileSize size)
{
_maxSize = size;
return this;
}
/// <summary>
/// Restricts uploads to the specified allowed extensions.
/// </summary>
public UploadPolicy AllowExtensions(params string[] extensions)
{
if (extensions != null) _allowedExtensions.AddRange(extensions);
return this;
}
/// <summary>
/// Restricts uploads by blocking specified extensions.
/// </summary>
public UploadPolicy BlockExtensions(params string[] extensions)
{
if (extensions != null) _blockedExtensions.AddRange(extensions);
return this;
}
/// <summary>
/// Restricts uploads to specified file categories.
/// </summary>
public UploadPolicy AllowCategories(params FileCategory[] categories)
{
if (categories != null) _allowedCategories.AddRange(categories);
return this;
}
/// <summary>
/// Restricts uploads by blocking specified file categories.
/// </summary>
public UploadPolicy BlockCategories(params FileCategory[] categories)
{
if (categories != null) _blockedCategories.AddRange(categories);
return this;
}
/// <summary>
/// Fluent shortcut to allow standard web image extensions and the image category.
/// </summary>
public UploadPolicy AllowImages()
{
AllowCategories(FileCategory.Image);
AllowExtensions(".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg");
return this;
}
/// <summary>
/// Configures whether byte headers / magic signatures are actively verified. Default is true.
/// </summary>
public UploadPolicy EnforceSignature(bool enforce = true)
{
_enforceSignature = enforce;
return this;
}
/// <summary>
/// Executes validation of a stream and filename against the configured policy.
/// </summary>
public FileValidationResult Validate(Stream stream, string fileName, string contentType = null)
{
var validator = FileValidator.Validate(stream, fileName, contentType)
.VerifySignature(_enforceSignature);
if (_maxSize.HasValue)
{
validator.MaxSize(_maxSize.Value);
}
if (_allowedExtensions.Count > 0)
{
validator.AllowedExtensions(_allowedExtensions.ToArray());
}
if (_blockedExtensions.Count > 0)
{
validator.BlockedExtensions(_blockedExtensions.ToArray());
}
if (_allowedCategories.Count > 0)
{
validator.AllowedCategories(_allowedCategories.ToArray());
}
if (_blockedCategories.Count > 0)
{
validator.BlockedCategories(_blockedCategories.ToArray());
}
return validator.Execute();
}
}
}