Skip to content

Commit 96455e0

Browse files
committed
7.0.15 release
- [ENHANCEMENT] added argon2 sha256 methods -- GetArgon2SHA256 and VerifyArgon2SHA256 - [BUG FIX] moved where trace.TrcRequestVerb = requestMethod; as if blockedurls used then verb wouldn't be set - [BUG FIX] updated GatherTraceInformation to take in an IRequest interface and check the interface type to determine the parameters - [ENHANCEMENT] upgraded EnyimMemcachedCore from 3.2.1 to 3.2.3 - [ENHANCEMENT] upgraded Microsoft.Extensions.DependencyInjection from 8.0.0 to 8.0.1 - [ENHANCEMENT] upgraded Microsoft.Extensions.Logging from 8.0.0 to 8.0.1 - [ENHANCEMENT] upgraded System.Diagnostics.PerformanceCounter from 8.0.0 to 8.0.1 - [ENHANCEMENT] upgraded System.DirectoryServices.AccountManagement from 8.0.0 to 8.0.1
1 parent 1649f27 commit 96455e0

File tree

6 files changed

+236
-52
lines changed

6 files changed

+236
-52
lines changed

rls/packages/API.Library.7.0.15.nupkg

68.4 KB
Binary file not shown.

src/API.Library/API.Library.csproj

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@
1212
<PackageId>API.Library</PackageId>
1313
<Product>API Library</Product>
1414
<Copyright>Central Statistics Office, Ireland</Copyright>
15-
<Version>7.0.14</Version>
15+
<Version>7.0.15</Version>
1616
<Authors>Central Statistics Office, Ireland</Authors>
1717
<SignAssembly>False</SignAssembly>
1818
<RepositoryUrl>https://github.com/CSOIreland/Server-API-Library</RepositoryUrl>
1919
<PackageReleaseNotes>
20-
- [ENHANCEMENT] updated code to allow application pool to start if memcache is unavailable
21-
- [ENHANCEMENT] removed ApplicationLoaded flag from api as if there is no API config loaded on application startup then let an exception be thrown that is not caught
22-
- [ENHANCEMENT] updated ICacheConfig to allow API_MEMCACHED_ENABLED to be set in Code
23-
- [ENHANCEMENT] updated MemCacheD constructor to have the stats query within try catch Block
24-
- [ENHANCEMENT] updated ConsoleConfiguration.cs to remove ApplicationLoaded flag
25-
- [BUG FIX] updated ReadJSONSettings to return null if the version that is being looked for is not there
20+
- [ENHANCEMENT] added argon2 sha256 methods -- GetArgon2SHA256 and VerifyArgon2SHA256
21+
- [BUG FIX] moved where trace.TrcRequestVerb = requestMethod; as if blockedurls used then verb wouldn't be set
22+
- [BUG FIX] updated GatherTraceInformation to take in an IRequest interface and check the interface type to determine the parameters
23+
- [ENHANCEMENT] upgraded EnyimMemcachedCore from 3.2.1 to 3.2.3
24+
- [ENHANCEMENT] upgraded Microsoft.Extensions.DependencyInjection from 8.0.0 to 8.0.1
25+
- [ENHANCEMENT] upgraded Microsoft.Extensions.Logging from 8.0.0 to 8.0.1
26+
- [ENHANCEMENT] upgraded System.Diagnostics.PerformanceCounter from 8.0.0 to 8.0.1
27+
- [ENHANCEMENT] upgraded System.DirectoryServices.AccountManagement from 8.0.0 to 8.0.1
2628
</PackageReleaseNotes>
2729
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
2830
<RestoreLockedMode>true</RestoreLockedMode>
@@ -45,15 +47,16 @@
4547
</ItemGroup>
4648

4749
<ItemGroup>
48-
<PackageReference Include="EnyimMemcachedCore" Version="3.2.1" />
50+
<PackageReference Include="EnyimMemcachedCore" Version="3.2.3" />
51+
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
4952
<PackageReference Include="log4net" Version="2.0.17" />
5053
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
51-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
52-
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
54+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
55+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
5356
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
5457
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
55-
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
56-
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="8.0.0" />
58+
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.1" />
59+
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="8.0.1" />
5760
</ItemGroup>
5861

5962

@@ -76,4 +79,11 @@
7679
<DebugType>portable</DebugType>
7780
</PropertyGroup>
7881

82+
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
83+
<IncludeSymbols>true</IncludeSymbols>
84+
<DebugSymbols>true</DebugSymbols>
85+
<DebugType>Portable</DebugType>
86+
</PropertyGroup>
87+
88+
7989
</Project>

src/API.Library/Entities/API.Common.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,23 @@ internal Cookie CheckCookie(string SessionCookieName, HttpContext httpContext)
479479
}
480480
return sessionCookie;
481481
}
482-
483-
internal void GatherTraceInformation(dynamic apiRequest, Trace trace)
482+
483+
internal void GatherTraceInformation(IRequest apiRequest, Trace trace)
484484
{
485485
if (ApiServicesHelper.ApiConfiguration.API_TRACE_ENABLED)
486486
{
487+
Type type = apiRequest.GetType();
488+
if (type == typeof(JSONRPC_API))
489+
{
490+
trace.TrcParams = MaskParameters(apiRequest.parameters.ToString());
491+
}
492+
else if (type == typeof(RESTful_API) || type == typeof(Static_API))
493+
{
494+
//in non jsonrpc its a list of strings
495+
trace.TrcParams = MaskParameters(apiRequest.parameters[0]);
496+
}
497+
487498
//gather trace information
488-
trace.TrcParams = MaskParameters(apiRequest.parameters.ToString());
489499
trace.TrcIp = apiRequest.ipAddress;
490500
trace.TrcUseragent = apiRequest.userAgent;
491501
trace.TrcMethod = apiRequest.method;

src/API.Library/Entities/Utility.cs

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Net;
55
using System.Security.Cryptography;
66
using System.Text;
7+
using Konscious.Security.Cryptography;
8+
using System.DirectoryServices.ActiveDirectory;
9+
using System.Threading;
10+
using System;
711

812
namespace API
913
{
@@ -41,10 +45,11 @@ public static string GetMD5(string input)
4145
}
4246

4347
/// <summary>
44-
/// Geberate the SHA256 hash of the input parameter
48+
/// Generate the SHA256 hash of the input parameter
4549
/// </summary>
4650
/// <param name="input"></param>
4751
/// <returns></returns>
52+
[ObsoleteAttribute("This property is obsolete. Use NewProperty GetArgon2SHA256.", false)]
4853
public static string GetSHA256(string input)
4954
{
5055
// Create a SHA256
@@ -66,6 +71,148 @@ public static string GetSHA256(string input)
6671
}
6772
}
6873

74+
75+
76+
/// <summary>
77+
/// Generate the SHA256 hash of the input parameter using argon2 and a salt
78+
/// uses https://github.com/kmaragon/Konscious.Security.Cryptography
79+
/// </summary>
80+
/// <param name="input"></param>
81+
/// <returns></returns>
82+
public static string GetArgon2SHA256(string input)
83+
{
84+
85+
//declare minimum allowable values
86+
//number between 1 and 10. -- High-security scenarios: 4–10 iterations (more iterations increase the time cost, making attacks more difficult).
87+
const int minNumIterations = 4;
88+
89+
//Moderate-security scenarios: 64 MB (65536 KB) to 128 MB (131072 KB).
90+
// High - security scenarios: 256 MB(262144 KB) to 1 GB(1048576 KB) for sensitive data.
91+
const int minMemorySize = 65536; //64mb of memory
92+
93+
//Low - security scenarios: 1–2(single - threaded hashing).
94+
//High - security scenarios: 4–8, depending on available CPU cores.For stronger security,
95+
// it's recommended to align this with the number of cores on your system.
96+
97+
//min set as 1 as every machine will have 1 thread
98+
const int minDegreeOfParallelism = 1;
99+
100+
var argon2 = new Argon2id(Encoding.UTF8.GetBytes(input));
101+
102+
var salt = ApiServicesHelper.ApiConfiguration.Settings["API_ENCRYPTION_SALT"];
103+
var sDegreeOfParallelism = ApiServicesHelper.ApiConfiguration.Settings["API_ENCRYPTION_DEGREEE_OF_PARALLELISM"];
104+
var sMemorySize = ApiServicesHelper.ApiConfiguration.Settings["API_ENCRYPTION_MEMORYSIZE"];
105+
var sIterations = ApiServicesHelper.ApiConfiguration.Settings["API_ENCRYPTION_ITERATIONS"];
106+
107+
108+
if (string.IsNullOrEmpty(salt))
109+
{
110+
throw new Exception("API_ENCRYPTION_SALT must be defined");
111+
}
112+
113+
int iDegreeOfParallelism = 0;
114+
if (!string.IsNullOrEmpty(sDegreeOfParallelism))
115+
{
116+
//check its a number
117+
bool DegreeOfParallelismFlag = int.TryParse(sDegreeOfParallelism, out iDegreeOfParallelism);
118+
119+
if (!DegreeOfParallelismFlag)
120+
{
121+
throw new Exception("API_ENCRYPTION_DEGREEE_OF_PARALLELISM must be a valid number");
122+
123+
}
124+
}
125+
else
126+
{
127+
//minimum number of threads to use.
128+
iDegreeOfParallelism = minDegreeOfParallelism;
129+
}
130+
131+
int iMemorySize = 0;
132+
if (!string.IsNullOrEmpty(sMemorySize))
133+
{
134+
//check its a number
135+
bool MemorySizeFlag = int.TryParse(sMemorySize, out iMemorySize);
136+
137+
if (!MemorySizeFlag)
138+
{
139+
throw new Exception("API_ENCRYPTION_MEMORYSIZE must be a valid number");
140+
141+
}
142+
}
143+
else
144+
{
145+
//default value of 64mb memory
146+
iMemorySize = minMemorySize;
147+
}
148+
149+
int iIterations = 0;
150+
if (!string.IsNullOrEmpty(sIterations))
151+
{
152+
//check its a number
153+
bool IterationsFlag = int.TryParse(sIterations, out iIterations);
154+
155+
if (!IterationsFlag)
156+
{
157+
throw new Exception("API_ENCRYPTION_ITERATIONS must be a valid number");
158+
159+
}
160+
}
161+
else
162+
{
163+
//minimum number of iteration required
164+
iIterations = minNumIterations;
165+
}
166+
167+
if(iDegreeOfParallelism < minDegreeOfParallelism)
168+
{
169+
iDegreeOfParallelism = minDegreeOfParallelism; //mandatory minimum value
170+
Log.Instance.Error("The number for DegreeOfParallelism specified for encryption is using the default value, as value supplied is to small");
171+
}
172+
173+
if (iMemorySize < minMemorySize)
174+
{
175+
iMemorySize = minMemorySize; //mandatory minimum memory size of 64mb
176+
Log.Instance.Error("The number for MemorySize specified for encryption is using the default value, as value supplied is to small");
177+
}
178+
179+
if (iIterations < minNumIterations)
180+
{
181+
iIterations = minNumIterations; //mandatory number of iterations for security
182+
Log.Instance.Error("The number of iterations specified for encryption is using the default value, as value supplied is to small");
183+
}
184+
185+
argon2.Salt = Encoding.UTF8.GetBytes(salt);
186+
argon2.DegreeOfParallelism = iDegreeOfParallelism; //number of thread;
187+
argon2.MemorySize = iMemorySize; //memory in KB (64mb)
188+
argon2.Iterations = iIterations; //number of iterations
189+
190+
// generate the hash
191+
return Convert.ToBase64String(argon2.GetBytes(32)); //256 but hash
192+
}
193+
194+
195+
/// <summary>
196+
/// Verify that new hash is equal to existing hash
197+
/// /// </summary>
198+
/// <param name="input"></param>
199+
/// <returns></returns>
200+
public static bool VerifyArgon2SHA256(string input, string expectedHash)
201+
{
202+
203+
string newSha256 = GetArgon2SHA256(input);
204+
205+
//if hashes are the same thrn return true
206+
if (newSha256.Equals(expectedHash)) {
207+
return true;
208+
}
209+
else
210+
{
211+
return false;
212+
}
213+
214+
}
215+
69216
/// <summary>
70217
/// Serialize to JSON ignoring looping references
71218
/// </summary>

src/API.Library/Middleware/API.Middleware.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public async Task InvokeAsync(HttpContext context)
117117
context.Request.EnableBuffering();
118118
string incomingUrl = context.Request.Path.ToString();
119119
var requestMethod = context.Request.Method;
120-
120+
//set the trace verb
121+
trace.TrcRequestVerb = requestMethod;
121122

122123
if (ApiServicesHelper.BlockedRequests.urls != null)
123124
{
@@ -133,8 +134,7 @@ public async Task InvokeAsync(HttpContext context)
133134
}
134135
}
135136
}
136-
//set the trace verb
137-
trace.TrcRequestVerb = requestMethod;
137+
138138

139139
incomingUrl = incomingUrl.ToLower();
140140
switch (true)
@@ -301,7 +301,9 @@ public async Task InvokeAsync(HttpContext context)
301301

302302

303303
if (string.IsNullOrEmpty(trace.TrcMethod))
304+
{
304305
trace.TrcErrorPath = MaskParameters(context.Request.Path.ToString());
306+
}
305307

306308
Trace_ADO.Create(trace);
307309

0 commit comments

Comments
 (0)