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.