-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCodeInjection.cs
More file actions
52 lines (43 loc) · 1.46 KB
/
CodeInjection.cs
File metadata and controls
52 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll ${testdir}/../../../resources/stubs/Microsoft.CSharp.cs /r:System.ComponentModel.Primitives.dll
using Microsoft.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Web;
namespace Microsoft.CodeAnalysis.CSharp.Scripting
{
public static class CSharpScript
{
public static void EvaluateAsync(string code)
{
// Dummy implementation
}
}
}
public class CommandInjectionHandler : IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{
string code = ctx.Request.QueryString["code"];
CSharpCodeProvider c = new CSharpCodeProvider();
ICodeCompiler icc = c.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
// BAD: Compiling unvalidated code from the user
CompilerResults cr = icc.CompileAssemblyFromSource(cp, code);
System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("MyNamespace.MyClass");
Type t = o.GetType();
MethodInfo mi = t.GetMethod("Eval");
object s = mi.Invoke(o, null);
// BAD: Use the Roslyn APIs to dynamically evaluate C#
CSharpScript.EvaluateAsync(code);
}
public bool IsReusable
{
get
{
return true;
}
}
}