This rule ensures that no sensitive information, such as passwords, personal identifiers, or confidential data, is written to logs. Logging sensitive data can lead to serious security vulnerabilities, including unauthorized access and data leaks, which can compromise user privacy and violate compliance requirements.
It is important to treat logs as potentially accessible by various parties, including developers, administrators, or attackers who gain access to the system. Therefore, sensitive information should never be recorded in logs in plaintext or any identifiable form.
To comply with this rule, developers should carefully review logging statements and avoid including sensitive parameters directly and sanitize information being logged. Log only non-sensitive metadata or sanitized information. For example, rather than logging password or full usernames, consider logging the occurrence of an event without sensitive details or use masking and sanitization techniques before logging.
By following these practices, you reduce the risk of sensitive data exposure while still maintaining useful logs for debugging and monitoring application behavior.
Non-Compliant Code Examples
usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.Extensions.Logging;publicclassAccountController:Controller{privatereadonlyILogger<AccountController>_logger;publicAccountController(ILogger<AccountController>logger){_logger=logger;} [HttpPost]publicIActionResultLogin(stringusername,stringpassword){_logger.LogInformation("User {username} attempting to log in",username);// authentication logic...returnOk();}}
usingSystem.Web;usingSystem.Web.Mvc;usingNLog;publicclassUserController:Controller{privatestaticreadonlyLogger_logger=LogManager.GetCurrentClassLogger(); [HttpPost]publicActionResultRegister(stringusername){if(!string.IsNullOrEmpty(username)){_logger.Warn("Registration attempt for user: "+username);// Noncompliant}returnView();}}voidmain(){}
Compliant Code Examples
publicclassUserController:Controller{privatestaticreadonlyLogger_logger=LogManager.GetCurrentClassLogger(); [HttpPost]publicActionResultRegister(stringusername){if(!string.IsNullOrEmpty(username)){stringsanitized=username.Replace('\n',' ').Replace('\r',' ').Replace('\t',' ');_logger.Warn("Registration attempt for user: "+sanitized);}returnView();}}
シームレスな統合。 Datadog Code Security をお試しください
Datadog Code Security
このルールを試し、Datadog Code Security でコードを解析する
このルールの使用方法
1
2
rulesets:- csharp-security # Rules to enforce C# security.