-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessValidator.cs
More file actions
188 lines (162 loc) · 6.22 KB
/
Copy pathProcessValidator.cs
File metadata and controls
188 lines (162 loc) · 6.22 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text.RegularExpressions;
using MarketAlly.ProcessMonitor.Interfaces;
using MarketAlly.ProcessMonitor.Models;
using Microsoft.Extensions.Logging;
namespace MarketAlly.ProcessMonitor.Services;
/// <summary>
/// Validates process configurations for security and correctness
/// </summary>
public class ProcessValidator : IProcessValidator
{
private readonly ILogger<ProcessValidator> _logger;
private readonly AppSettings _appSettings;
private readonly HashSet<string> _allowedPaths;
private static readonly Regex TimeFormatRegex = new(@"^([01]?[0-9]|2[0-3]):[0-5][0-9]$", RegexOptions.Compiled);
public ProcessValidator(ILogger<ProcessValidator> logger, AppSettings appSettings)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
_allowedPaths = new HashSet<string>(_appSettings.AllowedPaths ?? new List<string>(), StringComparer.OrdinalIgnoreCase);
}
public bool ValidateProcessPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
_logger.LogWarning("Process path is null or empty");
return false;
}
try
{
// Ensure absolute path
if (!Path.IsPathRooted(path))
{
_logger.LogWarning("Process path is not absolute: {Path}", path);
return false;
}
// Normalize path
var normalizedPath = Path.GetFullPath(path);
// Check if file exists
if (!File.Exists(normalizedPath))
{
_logger.LogWarning("Process file does not exist: {Path}", normalizedPath);
return false;
}
// Check against whitelist if enabled
if (_appSettings.EnablePathValidation && _allowedPaths.Count > 0)
{
var isAllowed = _allowedPaths.Any(allowedPath =>
normalizedPath.StartsWith(allowedPath, StringComparison.OrdinalIgnoreCase));
if (!isAllowed)
{
_logger.LogWarning("Process path not in allowed list: {Path}", normalizedPath);
return false;
}
}
// Verify it's an executable
var extension = Path.GetExtension(normalizedPath).ToLowerInvariant();
var executableExtensions = new[] { ".exe", ".bat", ".cmd", ".ps1", ".sh" };
if (!executableExtensions.Contains(extension))
{
_logger.LogWarning("File is not an executable: {Path}", normalizedPath);
return false;
}
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error validating process path: {Path}", path);
return false;
}
}
public ValidationResult ValidateProcessInfo(ProcessInfo processInfo)
{
var errors = new List<string>();
if (processInfo == null)
{
errors.Add("Process info is null");
return new ValidationResult(false, errors);
}
// Validate name
if (string.IsNullOrWhiteSpace(processInfo.Name))
{
errors.Add("Process name is required");
}
else if (processInfo.Name.Length > 260)
{
errors.Add("Process name is too long");
}
// Validate path
if (!ValidateProcessPath(processInfo.Path))
{
errors.Add($"Invalid process path: {processInfo.Path}");
}
// Validate count
if (processInfo.Count < 0 || processInfo.Count > 100)
{
errors.Add($"Process count must be between 0 and 100, got {processInfo.Count}");
}
// Validate time format
if (!string.IsNullOrWhiteSpace(processInfo.Time) && !TimeFormatRegex.IsMatch(processInfo.Time))
{
errors.Add($"Invalid time format: {processInfo.Time}. Expected HH:mm");
}
// Validate interval
if (processInfo.Interval.HasValue && processInfo.Interval.Value <= 0)
{
errors.Add($"Interval must be positive, got {processInfo.Interval.Value}");
}
// Validate working directory
if (!string.IsNullOrWhiteSpace(processInfo.WorkingDirectory))
{
if (!Directory.Exists(processInfo.WorkingDirectory))
{
errors.Add($"Working directory does not exist: {processInfo.WorkingDirectory}");
}
}
// Validate arguments for potential injection
if (!string.IsNullOrWhiteSpace(processInfo.Arguments))
{
var dangerousPatterns = new[] { ";", "|", "&", "`", "$(" };
if (dangerousPatterns.Any(pattern => processInfo.Arguments.Contains(pattern)))
{
errors.Add("Arguments contain potentially dangerous characters");
}
}
return new ValidationResult(errors.Count == 0, errors);
}
public async Task<bool> CheckPermissionsAsync(string path)
{
try
{
// Check if we can read the file
using (var stream = File.OpenRead(path))
{
// File is readable
}
// Check if running as administrator (Windows)
if (OperatingSystem.IsWindows())
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
var isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (!isAdmin)
{
_logger.LogWarning("Not running as administrator, some processes may fail to start");
}
}
return await Task.FromResult(true);
}
catch (UnauthorizedAccessException)
{
_logger.LogError("No permission to access file: {Path}", path);
return false;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error checking permissions for: {Path}", path);
return false;
}
}
}