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.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다