The Silverlight policy file format is described below using a DTD.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- A DTD for the Silverlight Policy File -->
<!ELEMENT access-policy (cross-domain-access)>
<!ELEMENT cross-domain-access (policy+)>
<!ELEMENT policy (allow-from)>
<!ELEMENT policy (grant-to)>
<!ELEMENT allow-from (domain+)>
<!ATTLIST allow-from http-request-headers CDATA>
<!ELEMENT domain EMPTY >
<!ATTLIST domain uri CDATA #REQUIRED>
<!ELEMENT allow-from http-methods CDATA>
<!ELEMENT grant-to (resource+)>
<!ELEMENT grant-to (socket-resource+)>
<!ELEMENT grant-to EMPTY>
<!ATTLIST resource path CDATA #REQUIRED>
<!ATTLIST resource include-subpaths (true|false) "false">
<!ATTLIST socket-resource port CDATA #REQUIRED protocol #REQUIRED>
<!-- End of file. --><!-- End of file. -->
The following table describes the possible ELEMENT entries.
Element | Child of | Required | Allow Multiple | Description |
|---|
access-policy | N/A (root) | Yes | No | The root element of a Silverlight policy file. |
cross-domain-access | access-policy | Yes | No | Defines all of the cross domain policies for a site. |
policy | cross-domain-access | Yes | Yes | Defines a cross domain policy for requests coming from a certain domain or set of domains. Multiple policies can be defined in the same file. |
allow-from | policy | Yes | No | Defines all the domains that are affected by this policy. Defines the sites that are allowed to access resources in a certain policy. By using the allow-from element for a particular policy, you implicitly deny access for all non-listed domains. If allow-from is empty, then the policy file grants access to no sites. |
domain | allow-from- or - allow-from http-methods | Yes | Yes | Defines a domain (or a specific Silverlight application) that is affected by this policy. |
grant-to | policy | Yes | No | Defines all the server’s resources that are affected by this policy. |
resource | grant-to | Yes (for WebClient and HTTP classes) | Yes | Defines the resources that can be accessed in a certain policy. This element applies only to connection requests that use WebClient and HTTP classes. |
socket-resource | grant-to | Yes (for sockets) | Yes | Defines the socket resources that can be accessed in a certain policy. This element applies only to connection requests that use sockets. |
The following table describes the possible ATTRIBUTE entries allowed on an ELEMENT.
Element | Attribute | Required | Default | Description |
|---|
allow-from | http-request-headers | No | No headers are allowed, except for the Content-Type header, which is always permitted. | A case-insensitive, comma-separated list of allowed headers for the domain specified in the domain attribute. Valid characters, per RFC822, are ASCII 33-41, 42-57, and 59-126. This includes all printable non-space ASCII characters except colons (which end a header name), and asterisks (which are used as wildcards). Wildcards can be used to specify all headers, or can be used with a single header name as a suffix allowing for all headers that start with the string value preceding the wildcard character. The '*' character used alone is interpreted as all request headers can be sent that are normally allowed with the HttpWebRequest class. Some common headers are considered restricted and are either exposed directly (Content-Type, for example) or are protected by the system and cannot be set or changed. For more information on restricted headers, see WebHeaderCollection. If this attribute is missing, no headers are allowed to be sent except the Content-Type header. |
allow from | http-methods | No | Only the GET and POST method are allowed by the client HTTP handler. | Defines whether the client HTTP handler allows custom request methods other than GET and POST to be used. This policy only applies if the HTTP handler for a URI is changed to use the client HTTP handler.The default browser HTTP handler allows only the GET and POST request methods to be used.The only possible value for the request methods allowed by this policy element is the wildcard of "*". |
domain | uri | Yes | N/A | Lists the allowed domains and specific Silverlight applications that can access the granted paths in this policy. The uri specifies a scheme and a domain that connections are allowed from. The scheme and port may be specified. Three types of wildcards are accepted: 1. The standalone '*' wildcard character used alone is interpreted as accept all connections from the same schemed applications hosted on any domain. 2. The "http://*" literal wildcard option is interpreted as accept all connections from HTTP schemed applications on any domain. 3. A subdomain wildcard option is represented as a scheme allowed followed by a '*' character and a '.' character followed by the rest of hostname. The wildcard is used as the first segment of the hostname. This is considered a subdomain wildcard. An attribute value of "http://*.contoso.com" allows only HTTP for the scheme and accepts connections from Silverlight applications hosted on any subdomain of "contoso.com". |
resource | path | Yes (for WebClient and HTTP classes) | N/A | The attribute is the URI relative to root of the domain. It refers to a specific path that can represent a web service or a file. The path cannot contain wildcard characters or characters not recognized by Uniform Resource Identifier (URI): Generic Syntax, http://ietf.org/rfc/rfc3986 This element and attribute are used solely for requests from WebClient and HTTP classes. |
resource | include-subpaths | No | False | Specifies whether subpaths of the associated path should be included. A true value will allow a path to match the exact path designated by the path attribute or the path designated by the path attribute and any appended subpaths. A false value will only allow a match to the exact path designated by the path attribute. The default value is false if this attribute is omitted. This element and attribute are used solely for requests from WebClient and HTTP classes. |
socket-resource | port | Yes | N/A | Specifies the port or port range that an application using sockets is allowed to connect to. This element and attribute are used solely for requests from socket classes. |
socket-resource | protocol | Yes | N/A | Specifies the protocol that an application using sockets is allowed to connect with. The only protocol currently supported is tcp. This element and attribute are used solely for requests from socket classes. |
The uri attribute of the domain element refers to the domains and specific Silverlight applications that are granted access to the paths specified by the <grant-to> element tags within the policy file. The included domain must exactly match the requesting domain. For example, a domain uri="http://contoso.com" only allows access from http://contoso.com, not from http://web.contoso.com.
There are three types of wildcards allowed:
A standalone '*' wildcard. This option is used to allow access to all domains of the same scheme. An HTTP service will allow all HTTP callers. An HTTPS service will allow all HTTPS callers.
An "http://*" literal wildcard. This option explicitly allows all HTTP callers, even if this is an HTTPS service.
A subdomain wildcard. This option uses a wildcard at the first segment of the path ("http://*.contoso.com", for example) that allows all subdomains of the domain specified. So for the example. http://web.contoso.com and http://mail.contoso.com would be allowed. Note that a uri path where the wildcard does not occur as a prefix (http://web.*.com, for example) is disallowed.
The Silverlight policy file allows connections to an HTTPS service using a cross- domain policy file. When an explicit allow-from attribute on a domain element is provided in the policy, that domain includes the scheme information. An example would be the following:
<allow-from>
<domain uri="https://contoso.com">
</allow-from>).
This above policy would allow only connection from https://contoso.com using the HTTPS scheme.
The wildcard behavior of a Silverlight policy file for a standalone '*' wildcard depends on the type of service being accessed:
| HTTP Service <domain uri=”*”/> | HTTPS Service <domain uri=”*”/> |
|---|
HTTP-hosted Silverlight application | Allowed | Denied |
HTTPS-hosted Silverlight application | Allowed | Allowed |
This wildcard behavior allows all Silverlight applications hosted on HTTP and HTTPS to connect to an HTTP service. This allows only Silverlight applications hosted on HTTPS to connect to an HTTPS service.
The wildcard behavior of a Silverlight policy file for an "http://*" literal wildcard depends on the type of service being accessed:
| HTTP Service <domain uri=”http://*”/> | HTTPS Service <domain uri=”http://*”/> |
|---|
HTTP-hosted Silverlight application | Allowed | Allowed |
HTTPS-hosted Silverlight application | Denied | Denied |
This behavior for "http://*" literal wildcard allows all Silverlight applications hosted on HTTP to connect to an HTTP or HTTPS service.
The wildcard behavior of a Silverlight policy file for an "https://*" literal wildcard depends on the type of service being accessed:
| HTTP Service <domain uri=”https://*”/> | HTTPS Service <domain uri=”https://*”/> |
|---|
HTTP-hosted Silverlight application | Denied | Denied |
HTTPS-hosted Silverlight application | Allowed | Allowed |
This behavior for "http://*" literal wildcard allows all Silverlight applications hosted on HTTP to connect to an HTTP or HTTPS service.
If an HTTPS service wants to allow all connections from both HTTP and HTTPS, then there are two methods to accomplish this on Silverlight version 3 . One method is that the policy file must include domain uri entries for both the standalone '*' wildcard and the "http://*" literal wildcard. An example would be the following:
<allow-from>
<domain uri="*">
<domain uri="http://*">
</allow-from>).
A second method is that the policy file must include domain uri entries for both the standalone "http://*" literal wildcard and the "https://*" literal wildcard. An example would be the following:
<allow-from>
<domain uri="http://*">
<domain uri="https://*">
</allow-from>).
You implicitly deny access for all domains not listed in a <domain> element tag in a Silverlight policy file.
Each domain must be in the following BNF format:
domain = "http:"|"https:" authority
authority = host [ ":" port ]
host = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
port = *DIGIT
Each Silverlight application uri is an absolute uri with no query or fragment for an .html, .xaml, or .xap file.
The Silverlight policy file allows custom methods other than GET and POST to be allowed when the client HTTP handler has been specified for a URI. The default browser HTTP handler allows only GET and POST requests to be used. When an allow-from http-methods element is specified, the allowed request methods must be specified as the wildcard of "*". The domain includes the scheme information. An example would be the following:
<allow-from http-methods="*">
<domain uri="*">
</allow-from>).
This above policy would allow only custom requests other than GET and POST requests to be used by the client HTTP handler. The allow-from http-methods policy element was added for Silverlight version 3. This policy element is not recognized by Silverlight version 2 clients. If a server needs to support both Silverlight 3 and Silverlight 2 clients, then the policy file should contain two policy sections, one with the allow-from http-methods policy element and one with the allow-from policy element. An example would be the following:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<!--Enables Silverlight 3 all methods functionality-->
<policy>
<allow-from http-methods=”*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/api" include-subpaths="true"/>
</grant-to>
</policy>
<!--Enables Silverlight 2 clients to continue to work normally -->
<policy>
<allow-from >
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/api" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
A single Silverlight policy file can have policies included for connection requests from WebClient and HTTP classes in the System.Net namespace and also policies for connection requests from sockets.
The process to determine if a request is permissible with the policy file is as follows:
Is the scheme of the requested URI the same as the application’s site of origin? Is HTTP the scheme of the requested URI and the scheme of the application's site of origin is HTTPS? Is HTTPS the scheme of the requested URI and the scheme of the application's site of origin is HTTP If not one of these three cases, the request does not have access and should not be sent.
For each <policy> element:
Is the application’s site of origin allowed access? This can be done either explicitly in the <policy> allow-from domain attribute or the allow-from attribute can have a domain element with a wildcard for the uri attribute, indicating that everyone is allowed access. Note that if allow-from is omitted, the default behavior is that nobody is allowed access.
Does the policy grant access to the request URI?
If any of the following conditions are true, then the request does have access and should be sent. Otherwise, go to next <policy> in file.
The requested URI exactly matches the domain and path in a <resource> tag.
The requested URI falls beneath the domain and path in a <grant-to> tag and the <grant-to> tag include-subpaths attribute has the value set to true.
For a socket request, the protocol and port attribute in a <socket-resource> tag in the <grant-to> section of this <policy> specifies tcp for the protocol and a port or port range within the allowable range of 4502-4534.