From f32efed457c61e82d53c472abe7af6955b96ac32 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Mon, 16 Sep 2024 11:59:38 +0100 Subject: [PATCH] Refactor conditional code --- .../Helpers/CookieHeaderRedacter.cs | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Elastic.Apm/Helpers/CookieHeaderRedacter.cs b/src/Elastic.Apm/Helpers/CookieHeaderRedacter.cs index 76d4b6209..174ed78bb 100644 --- a/src/Elastic.Apm/Helpers/CookieHeaderRedacter.cs +++ b/src/Elastic.Apm/Helpers/CookieHeaderRedacter.cs @@ -2,13 +2,13 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -#if !NETFRAMEWORK +using System.Collections.Generic; +#if NETFRAMEWORK +using System.Text; +#else using System; using System.Buffers; #endif -using System.Collections.Generic; -using System.Configuration; -using System.Text; namespace Elastic.Apm.Helpers; @@ -16,31 +16,22 @@ internal static class CookieHeaderRedacter { #if !NETFRAMEWORK private static ReadOnlySpan Redacted => Consts.Redacted.AsSpan(); - private static ReadOnlySpan SemiColon => ";".AsSpan(); - private static ReadOnlySpan Comma => ",".AsSpan(); + private static ReadOnlySpan SemiColon => [';']; + private static ReadOnlySpan Comma => [',']; #endif - public static string Redact(string cookieHeaderValue, IReadOnlyList namesToSanitize) - { - // Implementation notes: - // This method handles a cookie header value for both ASP.NET (classic) and - // ASP.NET Core. As a result it must handle two possible formats. In ASP.NET - // (classic) the string is the actual Cookie value as sent over the wire, conforming - // to the HTTP standards. This uses the semicolon separator and a space between - // entries. For ASP.NET Core, when we parse the headers, we convert from the - // StringValues by calling ToString. This results in each entry being separated - // by a regular colon and no space. - - // When the headers are stored into the `Request.Headers` on our APM data model, - // multiple headers of the same name, are concatenated into a comma-separated list. - // When redacting, we preserve this separation. - - if (string.IsNullOrEmpty(cookieHeaderValue)) - return null; - - var redactedCookieHeader = string.Empty; + // Implementation notes: + // This method handles a cookie header value for both ASP.NET (classic) and + // ASP.NET Core. As a result it must handle two possible formats. In ASP.NET + // (classic) the string is the actual Cookie value as sent over the wire, conforming + // to the HTTP standards. This uses the semicolon separator and a space between + // entries. For ASP.NET Core, when the headers are stored into the `Request.Headers` + // on our APM data model, multiple headers of the same name, are concatenated into a + // comma-separated list. When redacting, we preserve this separation. #if NETFRAMEWORK + public static string Redact(string cookieHeaderValue, IReadOnlyList namesToSanitize) + { // The use of `Span` in NETFX can cause runtime assembly loading issues due to our friend, // binding redirects. For now, we take a slightly less allocation efficient route here, rather // than risk introducing runtime issues for consumers. Technically, this should be "fixed" in @@ -50,6 +41,9 @@ public static string Redact(string cookieHeaderValue, IReadOnlyList namesToSanitize) + { + if (string.IsNullOrEmpty(cookieHeaderValue)) + return null; + + var redactedCookieHeader = string.Empty; var cookieHeaderValueSpan = cookieHeaderValue.AsSpan(); var written = 0; @@ -108,7 +108,7 @@ public static string Redact(string cookieHeaderValue, IReadOnlyList 0) { var foundSemiColon = true; - var semiColonIndex = cookieHeaderEntrySpan.IndexOf(';'); + var semiColonIndex = cookieHeaderEntrySpan.IndexOf(SemiColon); if (semiColonIndex == -1) // If there are no more separators, { @@ -200,7 +200,6 @@ public static string Redact(string cookieHeaderValue, IReadOnlyList.Shared.Return(redactedBufferArray); return redactedCookieHeader; -#endif } +#endif } -