Explanation of Cross Domain and Client Access Policy files for Silverlight

本文详细介绍了Silverlight中解决跨域安全问题的方法,包括如何使用clientaccesspolicy.xml和crossdomain.xml文件来配置不同场景下的访问权限。

 

 

Taking some of the good ideas from Adobe Flash in regards to security policy, Silverlight has implemented a similar security model to block unauthorized cross domain network calls. This plugs a potential security hole where a malicious Silverlight application could run on a page the user is viewing and make API and network calls to domains that the user is not on without his or her knowledge. Microsoft has a quick write-up on some of the security issues this solves here: http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx .

Commonly, this issue is usually encountered in Silverlight when you see an exception like this:

An error occurred while trying to make a request to URI "http://localhost/myservice.asmx". This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services.

In order for Silverlight to call a remote resource on a different domain from where the XAP file was served such as a Web Service or RSS feed, the domain where the service or feed resides must grant access to the Silverlight application. The way this is done is using an XML file with the name "clientaccesspolicy.xml" or "crossdomain.xml".

As the above diagram illustrates, when your Silverlight application communicates with the server that served the application, the communication here is allowed because this is on the same domain from where your app was served. But when your Silverlight application attempts to communicate with a 3rd party remote server, a policy must exist (which is defined in either the "clientaccesspolicy.xml" or "crossdomain.xml" file) that allows this communication. If not policy exists on the domain different from the domain from where your Silverlight app was served, the communication is not allowed (hence the name "cross domain script attacks" because it is communication a-"cross" domains).

Difference between Cross Domain and Client Access Policy files

"crossdomain.xml" was created originally for use with Flash applications. But Silverlight also supports this file. "clientaccesspolicy.xml" is specific to Silverlight and allows you to configure specific access rules for HTTP/HTTPS communication and allowable domains.

To enable communication to a server for a specific Silverlight application, both files do not need to be present. You can choose either one depending on your preference but at least one of the files must be there with the correct rules configured. If you want to enable access for both Silverlight and Flash applications, then you might consider just having a "crossdomain.xml" file (or you could still have both this case).

Both the Cross Domain and the Client Access Policy files are XML but have different elements and format.

Example of Client Access Policy file

Client Access Policy files give you more granular controls over traffic types, access to resources, and specific HTTP header information. The following example will allow any and all request to this web server from Silverlight:

<?xml version="1.0" encoding="utf-8"?>

<access-policy>

  <cross-domain-access>

    <!-- Silverlight 3 or higher requires the http-request-headers attribute. -->

    <policy>

      <allow-from http-request-headers="*">

          <domain uri="http://*" />

          <domain uri="https://*" />

      </allow-from>

      <grant-to>

        <resource path="/" include-subpaths="true"/>

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>

You can specify that only a specific file can be accessed. For instance, here we only allow HTTP traffic from any domain and we only allow these requests to serve the "myfile.xml" file:

<access-policy>

  <cross-domain-access>

    <policy>

      <allow-from http-request-headers="*">

        <domain uri="http://*" />

      </allow-from>

      <grant-to>

        <resource path="myfile.xml" include-subpaths="false" />

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>

Here we only allow the domains microsoft.com and repeatsys.com access:

<access-policy>

  <cross-domain-access>

    <policy>

      <allow-from http-request-headers="*">

        <domain uri="http://www.microsoft.com" />

        <domain uri="http://www.repeatsys.com" />

      </allow-from>

      <grant-to>

        <resource path="/" include-subpaths="true" />

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>

This example shows how to use the wildcard (*) to allow any sub-domain of a specific domain access. Here we are allowing all requests from any sub-domain of devtoolshed.com access:

<access-policy>

  <cross-domain-access>

    <policy>

      <allow-from http-request-headers="*">

        <domain uri="http://*.devtoolshed.com" />

      </allow-from>

      <grant-to>

        <resource path="/" include-subpaths="true" />

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>

Example of Cross Domain file

The following example will allow any and all requests to this web server from Flash or Silverlight:

<?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>

Technically, this completely eliminates any security from the cross domain policy so although this is really convenient for development purposes, it should probably not be used in production in most cases.

If you wanted to only allow certain domains to access the system, your file could be configured like this:

<!DOCTYPE cross-domain-policy

SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

  <allow-access-from domain="www.microsoft.com" />

  <allow-access-from domain="www.devtoolshed.com" />

</cross-domain-policy>

If you wanted to block all access to your server from any Silverlight or Flash application, you could use this code:

<!DOCTYPE cross-domain-policy

  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

</cross-domain-policy>

Also note, you can use the wildcard (*) character to handle the situation of multiple sub-domains on a specific domain. For instance, this file shows how to allow all sub-domains from devtoolshed.com to communicate with the server:

<!DOCTYPE cross-domain-policy

  SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>

  <allow-access-from domain="*.devtoolshed.com"/>

</cross-domain-policy>

Where do I put the crossdomain.xml or clientaccesspolicy.xml file?

The crossdomain.xml or clientaccesspolicy.xml MUST be located at the root of your web server. If you put the file(s) in a sub-directory where your website files might be, they don’t count and won’t work. The file(s) need to be in the root of your web server so if your domain is www.devtoolshed.com , then the file(s) should be located at: www.devtoolshed.com/crossdomain.xml or www.devtoolshed.com/clientaccesspolicy.xml respectively.

What is the order that Silverlight requests these files?

When a Silverlight app communicates to a remote server, it first requests the "clientaccesspolicy.xml" file and then if that is not found, it falls back on the "crossdomain.xml" file to check if it exists. If neither does, communication will fail if this request is to a different domain than the one from which the Silverlight app was served from.

A great diagram of the process to request the file is available at: https://community.dynamics.com/blogs/cesardalatorre/comments/9579.aspx

It is a bit confusing at first thought because Silverlight is actually the one that deciphers the policy even though the policy is set and maintained at the web server where the resources that are being requested reside. Once you get over that mental hurdle, the rest is pretty easy to understand.

Domains and sub-domains are different

Also note that when we say "domain" this does not include sub domains. To a Silverlight app, the domains "www.devtoolshed.com" and "fudge.devtoolshed.com" are completely different. They must both be allowed separately. Sub-domains on a specific domain are treated as different domains to cross domain policies.

HTTP and HTTPS are different

HTTP and HTTPS are treated as separate types of requests and must be specifically allowed. Even after doing this, you may still have problems in your Silverlight app if you are running HTTPS and try to make an HTTP call or vice versa so watch out for cross SSL communication (this is kind of a separate issue from the cross domain security issues but worth noting here).

Your XML must be well formed

Even if the clientaccesspolicy.xml or crossdomain.xml file is present, if it is not well-formed XML, then it will fail to load and communication will be not be allowed. In Silverlight version 3 or higher, the clientaccesspolicy.xml file MUST contain the "http-request-headers" attribute or the file will be treated as not well formed.

 

Link:http://www.devtoolshed.com/explanation-cross-domain-and-client-access-policy-files-silverlight

基于模拟退火的计算器 在线运行 访问run.bcjh.xyz。 先展示下效果 https://pan.quark.cn/s/cc95c98c3760 参见此仓库。 使用方法(本地安装包) 前往Releases · hjenryin/BCJH-Metropolis下载最新 ,解压后输入游戏内校验码即可使用。 配置厨具 已在2.0.0弃用。 直接使用白菜菊花代码,保留高级厨具,新手池厨具可变。 更改迭代次数 如有需要,可以更改 中39行的数字来设置迭代次数。 本地编译 如果在windows平台,需要使用MSBuild编译,并将 改为ANSI编码。 如有条件,强烈建议这种本地运行(运行可加速、可多次重复)。 在 下运行 ,是游戏中的白菜菊花校验码。 编译、运行: - 在根目录新建 文件夹并 至build - - 使用 (linux) 或 (windows) 运行。 最后在命令行就可以得到输出结果了! (注意顺序)(得到厨师-技法,表示对应新手池厨具) 注:linux下不支持多任务选择 云端编译已在2.0.0弃用。 局限性 已知的问题: - 无法得到最优解! 只能得到一个比较好的解,有助于开阔思路。 - 无法选择菜品数量(默认拉满)。 可能有一定门槛。 (这可能有助于防止这类辅助工具的滥用导致分数膨胀? )(你问我为什么不用其他语言写? python一个晚上就写好了,结果因为有涉及json读写很多类型没法推断,jit用不了,算这个太慢了,所以就用c++写了) 工作原理 采用两层模拟退火来最大化总能量。 第一层为三个厨师,其能量用第二层模拟退火来估计。 也就是说,这套方法理论上也能算厨神(只要能够在非常快的时间内,算出一个厨神面板的得分),但是加上厨神的食材限制工作量有点大……以后再说吧。 (...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值