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
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using MonoForks.Mono.Xml;
using MonoForks.System.Net;
using NUnit.Framework;
using MonoForks.System.Windows.Browser.Net;
using UnityEngine;
using Uri = MonoForks.System.Uri;
namespace CrossDomainPolicyParserTests
{
[TestFixture]
public class FlashPolicyParserTests
{
static string XDomainGlobal =
@"<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
<allow-access-from domain=""*"" />
</cross-domain-policy>";
string http_hosted = "http://www.host.com/coolgame.unity3d";
string https_hosted = "https://secure.host.net/coolgame.unity3d";
string file_hosted = "file:///coolgame.unity3";
[Test]
public void GlobalXDomainAcceptsRequestOnSameDomain()
{
string requesturl = "http://www.mach8.nl/index.html";
Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted));
}
[Test]
public void GlobalXDomainAcceptsRequestOnSubDomain()
{
string requesturl = "http://subdomain.mach8.nl/index.html";
Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted));
}
[Test]
public void GlobalXDomainAllowsSecureRequestWhenHostedNonSecure()
{
string requesturl = "https://www.mach8.nl/index.html";
Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted));
}
[Test]
public void GlobalXDomainAcceptsSecureRequestWhenHostedSecure()
{
string requesturl = "https://www.mach8.nl/index.html";
Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, https_hosted));
}
[Test]
public void GlobalXDomainDeniesNonSecureRequestWhenHostedSecure()
{
string requesturl = "http://www.mach8.nl/index.html";
Assert.IsFalse(RequestAllowed(XDomainGlobal, requesturl, https_hosted));
}
[Test]
public void AllDomain_Secure()
{
string policy = @"<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
<allow-access-from domain=""*"" secure=""true""/>
</cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void WhenRequestURLMatchesWildCardAccessIsAllowed()
{
string policy = @"<?xml version=""1.0""?>
<cross-domain-policy>
<allow-access-from domain=""*.mydomain.nl"" />
</cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://subdomain.mydomain.nl", http_hosted));
}
[Test]
public void WhenRequestURLDoesNotMatchWildCardAccessIsDisallowed()
{
string policy = @"<?xml version=""1.0""?>
<cross-domain-policy>
<allow-access-from domain=""*.mydomain.nl"" />
</cross-domain-policy>";
Assert.IsFalse(RequestAllowed(policy, "http://subdomain.myotherdomain.nl", http_hosted));
}
[Test]
public void AllDomains_NoDTD()
{
string policy = @"<?xml version='1.0'?><cross-domain-policy><allow-access-from domain='*'/></cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_NoXmlHeader()
{
string policy = @"<cross-domain-policy>
<allow-access-from domain=""*"" to-ports=""*""/>
</cross-domain-policy> ";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_PermittedCrossDomainPolicies_All()
{
// 'all' is the default value
// http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html#site-control-permitted-cross-domain-policies
string policy = @"<?xml version='1.0'?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
<site-control permitted-cross-domain-policies='all' />
<allow-access-from domain='*' />
</cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_PermittedCrossDomainPolicies_MasterOnly()
{
string policy = @"<?xml version='1.0'?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
<site-control permitted-cross-domain-policies='master-only' />
<allow-access-from domain='*' />
</cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_PermittedCrossDomainPolicies_None()
{
string policy = @"<?xml version='1.0'?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
<site-control permitted-cross-domain-policies='none' />
<allow-access-from domain='*' />
</cross-domain-policy>";
Assert.IsFalse(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_PermittedCrossDomainPolicies_ByContentType()
{
string policy = @"<?xml version='1.0'?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
<site-control permitted-cross-domain-policies='by-content-type' />
<allow-access-from domain='*' />
</cross-domain-policy>";
Assert.IsFalse(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
public void AllDomains_PermittedCrossDomainPolicies_ByFtpFilename()
{
string policy = @"<?xml version='1.0'?>
<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>
<cross-domain-policy>
<site-control permitted-cross-domain-policies='by-ftp-filename' />
<allow-access-from domain='*' />
</cross-domain-policy>";
Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted));
}
[Test]
[ExpectedException(typeof(MiniParser.XMLError))]
public void IllformedPolicyIsRejected()
{
FlashCrossDomainPolicyFromString("bogus", "http://www.host.com");
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void EmptyPolicyStringIsRejected()
{
FlashCrossDomainPolicyFromString("", "http://www.host.com");
}
private bool RequestAllowed(string xdomain, string requesturl, string hosturl)
{
FlashCrossDomainPolicy policy = FlashCrossDomainPolicyFromString(xdomain, hosturl);
var wr = new WebRequest(new Uri(requesturl), new Dictionary<string, string>());
return policy.IsAllowed(wr);
}
private FlashCrossDomainPolicy FlashCrossDomainPolicyFromString(string xdomain, string hosturl)
{
UnityCrossDomainHelper.SetWebSecurityHostUriDelegate(() => hosturl);
var ms = new MemoryStream(Encoding.UTF8.GetBytes(xdomain));
return FlashCrossDomainPolicy.FromStream(ms);
}
}
}
|