Skip to content

Commit 1eec192

Browse files
committed
Added null checks
1 parent d4870c5 commit 1eec192

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

DnsServerCore/Dns/Security/DnsCookieSecretManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void Rotate()
183183
{
184184
Snapshot currentSnapshot = Volatile.Read(ref _snapshot);
185185

186-
byte[] previous = currentSnapshot is null ? null : currentSnapshot.CurrentSecret;
186+
byte[] previous = currentSnapshot?.CurrentSecret;
187187
Snapshot nextSnapshot = GenerateNewSnapshot(previous);
188188

189189
SaveLocked(nextSnapshot);
@@ -195,13 +195,13 @@ public void Rotate()
195195
public byte[] GetCurrentSecret()
196196
{
197197
Snapshot snapshot = Volatile.Read(ref _snapshot);
198-
return snapshot is null ? null : snapshot.CurrentSecret;
198+
return snapshot?.CurrentSecret;
199199
}
200200

201201
public byte[] GetPreviousSecret()
202202
{
203203
Snapshot snapshot = Volatile.Read(ref _snapshot);
204-
return snapshot is null ? null : snapshot.PreviousSecret;
204+
return snapshot?.PreviousSecret;
205205
}
206206

207207
#endregion

DnsServerCore/Dns/Security/DnsCookieValidator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,14 @@ public bool Validate(IPAddress clientAddress, EDnsCookieOptionData cookie)
253253
if (cookie.ClientCookie.Length != ClientCookieLen)
254254
return false;
255255

256-
ReadOnlySpan<byte> currentSecret = _secretManager.GetCurrentSecret();
257-
if (!currentSecret.IsEmpty &&
256+
byte[] currentSecret = _secretManager.GetCurrentSecret();
257+
258+
if (currentSecret != null && currentSecret.Length > 0 &&
258259
ValidateServerCookieWithSecret(clientAddress, cookie.ClientCookie, cookie.ServerCookie, currentSecret))
259260
return true;
260261

261-
ReadOnlySpan<byte> previousSecret = _secretManager.GetPreviousSecret();
262-
if (!previousSecret.IsEmpty &&
262+
byte[] previousSecret = _secretManager.GetPreviousSecret();
263+
if (previousSecret != null && previousSecret.Length > 0 &&
263264
ValidateServerCookieWithSecret(clientAddress, cookie.ClientCookie, cookie.ServerCookie, previousSecret))
264265
return true;
265266

@@ -277,7 +278,7 @@ public EDnsCookieOptionData CreateResponseCookie(IPAddress clientAddress, EDnsCo
277278
if (requestCookie.ClientCookie.Length != ClientCookieLen)
278279
throw new ArgumentException($"Client cookie must be {ClientCookieLen} bytes.", nameof(requestCookie));
279280

280-
ReadOnlySpan<byte> currentSecret = _secretManager.GetCurrentSecret();
281+
byte[] currentSecret = _secretManager.GetCurrentSecret();
281282
ValidateSecret(currentSecret);
282283

283284
byte[] serverCookie = ComputeServerCookie(clientAddress, requestCookie.ClientCookie, currentSecret);

0 commit comments

Comments
 (0)