| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Text; |
| | | 3 | | using Kestrun.Logging; |
| | | 4 | | using Microsoft.AspNetCore.Http.Features; |
| | | 5 | | using Microsoft.AspNetCore.WebUtilities; |
| | | 6 | | using Microsoft.Net.Http.Headers; |
| | | 7 | | using Serilog; |
| | | 8 | | using Serilog.Events; |
| | | 9 | | using Logger = Serilog.ILogger; |
| | | 10 | | |
| | | 11 | | namespace Kestrun.Forms; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Parses incoming form payloads into normalized form payloads. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class KrFormParser |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Parses the incoming request into a normalized form payload. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="context">The HTTP context.</param> |
| | | 22 | | /// <param name="options">The form parsing options.</param> |
| | | 23 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 24 | | /// <returns>The parsed payload.</returns> |
| | | 25 | | public static async Task<IKrFormPayload> ParseAsync(HttpContext context, KrFormOptions options, CancellationToken ca |
| | | 26 | | { |
| | 8 | 27 | | ArgumentNullException.ThrowIfNull(context); |
| | 8 | 28 | | ArgumentNullException.ThrowIfNull(options); |
| | | 29 | | |
| | 8 | 30 | | var logger = ResolveLogger(context, options); |
| | 8 | 31 | | using var _ = logger.BeginTimedOperation("KrFormParser.ParseAsync"); |
| | | 32 | | |
| | | 33 | | try |
| | | 34 | | { |
| | 8 | 35 | | var (mediaType, normalizedMediaType) = ValidateAndNormalizeMediaType(context, options, logger); |
| | 6 | 36 | | ApplyRequestBodyLimit(context, options, logger); |
| | | 37 | | |
| | 6 | 38 | | return await ParseByContentTypeAsync(context, mediaType, normalizedMediaType, options, logger, cancellationT |
| | 6 | 39 | | .ConfigureAwait(false); |
| | | 40 | | } |
| | 3 | 41 | | catch (KrFormException) |
| | | 42 | | { |
| | 3 | 43 | | TryMarkConnectionClose(context, logger); |
| | 3 | 44 | | throw; |
| | | 45 | | } |
| | 5 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Marks the current connection as non-keep-alive for HTTP/1.x responses. |
| | | 50 | | /// This avoids Kestrel attempting to parse unread request-body bytes as a new request line |
| | | 51 | | /// when the application rejects a form upload early (e.g., 415/400/413). |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="context">The HTTP context.</param> |
| | | 54 | | /// <param name="logger">The logger.</param> |
| | | 55 | | private static void TryMarkConnectionClose(HttpContext context, Logger logger) |
| | | 56 | | { |
| | 3 | 57 | | if (context.Response.HasStarted) |
| | | 58 | | { |
| | 0 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | // Only meaningful on HTTP/1.x. For HTTP/2+, the header is ignored. |
| | 3 | 63 | | context.Response.Headers[HeaderNames.Connection] = "close"; |
| | 3 | 64 | | logger.Debug("Form parsing error: setting Connection: close to avoid unread-body keep-alive issues."); |
| | 3 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Resolves the logger to use for form parsing. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="context">The HTTP context.</param> |
| | | 71 | | /// <param name="options">The form parsing options.</param> |
| | | 72 | | /// <returns>The resolved logger.</returns> |
| | | 73 | | private static Logger ResolveLogger(HttpContext context, KrFormOptions options) |
| | | 74 | | { |
| | 8 | 75 | | return options.Logger |
| | 8 | 76 | | ?? context.RequestServices.GetService(typeof(Serilog.ILogger)) as Serilog.ILogger |
| | 8 | 77 | | ?? Log.Logger; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Validates the Content-Type header and returns the parsed and normalized media type. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="context">The HTTP context.</param> |
| | | 84 | | /// <param name="options">The form parsing options.</param> |
| | | 85 | | /// <param name="logger">The logger.</param> |
| | | 86 | | /// <returns>The parsed media type and normalized media type string.</returns> |
| | | 87 | | private static (MediaTypeHeaderValue MediaType, string NormalizedMediaType) ValidateAndNormalizeMediaType( |
| | | 88 | | HttpContext context, |
| | | 89 | | KrFormOptions options, |
| | | 90 | | Logger logger) |
| | | 91 | | { |
| | 8 | 92 | | var contentTypeHeader = context.Request.ContentType; |
| | 8 | 93 | | var contentEncoding = context.Request.Headers[HeaderNames.ContentEncoding].ToString(); |
| | 8 | 94 | | var requestDecompressionEnabled = DetectRequestDecompressionEnabled(context); |
| | 8 | 95 | | if (logger.IsEnabled(LogEventLevel.Debug)) |
| | | 96 | | { |
| | 0 | 97 | | logger.DebugSanitized( |
| | 0 | 98 | | "Form route start: Content-Type={ContentType}, Content-Encoding={ContentEncoding}, RequestDecompressionE |
| | 0 | 99 | | contentTypeHeader, |
| | 0 | 100 | | string.IsNullOrWhiteSpace(contentEncoding) ? "<none>" : contentEncoding, |
| | 0 | 101 | | requestDecompressionEnabled); |
| | | 102 | | } |
| | | 103 | | |
| | 8 | 104 | | if (string.IsNullOrWhiteSpace(contentTypeHeader)) |
| | | 105 | | { |
| | 0 | 106 | | logger.Error("Missing Content-Type header for form parsing."); |
| | 0 | 107 | | throw new KrFormException("Content-Type header is required for form parsing.", StatusCodes.Status415Unsuppor |
| | | 108 | | } |
| | | 109 | | |
| | 8 | 110 | | if (!MediaTypeHeaderValue.TryParse(contentTypeHeader, out var mediaType)) |
| | | 111 | | { |
| | 0 | 112 | | logger.WarningSanitized("Invalid Content-Type header: {ContentType}", contentTypeHeader); |
| | 0 | 113 | | throw new KrFormException("Invalid Content-Type header.", StatusCodes.Status415UnsupportedMediaType); |
| | | 114 | | } |
| | | 115 | | |
| | 8 | 116 | | var normalizedMediaType = mediaType.MediaType.Value ?? string.Empty; |
| | 8 | 117 | | if (!IsAllowedRequestContentType(normalizedMediaType, options.AllowedContentTypes)) |
| | | 118 | | { |
| | 1 | 119 | | if (options.RejectUnknownRequestContentType) |
| | | 120 | | { |
| | 1 | 121 | | logger.Error("Rejected request Content-Type: {ContentType}", normalizedMediaType); |
| | 1 | 122 | | throw new KrFormException("Unsupported Content-Type for form parsing.", StatusCodes.Status415Unsupported |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | logger.Warning("Unknown Content-Type allowed: {ContentType}", normalizedMediaType); |
| | | 126 | | } |
| | | 127 | | |
| | 7 | 128 | | if (IsMultipartContentType(normalizedMediaType) && !mediaType.Boundary.HasValue) |
| | | 129 | | { |
| | 1 | 130 | | logger.Error("Missing multipart boundary for Content-Type: {ContentType}", normalizedMediaType); |
| | 1 | 131 | | throw new KrFormException("Missing multipart boundary.", StatusCodes.Status400BadRequest); |
| | | 132 | | } |
| | | 133 | | |
| | 6 | 134 | | return (mediaType, normalizedMediaType); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Parses the request body based on the normalized content type. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="context">The HTTP context.</param> |
| | | 141 | | /// <param name="mediaType">The parsed media type.</param> |
| | | 142 | | /// <param name="normalizedMediaType">The normalized media type string.</param> |
| | | 143 | | /// <param name="options">The form parsing options.</param> |
| | | 144 | | /// <param name="logger">The logger.</param> |
| | | 145 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 146 | | /// <returns>The parsed payload.</returns> |
| | | 147 | | private static Task<IKrFormPayload> ParseByContentTypeAsync( |
| | | 148 | | HttpContext context, |
| | | 149 | | MediaTypeHeaderValue mediaType, |
| | | 150 | | string normalizedMediaType, |
| | | 151 | | KrFormOptions options, |
| | | 152 | | Logger logger, |
| | | 153 | | CancellationToken cancellationToken) |
| | | 154 | | { |
| | | 155 | | // application/x-www-form-urlencoded |
| | 6 | 156 | | if (normalizedMediaType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)) |
| | | 157 | | { |
| | 3 | 158 | | return ParseUrlEncodedAsync(context, options, logger, cancellationToken); |
| | | 159 | | } |
| | | 160 | | // multipart/form-data |
| | 3 | 161 | | if (normalizedMediaType.Equals("multipart/form-data", StringComparison.OrdinalIgnoreCase)) |
| | | 162 | | { |
| | 0 | 163 | | return ParseMultipartFormDataAsync(context, mediaType, options, logger, cancellationToken); |
| | | 164 | | } |
| | | 165 | | // ordered multipart types |
| | 3 | 166 | | if (normalizedMediaType.StartsWith("multipart/", StringComparison.OrdinalIgnoreCase)) |
| | | 167 | | { |
| | 3 | 168 | | return ParseMultipartOrderedAsync(context, mediaType, options, logger, 0, cancellationToken); |
| | | 169 | | } |
| | | 170 | | // unsupported content type |
| | 0 | 171 | | throw new KrFormException("Unsupported Content-Type for form parsing.", StatusCodes.Status415UnsupportedMediaTyp |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Parses the incoming request into a normalized form payload. Synchronous wrapper. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <param name="context">The HTTP context.</param> |
| | | 178 | | /// <param name="options">The form parsing options.</param> |
| | | 179 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 180 | | /// <returns>The parsed payload.</returns> |
| | | 181 | | public static IKrFormPayload Parse(HttpContext context, KrFormOptions options, CancellationToken cancellationToken) |
| | 0 | 182 | | ParseAsync(context, options, cancellationToken).GetAwaiter().GetResult(); |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Applies the request body size limit based on the provided options. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="context">The HTTP context of the current request.</param> |
| | | 188 | | /// <param name="options">The form parsing options containing limits.</param> |
| | | 189 | | /// <param name="logger">The logger for diagnostic messages.</param> |
| | | 190 | | private static void ApplyRequestBodyLimit(HttpContext context, KrFormOptions options, Logger logger) |
| | | 191 | | { |
| | 6 | 192 | | if (!options.Limits.MaxRequestBodyBytes.HasValue) |
| | | 193 | | { |
| | 0 | 194 | | return; |
| | | 195 | | } |
| | | 196 | | |
| | 6 | 197 | | var feature = context.Features.Get<IHttpMaxRequestBodySizeFeature>(); |
| | 6 | 198 | | if (feature == null || feature.IsReadOnly) |
| | | 199 | | { |
| | 6 | 200 | | logger.Debug("Request body size feature not available or read-only."); |
| | 6 | 201 | | return; |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | feature.MaxRequestBodySize = options.Limits.MaxRequestBodyBytes; |
| | 0 | 205 | | logger.Debug("Set MaxRequestBodySize to {MaxBytes}", options.Limits.MaxRequestBodyBytes); |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | private static async Task<IKrFormPayload> ParseUrlEncodedAsync(HttpContext context, KrFormOptions options, Logger lo |
| | | 209 | | { |
| | 3 | 210 | | var payload = new KrFormData(); |
| | 3 | 211 | | var form = await context.Request.ReadFormAsync(cancellationToken).ConfigureAwait(false); |
| | 12 | 212 | | foreach (var key in form.Keys) |
| | | 213 | | { |
| | | 214 | | payload.Fields[key] = [.. form[key].Select(static v => v ?? string.Empty)]; |
| | | 215 | | } |
| | | 216 | | |
| | 3 | 217 | | var rules = CreateRuleMap(options, isRoot: true, scopeName: null); |
| | 3 | 218 | | ValidateRequiredRules(payload, rules, logger); |
| | | 219 | | |
| | 3 | 220 | | logger.Information("Parsed x-www-form-urlencoded payload with {FieldCount} fields.", payload.Fields.Count); |
| | 3 | 221 | | return payload; |
| | 3 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Parses a multipart/form-data payload from the request. |
| | | 226 | | /// </summary> |
| | | 227 | | /// <param name="context">The HTTP context.</param> |
| | | 228 | | /// <param name="mediaType">The media type header value.</param> |
| | | 229 | | /// <param name="options">The form parsing options.</param> |
| | | 230 | | /// <param name="logger">The logger for diagnostic messages.</param> |
| | | 231 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 232 | | /// <returns>The parsed payload.</returns> |
| | | 233 | | /// <exception cref="KrFormLimitExceededException">Thrown when the multipart form exceeds configured limits.</except |
| | | 234 | | /// <exception cref="KrFormException">Thrown when a part is rejected by policy or other form errors occur.</exceptio |
| | | 235 | | private static async Task<IKrFormPayload> ParseMultipartFormDataAsync(HttpContext context, MediaTypeHeaderValue medi |
| | | 236 | | { |
| | 0 | 237 | | var boundary = GetBoundary(mediaType); |
| | 0 | 238 | | var reader = new MultipartReader(boundary, context.Request.Body) |
| | 0 | 239 | | { |
| | 0 | 240 | | HeadersLengthLimit = options.Limits.MaxHeaderBytesPerPart |
| | 0 | 241 | | }; |
| | | 242 | | |
| | 0 | 243 | | var payload = new KrFormData(); |
| | 0 | 244 | | var rules = CreateRuleMap(options, isRoot: true, scopeName: null); |
| | 0 | 245 | | var partIndex = 0; |
| | 0 | 246 | | long totalBytes = 0; |
| | 0 | 247 | | var stopwatch = Stopwatch.StartNew(); |
| | | 248 | | |
| | | 249 | | MultipartSection? section; |
| | 0 | 250 | | while ((section = await reader.ReadNextSectionAsync(cancellationToken).ConfigureAwait(false)) != null) |
| | | 251 | | { |
| | 0 | 252 | | partIndex++; |
| | 0 | 253 | | if (partIndex > options.Limits.MaxParts) |
| | | 254 | | { |
| | 0 | 255 | | logger.Error("Multipart form exceeded MaxParts limit ({MaxParts}).", options.Limits.MaxParts); |
| | 0 | 256 | | throw new KrFormLimitExceededException("Too many multipart sections."); |
| | | 257 | | } |
| | 0 | 258 | | var partContext = BuildFormDataPartContext(section, rules, partIndex, logger); |
| | 0 | 259 | | LogFormDataPartDebug(logger, partContext, partIndex - 1); |
| | | 260 | | |
| | 0 | 261 | | var contentEncoding = partContext.ContentEncoding; |
| | 0 | 262 | | if (await HandleFormDataPartActionAsync(section, options, partContext, logger, contentEncoding, cancellation |
| | | 263 | | { |
| | | 264 | | continue; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | if (IsFilePart(partContext.FileName)) |
| | | 268 | | { |
| | 0 | 269 | | totalBytes += await ProcessFormDataFilePartAsync( |
| | 0 | 270 | | section, |
| | 0 | 271 | | options, |
| | 0 | 272 | | payload, |
| | 0 | 273 | | partContext, |
| | 0 | 274 | | logger, |
| | 0 | 275 | | cancellationToken).ConfigureAwait(false); |
| | 0 | 276 | | continue; |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | totalBytes += await ProcessFormDataFieldPartAsync( |
| | 0 | 280 | | section, |
| | 0 | 281 | | options, |
| | 0 | 282 | | payload, |
| | 0 | 283 | | partContext, |
| | 0 | 284 | | logger, |
| | 0 | 285 | | cancellationToken).ConfigureAwait(false); |
| | 0 | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | ValidateRequiredRules(payload, rules, logger); |
| | 0 | 289 | | stopwatch.Stop(); |
| | 0 | 290 | | logger.Information("Parsed multipart/form-data with {Parts} parts, {Files} files, {Bytes} bytes in {ElapsedMs} m |
| | | 291 | | partIndex, payload.Files.Sum(k => k.Value.Length), totalBytes, stopwatch.ElapsedMilliseconds); |
| | | 292 | | |
| | 0 | 293 | | return payload; |
| | 0 | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Builds the part context for multipart/form-data sections. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name="section">The multipart section.</param> |
| | | 300 | | /// <param name="rules">The form part rule map.</param> |
| | | 301 | | /// <param name="partIndex">The current part index (1-based).</param> |
| | | 302 | | /// <param name="logger">The logger instance.</param> |
| | | 303 | | /// <returns>The constructed part context.</returns> |
| | | 304 | | private static KrPartContext BuildFormDataPartContext( |
| | | 305 | | MultipartSection section, |
| | | 306 | | IReadOnlyDictionary<string, KrFormPartRule> rules, |
| | | 307 | | int partIndex, |
| | | 308 | | Logger logger) |
| | | 309 | | { |
| | 0 | 310 | | var headers = ToHeaderDictionary(section.Headers ?? []); |
| | 0 | 311 | | var (name, fileName, _) = GetContentDisposition(section, logger); |
| | 0 | 312 | | var contentType = section.ContentType ?? (string.IsNullOrWhiteSpace(fileName) ? "text/plain" : "application/octe |
| | 0 | 313 | | var contentEncoding = GetHeaderValue(headers, HeaderNames.ContentEncoding); |
| | 0 | 314 | | var declaredLength = GetHeaderLong(headers, HeaderNames.ContentLength); |
| | | 315 | | |
| | 0 | 316 | | var rule = name != null && rules.TryGetValue(name, out var match) ? match : null; |
| | 0 | 317 | | return new KrPartContext |
| | 0 | 318 | | { |
| | 0 | 319 | | Index = partIndex - 1, |
| | 0 | 320 | | Name = name, |
| | 0 | 321 | | FileName = fileName, |
| | 0 | 322 | | ContentType = contentType, |
| | 0 | 323 | | ContentEncoding = contentEncoding, |
| | 0 | 324 | | DeclaredLength = declaredLength, |
| | 0 | 325 | | Headers = headers, |
| | 0 | 326 | | Rule = rule |
| | 0 | 327 | | }; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Logs multipart/form-data part details when debug logging is enabled. |
| | | 332 | | /// </summary> |
| | | 333 | | /// <param name="logger">The logger instance.</param> |
| | | 334 | | /// <param name="partContext">The part context.</param> |
| | | 335 | | /// <param name="index">The 0-based part index.</param> |
| | | 336 | | private static void LogFormDataPartDebug(Logger logger, KrPartContext partContext, int index) |
| | | 337 | | { |
| | 0 | 338 | | if (!logger.IsEnabled(LogEventLevel.Debug)) |
| | | 339 | | { |
| | 0 | 340 | | return; |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | logger.Debug("Multipart part {Index} name={Name} filename={FileName} contentType={ContentType} contentEncoding={ |
| | 0 | 344 | | index, |
| | 0 | 345 | | partContext.Name, |
| | 0 | 346 | | partContext.FileName, |
| | 0 | 347 | | partContext.ContentType, |
| | 0 | 348 | | string.IsNullOrWhiteSpace(partContext.ContentEncoding) ? "<none>" : partContext.ContentEncoding, |
| | 0 | 349 | | partContext.DeclaredLength); |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <summary> |
| | | 353 | | /// Handles the OnPart hook for multipart/form-data sections. |
| | | 354 | | /// </summary> |
| | | 355 | | /// <param name="section">The multipart section.</param> |
| | | 356 | | /// <param name="options">The form options.</param> |
| | | 357 | | /// <param name="partContext">The part context.</param> |
| | | 358 | | /// <param name="logger">The logger instance.</param> |
| | | 359 | | /// <param name="contentEncoding">The content encoding.</param> |
| | | 360 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 361 | | /// <returns><c>true</c> when the caller should skip further processing for this section.</returns> |
| | | 362 | | private static async Task<bool> HandleFormDataPartActionAsync( |
| | | 363 | | MultipartSection section, |
| | | 364 | | KrFormOptions options, |
| | | 365 | | KrPartContext partContext, |
| | | 366 | | Logger logger, |
| | | 367 | | string? contentEncoding, |
| | | 368 | | CancellationToken cancellationToken) |
| | | 369 | | { |
| | 0 | 370 | | var action = await InvokeOnPartAsync(options, partContext, logger).ConfigureAwait(false); |
| | 0 | 371 | | if (action == KrPartAction.Reject) |
| | | 372 | | { |
| | 0 | 373 | | logger.Error("Part rejected by hook: {PartIndex}", partContext.Index); |
| | 0 | 374 | | throw new KrFormException("Part rejected by policy.", StatusCodes.Status400BadRequest); |
| | | 375 | | } |
| | | 376 | | |
| | 0 | 377 | | if (action == KrPartAction.Skip) |
| | | 378 | | { |
| | 0 | 379 | | logger.Warning("Part skipped by hook: {PartIndex}", partContext.Index); |
| | 0 | 380 | | await DrainSectionAsync(section.Body, options, contentEncoding, logger, cancellationToken).ConfigureAwait(fa |
| | 0 | 381 | | return true; |
| | | 382 | | } |
| | | 383 | | |
| | 0 | 384 | | return false; |
| | 0 | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary> |
| | | 388 | | /// Determines whether a part represents a file based on the file name. |
| | | 389 | | /// </summary> |
| | | 390 | | /// <param name="fileName">The file name from the part.</param> |
| | | 391 | | /// <returns><c>true</c> if the part is a file; otherwise <c>false</c>.</returns> |
| | | 392 | | private static bool IsFilePart(string? fileName) |
| | 0 | 393 | | => !string.IsNullOrWhiteSpace(fileName); |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Processes a file part in multipart/form-data payloads. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="section">The multipart section.</param> |
| | | 399 | | /// <param name="options">The form options.</param> |
| | | 400 | | /// <param name="payload">The form payload to populate.</param> |
| | | 401 | | /// <param name="partContext">The part context.</param> |
| | | 402 | | /// <param name="logger">The logger instance.</param> |
| | | 403 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 404 | | /// <returns>The number of bytes processed.</returns> |
| | | 405 | | private static async Task<long> ProcessFormDataFilePartAsync( |
| | | 406 | | MultipartSection section, |
| | | 407 | | KrFormOptions options, |
| | | 408 | | KrFormData payload, |
| | | 409 | | KrPartContext partContext, |
| | | 410 | | Logger logger, |
| | | 411 | | CancellationToken cancellationToken) |
| | | 412 | | { |
| | 0 | 413 | | ValidateFilePart(partContext.Name, partContext.FileName!, partContext.ContentType, partContext.Rule, payload, lo |
| | 0 | 414 | | var result = await StorePartAsync(section.Body, options, partContext.Rule, partContext.FileName, partContext.Con |
| | 0 | 415 | | .ConfigureAwait(false); |
| | | 416 | | |
| | 0 | 417 | | var filePart = new KrFilePart |
| | 0 | 418 | | { |
| | 0 | 419 | | Name = partContext.Name!, |
| | 0 | 420 | | OriginalFileName = partContext.FileName!, |
| | 0 | 421 | | ContentType = partContext.ContentType, |
| | 0 | 422 | | Length = result.Length, |
| | 0 | 423 | | TempPath = result.TempPath, |
| | 0 | 424 | | Sha256 = result.Sha256, |
| | 0 | 425 | | Headers = partContext.Headers |
| | 0 | 426 | | }; |
| | | 427 | | |
| | 0 | 428 | | AppendFile(payload.Files, filePart, partContext.Rule, logger); |
| | 0 | 429 | | LogStoredFilePart(logger, partContext, result); |
| | 0 | 430 | | return result.Length; |
| | 0 | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Processes a field part in multipart/form-data payloads. |
| | | 435 | | /// </summary> |
| | | 436 | | /// <param name="section">The multipart section.</param> |
| | | 437 | | /// <param name="options">The form options.</param> |
| | | 438 | | /// <param name="payload">The form payload to populate.</param> |
| | | 439 | | /// <param name="partContext">The part context.</param> |
| | | 440 | | /// <param name="logger">The logger instance.</param> |
| | | 441 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 442 | | /// <returns>The number of bytes processed.</returns> |
| | | 443 | | private static async Task<long> ProcessFormDataFieldPartAsync( |
| | | 444 | | MultipartSection section, |
| | | 445 | | KrFormOptions options, |
| | | 446 | | KrFormData payload, |
| | | 447 | | KrPartContext partContext, |
| | | 448 | | Logger logger, |
| | | 449 | | CancellationToken cancellationToken) |
| | | 450 | | { |
| | 0 | 451 | | if (string.IsNullOrWhiteSpace(partContext.Name)) |
| | | 452 | | { |
| | 0 | 453 | | logger.Error("Field part missing name."); |
| | 0 | 454 | | throw new KrFormException("Field part must include a name.", StatusCodes.Status400BadRequest); |
| | | 455 | | } |
| | | 456 | | |
| | 0 | 457 | | var value = await ReadFieldValueAsync(section.Body, options, partContext.ContentEncoding, logger, cancellationTo |
| | 0 | 458 | | .ConfigureAwait(false); |
| | 0 | 459 | | AppendField(payload.Fields, partContext.Name ?? string.Empty, value); |
| | 0 | 460 | | var bytes = Encoding.UTF8.GetByteCount(value); |
| | 0 | 461 | | logger.Debug("Parsed field part {Index} name={Name} bytes={Bytes}", partContext.Index, partContext.Name, bytes); |
| | 0 | 462 | | return bytes; |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Logs file-part storage results for multipart/form-data payloads. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <param name="logger">The logger instance.</param> |
| | | 469 | | /// <param name="partContext">The part context.</param> |
| | | 470 | | /// <param name="result">The stored part result.</param> |
| | | 471 | | private static void LogStoredFilePart(Logger logger, KrPartContext partContext, KrPartWriteResult result) |
| | | 472 | | { |
| | 0 | 473 | | if (string.IsNullOrWhiteSpace(result.TempPath)) |
| | | 474 | | { |
| | 0 | 475 | | logger.Warning("File part {Index} name={Name} was not stored to disk (bytes={Bytes}).", partContext.Index, p |
| | 0 | 476 | | return; |
| | | 477 | | } |
| | | 478 | | |
| | 0 | 479 | | logger.Information("Stored file part {Index} name={Name} filename={FileName} contentType={ContentType} bytes={By |
| | 0 | 480 | | partContext.Index, |
| | 0 | 481 | | partContext.Name, |
| | 0 | 482 | | partContext.FileName, |
| | 0 | 483 | | partContext.ContentType, |
| | 0 | 484 | | result.Length); |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | /// <summary> |
| | | 488 | | /// Parses an ordered multipart payload from the request. |
| | | 489 | | /// </summary> |
| | | 490 | | /// <param name="context">The current HTTP context.</param> |
| | | 491 | | /// <param name="mediaType">The media type of the request.</param> |
| | | 492 | | /// <param name="options">The form options for parsing.</param> |
| | | 493 | | /// <param name="logger">The logger instance.</param> |
| | | 494 | | /// <param name="nestingDepth">The current nesting depth for multipart parsing.</param> |
| | | 495 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 496 | | /// <returns>Returns the parsed multipart form payload.</returns> |
| | | 497 | | private static async Task<IKrFormPayload> ParseMultipartOrderedAsync(HttpContext context, MediaTypeHeaderValue media |
| | | 498 | | { |
| | 3 | 499 | | var boundary = GetBoundary(mediaType); |
| | 3 | 500 | | return await ParseMultipartFromStreamAsync(context.Request.Body, boundary, options, logger, nestingDepth, isRoot |
| | 2 | 501 | | } |
| | | 502 | | |
| | | 503 | | /// <summary> |
| | | 504 | | /// Parses a multipart payload from the provided stream. |
| | | 505 | | /// </summary> |
| | | 506 | | /// <param name="body">The input stream containing the multipart payload.</param> |
| | | 507 | | /// <param name="boundary">The multipart boundary string.</param> |
| | | 508 | | /// <param name="options">The form options for parsing.</param> |
| | | 509 | | /// <param name="logger">The logger instance.</param> |
| | | 510 | | /// <param name="nestingDepth">The current nesting depth for multipart parsing.</param> |
| | | 511 | | /// <param name="isRoot">Indicates if this is the root multipart payload.</param> |
| | | 512 | | /// <param name="scopeName">The current scope name, or null if root.</param> |
| | | 513 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 514 | | /// <returns>Returns the parsed multipart form payload.</returns> |
| | | 515 | | private static async Task<IKrFormPayload> ParseMultipartFromStreamAsync(Stream body, string boundary, KrFormOptions |
| | | 516 | | { |
| | 4 | 517 | | var reader = new MultipartReader(boundary, body) |
| | 4 | 518 | | { |
| | 4 | 519 | | HeadersLengthLimit = options.Limits.MaxHeaderBytesPerPart |
| | 4 | 520 | | }; |
| | | 521 | | |
| | 4 | 522 | | var payload = new KrMultipart(); |
| | 4 | 523 | | var rules = CreateRuleMap(options, isRoot, scopeName); |
| | 4 | 524 | | var partIndex = 0; |
| | 4 | 525 | | long totalBytes = 0; |
| | | 526 | | |
| | | 527 | | MultipartSection? section; |
| | 8 | 528 | | while ((section = await reader.ReadNextSectionAsync(cancellationToken).ConfigureAwait(false)) != null) |
| | | 529 | | { |
| | 5 | 530 | | partIndex++; |
| | 5 | 531 | | if (partIndex > options.Limits.MaxParts) |
| | | 532 | | { |
| | 0 | 533 | | logger.Error("Multipart payload exceeded MaxParts limit ({MaxParts}).", options.Limits.MaxParts); |
| | 0 | 534 | | throw new KrFormLimitExceededException("Too many multipart sections."); |
| | | 535 | | } |
| | | 536 | | |
| | 5 | 537 | | var partContext = BuildOrderedPartContext(section, rules, partIndex, logger); |
| | 4 | 538 | | LogOrderedPartDebug(logger, partContext, partIndex - 1); |
| | | 539 | | |
| | 4 | 540 | | var contentEncoding = partContext.ContentEncoding; |
| | 4 | 541 | | if (await HandleOrderedPartActionAsync(section, options, partContext, logger, contentEncoding, cancellationT |
| | | 542 | | { |
| | | 543 | | continue; |
| | | 544 | | } |
| | | 545 | | |
| | 4 | 546 | | var result = await StorePartAsync(section.Body, options, partContext.Rule, null, contentEncoding, logger, ca |
| | 4 | 547 | | totalBytes += result.Length; |
| | | 548 | | |
| | 4 | 549 | | var nested = await TryParseNestedPayloadAsync( |
| | 4 | 550 | | partContext, |
| | 4 | 551 | | result, |
| | 4 | 552 | | options, |
| | 4 | 553 | | logger, |
| | 4 | 554 | | nestingDepth, |
| | 4 | 555 | | cancellationToken).ConfigureAwait(false); |
| | | 556 | | |
| | 4 | 557 | | AddOrderedPart(payload, partContext, result, nested); |
| | 4 | 558 | | LogStoredOrderedPart(logger, partContext, partIndex - 1, result); |
| | 4 | 559 | | } |
| | | 560 | | |
| | 3 | 561 | | logger.Information("Parsed multipart ordered payload with {Parts} parts and {Bytes} bytes.", partIndex, totalByt |
| | 3 | 562 | | return payload; |
| | 3 | 563 | | } |
| | | 564 | | |
| | | 565 | | /// <summary> |
| | | 566 | | /// Builds the part context for an ordered multipart section. |
| | | 567 | | /// </summary> |
| | | 568 | | /// <param name="section">The multipart section.</param> |
| | | 569 | | /// <param name="rules">The form part rule map.</param> |
| | | 570 | | /// <param name="partIndex">The current part index (1-based).</param> |
| | | 571 | | /// <param name="logger">The logger instance.</param> |
| | | 572 | | /// <returns>The constructed part context.</returns> |
| | | 573 | | private static KrPartContext BuildOrderedPartContext( |
| | | 574 | | MultipartSection section, |
| | | 575 | | IReadOnlyDictionary<string, KrFormPartRule> rules, |
| | | 576 | | int partIndex, |
| | | 577 | | Logger logger) |
| | | 578 | | { |
| | 5 | 579 | | var headers = ToHeaderDictionary(section.Headers ?? []); |
| | 5 | 580 | | var contentType = section.ContentType ?? "application/octet-stream"; |
| | 5 | 581 | | var allowMissingDisposition = IsMultipartContentType(contentType); |
| | 5 | 582 | | var (name, fileName, _) = GetContentDisposition(section, logger, allowMissing: allowMissingDisposition); |
| | 4 | 583 | | var contentEncoding = GetHeaderValue(headers, HeaderNames.ContentEncoding); |
| | 4 | 584 | | var declaredLength = GetHeaderLong(headers, HeaderNames.ContentLength); |
| | | 585 | | |
| | 4 | 586 | | var rule = name != null && rules.TryGetValue(name, out var match) ? match : null; |
| | 4 | 587 | | return new KrPartContext |
| | 4 | 588 | | { |
| | 4 | 589 | | Index = partIndex - 1, |
| | 4 | 590 | | Name = name, |
| | 4 | 591 | | FileName = fileName, |
| | 4 | 592 | | ContentType = contentType, |
| | 4 | 593 | | ContentEncoding = contentEncoding, |
| | 4 | 594 | | DeclaredLength = declaredLength, |
| | 4 | 595 | | Headers = headers, |
| | 4 | 596 | | Rule = rule |
| | 4 | 597 | | }; |
| | | 598 | | } |
| | | 599 | | |
| | | 600 | | /// <summary> |
| | | 601 | | /// Logs ordered multipart part details when debug logging is enabled. |
| | | 602 | | /// </summary> |
| | | 603 | | /// <param name="logger">The logger instance.</param> |
| | | 604 | | /// <param name="partContext">The part context.</param> |
| | | 605 | | /// <param name="index">The 0-based part index.</param> |
| | | 606 | | private static void LogOrderedPartDebug(Logger logger, KrPartContext partContext, int index) |
| | | 607 | | { |
| | 4 | 608 | | if (!logger.IsEnabled(LogEventLevel.Debug)) |
| | | 609 | | { |
| | 4 | 610 | | return; |
| | | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | logger.Debug("Ordered part {Index} name={Name} filename={FileName} contentType={ContentType} contentEncoding={Co |
| | 0 | 614 | | index, |
| | 0 | 615 | | partContext.Name, |
| | 0 | 616 | | partContext.FileName, |
| | 0 | 617 | | partContext.ContentType, |
| | 0 | 618 | | string.IsNullOrWhiteSpace(partContext.ContentEncoding) ? "<none>" : partContext.ContentEncoding, |
| | 0 | 619 | | partContext.DeclaredLength); |
| | 0 | 620 | | } |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Handles the OnPart hook for ordered multipart sections. |
| | | 624 | | /// </summary> |
| | | 625 | | /// <param name="section">The multipart section.</param> |
| | | 626 | | /// <param name="options">The form options.</param> |
| | | 627 | | /// <param name="partContext">The part context.</param> |
| | | 628 | | /// <param name="logger">The logger instance.</param> |
| | | 629 | | /// <param name="contentEncoding">The content encoding.</param> |
| | | 630 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 631 | | /// <returns><c>true</c> when the caller should skip further processing for this section.</returns> |
| | | 632 | | private static async Task<bool> HandleOrderedPartActionAsync( |
| | | 633 | | MultipartSection section, |
| | | 634 | | KrFormOptions options, |
| | | 635 | | KrPartContext partContext, |
| | | 636 | | Logger logger, |
| | | 637 | | string? contentEncoding, |
| | | 638 | | CancellationToken cancellationToken) |
| | | 639 | | { |
| | 4 | 640 | | var action = await InvokeOnPartAsync(options, partContext, logger).ConfigureAwait(false); |
| | 4 | 641 | | if (action == KrPartAction.Reject) |
| | | 642 | | { |
| | 0 | 643 | | logger.Error("Ordered part rejected by hook: {PartIndex}", partContext.Index); |
| | 0 | 644 | | throw new KrFormException("Part rejected by policy.", StatusCodes.Status400BadRequest); |
| | | 645 | | } |
| | | 646 | | |
| | 4 | 647 | | if (action == KrPartAction.Skip) |
| | | 648 | | { |
| | 0 | 649 | | logger.Warning("Ordered part skipped by hook: {PartIndex}", partContext.Index); |
| | 0 | 650 | | await DrainSectionAsync(section.Body, options, contentEncoding, logger, cancellationToken).ConfigureAwait(fa |
| | 0 | 651 | | return true; |
| | | 652 | | } |
| | | 653 | | |
| | 4 | 654 | | return false; |
| | 4 | 655 | | } |
| | | 656 | | |
| | | 657 | | /// <summary> |
| | | 658 | | /// Attempts to parse a nested multipart payload when the part content type is multipart. |
| | | 659 | | /// </summary> |
| | | 660 | | /// <param name="partContext">The part context.</param> |
| | | 661 | | /// <param name="result">The stored part result.</param> |
| | | 662 | | /// <param name="options">The form options.</param> |
| | | 663 | | /// <param name="logger">The logger instance.</param> |
| | | 664 | | /// <param name="nestingDepth">The current nesting depth.</param> |
| | | 665 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 666 | | /// <returns>The nested payload, or null if none was parsed.</returns> |
| | | 667 | | private static async Task<IKrFormPayload?> TryParseNestedPayloadAsync( |
| | | 668 | | KrPartContext partContext, |
| | | 669 | | KrPartWriteResult result, |
| | | 670 | | KrFormOptions options, |
| | | 671 | | Logger logger, |
| | | 672 | | int nestingDepth, |
| | | 673 | | CancellationToken cancellationToken) |
| | | 674 | | { |
| | 4 | 675 | | if (!IsMultipartContentType(partContext.ContentType)) |
| | | 676 | | { |
| | 3 | 677 | | return null; |
| | | 678 | | } |
| | | 679 | | |
| | 1 | 680 | | if (nestingDepth >= options.Limits.MaxNestingDepth) |
| | | 681 | | { |
| | 0 | 682 | | logger.Error("Nested multipart depth exceeded limit {MaxDepth}.", options.Limits.MaxNestingDepth); |
| | 0 | 683 | | throw new KrFormLimitExceededException("Nested multipart depth exceeded."); |
| | | 684 | | } |
| | | 685 | | |
| | 1 | 686 | | if (!TryGetBoundary(partContext.ContentType, out var nestedBoundary)) |
| | | 687 | | { |
| | 0 | 688 | | logger.Warning("Nested multipart part missing boundary header."); |
| | 0 | 689 | | return null; |
| | | 690 | | } |
| | | 691 | | |
| | 1 | 692 | | if (string.IsNullOrWhiteSpace(result.TempPath)) |
| | | 693 | | { |
| | 0 | 694 | | logger.Warning("Nested multipart part was not stored to disk; skipping nested parse."); |
| | 0 | 695 | | return null; |
| | | 696 | | } |
| | | 697 | | |
| | 1 | 698 | | await using var nestedStream = File.OpenRead(result.TempPath); |
| | 1 | 699 | | return await ParseMultipartFromStreamAsync( |
| | 1 | 700 | | nestedStream, |
| | 1 | 701 | | nestedBoundary, |
| | 1 | 702 | | options, |
| | 1 | 703 | | logger, |
| | 1 | 704 | | nestingDepth + 1, |
| | 1 | 705 | | isRoot: false, |
| | 1 | 706 | | scopeName: partContext.Name, |
| | 1 | 707 | | cancellationToken).ConfigureAwait(false); |
| | 4 | 708 | | } |
| | | 709 | | |
| | | 710 | | /// <summary> |
| | | 711 | | /// Adds a parsed ordered part to the payload. |
| | | 712 | | /// </summary> |
| | | 713 | | /// <param name="payload">The multipart payload.</param> |
| | | 714 | | /// <param name="partContext">The part context.</param> |
| | | 715 | | /// <param name="result">The stored part result.</param> |
| | | 716 | | /// <param name="nested">The nested payload.</param> |
| | | 717 | | private static void AddOrderedPart(KrMultipart payload, KrPartContext partContext, KrPartWriteResult result, IKrForm |
| | | 718 | | { |
| | 4 | 719 | | payload.Parts.Add(new KrRawPart |
| | 4 | 720 | | { |
| | 4 | 721 | | Name = partContext.Name, |
| | 4 | 722 | | ContentType = partContext.ContentType, |
| | 4 | 723 | | Length = result.Length, |
| | 4 | 724 | | TempPath = result.TempPath, |
| | 4 | 725 | | Headers = partContext.Headers, |
| | 4 | 726 | | NestedPayload = nested |
| | 4 | 727 | | }); |
| | 4 | 728 | | } |
| | | 729 | | |
| | | 730 | | /// <summary> |
| | | 731 | | /// Logs ordered multipart part storage results. |
| | | 732 | | /// </summary> |
| | | 733 | | /// <param name="logger">The logger instance.</param> |
| | | 734 | | /// <param name="partContext">The part context.</param> |
| | | 735 | | /// <param name="index">The 0-based part index.</param> |
| | | 736 | | /// <param name="result">The stored part result.</param> |
| | | 737 | | private static void LogStoredOrderedPart(Logger logger, KrPartContext partContext, int index, KrPartWriteResult resu |
| | | 738 | | { |
| | 4 | 739 | | if (string.IsNullOrWhiteSpace(result.TempPath)) |
| | | 740 | | { |
| | 3 | 741 | | logger.Warning("Ordered part {Index} name={Name} was not stored to disk (bytes={Bytes}).", index, partContex |
| | 3 | 742 | | return; |
| | | 743 | | } |
| | | 744 | | |
| | 1 | 745 | | logger.Information("Stored ordered part {Index} name={Name} contentType={ContentType} bytes={Bytes}", index, par |
| | 1 | 746 | | } |
| | | 747 | | |
| | | 748 | | /// <summary> |
| | | 749 | | /// Stores a multipart part to disk or consumes it based on the provided options and rules. |
| | | 750 | | /// </summary> |
| | | 751 | | /// <param name="body">The input stream of the multipart part.</param> |
| | | 752 | | /// <param name="options">The form options for parsing.</param> |
| | | 753 | | /// <param name="rule">The form part rule, if any.</param> |
| | | 754 | | /// <param name="originalFileName">The original file name of the part, if any.</param> |
| | | 755 | | /// <param name="contentEncoding">The content encoding of the part, if any.</param> |
| | | 756 | | /// <param name="logger">The logger instance.</param> |
| | | 757 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 758 | | /// <returns>Returns the result of storing the part.</returns> |
| | | 759 | | private static async Task<KrPartWriteResult> StorePartAsync(Stream body, KrFormOptions options, KrFormPartRule? rule |
| | | 760 | | { |
| | 4 | 761 | | var maxBytes = rule?.MaxBytes ?? options.Limits.MaxPartBodyBytes; |
| | 4 | 762 | | var effectiveMax = options.EnablePartDecompression ? Math.Min(maxBytes, options.MaxDecompressedBytesPerPart) : m |
| | | 763 | | |
| | 4 | 764 | | var source = body; |
| | 4 | 765 | | if (options.EnablePartDecompression) |
| | | 766 | | { |
| | 0 | 767 | | var (decoded, normalizedEncoding) = KrPartDecompression.CreateDecodedStream(body, contentEncoding); |
| | 0 | 768 | | if (!IsEncodingAllowed(normalizedEncoding, options.AllowedPartContentEncodings)) |
| | | 769 | | { |
| | 0 | 770 | | var message = $"Unsupported Content-Encoding '{normalizedEncoding}' for multipart part."; |
| | 0 | 771 | | if (options.RejectUnknownContentEncoding) |
| | | 772 | | { |
| | 0 | 773 | | logger.Error(message); |
| | 0 | 774 | | throw new KrFormException(message, StatusCodes.Status415UnsupportedMediaType); |
| | | 775 | | } |
| | 0 | 776 | | logger.Warning(message); |
| | | 777 | | } |
| | | 778 | | else |
| | | 779 | | { |
| | 0 | 780 | | logger.Debug("Part-level decompression enabled for encoding {Encoding}.", normalizedEncoding); |
| | | 781 | | } |
| | 0 | 782 | | source = decoded; |
| | | 783 | | } |
| | 4 | 784 | | else if (!string.IsNullOrWhiteSpace(contentEncoding) && !contentEncoding.Equals("identity", StringComparison.Ord |
| | | 785 | | { |
| | 0 | 786 | | var message = $"Part Content-Encoding '{contentEncoding}' was supplied but part decompression is disabled."; |
| | 0 | 787 | | if (options.RejectUnknownContentEncoding) |
| | | 788 | | { |
| | 0 | 789 | | logger.Error(message); |
| | 0 | 790 | | throw new KrFormException(message, StatusCodes.Status415UnsupportedMediaType); |
| | | 791 | | } |
| | 0 | 792 | | logger.Warning(message); |
| | | 793 | | } |
| | | 794 | | |
| | 4 | 795 | | await using var limited = new LimitedReadStream(source, effectiveMax); |
| | | 796 | | |
| | 4 | 797 | | if (rule?.StoreToDisk == false) |
| | | 798 | | { |
| | 3 | 799 | | var length = await ConsumeStreamAsync(limited, cancellationToken).ConfigureAwait(false); |
| | 3 | 800 | | return new KrPartWriteResult |
| | 3 | 801 | | { |
| | 3 | 802 | | TempPath = string.Empty, |
| | 3 | 803 | | Length = length, |
| | 3 | 804 | | Sha256 = null |
| | 3 | 805 | | }; |
| | | 806 | | } |
| | | 807 | | |
| | 1 | 808 | | var targetPath = rule?.DestinationPath ?? options.DefaultUploadPath; |
| | 1 | 809 | | _ = Directory.CreateDirectory(targetPath); |
| | 1 | 810 | | var sanitizedFileName = string.IsNullOrWhiteSpace(originalFileName) ? null : options.SanitizeFileName(originalFi |
| | 1 | 811 | | var sink = new KrDiskPartSink(targetPath, options.ComputeSha256, sanitizedFileName); |
| | 1 | 812 | | return await sink.WriteAsync(limited, cancellationToken).ConfigureAwait(false); |
| | 4 | 813 | | } |
| | | 814 | | |
| | | 815 | | private static async Task<string> ReadFieldValueAsync(Stream body, KrFormOptions options, string? contentEncoding, L |
| | | 816 | | { |
| | 0 | 817 | | var source = body; |
| | 0 | 818 | | if (options.EnablePartDecompression) |
| | | 819 | | { |
| | 0 | 820 | | var (decoded, normalizedEncoding) = KrPartDecompression.CreateDecodedStream(body, contentEncoding); |
| | 0 | 821 | | if (!IsEncodingAllowed(normalizedEncoding, options.AllowedPartContentEncodings)) |
| | | 822 | | { |
| | 0 | 823 | | var message = $"Unsupported Content-Encoding '{normalizedEncoding}' for multipart field."; |
| | 0 | 824 | | if (options.RejectUnknownContentEncoding) |
| | | 825 | | { |
| | 0 | 826 | | logger.Error(message); |
| | 0 | 827 | | throw new KrFormException(message, StatusCodes.Status415UnsupportedMediaType); |
| | | 828 | | } |
| | 0 | 829 | | logger.Warning(message); |
| | | 830 | | } |
| | | 831 | | else |
| | | 832 | | { |
| | 0 | 833 | | logger.Debug("Field-level decompression enabled for encoding {Encoding}.", normalizedEncoding); |
| | | 834 | | } |
| | 0 | 835 | | source = decoded; |
| | | 836 | | } |
| | | 837 | | |
| | 0 | 838 | | await using var limited = new LimitedReadStream(source, options.Limits.MaxFieldValueBytes); |
| | 0 | 839 | | using var reader = new StreamReader(limited, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: f |
| | 0 | 840 | | var value = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 841 | | return value; |
| | 0 | 842 | | } |
| | | 843 | | |
| | | 844 | | private static async Task DrainSectionAsync(Stream body, KrFormOptions options, string? contentEncoding, Logger logg |
| | | 845 | | { |
| | 0 | 846 | | var source = body; |
| | 0 | 847 | | if (options.EnablePartDecompression) |
| | | 848 | | { |
| | 0 | 849 | | var (decoded, normalizedEncoding) = KrPartDecompression.CreateDecodedStream(body, contentEncoding); |
| | 0 | 850 | | source = decoded; |
| | 0 | 851 | | logger.Debug("Draining part with encoding {Encoding}.", normalizedEncoding); |
| | | 852 | | } |
| | | 853 | | |
| | 0 | 854 | | await using var limited = new LimitedReadStream(source, options.Limits.MaxPartBodyBytes); |
| | 0 | 855 | | await limited.CopyToAsync(Stream.Null, cancellationToken).ConfigureAwait(false); |
| | 0 | 856 | | } |
| | | 857 | | |
| | | 858 | | private static async Task<long> ConsumeStreamAsync(Stream body, CancellationToken cancellationToken) |
| | | 859 | | { |
| | 3 | 860 | | var buffer = new byte[81920]; |
| | 3 | 861 | | long total = 0; |
| | | 862 | | int read; |
| | 6 | 863 | | while ((read = await body.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0) |
| | | 864 | | { |
| | 3 | 865 | | total += read; |
| | | 866 | | } |
| | 3 | 867 | | return total; |
| | 3 | 868 | | } |
| | | 869 | | |
| | | 870 | | private static void ValidateFilePart(string? name, string fileName, string contentType, KrFormPartRule? rule, KrForm |
| | | 871 | | { |
| | 0 | 872 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 873 | | { |
| | 0 | 874 | | logger.Error("File part missing name."); |
| | 0 | 875 | | throw new KrFormException("File part must include a name.", StatusCodes.Status400BadRequest); |
| | | 876 | | } |
| | | 877 | | |
| | 0 | 878 | | if (rule == null) |
| | | 879 | | { |
| | 0 | 880 | | return; |
| | | 881 | | } |
| | | 882 | | |
| | 0 | 883 | | if (!rule.AllowMultiple && payload.Files.ContainsKey(name)) |
| | | 884 | | { |
| | 0 | 885 | | logger.Error("Part rule disallows multiple files for name {Name}.", name); |
| | 0 | 886 | | throw new KrFormException($"Multiple files not allowed for '{name}'.", StatusCodes.Status400BadRequest); |
| | | 887 | | } |
| | | 888 | | |
| | 0 | 889 | | if (rule.AllowedContentTypes.Count > 0 && !IsAllowedRequestContentType(contentType, rule.AllowedContentTypes)) |
| | | 890 | | { |
| | 0 | 891 | | logger.Error("Rejected content type {ContentType} for part {Name}.", contentType, name); |
| | 0 | 892 | | throw new KrFormException("Content type is not allowed for this part.", StatusCodes.Status415UnsupportedMedi |
| | | 893 | | } |
| | | 894 | | |
| | 0 | 895 | | if (rule.AllowedExtensions.Count > 0) |
| | | 896 | | { |
| | 0 | 897 | | var ext = Path.GetExtension(fileName); |
| | 0 | 898 | | if (string.IsNullOrWhiteSpace(ext) || !rule.AllowedExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase |
| | | 899 | | { |
| | 0 | 900 | | logger.Error("Rejected extension {Extension} for part {Name}.", ext, name); |
| | 0 | 901 | | throw new KrFormException("File extension is not allowed for this part.", StatusCodes.Status400BadReques |
| | | 902 | | } |
| | | 903 | | } |
| | | 904 | | |
| | 0 | 905 | | if (rule.MaxBytes.HasValue && rule.MaxBytes.Value <= 0) |
| | | 906 | | { |
| | 0 | 907 | | logger.Warning("Part rule for {Name} has non-positive MaxBytes.", name); |
| | | 908 | | } |
| | 0 | 909 | | } |
| | | 910 | | |
| | | 911 | | private static void AppendFile(Dictionary<string, KrFilePart[]> files, KrFilePart part, KrFormPartRule? rule, Logger |
| | | 912 | | { |
| | 0 | 913 | | files[part.Name] = files.TryGetValue(part.Name, out var existing) |
| | 0 | 914 | | ? [.. existing, part] |
| | 0 | 915 | | : [part]; |
| | | 916 | | |
| | 0 | 917 | | if (rule != null && !rule.AllowMultiple && files[part.Name].Length > 1) |
| | | 918 | | { |
| | 0 | 919 | | logger.Error("Rule disallows multiple files for {Name}.", part.Name); |
| | 0 | 920 | | throw new KrFormException($"Multiple files not allowed for '{part.Name}'.", StatusCodes.Status400BadRequest) |
| | | 921 | | } |
| | 0 | 922 | | } |
| | | 923 | | |
| | | 924 | | private static void AppendField(Dictionary<string, string[]> fields, string name, string value) |
| | | 925 | | { |
| | 0 | 926 | | fields[name] = fields.TryGetValue(name, out var existing) |
| | 0 | 927 | | ? [.. existing, value] |
| | 0 | 928 | | : [value]; |
| | 0 | 929 | | } |
| | | 930 | | |
| | | 931 | | private static void ValidateRequiredRules(KrFormData payload, Dictionary<string, KrFormPartRule> rules, Logger logge |
| | | 932 | | { |
| | 6 | 933 | | foreach (var rule in rules.Values) |
| | | 934 | | { |
| | 0 | 935 | | if (!rule.Required) |
| | | 936 | | { |
| | | 937 | | continue; |
| | | 938 | | } |
| | | 939 | | |
| | 0 | 940 | | var hasField = payload.Fields.ContainsKey(rule.Name); |
| | 0 | 941 | | var hasFile = payload.Files.ContainsKey(rule.Name); |
| | 0 | 942 | | if (!hasField && !hasFile) |
| | | 943 | | { |
| | 0 | 944 | | logger.Error("Required form part missing: {Name}", rule.Name); |
| | 0 | 945 | | throw new KrFormException($"Required form part '{rule.Name}' missing.", StatusCodes.Status400BadRequest) |
| | | 946 | | } |
| | | 947 | | } |
| | 3 | 948 | | } |
| | | 949 | | |
| | | 950 | | private static Dictionary<string, KrFormPartRule> CreateRuleMap(KrFormOptions options, bool isRoot, string? scopeNam |
| | | 951 | | { |
| | 7 | 952 | | var map = new Dictionary<string, KrFormPartRule>(StringComparer.OrdinalIgnoreCase); |
| | 30 | 953 | | foreach (var rule in options.Rules) |
| | | 954 | | { |
| | 8 | 955 | | if (!IsRuleInScope(rule, isRoot, scopeName)) |
| | | 956 | | { |
| | | 957 | | continue; |
| | | 958 | | } |
| | 6 | 959 | | map[rule.Name] = rule; |
| | | 960 | | } |
| | 7 | 961 | | return map; |
| | | 962 | | } |
| | | 963 | | |
| | | 964 | | /// <summary> |
| | | 965 | | /// Determines if a rule applies to the current scope. |
| | | 966 | | /// </summary> |
| | | 967 | | /// <param name="rule">The form part rule.</param> |
| | | 968 | | /// <param name="isRoot">Indicates if the current scope is the root.</param> |
| | | 969 | | /// <param name="scopeName">The current scope name, or null if root.</param> |
| | | 970 | | /// <returns>True if the rule is in scope; otherwise, false.</returns> |
| | | 971 | | private static bool IsRuleInScope(KrFormPartRule rule, bool isRoot, string? scopeName) |
| | | 972 | | { |
| | 8 | 973 | | var ruleScope = string.IsNullOrWhiteSpace(rule.Scope) ? null : rule.Scope; |
| | 8 | 974 | | return isRoot |
| | 8 | 975 | | ? ruleScope is null |
| | 8 | 976 | | : !string.IsNullOrWhiteSpace(scopeName) && string.Equals(ruleScope, scopeName, StringComparison.OrdinalIgnor |
| | | 977 | | } |
| | | 978 | | |
| | | 979 | | private static (string? Name, string? FileName, ContentDispositionHeaderValue? Disposition) GetContentDisposition(Mu |
| | | 980 | | { |
| | 5 | 981 | | if (string.IsNullOrWhiteSpace(section.ContentDisposition)) |
| | | 982 | | { |
| | 1 | 983 | | if (allowMissing) |
| | | 984 | | { |
| | 0 | 985 | | return (null, null, null); |
| | | 986 | | } |
| | | 987 | | |
| | 1 | 988 | | logger.Error("Multipart section missing Content-Disposition header."); |
| | 1 | 989 | | throw new KrFormException("Missing Content-Disposition header.", StatusCodes.Status400BadRequest); |
| | | 990 | | } |
| | | 991 | | |
| | 4 | 992 | | if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var disposition)) |
| | | 993 | | { |
| | 0 | 994 | | logger.Error("Invalid Content-Disposition header: {Header}", section.ContentDisposition); |
| | 0 | 995 | | throw new KrFormException("Invalid Content-Disposition header.", StatusCodes.Status400BadRequest); |
| | | 996 | | } |
| | | 997 | | |
| | 4 | 998 | | var name = disposition.Name.HasValue ? HeaderUtilities.RemoveQuotes(disposition.Name).Value : null; |
| | 4 | 999 | | var fileName = disposition.FileNameStar.HasValue |
| | 4 | 1000 | | ? HeaderUtilities.RemoveQuotes(disposition.FileNameStar).Value |
| | 4 | 1001 | | : disposition.FileName.HasValue ? HeaderUtilities.RemoveQuotes(disposition.FileName).Value : null; |
| | | 1002 | | |
| | 4 | 1003 | | return (name, fileName, disposition); |
| | | 1004 | | } |
| | | 1005 | | |
| | | 1006 | | private static string GetBoundary(MediaTypeHeaderValue mediaType) |
| | | 1007 | | { |
| | 3 | 1008 | | if (!mediaType.Boundary.HasValue) |
| | | 1009 | | { |
| | 0 | 1010 | | throw new KrFormException("Missing multipart boundary.", StatusCodes.Status400BadRequest); |
| | | 1011 | | } |
| | | 1012 | | |
| | 3 | 1013 | | var boundary = HeaderUtilities.RemoveQuotes(mediaType.Boundary).Value; |
| | 3 | 1014 | | return string.IsNullOrWhiteSpace(boundary) |
| | 3 | 1015 | | ? throw new KrFormException("Missing multipart boundary.", StatusCodes.Status400BadRequest) |
| | 3 | 1016 | | : boundary; |
| | | 1017 | | } |
| | | 1018 | | |
| | | 1019 | | private static bool TryGetBoundary(string contentType, out string boundary) |
| | | 1020 | | { |
| | 1 | 1021 | | boundary = string.Empty; |
| | 1 | 1022 | | if (!MediaTypeHeaderValue.TryParse(contentType, out var mediaType)) |
| | | 1023 | | { |
| | 0 | 1024 | | return false; |
| | | 1025 | | } |
| | | 1026 | | |
| | 1 | 1027 | | if (!mediaType.Boundary.HasValue) |
| | | 1028 | | { |
| | 0 | 1029 | | return false; |
| | | 1030 | | } |
| | | 1031 | | |
| | 1 | 1032 | | var parsed = HeaderUtilities.RemoveQuotes(mediaType.Boundary).Value; |
| | 1 | 1033 | | if (string.IsNullOrWhiteSpace(parsed)) |
| | | 1034 | | { |
| | 0 | 1035 | | return false; |
| | | 1036 | | } |
| | | 1037 | | |
| | 1 | 1038 | | boundary = parsed; |
| | 1 | 1039 | | return true; |
| | | 1040 | | } |
| | | 1041 | | |
| | | 1042 | | private static Dictionary<string, string[]> ToHeaderDictionary(IEnumerable<KeyValuePair<string, Microsoft.Extensions |
| | | 1043 | | { |
| | 5 | 1044 | | var dict = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase); |
| | 28 | 1045 | | foreach (var header in headers) |
| | | 1046 | | { |
| | 18 | 1047 | | dict[header.Key] = [.. header.Value.Select(static v => v ?? string.Empty)]; |
| | | 1048 | | } |
| | 5 | 1049 | | return dict; |
| | | 1050 | | } |
| | | 1051 | | |
| | | 1052 | | private static string? GetHeaderValue(IReadOnlyDictionary<string, string[]> headers, string name) |
| | 4 | 1053 | | => headers.TryGetValue(name, out var values) ? values.FirstOrDefault() : null; |
| | | 1054 | | |
| | | 1055 | | private static long? GetHeaderLong(IReadOnlyDictionary<string, string[]> headers, string name) |
| | 4 | 1056 | | => headers.TryGetValue(name, out var values) && long.TryParse(values.FirstOrDefault(), out var result) |
| | 4 | 1057 | | ? result |
| | 4 | 1058 | | : null; |
| | | 1059 | | private static bool IsAllowedRequestContentType(string contentType, IEnumerable<string> allowed) |
| | | 1060 | | { |
| | 39 | 1061 | | foreach (var allowedType in allowed) |
| | | 1062 | | { |
| | 15 | 1063 | | if (string.IsNullOrWhiteSpace(allowedType)) |
| | | 1064 | | { |
| | | 1065 | | continue; |
| | | 1066 | | } |
| | | 1067 | | |
| | 15 | 1068 | | if (allowedType.EndsWith("/*", StringComparison.Ordinal)) |
| | | 1069 | | { |
| | 0 | 1070 | | var prefix = allowedType[..^1]; |
| | 0 | 1071 | | if (contentType.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| | | 1072 | | { |
| | 0 | 1073 | | return true; |
| | | 1074 | | } |
| | | 1075 | | } |
| | 15 | 1076 | | else if (contentType.Equals(allowedType, StringComparison.OrdinalIgnoreCase)) |
| | | 1077 | | { |
| | 7 | 1078 | | return true; |
| | | 1079 | | } |
| | | 1080 | | } |
| | 1 | 1081 | | return false; |
| | 7 | 1082 | | } |
| | | 1083 | | |
| | | 1084 | | private static bool IsMultipartContentType(string contentType) |
| | 16 | 1085 | | => contentType.StartsWith("multipart/", StringComparison.OrdinalIgnoreCase); |
| | | 1086 | | |
| | | 1087 | | private static bool IsEncodingAllowed(string encoding, IEnumerable<string> allowed) |
| | 0 | 1088 | | => allowed.Any(a => string.Equals(a, encoding, StringComparison.OrdinalIgnoreCase)); |
| | | 1089 | | |
| | | 1090 | | private static bool DetectRequestDecompressionEnabled(HttpContext context) |
| | | 1091 | | { |
| | 8 | 1092 | | var type = Type.GetType("Microsoft.AspNetCore.RequestDecompression.IRequestDecompressionProvider, Microsoft.AspN |
| | 8 | 1093 | | return type is not null && context.RequestServices.GetService(type) is not null; |
| | | 1094 | | } |
| | | 1095 | | |
| | | 1096 | | private static async ValueTask<KrPartAction> InvokeOnPartAsync(KrFormOptions options, KrPartContext context, Logger |
| | | 1097 | | { |
| | 4 | 1098 | | if (options.OnPart == null) |
| | | 1099 | | { |
| | 4 | 1100 | | return KrPartAction.Continue; |
| | | 1101 | | } |
| | | 1102 | | |
| | | 1103 | | try |
| | | 1104 | | { |
| | 0 | 1105 | | return await options.OnPart(context).ConfigureAwait(false); |
| | | 1106 | | } |
| | 0 | 1107 | | catch (Exception ex) |
| | | 1108 | | { |
| | 0 | 1109 | | logger.Error(ex, "Part hook failed for part {Index}.", context.Index); |
| | 0 | 1110 | | throw new KrFormException("Part hook failed.", StatusCodes.Status400BadRequest); |
| | | 1111 | | } |
| | 4 | 1112 | | } |
| | | 1113 | | } |
| | | 1114 | | |
| | | 1115 | | internal static class LoggerExtensions |
| | | 1116 | | { |
| | | 1117 | | /// <summary> |
| | | 1118 | | /// Adds a simple timed logging scope. |
| | | 1119 | | /// </summary> |
| | | 1120 | | /// <param name="logger">The logger.</param> |
| | | 1121 | | /// <param name="operation">The operation name.</param> |
| | | 1122 | | /// <returns>The disposable scope.</returns> |
| | | 1123 | | public static IDisposable BeginTimedOperation(this Logger logger, string operation) |
| | | 1124 | | => new TimedOperation(logger, operation); |
| | | 1125 | | |
| | | 1126 | | private sealed class TimedOperation : IDisposable |
| | | 1127 | | { |
| | | 1128 | | private readonly Logger _logger; |
| | | 1129 | | private readonly string _operation; |
| | | 1130 | | private readonly Stopwatch _stopwatch; |
| | | 1131 | | |
| | | 1132 | | public TimedOperation(Logger logger, string operation) |
| | | 1133 | | { |
| | | 1134 | | _logger = logger; |
| | | 1135 | | _operation = operation; |
| | | 1136 | | _stopwatch = Stopwatch.StartNew(); |
| | | 1137 | | if (_logger.IsEnabled(LogEventLevel.Information)) |
| | | 1138 | | { |
| | | 1139 | | _logger.Information("Form parsing started: {Operation}", _operation); |
| | | 1140 | | } |
| | | 1141 | | } |
| | | 1142 | | |
| | | 1143 | | public void Dispose() |
| | | 1144 | | { |
| | | 1145 | | _stopwatch.Stop(); |
| | | 1146 | | if (_logger.IsEnabled(LogEventLevel.Information)) |
| | | 1147 | | { |
| | | 1148 | | _logger.Information("Form parsing completed: {Operation} in {ElapsedMs} ms", _operation, _stopwatch.Elap |
| | | 1149 | | } |
| | | 1150 | | } |
| | | 1151 | | } |
| | | 1152 | | } |