| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Http.Bookmarks; |
| | | 6 | | using Elsa.Http.Extensions; |
| | | 7 | | using Elsa.Http.UIHints; |
| | | 8 | | using Elsa.Workflows; |
| | | 9 | | using Elsa.Workflows.Attributes; |
| | | 10 | | using Elsa.Workflows.UIHints; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | using Microsoft.AspNetCore.Routing; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | |
| | | 16 | | namespace Elsa.Http; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Wait for an inbound HTTP request that matches the specified path and methods. |
| | | 20 | | /// </summary> |
| | | 21 | | [Activity("Elsa", "HTTP", "Wait for an inbound HTTP request that matches the specified path and methods.", DisplayName = |
| | | 22 | | [Output(IsSerializable = false)] |
| | | 23 | | public class HttpEndpoint : Trigger<HttpRequest> |
| | | 24 | | { |
| | | 25 | | internal const string HttpContextInputKey = "HttpContext"; |
| | | 26 | | internal const string PathInputKey = "Path"; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 1151 | 29 | | public HttpEndpoint([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line |
| | | 30 | | { |
| | 1151 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 0 | 34 | | public HttpEndpoint(Input<string> path, Input<string> method, [CallerFilePath] string? source = null, [CallerLineNum |
| | | 35 | | { |
| | 0 | 36 | | Path = path; |
| | 0 | 37 | | SupportedMethods = new(ObjectLiteral.From(new[] { method })); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | 1 | 41 | | public HttpEndpoint(Input<string> path, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 42 | | { |
| | 1 | 43 | | Path = path; |
| | 1 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The path to associate with the workflow. |
| | | 48 | | /// </summary> |
| | | 49 | | [Input( |
| | | 50 | | Description = "The path to associate with the workflow.", |
| | | 51 | | UIHint = InputUIHints.SingleLine, |
| | | 52 | | UIHandler = typeof(HttpEndpointPathUIHandler) |
| | | 53 | | )] |
| | 4593 | 54 | | public Input<string> Path { get; set; } = null!; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// The HTTP methods to accept. |
| | | 58 | | /// </summary> |
| | | 59 | | [Input( |
| | | 60 | | Description = "The HTTP methods to accept.", |
| | | 61 | | Options = new[] { "GET", "POST", "PUT", "HEAD", "DELETE" }, |
| | | 62 | | UIHint = InputUIHints.CheckList)] |
| | 5745 | 63 | | public Input<ICollection<string>> SupportedMethods { get; set; } = new(ObjectLiteral.From(new[] { HttpMethods.Get }) |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Allow authenticated requests only. |
| | | 67 | | /// </summary> |
| | | 68 | | [Input(Description = "Allow authenticated requests only.", Category = "Security")] |
| | 4747 | 69 | | public Input<bool> Authorize { get; set; } = new(false); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Provide a policy to evaluate. If the policy fails, the request is forbidden. |
| | | 73 | | /// </summary> |
| | | 74 | | [Input(Description = "Provide a policy to evaluate. If the policy fails, the request is forbidden.", Category = "Sec |
| | 4746 | 75 | | public Input<string?> Policy { get; set; } = new(default(string?)); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// The maximum time allowed to process the request. |
| | | 79 | | /// </summary> |
| | | 80 | | [Input(Description = "The maximum time allowed to process the request.", Category = "Upload")] |
| | 3594 | 81 | | public Input<TimeSpan?> RequestTimeout { get; set; } = null!; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// The maximum request size allowed in bytes. |
| | | 85 | | /// </summary> |
| | | 86 | | [Input(Description = "The maximum request size allowed in bytes.", Category = "Upload")] |
| | 4170 | 87 | | public Input<long?> RequestSizeLimit { get; set; } = null!; |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// The maximum request size allowed in bytes. |
| | | 91 | | /// </summary> |
| | | 92 | | [Input(Description = "The maximum file size allowed in bytes for an individual file.", Category = "Upload")] |
| | 3324 | 93 | | public Input<long?> FileSizeLimit { get; set; } = null!; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// The allowed file extensions, |
| | | 97 | | /// </summary> |
| | | 98 | | [Input(Description = "Only file extensions in this list are allowed. Leave empty to allow all extensions", Category |
| | 3324 | 99 | | public Input<ICollection<string>> AllowedFileExtensions { get; set; } = null!; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// The allowed file extensions, |
| | | 103 | | /// </summary> |
| | | 104 | | [Input(Description = "File extensions in this list are forbidden. Leave empty to not block any extension.", Category |
| | 3380 | 105 | | public Input<ICollection<string>> BlockedFileExtensions { get; set; } = null!; |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// The allowed file extensions, |
| | | 109 | | /// </summary> |
| | | 110 | | [Input(Description = "Only MIME types in this list are allowed. Leave empty to allow all types", Category = "Upload" |
| | 3323 | 111 | | public Input<ICollection<string>> AllowedMimeTypes { get; set; } = null!; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// A value indicating whether to expose the "Request too large" outcome. |
| | | 115 | | /// </summary> |
| | | 116 | | [Input(Description = "A value indicating whether to expose the \"Request too large\" outcome.", Category = "Outcomes |
| | 1253 | 117 | | public bool ExposeRequestTooLargeOutcome { get; set; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// A value indicating whether to expose the "File too large" outcome. |
| | | 121 | | /// </summary> |
| | | 122 | | [Input(Description = "A value indicating whether to expose the \"File too large\" outcome.", Category = "Outcomes")] |
| | 1252 | 123 | | public bool ExposeFileTooLargeOutcome { get; set; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// A value indicating whether to expose the "Invalid file extension" outcome. |
| | | 127 | | /// </summary> |
| | | 128 | | [Input(Description = "A value indicating whether to expose the \"Invalid file extension\" outcome.", Category = "Out |
| | 1309 | 129 | | public bool ExposeInvalidFileExtensionOutcome { get; set; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// A value indicating whether to expose the "Invalid file MIME type" outcome. |
| | | 133 | | /// </summary> |
| | | 134 | | [Input(Description = "A value indicating whether to expose the \"Invalid file MIME type\" outcome.", Category = "Out |
| | 1252 | 135 | | public bool ExposeInvalidFileMimeTypeOutcome { get; set; } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// The parsed request content, if any. |
| | | 139 | | /// </summary> |
| | | 140 | | [Output(Description = "The parsed request content, if any.")] |
| | 3454 | 141 | | public Output<object?> ParsedContent { get; set; } = null!; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// The uploaded files, if any. |
| | | 145 | | /// </summary> |
| | | 146 | | [Output(Description = "The uploaded files, if any.", IsSerializable = false)] |
| | 2890 | 147 | | public Output<IFormFile[]> Files { get; set; } = null!; |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// The first uploaded file, if any. |
| | | 151 | | /// </summary> |
| | | 152 | | [Output(Description = "The first uploaded file, if any.", IsSerializable = false)] |
| | 2440 | 153 | | public Output<IFormFile?> File { get; set; } = null!; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// The parsed route data, if any. |
| | | 157 | | /// </summary> |
| | | 158 | | [Output(Description = "The parsed route data, if any.")] |
| | 3149 | 159 | | public Output<IDictionary<string, object>> RouteData { get; set; } = null!; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// The querystring data, if any. |
| | | 163 | | /// </summary> |
| | | 164 | | [Output(Description = "The querystring data, if any.")] |
| | 3093 | 165 | | public Output<IDictionary<string, object>> QueryStringData { get; set; } = null!; |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// The headers, if any. |
| | | 169 | | /// </summary> |
| | | 170 | | [Output(Description = "The headers, if any.")] |
| | 3093 | 171 | | public Output<IDictionary<string, object>> Headers { get; set; } = null!; |
| | | 172 | | |
| | | 173 | | /// <inheritdoc /> |
| | | 174 | | protected override IEnumerable<object> GetTriggerPayloads(TriggerIndexingContext context) |
| | | 175 | | { |
| | 278 | 176 | | context.TriggerName = HttpStimulusNames.HttpEndpoint; |
| | 278 | 177 | | return GetBookmarkPayloads(context.ExpressionExecutionContext); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <inheritdoc /> |
| | | 181 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 182 | | { |
| | 265 | 183 | | var path = Path.Get(context); |
| | 265 | 184 | | var methods = SupportedMethods.GetOrDefault(context) ?? new List<string> { HttpMethods.Get }; |
| | 265 | 185 | | await context.WaitForHttpRequestAsync(path, methods, OnResumeAsync, Elsa.Http.HttpStimulusNames.HttpEndpoint); |
| | 265 | 186 | | } |
| | | 187 | | |
| | | 188 | | private async ValueTask OnResumeAsync(ActivityExecutionContext context) |
| | | 189 | | { |
| | 260 | 190 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 260 | 191 | | var httpContext = httpContextAccessor.HttpContext; |
| | | 192 | | |
| | 260 | 193 | | if (httpContext == null) |
| | | 194 | | { |
| | | 195 | | // We're executing in a non-HTTP context (e.g. in a virtual actor). |
| | | 196 | | // Create a bookmark to allow the invoker to export the state and resume execution from there. |
| | 0 | 197 | | context.CreateCrossBoundaryBookmark(); |
| | 0 | 198 | | return; |
| | | 199 | | } |
| | | 200 | | |
| | 260 | 201 | | await HandleRequestAsync(context); |
| | 260 | 202 | | } |
| | | 203 | | |
| | | 204 | | private async Task HandleRequestAsync(ActivityExecutionContext context) |
| | | 205 | | { |
| | 260 | 206 | | var httpContextAccessor = context.GetRequiredService<IHttpContextAccessor>(); |
| | 260 | 207 | | var httpContext = httpContextAccessor.HttpContext!; |
| | | 208 | | |
| | | 209 | | // Provide the received HTTP request as output. |
| | 260 | 210 | | var request = httpContext.Request; |
| | 260 | 211 | | context.Set(Result, request); |
| | | 212 | | |
| | | 213 | | // Read route data, if any. |
| | 260 | 214 | | var path = context.GetWorkflowInput<PathString>(PathInputKey); |
| | 260 | 215 | | var routeData = GetRouteData(httpContext, path); |
| | 294 | 216 | | var routeDictionary = routeData.Values.ToDictionary(route => route.Key, route => route.Value!); |
| | 260 | 217 | | var queryStringDictionary = httpContext.Request.Query.ToObjectDictionary(); |
| | 260 | 218 | | var headersDictionary = httpContext.Request.Headers.ToObjectDictionary(); |
| | | 219 | | |
| | 260 | 220 | | context.Set(RouteData, routeDictionary); |
| | 260 | 221 | | context.Set(QueryStringData, queryStringDictionary); |
| | 260 | 222 | | context.Set(Headers, headersDictionary); |
| | | 223 | | |
| | | 224 | | // Validate declared request size before reading, then enforce the same limit while reading. |
| | 260 | 225 | | if (!ValidateDeclaredRequestSize(context, httpContext)) |
| | | 226 | | { |
| | 1 | 227 | | await HandleRequestTooLargeAsync(context, httpContext); |
| | 1 | 228 | | return; |
| | | 229 | | } |
| | | 230 | | |
| | 259 | 231 | | ApplyRequestSizeLimit(context, request); |
| | | 232 | | |
| | | 233 | | try |
| | | 234 | | { |
| | | 235 | | // Handle Form Fields |
| | 259 | 236 | | if (request.HasFormContentType) |
| | | 237 | | { |
| | 12 | 238 | | var form = await request.ReadFormAsync(context.CancellationToken); |
| | 12 | 239 | | var formFields = form.ToObjectDictionary(); |
| | | 240 | | |
| | 12 | 241 | | ParsedContent.Set(context, formFields); |
| | | 242 | | |
| | | 243 | | // Read files, if any. |
| | 12 | 244 | | var files = form.Files; |
| | | 245 | | |
| | 12 | 246 | | if (files.Any()) |
| | | 247 | | { |
| | 8 | 248 | | if (!ValidateFileSizes(context, httpContext, files)) |
| | | 249 | | { |
| | 0 | 250 | | await HandleFileSizeTooLargeAsync(context, httpContext); |
| | 0 | 251 | | return; |
| | | 252 | | } |
| | | 253 | | |
| | 8 | 254 | | if (!ValidateFileExtensionWhitelist(context, httpContext, files)) |
| | | 255 | | { |
| | 0 | 256 | | await HandleInvalidFileExtensionWhitelistAsync(context, httpContext); |
| | 0 | 257 | | return; |
| | | 258 | | } |
| | | 259 | | |
| | 8 | 260 | | if (!ValidateFileExtensionBlacklist(context, httpContext, files)) |
| | | 261 | | { |
| | 1 | 262 | | await HandleInvalidFileExtensionBlacklistAsync(context, httpContext); |
| | 1 | 263 | | return; |
| | | 264 | | } |
| | | 265 | | |
| | 7 | 266 | | if (!ValidateFileMimeTypes(context, httpContext, files)) |
| | | 267 | | { |
| | 0 | 268 | | await HandleInvalidFileMimeTypesAsync(context, httpContext); |
| | 0 | 269 | | return; |
| | | 270 | | } |
| | | 271 | | |
| | 7 | 272 | | Files.Set(context, files.ToArray()); |
| | 7 | 273 | | File.Set(context, files.FirstOrDefault()); |
| | | 274 | | } |
| | | 275 | | } |
| | | 276 | | else |
| | | 277 | | { |
| | | 278 | | // Parse Non-Form content. |
| | | 279 | | try |
| | | 280 | | { |
| | 247 | 281 | | var content = await ParseContentAsync(context, request); |
| | 246 | 282 | | ParsedContent.Set(context, content); |
| | 246 | 283 | | } |
| | 1 | 284 | | catch (JsonException e) |
| | | 285 | | { |
| | 1 | 286 | | await HandleInvalidJsonPayloadAsync(context, httpContext, e); |
| | 1 | 287 | | return; |
| | | 288 | | } |
| | | 289 | | } |
| | 257 | 290 | | } |
| | | 291 | | catch (RequestBodyTooLargeException) |
| | | 292 | | { |
| | 0 | 293 | | await HandleRequestTooLargeAsync(context, httpContext); |
| | 0 | 294 | | return; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | // Complete. |
| | 257 | 298 | | await context.CompleteActivityAsync(); |
| | 260 | 299 | | } |
| | | 300 | | |
| | | 301 | | private bool ValidateDeclaredRequestSize(ActivityExecutionContext context, HttpContext httpContext) |
| | | 302 | | { |
| | 260 | 303 | | var requestSizeLimit = RequestSizeLimit.GetOrDefault(context); |
| | | 304 | | |
| | 260 | 305 | | if (!requestSizeLimit.HasValue) |
| | 258 | 306 | | return true; |
| | | 307 | | |
| | 2 | 308 | | var requestSize = httpContext.Request.ContentLength ?? 0; |
| | 2 | 309 | | return requestSize <= requestSizeLimit; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | private void ApplyRequestSizeLimit(ActivityExecutionContext context, HttpRequest request) |
| | | 313 | | { |
| | 259 | 314 | | var requestSizeLimit = RequestSizeLimit.GetOrDefault(context); |
| | | 315 | | |
| | 259 | 316 | | if (!requestSizeLimit.HasValue || request.Body is RequestSizeLimitStream) |
| | 258 | 317 | | return; |
| | | 318 | | |
| | 1 | 319 | | request.Body = new RequestSizeLimitStream(request.Body, requestSizeLimit.Value); |
| | 1 | 320 | | } |
| | | 321 | | |
| | | 322 | | private async Task HandleRequestTooLargeAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 323 | | { |
| | 1 | 324 | | var exposeRequestTooLargeOutcome = ExposeRequestTooLargeOutcome; |
| | | 325 | | |
| | 1 | 326 | | if (exposeRequestTooLargeOutcome) |
| | | 327 | | { |
| | 0 | 328 | | await context.CompleteActivityWithOutcomesAsync("Request too large"); |
| | | 329 | | } |
| | | 330 | | else |
| | | 331 | | { |
| | 1 | 332 | | var response = httpContext.Response; |
| | 1 | 333 | | response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| | 1 | 334 | | await response.WriteAsJsonAsync(new |
| | 1 | 335 | | { |
| | 1 | 336 | | Message = $"The maximum request size allowed is {RequestSizeLimit.Get(context)} bytes." |
| | 1 | 337 | | }); |
| | 1 | 338 | | await response.Body.FlushAsync(); |
| | 1 | 339 | | } |
| | 1 | 340 | | } |
| | | 341 | | |
| | | 342 | | private bool ValidateFileSizes(ActivityExecutionContext context, HttpContext httpContext, IFormFileCollection files) |
| | | 343 | | { |
| | 8 | 344 | | var fileSizeLimit = FileSizeLimit.GetOrDefault(context); |
| | | 345 | | |
| | 8 | 346 | | if (!fileSizeLimit.HasValue) |
| | 8 | 347 | | return true; |
| | | 348 | | |
| | 0 | 349 | | if (!files.Any(file => file.Length > fileSizeLimit.Value)) |
| | 0 | 350 | | return true; |
| | | 351 | | |
| | 0 | 352 | | return false; |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | private async Task HandleFileSizeTooLargeAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 356 | | { |
| | 0 | 357 | | var exposeFileTooLargeOutcome = ExposeFileTooLargeOutcome; |
| | | 358 | | |
| | 0 | 359 | | if (exposeFileTooLargeOutcome) |
| | | 360 | | { |
| | 0 | 361 | | await context.CompleteActivityWithOutcomesAsync("File too large"); |
| | | 362 | | } |
| | | 363 | | else |
| | | 364 | | { |
| | 0 | 365 | | var response = httpContext.Response; |
| | 0 | 366 | | response.StatusCode = StatusCodes.Status413PayloadTooLarge; |
| | 0 | 367 | | await response.WriteAsJsonAsync(new |
| | 0 | 368 | | { |
| | 0 | 369 | | Message = $"The maximum file size allowed is {FileSizeLimit.Get(context)} bytes." |
| | 0 | 370 | | }); |
| | 0 | 371 | | await response.Body.FlushAsync(); |
| | 0 | 372 | | } |
| | 0 | 373 | | } |
| | | 374 | | |
| | | 375 | | private bool ValidateFileExtensionWhitelist(ActivityExecutionContext context, HttpContext httpContext, IFormFileColl |
| | | 376 | | { |
| | 8 | 377 | | var allowedFileExtensions = AllowedFileExtensions.GetOrDefault(context); |
| | | 378 | | |
| | 8 | 379 | | if (allowedFileExtensions == null || !allowedFileExtensions.Any()) |
| | 8 | 380 | | return true; |
| | | 381 | | |
| | 0 | 382 | | if (files.All(file => allowedFileExtensions.Contains(System.IO.Path.GetExtension(file.FileName), StringComparer. |
| | 0 | 383 | | return true; |
| | | 384 | | |
| | 0 | 385 | | return false; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | private async Task HandleInvalidFileExtensionWhitelistAsync(ActivityExecutionContext context, HttpContext httpContex |
| | | 389 | | { |
| | 0 | 390 | | if (ExposeInvalidFileExtensionOutcome) |
| | | 391 | | { |
| | 0 | 392 | | await context.CompleteActivityWithOutcomesAsync("Invalid file extension"); |
| | 0 | 393 | | return; |
| | | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | var response = httpContext.Response; |
| | 0 | 397 | | var allowedFileExtensions = AllowedFileExtensions.GetOrDefault(context)!; |
| | 0 | 398 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 399 | | await response.WriteAsJsonAsync(new |
| | 0 | 400 | | { |
| | 0 | 401 | | Message = $"Only the following file extensions are allowed: {string.Join(", ", allowedFileExtensions)}" |
| | 0 | 402 | | }); |
| | 0 | 403 | | await response.Body.FlushAsync(); |
| | 0 | 404 | | } |
| | | 405 | | |
| | | 406 | | private bool ValidateFileExtensionBlacklist(ActivityExecutionContext context, HttpContext httpContext, IFormFileColl |
| | | 407 | | { |
| | 8 | 408 | | var blockedFileExtensions = BlockedFileExtensions.GetOrDefault(context); |
| | | 409 | | |
| | 8 | 410 | | if (blockedFileExtensions == null || !blockedFileExtensions.Any()) |
| | 6 | 411 | | return true; |
| | | 412 | | |
| | 4 | 413 | | if (!files.Any(file => blockedFileExtensions.Contains(System.IO.Path.GetExtension(file.FileName), StringComparer |
| | 1 | 414 | | return true; |
| | | 415 | | |
| | 1 | 416 | | return false; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | private async Task HandleInvalidFileExtensionBlacklistAsync(ActivityExecutionContext context, HttpContext httpContex |
| | | 420 | | { |
| | 1 | 421 | | if (ExposeInvalidFileExtensionOutcome) |
| | | 422 | | { |
| | 1 | 423 | | await context.CompleteActivityWithOutcomesAsync("Invalid file extension"); |
| | 1 | 424 | | return; |
| | | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | var blockedFileExtensions = BlockedFileExtensions.GetOrDefault(context)!; |
| | 0 | 428 | | var response = httpContext.Response; |
| | 0 | 429 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 430 | | await response.WriteAsJsonAsync(new |
| | 0 | 431 | | { |
| | 0 | 432 | | Message = $"The following file extensions are not allowed: {string.Join(", ", blockedFileExtensions)}" |
| | 0 | 433 | | }); |
| | 0 | 434 | | await response.Body.FlushAsync(); |
| | 1 | 435 | | } |
| | | 436 | | |
| | | 437 | | private bool ValidateFileMimeTypes(ActivityExecutionContext context, HttpContext httpContext, IFormFileCollection fi |
| | | 438 | | { |
| | 7 | 439 | | var allowedMimeTypes = AllowedMimeTypes.GetOrDefault(context); |
| | | 440 | | |
| | 7 | 441 | | if (allowedMimeTypes == null || !allowedMimeTypes.Any()) |
| | 7 | 442 | | return true; |
| | | 443 | | |
| | 0 | 444 | | if (files.All(file => allowedMimeTypes.Contains(file.ContentType, StringComparer.OrdinalIgnoreCase))) |
| | 0 | 445 | | return true; |
| | | 446 | | |
| | 0 | 447 | | return false; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private async Task HandleInvalidFileMimeTypesAsync(ActivityExecutionContext context, HttpContext httpContext) |
| | | 451 | | { |
| | 0 | 452 | | if (ExposeInvalidFileMimeTypeOutcome) |
| | | 453 | | { |
| | 0 | 454 | | await context.CompleteActivityWithOutcomesAsync("Invalid file MIME type"); |
| | 0 | 455 | | return; |
| | | 456 | | } |
| | | 457 | | |
| | 0 | 458 | | var allowedMimeTypes = AllowedMimeTypes.GetOrDefault(context)!; |
| | 0 | 459 | | var response = httpContext.Response; |
| | 0 | 460 | | response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 0 | 461 | | await response.WriteAsJsonAsync(new |
| | 0 | 462 | | { |
| | 0 | 463 | | Message = $"Only the following MIME types are allowed: {string.Join(", ", allowedMimeTypes)}" |
| | 0 | 464 | | }); |
| | 0 | 465 | | await response.Body.FlushAsync(); |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | private async Task HandleInvalidJsonPayloadAsync(ActivityExecutionContext context, HttpContext httpContext, JsonExce |
| | | 469 | | { |
| | 1 | 470 | | var response = httpContext.Response; |
| | 1 | 471 | | response.StatusCode = StatusCodes.Status400BadRequest; |
| | 1 | 472 | | await response.WriteAsJsonAsync(new |
| | 1 | 473 | | { |
| | 1 | 474 | | exception.Message, |
| | 1 | 475 | | exception.Path, |
| | 1 | 476 | | exception.LineNumber, |
| | 1 | 477 | | }); |
| | 1 | 478 | | await response.Body.FlushAsync(); |
| | 1 | 479 | | } |
| | | 480 | | |
| | | 481 | | private async Task<object?> ParseContentAsync(ActivityExecutionContext context, HttpRequest httpRequest) |
| | | 482 | | { |
| | 247 | 483 | | if (!HasContent(httpRequest)) |
| | 44 | 484 | | return null; |
| | | 485 | | |
| | 203 | 486 | | var cancellationToken = context.CancellationToken; |
| | 203 | 487 | | var targetType = ParsedContent.GetTargetType(context); |
| | 203 | 488 | | var contentStream = httpRequest.Body; |
| | 203 | 489 | | var contentType = httpRequest.ContentType!; |
| | 1827 | 490 | | var headers = httpRequest.Headers.ToDictionary(x => x.Key, x => x.Value.ToArray()); |
| | | 491 | | |
| | 203 | 492 | | return await context.ParseContentAsync(contentStream, contentType, targetType, headers!, cancellationToken); |
| | 246 | 493 | | } |
| | | 494 | | |
| | | 495 | | private static bool HasContent(HttpRequest httpRequest) |
| | | 496 | | { |
| | 247 | 497 | | if (httpRequest.ContentLength > 0) |
| | 203 | 498 | | return true; |
| | | 499 | | |
| | 44 | 500 | | return httpRequest.ContentLength == null && !string.IsNullOrWhiteSpace(httpRequest.ContentType); |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | private IEnumerable<object> GetBookmarkPayloads(ExpressionExecutionContext context) |
| | | 504 | | { |
| | | 505 | | // Generate bookmark data for path and selected methods. |
| | 278 | 506 | | var normalizedRoute = context.Get(Path)!.NormalizeRoute(); |
| | 278 | 507 | | var methods = SupportedMethods.GetOrDefault(context) ?? new List<string> { HttpMethods.Get }; |
| | 278 | 508 | | var authorize = Authorize.GetOrDefault(context); |
| | 278 | 509 | | var policy = Policy.GetOrDefault(context); |
| | 278 | 510 | | var requestTimeout = RequestTimeout.GetOrDefault(context); |
| | 278 | 511 | | var requestSizeLimit = RequestSizeLimit.GetOrDefault(context); |
| | | 512 | | |
| | 278 | 513 | | return methods |
| | 332 | 514 | | .Select(x => new HttpEndpointBookmarkPayload(normalizedRoute, x.ToLowerInvariant(), authorize, policy, reque |
| | 278 | 515 | | .Cast<object>() |
| | 278 | 516 | | .ToArray(); |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | private static RouteData GetRouteData(HttpContext httpContext, string path) |
| | | 520 | | { |
| | 260 | 521 | | var routeData = httpContext.GetRouteData(); |
| | 260 | 522 | | var routeTable = httpContext.RequestServices.GetRequiredService<IRouteTable>(); |
| | 260 | 523 | | var routeMatcher = httpContext.RequestServices.GetRequiredService<IRouteMatcher>(); |
| | | 524 | | |
| | 260 | 525 | | var matchingRouteQuery = |
| | 260 | 526 | | from route in routeTable |
| | 2298 | 527 | | let routeValues = routeMatcher.Match(route.Route, path) |
| | 2298 | 528 | | where routeValues != null |
| | 520 | 529 | | select new { route, routeValues }; |
| | | 530 | | |
| | 260 | 531 | | var matchingRoute = matchingRouteQuery.FirstOrDefault(); |
| | | 532 | | |
| | 260 | 533 | | if (matchingRoute == null) |
| | 0 | 534 | | return routeData; |
| | | 535 | | |
| | 554 | 536 | | foreach (var (key, value) in matchingRoute.routeValues!) |
| | 17 | 537 | | routeData.Values[key] = value; |
| | | 538 | | |
| | 260 | 539 | | return routeData; |
| | | 540 | | } |
| | | 541 | | |
| | 1 | 542 | | private sealed class RequestSizeLimitStream(Stream inner, long requestSizeLimit) : Stream |
| | | 543 | | { |
| | | 544 | | private long _bytesRead; |
| | | 545 | | |
| | 1 | 546 | | public override bool CanRead => inner.CanRead; |
| | 0 | 547 | | public override bool CanSeek => false; |
| | 0 | 548 | | public override bool CanWrite => false; |
| | 0 | 549 | | public override long Length => inner.Length; |
| | | 550 | | |
| | | 551 | | public override long Position |
| | | 552 | | { |
| | 0 | 553 | | get => inner.Position; |
| | 0 | 554 | | set => throw new NotSupportedException(); |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | public override void Flush() => inner.Flush(); |
| | | 558 | | |
| | | 559 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 560 | | { |
| | 0 | 561 | | var bytesRead = inner.Read(buffer, offset, GetPermittedReadCount(count)); |
| | 0 | 562 | | CountBytesRead(bytesRead); |
| | 0 | 563 | | return bytesRead; |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | public override int Read(Span<byte> buffer) |
| | | 567 | | { |
| | 0 | 568 | | var bytesRead = inner.Read(buffer[..GetPermittedReadCount(buffer.Length)]); |
| | 0 | 569 | | CountBytesRead(bytesRead); |
| | 0 | 570 | | return bytesRead; |
| | | 571 | | } |
| | | 572 | | |
| | | 573 | | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = defaul |
| | | 574 | | { |
| | 2 | 575 | | var bytesRead = await inner.ReadAsync(buffer[..GetPermittedReadCount(buffer.Length)], cancellationToken); |
| | 2 | 576 | | CountBytesRead(bytesRead); |
| | 2 | 577 | | return bytesRead; |
| | 2 | 578 | | } |
| | | 579 | | |
| | | 580 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 581 | | { |
| | 0 | 582 | | var bytesRead = await inner.ReadAsync(buffer, offset, GetPermittedReadCount(count), cancellationToken); |
| | 0 | 583 | | CountBytesRead(bytesRead); |
| | 0 | 584 | | return bytesRead; |
| | 0 | 585 | | } |
| | | 586 | | |
| | 0 | 587 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | | 588 | | |
| | 0 | 589 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 590 | | |
| | 0 | 591 | | public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| | | 592 | | |
| | | 593 | | private int GetPermittedReadCount(int requestedCount) |
| | | 594 | | { |
| | 2 | 595 | | if (requestedCount == 0) |
| | 0 | 596 | | return 0; |
| | | 597 | | |
| | 2 | 598 | | var remainingBytes = requestSizeLimit - _bytesRead; |
| | 2 | 599 | | if (remainingBytes >= requestedCount) |
| | 0 | 600 | | return requestedCount; |
| | | 601 | | |
| | 2 | 602 | | if (remainingBytes < 0) |
| | 0 | 603 | | return 1; |
| | | 604 | | |
| | 2 | 605 | | return (int)remainingBytes + 1; |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | private void CountBytesRead(int bytesRead) |
| | | 609 | | { |
| | 2 | 610 | | _bytesRead += bytesRead; |
| | | 611 | | |
| | 2 | 612 | | if (_bytesRead > requestSizeLimit) |
| | 0 | 613 | | throw new RequestBodyTooLargeException(); |
| | 2 | 614 | | } |
| | | 615 | | } |
| | | 616 | | |
| | | 617 | | private sealed class RequestBodyTooLargeException : Exception; |
| | | 618 | | } |