Chnaged the function to chek if base64 is valid or not
This commit is contained in:
parent
3915e9b9d0
commit
c8435020a4
@ -18,7 +18,6 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
||||
|
||||
@ -243,7 +242,8 @@ namespace Marco.Pms.Services.Controllers
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(model.TaxId))
|
||||
{
|
||||
return await _context.Tenants.AnyAsync(t => t.TaxId == model.TaxId);
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||
return await context.Tenants.AnyAsync(t => t.TaxId == model.TaxId);
|
||||
|
||||
}
|
||||
return false;
|
||||
@ -252,7 +252,8 @@ namespace Marco.Pms.Services.Controllers
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(model.DomainName))
|
||||
{
|
||||
return await _context.Tenants.AnyAsync(t => t.DomainName == model.DomainName);
|
||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||
return await context.Tenants.AnyAsync(t => t.DomainName == model.DomainName);
|
||||
|
||||
}
|
||||
return false;
|
||||
@ -474,29 +475,40 @@ namespace Marco.Pms.Services.Controllers
|
||||
private bool IsBase64String(string? input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Normalize string
|
||||
input = input.Trim();
|
||||
string base64Data = input;
|
||||
const string dataUriMarker = "base64,";
|
||||
int markerIndex = input.IndexOf(dataUriMarker, StringComparison.Ordinal);
|
||||
|
||||
// Length must be multiple of 4
|
||||
if (input.Length % 4 != 0)
|
||||
return false;
|
||||
|
||||
// Valid Base64 characters with correct padding
|
||||
var base64Regex = new Regex(@"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
|
||||
if (!base64Regex.IsMatch(input))
|
||||
// If the marker is found, extract the actual Base64 data
|
||||
if (markerIndex >= 0)
|
||||
{
|
||||
base64Data = input.Substring(markerIndex + dataUriMarker.Length);
|
||||
}
|
||||
|
||||
// Now, validate the extracted payload
|
||||
base64Data = base64Data.Trim();
|
||||
|
||||
// Check for valid length (must be a multiple of 4) and non-empty
|
||||
if (base64Data.Length == 0 || base64Data.Length % 4 != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// The most reliable test is to simply try to convert it.
|
||||
// The .NET converter is strict and will throw a FormatException
|
||||
// for invalid characters or incorrect padding.
|
||||
try
|
||||
{
|
||||
// Decode and re-encode to confirm validity
|
||||
var bytes = Convert.FromBase64String(input);
|
||||
var reEncoded = Convert.ToBase64String(bytes);
|
||||
return input == reEncoded;
|
||||
Convert.FromBase64String(base64Data);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
catch (FormatException)
|
||||
{
|
||||
// The string is not a valid Base64 payload.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user