Why it matters
A value such as ..\..\web.config walks out of the intended directory.
How to fix it
Reduce the value to a bare file name with Path.GetFileName before combining it with a fixed directory, then check that Path.GetFullPath of the result still starts with that directory.
Example
The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.
using System.IO;
using Microsoft.AspNetCore.Mvc;
public class FilesController : Controller
{
public IActionResult Download(string name)
{
var bytes = System.IO.File.ReadAllBytes(Path.Combine("/srv/uploads", name));
return File(bytes, "application/octet-stream");
}
}using System.IO;
using Microsoft.AspNetCore.Mvc;
public class FilesController : Controller
{
public IActionResult Download(string name)
{
var bytes = System.IO.File.ReadAllBytes(Path.Combine("/srv/uploads", Path.GetFileName(name)));
return File(bytes, "application/octet-stream");
}
}What TrustIvan reads
Runs on repository scans of C# source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.
Weakness classes and references
Related checks
- Command injection from request input (C#)
C#Severity: High
trustivan-csharp-taint-command-injection - SSRF from request input (C#)
C#Severity: High
trustivan-csharp-taint-ssrf - SQL injection from request input (C#)
C#Severity: High
trustivan-csharp-taint-sql-injection - XSS from request input (C#)
C#Severity: High
trustivan-csharp-taint-xss - Deserialization from request input (C#)
C#Severity: High
trustivan-csharp-taint-deserialization - Open redirect from request input (C#)
C#Severity: Medium
trustivan-csharp-taint-open-redirect
Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10
All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation