/
EndpointRestrictionTests.cs
227 lines (189 loc) · 12.1 KB
/
EndpointRestrictionTests.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ServiceStack.Common.Extensions;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints.Tests.Support.Host;
using ServiceStack.WebHost.Endpoints.Tests.Support.Services;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[TestFixture]
public class EndpointRestrictionTests
: ServiceHostTestBase
{
//Localhost and LocalSubnet is always included with the Internal flag
private const int EndpointAttributeCount = 17;
private static readonly List<EndpointAttributes> AllAttributes = (EndpointAttributeCount).Times().ConvertAll<EndpointAttributes>(x => (EndpointAttributes)(1 << (int)x));
TestAppHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
appHost = CreateAppHost();
}
public void ShouldAllowAccessWhen<TRequestDto>(EndpointAttributes withScenario)
where TRequestDto : new()
{
ShouldNotThrow<UnauthorizedAccessException>(() => appHost.ExecuteService(new TRequestDto(), withScenario));
}
public void ShouldDenyAccessWhen<TRequestDto>(EndpointAttributes withScenario)
where TRequestDto : new()
{
ShouldThrow<UnauthorizedAccessException>(() => appHost.ExecuteService(new TRequestDto(), withScenario));
}
public void ShouldDenyAccessForAllOtherScenarios<TRequestDto>(params EndpointAttributes[] notIncluding)
where TRequestDto : new()
{
ShouldDenyAccessForOtherScenarios<TRequestDto>(AllAttributes.Where(x => !notIncluding.Contains(x)).ToList());
}
public void ShouldDenyAccessForOtherNetworkAccessScenarios<TRequestDto>(params EndpointAttributes[] notIncluding)
where TRequestDto : new()
{
var scenarios = new List<EndpointAttributes> { EndpointAttributes.Localhost, EndpointAttributes.LocalSubnet, EndpointAttributes.External };
ShouldDenyAccessForOtherScenarios<TRequestDto>(scenarios.Where(x => !notIncluding.Contains(x)).ToList());
}
public void ShouldDenyAccessForOtherHttpRequestTypesScenarios<TRequestDto>(params EndpointAttributes[] notIncluding)
where TRequestDto : new()
{
var scenarios = new List<EndpointAttributes> { EndpointAttributes.HttpHead, EndpointAttributes.HttpGet,
EndpointAttributes.HttpPost, EndpointAttributes.HttpPut, EndpointAttributes.HttpDelete };
ShouldDenyAccessForOtherScenarios<TRequestDto>(scenarios.Where(x => !notIncluding.Contains(x)).ToList());
}
private void ShouldDenyAccessForOtherScenarios<TRequestDto>(IEnumerable<EndpointAttributes> otherScenarios)
where TRequestDto : new()
{
var requestDto = new TRequestDto();
foreach (var otherScenario in otherScenarios)
{
try
{
ShouldThrow<UnauthorizedAccessException>(() => appHost.ExecuteService(requestDto, otherScenario));
}
catch (Exception ex)
{
throw new Exception("Failed to throw on: " + otherScenario, ex);
}
}
}
[Test]
public void InternalRestriction_allows_calls_from_Localhost_or_LocalSubnet()
{
ShouldAllowAccessWhen<InternalRestriction>(EndpointAttributes.Localhost);
ShouldAllowAccessWhen<InternalRestriction>(EndpointAttributes.LocalSubnet);
ShouldDenyAccessForOtherNetworkAccessScenarios<InternalRestriction>(EndpointAttributes.Localhost, EndpointAttributes.LocalSubnet);
}
[Test]
public void LocalhostRestriction_allows_calls_from_localhost()
{
ShouldAllowAccessWhen<LocalhostRestriction>(EndpointAttributes.Localhost);
ShouldDenyAccessForOtherNetworkAccessScenarios<LocalhostRestriction>(EndpointAttributes.Localhost);
}
[Test]
public void LocalSubnetRestriction_allows_calls_from_LocalSubnet()
{
ShouldAllowAccessWhen<LocalSubnetRestriction>(EndpointAttributes.LocalSubnet);
ShouldDenyAccessForOtherNetworkAccessScenarios<LocalSubnetRestriction>(EndpointAttributes.LocalSubnet);
}
[Test]
public void LocalSubnetRestriction_does_not_allow_calls_from_Localhost()
{
ShouldDenyAccessWhen<LocalSubnetRestriction>(EndpointAttributes.Localhost);
ShouldDenyAccessWhen<LocalSubnetRestriction>(EndpointAttributes.External);
}
[Test]
public void InternalRestriction_allows_calls_from_Localhost_and_LocalSubnet()
{
ShouldAllowAccessWhen<InternalRestriction>(EndpointAttributes.Localhost);
ShouldAllowAccessWhen<InternalRestriction>(EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<LocalSubnetRestriction>(EndpointAttributes.External);
}
[Test]
public void SecureLocalSubnetRestriction_does_not_allow_partial_success()
{
ShouldDenyAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.Localhost);
ShouldDenyAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.InSecure | EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.InSecure);
ShouldDenyAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.Secure | EndpointAttributes.Localhost);
ShouldAllowAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<SecureLocalSubnetRestriction>(EndpointAttributes.Secure | EndpointAttributes.InternalNetworkAccess);
ShouldDenyAccessForOtherNetworkAccessScenarios<SecureLocalSubnetRestriction>(EndpointAttributes.LocalSubnet);
}
[Test]
public void HttpPostXmlAndSecureLocalSubnetRestriction_does_not_allow_partial_success()
{
ShouldDenyAccessForOtherNetworkAccessScenarios<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.LocalSubnet);
ShouldDenyAccessForOtherHttpRequestTypesScenarios<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.Localhost);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Json | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.Secure | EndpointAttributes.Localhost);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.Secure | EndpointAttributes.HttpHead);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.InSecure );
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Json | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.Secure | EndpointAttributes.Localhost);
ShouldAllowAccessWhen<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
}
[Test]
public void HttpPostXmlOrSecureLocalSubnetRestriction_does_allow_partial_success()
{
ShouldDenyAccessForOtherNetworkAccessScenarios<HttpPostXmlAndSecureLocalSubnetRestriction>(EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.Localhost | EndpointAttributes.HttpPut);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldDenyAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Json | EndpointAttributes.Secure | EndpointAttributes.Localhost);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Json | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.Secure | EndpointAttributes.Localhost);
ShouldAllowAccessWhen<HttpPostXmlOrSecureLocalSubnetRestriction>(EndpointAttributes.HttpPost | EndpointAttributes.Xml | EndpointAttributes.Secure | EndpointAttributes.LocalSubnet);
}
[Test]
public void Can_access_from_insecure_dev_environment()
{
ShouldAllowAccessWhen<InSecureDevEnvironmentRestriction>(EndpointAttributes.Localhost | EndpointAttributes.InSecure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<InSecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.InSecure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<InSecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.InSecure | EndpointAttributes.HttpPost | EndpointAttributes.Reply);
ShouldAllowAccessWhen<InSecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.InSecure | EndpointAttributes.HttpPost | EndpointAttributes.OneWay);
}
[Test]
public void Can_access_from_secure_dev_environment()
{
ShouldAllowAccessWhen<SecureDevEnvironmentRestriction>(EndpointAttributes.Localhost | EndpointAttributes.Secure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<SecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.Secure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<SecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.Secure | EndpointAttributes.HttpPost | EndpointAttributes.Reply);
ShouldAllowAccessWhen<SecureDevEnvironmentRestriction>(EndpointAttributes.LocalSubnet | EndpointAttributes.Secure | EndpointAttributes.HttpPost | EndpointAttributes.OneWay);
}
[Test]
public void Can_access_from_insecure_live_environment()
{
ShouldAllowAccessWhen<InSecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.InSecure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<InSecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.InSecure | EndpointAttributes.HttpPost | EndpointAttributes.Reply);
ShouldAllowAccessWhen<InSecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.InSecure | EndpointAttributes.HttpPost | EndpointAttributes.OneWay);
}
[Test]
public void Can_access_from_secure_live_environment()
{
ShouldAllowAccessWhen<SecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.Secure | EndpointAttributes.HttpPost);
ShouldAllowAccessWhen<SecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.Secure | EndpointAttributes.HttpPost | EndpointAttributes.Reply);
ShouldAllowAccessWhen<SecureLiveEnvironmentRestriction>(EndpointAttributes.External | EndpointAttributes.Secure | EndpointAttributes.HttpPost | EndpointAttributes.OneWay);
}
[Ignore]
[Test]
public void Print_enum_results()
{
PrintEnumResult(EndpointAttributes.InternalNetworkAccess, EndpointAttributes.Secure);
PrintEnumResult(EndpointAttributes.InternalNetworkAccess, EndpointAttributes.Secure | EndpointAttributes.External);
PrintEnumResult(EndpointAttributes.InternalNetworkAccess, EndpointAttributes.Secure | EndpointAttributes.Localhost);
PrintEnumResult(EndpointAttributes.InternalNetworkAccess, EndpointAttributes.Localhost);
PrintEnumResult(EndpointAttributes.Localhost, EndpointAttributes.Secure | EndpointAttributes.External);
PrintEnumResult(EndpointAttributes.Localhost, EndpointAttributes.Secure | EndpointAttributes.InternalNetworkAccess);
PrintEnumResult(EndpointAttributes.Localhost, EndpointAttributes.LocalSubnet);
PrintEnumResult(EndpointAttributes.Localhost, EndpointAttributes.Secure);
}
public void PrintEnumResult(EndpointAttributes actual, EndpointAttributes required)
{
Console.WriteLine(string.Format("({0} | {1}): {2}", actual, required, (actual | required)));
Console.WriteLine(string.Format("({0} & {1}): {2}", actual, required, (actual & required)));
Console.WriteLine(string.Format("({0} ^ {1}): {2}", actual, required, (actual ^ required)));
Console.WriteLine();
}
}
}