-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from QuantozTechnology/nounce-security
Implemented Signature Verification Middleware
- Loading branch information
Showing
7 changed files
with
241 additions
and
3 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
187 changes: 187 additions & 0 deletions
187
backend/core/src/Core.API/ResponseHandling/SignatureVerificationMiddleware.cs
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,187 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain.Exceptions; | ||
using Core.Presentation.Models; | ||
using Newtonsoft.Json.Linq; | ||
using System.Net; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using static Core.Domain.Constants; | ||
|
||
namespace Core.API.ResponseHandling | ||
{ | ||
public class SignatureVerificationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly ILogger<SignatureVerificationMiddleware> _logger; | ||
|
||
public SignatureVerificationMiddleware( | ||
RequestDelegate next, | ||
ILogger<SignatureVerificationMiddleware> logger) | ||
{ | ||
_next = next; | ||
_logger = logger; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
try | ||
{ | ||
// Retrieve headers from the request | ||
string? signatureHeader = context.Request.Headers["x-signature"]; | ||
string? payloadHeader = context.Request.Headers["x-payload"]; | ||
|
||
// Retrieve method from the request | ||
var method = context.Request.Method; | ||
|
||
if (string.IsNullOrWhiteSpace(payloadHeader)) | ||
{ | ||
_logger.LogError("Missing payload header"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Missing Header", "x-payload")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
return; | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(signatureHeader)) | ||
{ | ||
_logger.LogError("Missing x-signature header"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Missing Header", "x-signature")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
return; | ||
} | ||
|
||
byte[] payloadBytes = Encoding.UTF8.GetBytes(payloadHeader); | ||
|
||
JObject payloadJson = JObject.Parse(payloadHeader); | ||
|
||
string? publicKey = null; | ||
string? postPayload = null; | ||
|
||
if (method == "POST" || method == "PUT") | ||
{ | ||
// Check if the "PostPayload" property is present | ||
if (payloadJson.TryGetValue(SignaturePayload.PostPayload, out var post)) | ||
{ | ||
postPayload = (string?)post; | ||
} | ||
else | ||
{ | ||
_logger.LogError("Missing postPayload header"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Missing Header", "postPayload")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
return; | ||
} | ||
} | ||
|
||
// Check if the "publicKey" property is present | ||
if (payloadJson.TryGetValue(SignaturePayload.PublicKey, out var pubKey)) | ||
{ | ||
publicKey = (string?)pubKey; | ||
} | ||
else | ||
{ | ||
_logger.LogError("Missing publicKey header"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Missing Header", "publicKey")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
return; | ||
} | ||
|
||
// Get the current Unix UTC timestamp (rounded to 30 seconds) | ||
long currentTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | ||
currentTimestamp = (currentTimestamp / 30) * 30; // Round to the nearest 30 seconds | ||
|
||
// Decode the signature header from Base64 | ||
byte[]? signatureBytes = Convert.FromBase64String(signatureHeader); | ||
|
||
long timestamp = 0; | ||
|
||
// Check if the "timestamp" property is present | ||
if (payloadJson.TryGetValue(SignaturePayload.Timestamp, out var timestampToken)) | ||
{ | ||
// Extract the timestamp value | ||
timestamp = (long)timestampToken; | ||
} | ||
else | ||
{ | ||
_logger.LogError("Missing timestamp in header"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Missing Header", "timestamp")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
return; | ||
} | ||
|
||
bool isCurrentTime = timestamp == currentTimestamp; | ||
|
||
long allowedDifference = 30; // 30 seconds | ||
bool isWithin30Seconds = Math.Abs(currentTimestamp - timestamp) <= allowedDifference; | ||
|
||
if (isCurrentTime || isWithin30Seconds) | ||
{ | ||
if (VerifySignature(publicKey!, payloadBytes, signatureBytes)) | ||
{ | ||
await _next(context); // Signature is valid, continue with the request | ||
} | ||
else | ||
{ | ||
_logger.LogError("Invalid signature"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Invalid signature", "x-signature")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
} | ||
} | ||
else | ||
{ | ||
_logger.LogError("Invalid signature"); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", "Invalid signature", "x-signature")); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError("Unknown exception thrown: {message}", ex.Message); | ||
var customErrors = new CustomErrors(new CustomError("Forbidden", ex.Message, ex.Source!)); | ||
await WriteCustomErrors(context.Response, customErrors, (int)HttpStatusCode.Forbidden); | ||
} | ||
} | ||
|
||
private static async Task WriteCustomErrors(HttpResponse httpResponse, CustomErrors customErrors, int statusCode) | ||
{ | ||
httpResponse.StatusCode = statusCode; | ||
httpResponse.ContentType = "application/json"; | ||
|
||
var response = CustomErrorsResponse.FromCustomErrors(customErrors); | ||
var json = System.Text.Json.JsonSerializer.Serialize(response); | ||
await httpResponse.WriteAsync(json); | ||
} | ||
|
||
private static bool VerifySignature(string publicKey, byte[] payload, byte[] signature) | ||
{ | ||
try | ||
{ | ||
using (RSA rsa = RSA.Create()) | ||
{ | ||
// Import the public key (assuming it's in PEM format) | ||
rsa.ImportFromPem(publicKey); | ||
|
||
// Verify the signature using the SHA256 algorithm and PKCS1 padding | ||
var isValidSignature = rsa.VerifyData(payload, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); | ||
|
||
return isValidSignature; | ||
} | ||
} | ||
catch (CryptographicException) | ||
{ | ||
// Signature verification failed | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
public static class SignatureVerificationMiddlewareExtensions | ||
{ | ||
public static void ConfigureSignatureVerificationMiddleware(this IApplicationBuilder app) | ||
{ | ||
app.UseMiddleware<SignatureVerificationMiddleware>(); | ||
} | ||
} | ||
} |
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="Moq" Version="4.18.4" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Core.API\Core.API.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,5 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |