The ‘Enforce trust boundaries’ rule is crucial in maintaining the security and integrity of your application. This rule is designed to prevent unauthorized access or manipulation of sensitive data by ensuring that trust boundaries are properly implemented and respected. Trust boundaries are interfaces where data is exchanged between components with different levels of trust.
Violations of this rule can lead to serious security issues such as data breaches, unauthorized access to sensitive data, and other forms of security compromise. In the non-compliant code sample, the user’s input is directly stored into the session without any form of validation or sanitization, which could lead to Cross-Site Scripting (XSS) or SQL Injection attacks if the input data is used in a context that interprets it as code.
How to remediate
Validate and sanitize all inputs, especially those that cross trust boundaries. This could be achieved by using functions that ensure the input matches expected patterns and by encoding or escaping inputs before using them in a different context. In the compliant code sample, the input data is URL decoded and used in a way that doesn’t interpret it as code, which reduces the risk of XSS attacks. Also, the session cookie is set to be secure and has an expiration time, which limits the time window for potential attacks.
Non-Compliant Code Examples
usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.AspNetCore.Http;usingSystem.Collections.Generic;usingMicrosoft.AspNetCore.Mvc.Filters;usingMicrosoft.AspNetCore.Mvc.Controllers;usingSystem.Linq;usingSystem;namespaceOwaspBenchmarkTest.Controllers{publicclassBenchmarkTest00031Controller:Controller{ [HttpGet("/trustbound-00/BenchmarkTest00031")] [HttpPost("/trustbound-00/BenchmarkTest00031")]publicIActionResultIndex(){varparam=Request.Query["BenchmarkTest00031"].FirstOrDefault();HttpContext.Session.SetString("userid",param);returnContent("Item: 'userid' with value: '"+Microsoft.Security.Encoder.Encoder.HtmlEncode(param)+"' saved in session.","text/html;charset=UTF-8");}}}
Compliant Code Examples
usingMicrosoft.AspNetCore.Http;usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.AspNetCore.Mvc.RazorPages;usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Routing;usingMicrosoft.AspNetCore.Session;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.Hosting;usingSystem.Text;namespaceOwaspBenchmarkTest.Controllers{publicclassBenchmarkTest00097Controller:Controller{privatereadonlyIHttpContextAccessor_httpContextAccessor;publicBenchmarkTest00097Controller(IHttpContextAccessorhttpContextAccessor){_httpContextAccessor=httpContextAccessor;} [HttpGet("/trustbound-00/BenchmarkTest00097")]publicIActionResultGet(){CookieOptionsoption=newCookieOptions();option.Expires=DateTime.Now.AddMinutes(3);option.Secure=true;stringrequestURI=_httpContextAccessor.HttpContext.Request.Path.ToString();_httpContextAccessor.HttpContext.Response.Cookies.Append("BenchmarkTest00097","color",option);returnView();} [HttpPost("/trustbound-00/BenchmarkTest00097")]publicIActionResultPost(){stringparam="noCookieValueSupplied";if(_httpContextAccessor.HttpContext.Request.Cookies.ContainsKey("BenchmarkTest00097")){//Vulnerability is maintainedparam=WebUtility.UrlDecode(_httpContextAccessor.HttpContext.Request.Cookies["BenchmarkTest00097"]);}stringbar;intnum=106;bar=(7*18)+num>200?"This_should_always_happen":param;HttpContext.Session.SetString(bar,"10340");returnContent("Item: '"+System.Security.SecurityElement.Escape(bar)+"' with value: 10340 saved in session.");}}}
원활한 통합. 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 검사를 추가합니다