using Microsoft.Extensions.Primitives; namespace ErsatzTV.Middleware; /// /// Builds the request path Serilog logs with the access_token query value redacted (#559). /// The default UseSerilogRequestLogging template logs the full query for every request; the /// ex != null and StatusCode > 499 branches log at Error regardless of path, so a 5xx /// on an /iptv/...?access_token=<jwt> URL would otherwise write the replayable, globally /// scoped token to the log. This keeps the rest of the query (e.g. mode=segmenter) for /// debugging while masking only the credential. /// internal static class RequestLogScrubber { private const string TokenParameter = "access_token"; private const string Redacted = "***"; public static string ScrubbedPath(HttpRequest request) { string path = request.Path.ToString(); if (!request.QueryString.HasValue) { return path; } var pairs = new List(); foreach (KeyValuePair pair in request.Query) { bool isToken = string.Equals(pair.Key, TokenParameter, StringComparison.OrdinalIgnoreCase); pairs.Add($"{pair.Key}={(isToken ? Redacted : pair.Value.ToString())}"); } return pairs.Count == 0 ? path : $"{path}?{string.Join('&', pairs)}"; } }