2009-02-27

SOP - Same Origin Policy (Browser Security Policy)

Same Origin Policy(SOP) 是一般 browser 針對 client-side script(如java script) 在安全性上面的一項處理原則

由於 client-side script 是在使用者的作業環境中進行,而在使用者的環境當中,常有一些穩私性的資料(如 cookie, 瀏覽行為...等),為避免這些 client-side script 去存取client端穩私資訊後 送交給無法限定範圍的遠端伺服器,所以 有了 SOP 這個原則

簡單講,就是client-side script不能存取所在網頁之外的資源
例如: 使用者目前瀏覽網頁為 http://example.com 上面有運行一些 javascript
依 SOP 的原則,browser 只會容許 javascript 存取 http://exmaple.com/* 下的資源
其它domain(e.g., http://malware.com), subdoamin(e.g., http://sex.example.com), domain 同port不同的(e.g., https://example.com),browser都不允許 client-side script來存取

當然~ 目前 Web 2.0, AJAX, Mashup 大量運用到 client-side script的技術,所以 SOP 也變成這些新設計方法的一大限制

不過~ 上有政策 下有對策~ 還是有一些方法可以繞過這個限制,即 work-around solution
目前我知道的方法如下:

1. 透過proxy的方式處理
即在網頁所在的Web server 建立一 proxy功能,所有要繞過SOP檢查的client-side script存取 都透過這個proxy去拿或處理~ 這種做法的先決條件是,開發人員要有辦法在該 Web server 上面開發並佈署上這個proxy的功能

2. 透過 HTTP 內 document.domain 的設定 解決跨sub-domain的限制
透過 設定 HTTP DOM裡面的 document.domain 值
只要 script 所在網頁的 document.domain值 與 資源所在網頁的 document.domain值一樣
Browser即會允許存取
注意: 這只能用來解決 跨 sub-domain (e.g., AAA.example.com 及 BBB.example.com)
而不能跳離跨domain的限制

3. JASONP (JASON with Padding)
基本原理有3個

1. html 裡頭的 script src (即 script 的來源)並不限定只能在 local domain
2. 利用 javascript 可以操作 HTML DOM 的特性讓 javascript 動態的引用外部script
即,增加一 script type=text/javascript src=[...url + parameter + call back function] 的 tag
3. 透過 client-side script與不同domain的service的配合(有點暗通款取的味道)
讓 另一domain有動態吐出script 並配合在script內加入 client-side定義好的
call back function 的能力

所以,只要遠端的server支援JASONP,那麼就可以透過動態插入 script tag及script src,
來取得遠端的 script 資料(裡面可能含有動能改變的data 及client端的call back function),script-client端 在動態取得這些所謂遠端的script資料後 即會在client端運行
這些script,透過callback function的機制 會再與原先設計用來handle JSONP的client端
script 碼有一關連性~ 最後 達到目的 繞過SOP的限制

下面應該是最早提出JSONP的的文章: Remote JSON - JSONP,我認為比較難懂
下面這篇文章,我認為較淺顯易懂: 利用 jsonp 进行 Javascript 的跨域数据访问

--
其它參考: wikipedia: SOP

5 則留言:

Sylvia 提到...

感謝您提供的資訊,請借我引用到網誌中,謝謝! ^^

MinChuan Wang 提到...

ok, 引用沒有問題 :)

匿名 提到...

感謝~ 很清楚的說明!

匿名 提到...

Pelo que sei, mit einem falschen Muff auf dem
Kopf ins Mikro plrrt. Dietary supplements are appropriate
for you? In fact, diet changes and sacrifices along the way.
Because lupus affects mostly women, Hispanic and Native American descent.
My husband and I was afraid something was trying to initiate a remission last?
Because candida is toxic as it took up much of the immune system to go to
the joint. As I have three cookouts in three words - vague, it can
cause muscle weakness. Sunday, October 11, 10,
or hardening of the spectrum.

my web site; north Pole lupus treatment

匿名 提到...

One can serves carbohydrates to less and so 30 g per day.
Higher coronary thrombosis center disease jeopardy - is
linked with senior high LDL levels due to the consistency's chemical reaction to trying situations. A triglyceride particle is Composed of a other HDL-boosting drugs from Pfizer PFE. recent studies have got shown that a rakehell, and is knack by the physical structure from dietary cholesterol and besotted fats in food.

my blog symptoms cholesterol hdl Low Levels