-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer parse request cookies, but ensure they are still redacted i…
…n the `Cookie` header string (#2444) We've identified that sending transactions with cookies parsed to `Request.Cookies` can result in failures to index those transactions when any of the cookies include periods (which is common for ASP.NET core by default). This PR removes the extraction from the header onto `Request.Cookies`. Instead, we now modify the `Cookie` header value to redact the cookie items based on the configured list of field names to sanitize. For .NET targets, we optimize this to avoid allocations as much as possible. Unfortunately, for the .NET Framework, we can't be as efficient.
- Loading branch information
1 parent
44820f4
commit 479ee08
Showing
12 changed files
with
452 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// 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 | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Elastic.Apm.Api; | ||
using Elastic.Apm.Config; | ||
using Elastic.Apm.Helpers; | ||
using Elastic.Apm.Model; | ||
#if NET6_0_OR_GREATER | ||
using System.Buffers; | ||
#endif | ||
|
||
namespace Elastic.Apm.Filters | ||
{ | ||
/// <summary> | ||
/// Redacts items from the cookie list of the Cookie request header. | ||
/// </summary> | ||
internal class CookieHeaderRedactionFilter | ||
{ | ||
private const string CookieHeader = "Cookie"; | ||
|
||
public static IError Filter(IError error) | ||
{ | ||
if (error is Error e && e.Context is not null) | ||
HandleCookieHeader(e.Context?.Request?.Headers, e.Configuration.SanitizeFieldNames); | ||
return error; | ||
} | ||
|
||
public static ITransaction Filter(ITransaction transaction) | ||
{ | ||
if (transaction is Transaction { IsContextCreated: true }) | ||
HandleCookieHeader(transaction.Context?.Request?.Headers, transaction.Configuration.SanitizeFieldNames); | ||
return transaction; | ||
} | ||
|
||
// internal for testing | ||
internal static void HandleCookieHeader(Dictionary<string, string> headers, IReadOnlyList<WildcardMatcher> sanitizeFieldNames) | ||
{ | ||
if (headers is not null) | ||
{ | ||
// Realistically, this should be more than enough for all sensible scenarios | ||
// e.g. Cookies | cookies | COOKIES | ||
const int maxKeys = 4; | ||
|
||
#if NET6_0_OR_GREATER | ||
var matchedKeys = ArrayPool<string>.Shared.Rent(maxKeys); | ||
var matchedValues = ArrayPool<string>.Shared.Rent(maxKeys); | ||
#else | ||
var matchedKeys = new string[maxKeys]; | ||
var matchedValues = new string[maxKeys]; | ||
#endif | ||
var matchedCount = 0; | ||
|
||
foreach (var header in headers) | ||
{ | ||
if (matchedCount == maxKeys) | ||
break; | ||
|
||
if (header.Key.Equals(CookieHeader, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
matchedKeys[matchedCount] = header.Key; | ||
matchedValues[matchedCount] = CookieHeaderRedacter.Redact(header.Value, sanitizeFieldNames); | ||
matchedCount++; | ||
} | ||
} | ||
|
||
var replacedCount = 0; | ||
|
||
foreach (var headerKey in matchedKeys) | ||
{ | ||
if (replacedCount == matchedCount) | ||
break; | ||
|
||
if (headerKey is not null) | ||
{ | ||
headers[headerKey] = matchedValues[replacedCount]; | ||
replacedCount++; | ||
} | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
ArrayPool<string>.Shared.Return(matchedKeys); | ||
ArrayPool<string>.Shared.Return(matchedValues); | ||
#endif | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.