-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C#: Adds check for Server Side Template Injection vulnerabilities in RazorEngine #4313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @name Server-Side Template Injection in RazorEngine | ||
* @description User-controlled data may be evaluated, leading to arbitrary code execution. | ||
* @kind path-problem | ||
* @problem.severity error | ||
* @precision high | ||
* @id csharp/razor-injection | ||
* @tags security | ||
*/ | ||
|
||
import csharp | ||
import semmle.code.csharp.dataflow.TaintTracking | ||
import DataFlow::PathGraph | ||
|
||
/* | ||
* The offending method is Parse from RazorEngine.Razor. | ||
*/ | ||
|
||
class RazorEngineClass extends Class { | ||
RazorEngineClass() { this.hasQualifiedName("RazorEngine.Razor") } | ||
|
||
Method getIsValidMethod() { | ||
result.getDeclaringType() = this and | ||
result.hasName("Parse") | ||
} | ||
} | ||
|
||
/* | ||
* We are only interested in ASP.NET MVC Controller classes | ||
*/ | ||
|
||
class ControllerMVC extends Class { | ||
ControllerMVC() { this.hasQualifiedName("System.Web.Mvc", "Controller") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New ASP.NET Core MVC has a different namespace. It's possible that this query can cover new MVC if you import codeql/csharp/ql/src/semmle/code/csharp/frameworks/microsoft/AspNetCore.qll and include the new namespace controller ('this.hasQualifiedName("Microsoft.AspNetCore.Mvc", "Controller")' or 'this instanceof MicrosoftAspNetCoreMvcController' ) Also, there could be custom controllers derived from ControllerBase. Consider defining namespace separately from the controller (see codeql/csharp/ql/src/semmle/code/csharp/frameworks/microsoft/AspNetCore.qll ): There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing this out. I've added the support for both the new namespace and ControllerBase classes. |
||
} | ||
|
||
/* | ||
* We filter by the ActionResult. I think this might not be needed. | ||
*/ | ||
|
||
class ActionResultCall extends Call { | ||
ActionResultCall() { | ||
this.getEnclosingCallable().getAnnotatedReturnType().toString() = "ActionResult" | ||
} | ||
} | ||
|
||
/* | ||
* TaintTracking configuration that will track any public method in MVC controllers that takes arguments that are parsed later by RazorEngine.Parse. | ||
*/ | ||
|
||
class RazorEngineInjection extends TaintTracking::Configuration { | ||
RazorEngineInjection() { this = "RazorEngineInjection" } | ||
|
||
override predicate isSource(DataFlow::Node source) { | ||
exists(ActionResultCall ar | | ||
ar.getEnclosingCallable().getDeclaringType().getABaseType() instanceof ControllerMVC and | ||
source.asParameter() = ar.getEnclosingCallable().getAParameter() | ||
) | ||
} | ||
|
||
override predicate isSink(DataFlow::Node sink) { | ||
exists(RazorEngineClass rec, MethodCall mc | | ||
mc.getQualifiedDeclaration() = rec.getIsValidMethod() and | ||
sink.asExpr() = mc.getArgument(0) | ||
) | ||
} | ||
} | ||
|
||
from RazorEngineInjection cfg, DataFlow::PathNode source, DataFlow::PathNode sink | ||
where cfg.hasFlowPath(source, sink) | ||
select sink, source, sink, | ||
"Server-Side Template Injection in RazorEngine leads to Remote Code Execution" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE qhelp PUBLIC | ||
"-//Semmle//qhelp//EN" | ||
"qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
<p>This rule finds public <code>ActualResults</code> methods that have user controlled parameters that could contain malicious code that | ||
is parsed by <code>RazorEngine.Parse()</code> leading to a Server-Side Template Injection vulnerability. | ||
|
||
</overview> | ||
<recommendation> | ||
<p>Do not use the class RazorEngine. If not possible, do not parse user input into the template.</p> | ||
|
||
</recommendation> | ||
<references> | ||
<li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.web.razor.parser.razorparser.parse?view=aspnet-webpages-3.2">RazorParser</a>.</li> | ||
<li><a href="https://clement.notin.org/blog/2020/04/15/Server-Side-Template-Injection-(SSTI)-in-ASP.NET-Razor/">RazorEngine Injection ASP.NET</a>.</li> | ||
|
||
</references> | ||
</qhelp> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this called
getIsValidMethod
and not e.g.get(A)ParseMethod
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Old function name. Changed it to getParseMethod()