# REST 安全備忘單
> 原文:[Cryptographic Storage Cheat Sheet](https://www.owasp.org/index.php/Cryptographic_Storage_Cheat_Sheet)
> 來源:[REST安全備忘單](http://cheatsheets.hackdig.com/?6.htm)
百度dor加入翻譯項目,并翻譯此章節
## 簡介
REST 或 REpresentational State Transfer 是通過URL元素 系統的表達特定的實體 ,REST是一個以架構風格來構建 Web頂層服務而不是一個架構。REST是通過使用簡單的URL用基于web系統的交互而不是通過復雜的http請求實體 或者 post 參數 從系統中請求特定的條目。本文是幫助基于REST服務最佳實踐的向導(而不是詳盡的手冊)
## 認證和會話管理
RSET風格的Web服務應該(should)使用基于會話的認證,使用通過POST請求 或使用作為 POST 請求體中的參數的API key 或 cookie 來建立的會話 。 用戶名(usernames)和密碼(passwords) , 會話標識符(session tokens) 和 API keys 都不應該(should not)出現在 URL 中,因為這樣做的話web 服務器的日志系統能夠捕獲這些值并且他們具有內在的價值。
如下面的兩個url是合理的
+ https://example.com/resourceCollection//action
+ https://twitter.com/vanderaj/lists
下面的就是不合理的
+ https://example.com/controller//action?apiKey=a53f435643de32 (API Key 在 URL中)
+ http://example.com/controller//action?apiKey=a53f435643de32 ( API Key 在URL中 而且沒有使用https 加密傳輸 )
## 保護會話狀態
很多web services 都被盡可能的寫成無狀態的。This usually ends up with a state blob being sent as part of the transaction.
+ 考慮到僅僅使用 session token 或者 api key 在服務端的緩存中維系客戶端的狀態 。這是很多web app 的做法,這也是比較安全的原因。
+ 反重放(Anti-replay ) 攻擊者能夠通過剪貼和復制報文偽裝成某個用戶。考慮到使用有時間限制的加密秘鑰 加密 session token 或者 api key 日期 時間 和 來源IP ,總的來說 對本地客戶端存儲的認證token(authentication token)做的保護 能夠 避免一些 重放攻擊的
+ 不要讓它容易解密和改變內部狀態比它應該做得更好
簡短的說,即使你有個一個 brochureware web 站點 , 你不要放入到 https://example.com/users/2313/edit?isAdmin=false&debug=false&allowCSRPanel=false 這個url中,這樣你將很快以有很多 、 管理員(admins ) 桌面使用幫助用戶(help desk helpers ) 以及 開發者(developers) 結束
## Authorization(授權)
### Anti-farming
就像比價站點又或像一些聚合站點興起一樣 , 很多REST 風格的的web服務興起 并且提供服務 。由于沒有技術手段阻止它的使用,所以強烈考 通過提供高速服務(farming)作為一種商業模式來激勵使收費成為可能 或者通過合約限制服務的使用的條目和條件。CAPTHAs 和 一些相似的方法能夠減少一些簡單的攻擊行為,當是這個不能阻止一些完備機構或者有很強技術能力的攻擊 。使用雙向認證的客戶端TLS 也許是一個限制授信組織訪問的一種途徑,但是這并不能保證萬無一失 尤其是當憑證被人為的重放或者由于互聯網事故造成的重放。
### Protect HTTP methods
REST 風格的API 一般使用 GET(讀) POST(創建) PUT(替換/更新) 和 DELETE(刪除一條記錄) 這幾種http請求. 對于每一個單獨的資源集合、用戶或者動作 并不是所有的這些方法都是可用的。確保對會話(session)的token/API key 和相關聯的資源集合、動作和記錄對于請求的HTTP方法是可用的.例如 你有一個關于圖書館REST風格的API ,允許一個匿名的用戶刪除書的目錄實體的做法是不合適的,但是不管是圖書管理員還是匿名用戶 允許他們獲得圖書的目錄實體是沒有不妥的地方的。
### Whitelist Allowable Methods (白名單方法)
在同一個實體上對給定的一個URL允許多種方法進行不同的操作在REST 風格的服務是很常見的。 例如,一個GET的請求也許是讀一個實體 然而 當請求為PUT方法是就是更新一個已經存在的實體, 當請求是POST 方法時 將創建一個實體 , 請求為DELETE方法時將刪除一個已經存在的實體. 適當的限制可允許的動作以便只有被允許的動作才能正常運行而其他動作都將返回一個適當的狀態碼(如 403 禁止訪問 ) 這點對服務(service)來說很重要 。
尤其在Java EE中 要實現這點比較困難。可以參看 Bypassing Web Authentication and Authorization with HTTP Verb Tampering 對常見配置的說明
### Protect privileged actions and sensitive resource collections( 保護私有的方法和敏感的資源集合)
并不是所有的用戶都能訪問所有的web service . 不想讓一個管理web 的 services 被濫用 這是至關重要的:
+ https://example.com/admin/exportAllData
會話token 或 API key 應該被單獨的作為cookie 或 請求體的參數(body parameter)來發送 用來確保 私有集合或者私有方法對沒有授權的使用是被適當的保護的
### Protect against cross-site request forgery( 保護偽造的跨域請求)
通過REST 風格的web services 暴露的資源要確保任何 PUT POST DELETE 方法的請求對偽造的跨站點請求是被保護的 這點很重要。通過基于token 的訪問 是一個典型的案例。
你可以通過查看 [Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%40CSRF%41_Prevention_Cheat_Sheet) 來獲得更多的關于如何防范 CSRF 的信息。
如果在里的應用中存在任何的XSS 即使使用隨機的tokens CSRF 還是很容易實現的。所以請確保你已經知道如何防止 XSS 。
### Insecure Direct object references ( 不安全的直接對象引用 )
這個也許看起來很明顯,但是如果你有一個銀行賬戶的REST 的 WEB 服務 ,你必須確保有對主鍵和外鍵有足夠的認證 :
+ https://example.com/account/325365436/transfer?amount=$100.00&toAccount=473846376
在這個例子中 , 將錢從任何一個賬戶中轉賬到另外一個賬戶中是可能的,這明顯是很愚蠢的。這個例子中甚至沒有一個隨機的token 來確保安全
+ https://example.com/invoice/2362365
在這個例子中,獲取所有單據的拷貝時有可能的。
請確保你懂得怎么防護在OWASP (2010 年公布的 )中前10的 [insecure direct object references](https://www.owasp.org/index.php/Top_10_2010-A4-Insecure_Direct_Object_References ( 不安全的直接對象引用 )
## Input Validation ( 輸入驗證 )
### Input validation 101 (輸入驗證 101 )
除了將你知道所有到R的輸入驗證應用ESTful 的web 服務中去 還有添加額外10%驗證 ,因為自動化的工具能夠在數小時內很簡單的模糊你的接口 高速的結束。所以:
+ Assist the user > Reject input > Sanitize (filtering) > No input validation
協助用戶是最有意義的, 在很多場景中的情況是 問題存在于鍵盤和電腦兩者之間(PRBACK) 。幫助用戶輸入高質量的數據到你的 web 服務上,例如 一個有效的郵政編碼對一個被提供的地址是有意義的,或者一個有意義的日期。 如果不是 拒絕這個輸入。如果他們繼續 或者 文本框 又或其他難以驗證的領域 ,過濾輸入也許是不合算的但是對防止XSS和SQL 注入 攻擊是有幫助的。如果你已經減少了對輸入的過濾或者對輸入不做驗證了 ,確保輸入編碼對你的應用是非常強壯的。
要記錄輸入驗證失敗的條目,尤其如果你假設你寫的客戶端的編碼將調用你的web服務。事實上是任何人都能調用你的web服務 , 假設有個用戶每秒執行上百次的輸入驗證的失敗不是一個好的現象。考慮到在一定的數字每小時或每天速率限制API請求 用來防止API的濫用。
### Secure parsing
用安全解析器來分析請求消息。如果你使用的是XML , 確保使用的解析器是不容易被 XXE attacks(XXE 攻擊的)。
### Strong typing( 強類型 )
如果僅僅允許的指是true 或者 false 或者 是一個 數字 或者 小的可接受的數字 ,那執行大部分的攻擊是困難的 。盡快的使用強類型的輸入數據。
### Validate Incoming Content-Types ( 驗證輸入內容類型 )
當POST 或者 PUT 新數據時,客戶端將指定 Content-Type( 例如:application/xml or application/json ) 。客戶端應當不假設Content-Type , 但是需要檢測頭部設定的Content-Type 和 實際內容是否一致。 一個沒有指定Content-Type的頭部 或者一個 非預期Content-Type 的頭部 將導致服務器返回一個 406 不被接受 內容拒絕的響應。
### Validate Response Types ( 驗證響應類型 )
對于REST 服務允許多個響應類型是很常見的。(例如:application/xml 或者 application/json 和 客戶端通過請求頭部指定的響應類型的優先順序 )不要簡單的拷貝接受頭部到響應頭部的Content-type 。如果接受頭部和指定的允許的類型不一致將拒絕請求( 返回406 不被可接受的 響應)。
因為對于典型的響應類型有很多MIME類型,對于客戶端的文檔指定哪些MIME的類型應當被使用是很重要的。
### XML Input Validation(XML 輸入驗證 )
基于XML的服務必須確保對通過使用安全的XML解析器對常見的基于XML 攻擊有防護能力。這通常以為著需要防XML External Entity 攻擊,XML-signature wrapping 等。
對于這些攻擊可以參看http://ws-attacks.org
## Output Encoding (輸入編碼)
### Send security headers( 發送安全的頭部)
為了確保給定資源的內容能夠被瀏覽器正確的解析,服務器應當總是發送 正確的Content-Type 的頭部 而且正確的 Content-Type 頭部應當包含字符設置 。服務器還應當發送X-Content-Type-Options:無探測確保了瀏覽器不能嘗試決定不同的Content-Type 而是實際發送的(能導致XSS)。
此外 客戶端應當發送X-Frame-Options:防止在較老的瀏覽器中拖拽“點擊劫持”攻擊
### JSON encoding ( json 編碼 )
阻止任意的遠端javascript代碼在瀏覽器中執行或者如果你在使用node.js 阻止其在服務器上執行 是json 編碼者一個主要的關心的地方。使用一個合適的JSON 序列化器去編碼用戶提供的數據以阻止用戶提供的輸入在瀏覽器中執行 是很重要的。
當向瀏覽器的DOM樹種插入一個值時,強烈建議使用 .value/.innerText/.textContent 而不是使用 .innerHTML ,這樣能避免簡單的DOM XSS 攻擊。
### XML encoding( XML 編碼)
XML 應當不使用連接的字符串來建立。應當總是使用XML 序列化器來構造 。這能保證發送到瀏覽器的XML 是可解析的 而且不包含XML 注入。如果需要查看更多的信息可以參看 Web Service Security Cheat Sheet
## Cryptography ( 加密 )
### Data in transit( 數據傳輸 )
除了完全是對公眾公開的信息 其他都應該使用 TLS , 尤其是有證書 更新 刪除 和 其他任何傳輸的內容 都應該使用TLS來傳輸。在現代的硬件下TLS的花費是微不足道的 ,這點微不足道成本遠遠低于因為安全對終端用戶的賠償。
對高度隱私的web 服務 可以考慮使用相互認證的客戶端憑證來提供額外的保護。
### Data in storage( 存儲數據 )
當談到正確的存儲敏感或受管制的數據時,任何一個web應用程序都應該建議使用領先的做法。關于更多信息可以參看 OWASP Top 10 2010 - A7 Insecure Cryptographic Storage.
## Related Articles
OWASP Cheat Sheets Project Homepage
* [OWASP Cheat Sheet Series](https://www.owasp.org/index.php/OWASP_Cheat_Sheet_Series "OWASP Cheat Sheet Series")
Developer Cheat Sheets (Builder)
* [Authentication Cheat Sheet](https://www.owasp.org/index.php/Authentication_Cheat_Sheet "Authentication Cheat Sheet")
* [Choosing and Using Security Questions Cheat Sheet](https://www.owasp.org/index.php/Choosing_and_Using_Security_Questions_Cheat_Sheet "Choosing and Using Security Questions Cheat Sheet")
* [Clickjacking Defense Cheat Sheet](https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet "Clickjacking Defense Cheat Sheet")
* [C-Based Toolchain Hardening Cheat Sheet](https://www.owasp.org/index.php/C-Based_Toolchain_Hardening_Cheat_Sheet "C-Based Toolchain Hardening Cheat Sheet")
* [Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%40CSRF%42_Prevention_Cheat_Sheet)
* [Cryptographic Storage Cheat Sheet](https://www.owasp.org/index.php/Cryptographic_Storage_Cheat_Sheet "Cryptographic Storage Cheat Sheet")
* [DOM based XSS Prevention Cheat Sheet](https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet "DOM based XSS Prevention Cheat Sheet")
* [Forgot Password Cheat Sheet](https://www.owasp.org/index.php/Forgot_Password_Cheat_Sheet "Forgot Password Cheat Sheet")
* [HTML5 Security Cheat Sheet](https://www.owasp.org/index.php/HTML5_Security_Cheat_Sheet "HTML5 Security Cheat Sheet")
* [Input Validation Cheat Sheet](https://www.owasp.org/index.php/Input_Validation_Cheat_Sheet "Input Validation Cheat Sheet")
* [JAAS Cheat Sheet](https://www.owasp.org/index.php/JAAS_Cheat_Sheet "JAAS Cheat Sheet")
* [Logging Cheat Sheet](https://www.owasp.org/index.php/Logging_Cheat_Sheet "Logging Cheat Sheet")
* [.NET Security Cheat Sheet](https://www.owasp.org/index.php/.NET_Security_Cheat_Sheet ".NET Security Cheat Sheet")
* [OWASP Top Ten Cheat Sheet](https://www.owasp.org/index.php/OWASP_Top_Ten_Cheat_Sheet "OWASP Top Ten Cheat Sheet")
* [Password Storage Cheat Sheet](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet "Password Storage Cheat Sheet")
* [Pinning Cheat Sheet](https://www.owasp.org/index.php/Pinning_Cheat_Sheet "Pinning Cheat Sheet")
* [Query Parameterization Cheat Sheet](https://www.owasp.org/index.php/Query_Parameterization_Cheat_Sheet "Query Parameterization Cheat Sheet")
* [Ruby on Rails Cheatsheet](https://www.owasp.org/index.php/Ruby_on_Rails_Cheatsheet "Ruby on Rails Cheatsheet")
* REST Security Cheat Sheet
* [Session Management Cheat Sheet](https://www.owasp.org/index.php/Session_Management_Cheat_Sheet "Session Management Cheat Sheet")
* [SQL Injection Prevention Cheat Sheet](https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet "SQL Injection Prevention Cheat Sheet")
* [Transport Layer Protection Cheat Sheet](https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet "Transport Layer Protection Cheat Sheet")
* [Unvalidated Redirects and Forwards Cheat Sheet](https://www.owasp.org/index.php/Unvalidated_Redirects_and_Forwards_Cheat_Sheet "Unvalidated Redirects and Forwards Cheat Sheet")
* [User Privacy Protection Cheat Sheet](https://www.owasp.org/index.php/User_Privacy_Protection_Cheat_Sheet "User Privacy Protection Cheat Sheet")
* [Web Service Security Cheat Sheet](https://www.owasp.org/index.php/Web_Service_Security_Cheat_Sheet "Web Service Security Cheat Sheet")
* [XSS (Cross Site Scripting) Prevention Cheat Sheet](https://www.owasp.org/index.php/XSS_%40Cross_Site_Scripting%42_Prevention_Cheat_Sheet )
Assessment Cheat Sheets (Breaker)
* [Attack Surface Analysis Cheat Sheet](https://www.owasp.org/index.php/Attack_Surface_Analysis_Cheat_Sheet "Attack Surface Analysis Cheat Sheet")
* [XSS Filter Evasion Cheat Sheet](https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet "XSS Filter Evasion Cheat Sheet")
* [REST Assessment Cheat Sheet](https://www.owasp.org/index.php/REST_Assessment_Cheat_Sheet "REST Assessment Cheat Sheet")
Mobile Cheat Sheets
* [IOS Developer Cheat Sheet](https://www.owasp.org/index.php/IOS_Developer_Cheat_Sheet "IOS Developer Cheat Sheet")
* [Mobile Jailbreaking Cheat Sheet](https://www.owasp.org/index.php/Mobile_Jailbreaking_Cheat_Sheet "Mobile Jailbreaking Cheat Sheet")
OpSec Cheat Sheets (Defender)
* [Virtual Patching Cheat Sheet](https://www.owasp.org/index.php/Virtual_Patching_Cheat_Sheet "Virtual Patching Cheat Sheet")
Draft Cheat Sheets
* [Access Control Cheat Sheet](https://www.owasp.org/index.php/Access_Control_Cheat_Sheet "Access Control Cheat Sheet")
* [Application Security Architecture Cheat Sheet](https://www.owasp.org/index.php/Application_Security_Architecture_Cheat_Sheet "Application Security Architecture Cheat Sheet")
* [Business Logic Security Cheat Sheet](https://www.owasp.org/index.php/Business_Logic_Security_Cheat_Sheet "Business Logic Security Cheat Sheet")
* [PHP Security Cheat Sheet](https://www.owasp.org/index.php/PHP_Security_Cheat_Sheet "PHP Security Cheat Sheet")
* [Secure Coding Cheat Sheet](https://www.owasp.org/index.php/Secure_Coding_Cheat_Sheet "Secure Coding Cheat Sheet")
* [Secure SDLC Cheat Sheet](https://www.owasp.org/index.php/Secure_SDLC_Cheat_Sheet "Secure SDLC Cheat Sheet")
* [Threat Modeling Cheat Sheet](https://www.owasp.org/index.php/Threat_Modeling_Cheat_Sheet "Threat Modeling Cheat Sheet")
* [Web Application Security Testing Cheat Sheet](https://www.owasp.org/index.php/Web_Application_Security_Testing_Cheat_Sheet "Web Application Security Testing Cheat Sheet")
* [Grails Secure Code Review Cheat Sheet](https://www.owasp.org/index.php/Grails_Secure_Code_Review_Cheat_Sheet "Grails Secure Code Review Cheat Sheet")
* [IOS Application Security Testing Cheat Sheet](https://www.owasp.org/index.php/IOS_Application_Security_Testing_Cheat_Sheet "IOS Application Security Testing Cheat Sheet")
* [Key Management Cheat Sheet](https://www.owasp.org/index.php/Key_Management_Cheat_Sheet "Key Management Cheat Sheet")
* [Insecure Direct Object Reference Prevention Cheat Sheet](https://www.owasp.org/index.php/Insecure_Direct_Object_Reference_Prevention_Cheat_Sheet "Insecure Direct Object Reference Prevention Cheat Sheet")
* [Content Security Policy Cheat Sheet](https://www.owasp.org/index.php/Content_Security_Policy_Cheat_Sheet "Content Security Policy Cheat Sheet")
## Authors and Primary Editors
Erlend Oftedal -?[erlend.ofted<wbr>al@owasp.org](mailto:erlend.oftedal@owasp.org)
Andrew van der Stock -?[vanderaj@owa<wbr>sp.org](mailto:vanderaj@owasp.org)