-
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
Open
cldrn
wants to merge
4
commits into
github:main
Choose a base branch
from
cldrn:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 getParseMethod() { | ||
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") } | ||
} | ||
|
||
/* | ||
* 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
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 ):
class MicrosoftAspNetCoreMvcControllerBaseClass extends Class { MicrosoftAspNetCoreMvcControllerBaseClass() { getNamespace() = any(MicrosoftAspNetCoreMvcNamespace h) and (hasName("Controller") or hasName("ControllerBase")) } }
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.
Thanks for pointing this out. I've added the support for both the new namespace and ControllerBase classes.