From c8435020a4e3998029abd14803e44f039de10c55 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Fri, 1 Aug 2025 18:22:31 +0530 Subject: [PATCH] Chnaged the function to chek if base64 is valid or not --- .../Controllers/TenantController.cs | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/Marco.Pms.Services/Controllers/TenantController.cs b/Marco.Pms.Services/Controllers/TenantController.cs index 0b53f80..a922832 100644 --- a/Marco.Pms.Services/Controllers/TenantController.cs +++ b/Marco.Pms.Services/Controllers/TenantController.cs @@ -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; } }