Skip to content

Commit fa8501f

Browse files
committed
don't allocate new dictionary
1 parent 55b9f6e commit fa8501f

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

tracer/src/Datadog.Trace/AppSec/Coordinator/SecurityCoordinator.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,21 @@ internal readonly partial struct SecurityCoordinator
106106
var additiveContext = GetOrCreateAdditiveContext();
107107
if (additiveContext?.ShouldRunWith(_security, userId, userLogin, userSessionId, fromSdk) is { Count: > 0 } userAddresses)
108108
{
109-
addresses = userAddresses.ToDictionary(k => k.Key, object (v) => v.Value);
110-
111109
if (otherTags is not null)
112110
{
113111
foreach (var kvp in otherTags)
114112
{
115-
if (!addresses.ContainsKey(kvp.Key))
113+
if (!userAddresses.ContainsKey(kvp.Key))
116114
{
117-
addresses.Add(kvp.Key, kvp.Value);
115+
userAddresses.Add(kvp.Key, kvp.Value);
118116
}
119117
}
120118
}
121119

122-
SecurityReporter.LogAddressIfDebugEnabled(addresses);
120+
SecurityReporter.LogAddressIfDebugEnabled(userAddresses);
123121

124122
// run the WAF and execute the results
125-
result = additiveContext.Run(addresses, _security.Settings.WafTimeoutMicroSeconds);
123+
result = additiveContext.Run(userAddresses, _security.Settings.WafTimeoutMicroSeconds);
126124
additiveContext.CommitUserRuns(userAddresses, fromSdk);
127125
RecordTelemetry(result);
128126

tracer/src/Datadog.Trace/AppSec/Waf/Context.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ private Context(IntPtr contextHandle, IWaf waf, IWafLibraryInvoker wafLibraryInv
6363
public IResult? RunWithEphemeral(IDictionary<string, object> ephemeralAddressData, ulong timeoutMicroSeconds, bool isRasp)
6464
=> RunInternal(null, ephemeralAddressData, timeoutMicroSeconds, isRasp);
6565

66-
public Dictionary<string, string> ShouldRunWith(IDatadogSecurity security, string? userId = null, string? userLogin = null, string? userSessionId = null, bool fromSdk = false)
66+
public Dictionary<string, object> ShouldRunWith(IDatadogSecurity security, string? userId = null, string? userLogin = null, string? userSessionId = null, bool fromSdk = false)
6767
{
68-
var addresses = new Dictionary<string, string>();
68+
var addresses = new Dictionary<string, object>();
6969
ShouldRun(_userEventsState.Id, userId, AddressesConstants.UserId);
7070
ShouldRun(_userEventsState.Login, userLogin, AddressesConstants.UserLogin);
7171
ShouldRun(_userEventsState.SessionId, userSessionId, AddressesConstants.UserSessionId);
@@ -92,21 +92,21 @@ void ShouldRun(UserEventsState.UserRecord? userRecord, string? value, string add
9292
}
9393
}
9494

95-
public void CommitUserRuns(IDictionary<string, string> addresses, bool fromSdk)
95+
public void CommitUserRuns(Dictionary<string, object> addresses, bool fromSdk)
9696
{
9797
if (addresses.TryGetValue(AddressesConstants.UserId, out var address))
9898
{
99-
_userEventsState.Id = new(address, fromSdk);
99+
_userEventsState.Id = new(address.ToString(), fromSdk);
100100
}
101101

102102
if (addresses.TryGetValue(AddressesConstants.UserLogin, out address))
103103
{
104-
_userEventsState.Login = new(address, fromSdk);
104+
_userEventsState.Login = new(address.ToString(), fromSdk);
105105
}
106106

107107
if (addresses.TryGetValue(AddressesConstants.UserSessionId, out address))
108108
{
109-
_userEventsState.SessionId = new(address, fromSdk);
109+
_userEventsState.SessionId = new(address.ToString(), fromSdk);
110110
}
111111
}
112112

tracer/src/Datadog.Trace/AppSec/Waf/IContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal interface IContext : IDisposable
1616

1717
IResult? RunWithEphemeral(IDictionary<string, object> ephemeralAddressData, ulong timeoutMicroSeconds, bool isRasp);
1818

19-
Dictionary<string, string> ShouldRunWith(IDatadogSecurity security, string? userId = null, string? userLogin = null, string? userSessionId = null, bool fromSdk = false);
19+
Dictionary<string, object> ShouldRunWith(IDatadogSecurity security, string? userId = null, string? userLogin = null, string? userSessionId = null, bool fromSdk = false);
2020

21-
void CommitUserRuns(IDictionary<string, string> addresses, bool fromSdk);
21+
void CommitUserRuns(Dictionary<string, object> addresses, bool fromSdk);
2222
}

tracer/test/Datadog.Trace.Security.Unit.Tests/ContextUserEventTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public void NewValuesTests()
2828
var userId = "toto";
2929
var addresses = context!.ShouldRunWith(security.Object, userId: userId);
3030
addresses.Should().HaveCount(1);
31-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserId, userId));
31+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserId, userId));
3232
context.CommitUserRuns(addresses, false);
3333
userId = "tata";
3434
// should run with a different value
3535
addresses = context!.ShouldRunWith(security.Object, userId: userId);
36-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserId, userId));
36+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserId, userId));
3737
addresses.Should().HaveCount(1);
3838
context.CommitUserRuns(addresses, false);
3939

@@ -55,17 +55,17 @@ public void NewValuesSessionTests()
5555
var userId = "toto";
5656
var addresses = context!.ShouldRunWith(security.Object, userId: userId, fromSdk: true);
5757
addresses.Should().HaveCount(1);
58-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserId, userId));
58+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserId, userId));
5959
context.CommitUserRuns(addresses, true);
6060
var ssessionId = "234";
6161
addresses = context!.ShouldRunWith(security.Object, userSessionId: ssessionId);
6262
addresses.Should().HaveCount(1);
63-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserSessionId, ssessionId));
63+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserSessionId, ssessionId));
6464
context.CommitUserRuns(addresses, false);
6565
ssessionId = "tata";
6666
// should run with a different value
6767
addresses = context!.ShouldRunWith(security.Object, userSessionId: ssessionId);
68-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserSessionId, ssessionId));
68+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserSessionId, ssessionId));
6969
addresses.Should().HaveCount(1);
7070
context.CommitUserRuns(addresses, false);
7171

@@ -92,7 +92,7 @@ public void AddressDisabledNo()
9292

9393
// should run with a different value
9494
addresses = context!.ShouldRunWith(security.Object, userId: userId, userSessionId: userSessionId);
95-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserSessionId, userSessionId));
95+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserSessionId, userSessionId));
9696
addresses.Should().HaveCount(1);
9797
context.CommitUserRuns(addresses, false);
9898
}
@@ -122,7 +122,7 @@ public void SdkOverrideTest()
122122
var userId = "toto";
123123
var addresses = context!.ShouldRunWith(security.Object, userId: userId);
124124
addresses.Should().HaveCount(1);
125-
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserId, userId));
125+
addresses.Should().Contain(new KeyValuePair<string, object>(AddressesConstants.UserId, userId));
126126
context.CommitUserRuns(addresses, true);
127127
addresses = context!.ShouldRunWith(security.Object, userId: "other");
128128
addresses.Should().HaveCount(0);

0 commit comments

Comments
 (0)