Skip to content

Commit 0526ab8

Browse files
Potential fix for code scanning alert no. 4: Exposure of private information
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent e8ec4dc commit 0526ab8

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

  • YTVidShareBackend/VideoSharingService/VideoSharingService/Controllers

YTVidShareBackend/VideoSharingService/VideoSharingService/Controllers/UserController.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Extensions.Logging;
77
using System;
8+
using System.Security.Cryptography;
9+
using System.Text;
810
using System.Collections.Generic;
911
using System.Threading.Tasks;
1012
using VideoSharingService.Data.DTOs;
@@ -34,6 +36,19 @@ private string MaskEmail(string email)
3436
: prefix.Substring(0, 1) + new string('*', prefix.Length - 2) + prefix.Substring(prefix.Length - 1, 1);
3537
return $"{maskedPrefix}@{domain}";
3638
}
39+
40+
// Hash email address using SHA256 for logging (not reversible)
41+
private string HashEmail(string email)
42+
{
43+
if (string.IsNullOrEmpty(email))
44+
return "unknown";
45+
46+
using (SHA256 sha256Hash = SHA256.Create())
47+
{
48+
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(email));
49+
return Convert.ToBase64String(bytes);
50+
}
51+
}
3752
private readonly IUnitOfWork _unitOfWork;
3853
private readonly ILogger<UserController> _logger;
3954
private readonly IMapper _mapper;
@@ -133,7 +148,7 @@ public async Task<IActionResult> Register([FromBody] CreateUserDTO userDTO)
133148
public async Task<IActionResult> Login([FromBody] LoginDTO userDTO)
134149
{
135150
var sanitizedEmail = userDTO.Email?.Replace("\r", "").Replace("\n", "");
136-
_logger.LogInformation($"Login attempt for {MaskEmail(sanitizedEmail)}");
151+
_logger.LogInformation($"Login attempt for email hash {HashEmail(sanitizedEmail)}");
137152
if (!ModelState.IsValid)
138153
{
139154
_logger.LogError($"Invalid post attempt {nameof(Login)}");

0 commit comments

Comments
 (0)