forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlockedMethods.cs
119 lines (104 loc) · 3.92 KB
/
BlockedMethods.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -----------------------------------------------------------------------
// <copyright file="CanUsePropertyNow.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Linq;
using System.Security;
using Raven.Abstractions.Exceptions;
using Raven.Abstractions.Indexing;
using Raven.Abstractions.MEF;
using Raven.Database.Config;
using Raven.Database.Linq;
using Raven.Database.Plugins;
using Xunit;
namespace Raven.Tests.MailingList
{
public class BlockedMethods : IDisposable
{
public BlockedMethods()
{
CodeVerifier.Active = true;
}
[Fact]
public void CanUseNowProp()
{
Compile("from doc in docs select new { doc.Now }");
}
[Fact]
public void CannotDefineLambdaInLet()
{
Assert.Throws<SecurityException>(
() =>
Compile(
"from doc in docs let _ = (Func<int>)(()=>1) select new { doc.Now , die = _()}"));
}
[Fact]
public void CannotDefineLambdaWithExpression()
{
Assert.Throws<SecurityException>(
() =>
Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ return 1;}))()}"));
}
[Fact]
public void CannotUseEnvironment()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ System.Environment.Exit(1); return 1; }))()}"));
}
[Fact]
public void CannotUseTaskFactory()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = System.Threading.Tasks.Task.Factory.StartNew((Func<int>)(()=>{ return 1; }))}"));
}
[Fact]
public void CannotUseTaskStart()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ new System.Threading.Tasks.Task(()=>{}).Start(); return 1; }))()}"));
}
[Fact]
public void CannotUseIO()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ System.IO.File.Delete(\"test\"); return 1; }))()}"));
}
[Fact]
public void CannotCreateTask()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ new System.Threading.Tasks.Task(()=>{}); return 1; }))()}"));
}
[Fact]
public void CannotStartThread()
{
Assert.Throws<SecurityException>(() => Compile(
"from doc in docs select new { doc.Now , die = ((Func<int>)(()=>{ new System.Threading.Thread(()=>{}).Start(); return 1; }))()}"));
}
[Fact]
public void CannotUseDatetimeNowProp()
{
Assert.Throws<IndexCompilationException>(() => Compile("from doc in docs select new { DateTime.Now }"));
}
[Fact]
public void CannotUseSystemDatetimeNowProp()
{
Assert.Throws<IndexCompilationException>(() => Compile("from doc in docs select new { System.DateTime.Now }"));
}
private void Compile(string code)
{
var dynamicViewCompiler = new DynamicViewCompiler("test", new IndexDefinition
{
Map = code
}, new OrderedPartCollection<AbstractDynamicCompilationExtension>(), ".", new InMemoryRavenConfiguration());
dynamicViewCompiler.GenerateInstance();
}
public void Dispose()
{
CodeVerifier.Active = false;
}
}
}