-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
58 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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
export async function validateToken(token) { | ||
var response = await fetch(`/validate-token?token=${encodeURIComponent(token)}`); | ||
return response.text(); | ||
try { | ||
var url = `/api/validate-token?token=${encodeURIComponent(token)}`; | ||
var response = await fetch(url, { method: 'POST' }); | ||
return response.text(); | ||
} catch (ex) { | ||
return `Error validating token: ${ex}`; | ||
} | ||
} |
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,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Aspire.Dashboard.Utils; | ||
|
||
internal static class CompareHelpers | ||
{ | ||
// This method is used to compare two keys in a way that avoids timing attacks. | ||
public static bool CompareKey(byte[] expectedKeyBytes, string requestKey) | ||
{ | ||
const int StackAllocThreshold = 256; | ||
|
||
var requestByteCount = Encoding.UTF8.GetByteCount(requestKey); | ||
|
||
// Key will never match if lengths are different. But still do all the comparison work to avoid timing attacks. | ||
var lengthsEqual = expectedKeyBytes.Length == requestByteCount; | ||
|
||
var requestSpanLength = Math.Max(requestByteCount, expectedKeyBytes.Length); | ||
byte[]? requestPooled = null; | ||
var requestBytesSpan = (requestSpanLength <= StackAllocThreshold ? | ||
stackalloc byte[StackAllocThreshold] : | ||
(requestPooled = RentClearedArray(requestSpanLength))).Slice(0, requestSpanLength); | ||
|
||
try | ||
{ | ||
// Always succeeds because the byte span is always as big or bigger than required. | ||
Encoding.UTF8.GetBytes(requestKey, requestBytesSpan); | ||
|
||
// Trim request bytes to the same length as expected bytes. Need to be the same size for fixed time comparison. | ||
var equals = CryptographicOperations.FixedTimeEquals(expectedKeyBytes, requestBytesSpan.Slice(0, expectedKeyBytes.Length)); | ||
|
||
return equals && lengthsEqual; | ||
} | ||
finally | ||
{ | ||
if (requestPooled != null) | ||
{ | ||
ArrayPool<byte>.Shared.Return(requestPooled); | ||
} | ||
} | ||
|
||
static byte[] RentClearedArray(int byteCount) | ||
{ | ||
// UTF8 bytes are copied into the array but remaining bytes are untouched. | ||
// Because all bytes in the array are compared, clear the array to avoid comparing previous data. | ||
var array = ArrayPool<byte>.Shared.Rent(byteCount); | ||
Array.Clear(array); | ||
return array; | ||
} | ||
} | ||
} |