Skip to content

Commit a82dba1

Browse files
authored
Fix bug when handling of multiple cookie entries with the same name (#2310)
* Fix bug when handling of multiple cookie entries with the same name * Formatting
1 parent 2805f3a commit a82dba1

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/Elastic.Apm/Filters/RequestCookieExtractionFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Elastic.Apm.Filters
1414
/// </summary>
1515
internal class RequestCookieExtractionFilter
1616
{
17-
private static readonly WildcardMatcher[] CookieMatcher = new WildcardMatcher[] { new WildcardMatcher.VerbatimMatcher("Cookie", true) };
17+
private static readonly WildcardMatcher[] CookieMatcher = [new WildcardMatcher.VerbatimMatcher("Cookie", true)];
1818

1919
public static IError Filter(IError error)
2020
{

src/Elastic.Apm/Helpers/CookieHeaderParser.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public static Dictionary<string, string> ParseCookies(string cookieHeader)
4242
var trimmed = cookieValue.Trim();
4343
var parts = trimmed.Split('=');
4444

45-
if (parts.Length == 2 && !string.IsNullOrEmpty(parts[0]) && !string.IsNullOrEmpty(parts[1]))
45+
// Fow now, we store only the first value for a given key. This aligns to our nodeJS agent behavior.
46+
if (parts.Length == 2
47+
&& !string.IsNullOrEmpty(parts[0])
48+
&& !cookies.ContainsKey(parts[0])
49+
&& !string.IsNullOrEmpty(parts[1]))
4650
{
4751
cookies.Add(parts[0], parts[1]);
4852
}
@@ -75,8 +79,14 @@ public static Dictionary<string, string> ParseCookies(string cookieHeader)
7579
var keyString = key.ToString();
7680
var valueString = value.ToString();
7781

78-
if (!string.IsNullOrEmpty(keyString) && !string.IsNullOrEmpty(valueString))
82+
// Fow now, we store only the first value for a given key. This aligns to our nodeJS agent behavior.
83+
#if NETSTANDARD2_0
84+
if (!string.IsNullOrEmpty(keyString) && !cookies.ContainsKey(keyString) && !string.IsNullOrEmpty(valueString))
7985
cookies.Add(keyString, valueString);
86+
#else
87+
if (!string.IsNullOrEmpty(keyString) && !string.IsNullOrEmpty(valueString))
88+
cookies.TryAdd(keyString, valueString);
89+
#endif
8090
}
8191

8292
span = span.Slice(foundComma ? separatorIndex + 1 : span.Length);

test/Elastic.Apm.Tests/HelpersTests/CookieHeaderParserTests.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,36 @@ public void ParsesHeadersCorrectly(string input, Dictionary<string, string> expe
2626
result.Should().Contain(expected);
2727
}
2828

29-
public static IEnumerable<object[]> TestData() => new[] {
30-
new object[] { null, null },
31-
new object[] { "", null },
32-
new object[] { "Key1=Value1", new Dictionary<string, string>() { { "Key1", "Value1" }} },
33-
new object[] { "Key1=Value1,Key2=Value2", new Dictionary<string, string>()
29+
public static IEnumerable<object[]> TestData() => [
30+
[null, null],
31+
["", null],
32+
["Key1=Value1", new Dictionary<string, string>() { { "Key1", "Value1" }}],
33+
[ "Key1=Value1,Key2=Value2", new Dictionary<string, string>()
3434
{
3535
{ "Key1", "Value1" },
3636
{ "Key2", "Value2" }
37-
}},
38-
new object[] { "Key1=Value1,Key2=Value2,Key3=Value3", new Dictionary<string, string>()
37+
}],
38+
[ "Key1=Value1,Key2=Value2,Key3=Value3", new Dictionary<string, string>()
3939
{
4040
{ "Key1", "Value1" },
4141
{ "Key2", "Value2" },
4242
{ "Key3", "Value3" }
43-
}},
44-
new object[] { "Key1=Value1; Key2=Value2", new Dictionary<string, string>()
43+
}],
44+
[ "Key1=Value1; Key2=Value2", new Dictionary<string, string>()
4545
{
4646
{ "Key1", "Value1" },
4747
{ "Key2", "Value2" }
48-
}},
49-
new object[] { "Key1=Value1; Key2=Value2; Key3=Value3", new Dictionary<string, string>()
48+
}],
49+
[ "Key1=Value1; Key2=Value2; Key3=Value3", new Dictionary<string, string>()
5050
{
5151
{ "Key1", "Value1" },
5252
{ "Key2", "Value2" },
5353
{ "Key3", "Value3" }
54-
}}
55-
};
54+
}],
55+
[ "Key1=Value1; Key2=Value2; Key1=Value3", new Dictionary<string, string>()
56+
{
57+
{ "Key1", "Value1" },
58+
{ "Key2", "Value2" }
59+
}],
60+
];
5661
}

0 commit comments

Comments
 (0)