在應用程序中添加HTTP響應頭可以提高Web應用程序的安全性。本文介紹如何通過定義EnvoyFilter添加HTTP響應頭。
前提條件
已創建一個ASM實例,并已將ACK集群添加到ASM實例中。具體操作,請參見創建ASM實例和添加集群到ASM實例。
背景信息
OWASP提供了最佳實踐指南和編程框架,描述了如何使用安全響應頭保護應用程序的安全。HTTP響應頭的基準配置如下:
HTTP響應頭 | 默認值 | 描述 |
Content-Security-Policy | frame-ancestors none; | 防止其他網站進行Clickjacking攻擊。 |
X-XSS-Protection | 1;mode=block | 激活瀏覽器的XSS過濾器(如果可用),檢測到XSS時阻止渲染。 |
X-Content-Type-Options | Nosniff | 禁用瀏覽器的內容嗅探。 |
Referrer-Policy | no-referrer | 禁用自動發送引薦來源。 |
X-Download-Options | noopen | 禁用舊版本IE中的自動打開下載功能。 |
X-DNS-Prefetch-Control | off | 禁用對頁面上的外部鏈接的推測性DNS解析。 |
Server | envoy | 由Istio的入口網關自動設置。 |
X-Powered-by | 無默認值 | 去掉該值來隱藏潛在易受攻擊的應用程序服務器的名稱和版本。 |
Feature-Policy | camera ‘none’; microphone ‘none’; geolocation ‘none’; encrypted-media ‘none’; payment ‘none’; speaker ‘none’; usb ‘none’; | 控制可以在瀏覽器中使用的功能和API。 |
以Bookinfo應用程序為例(詳情請參見在ASM實例關聯的集群中部署應用),通過Curl命令可以看到應用程序的HTTP響應頭信息如下。
curl -I http://{入口網關服務的IP地址}/productpage
# 輸出示例
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 5183
server: istio-envoy
date: Tue, 28 Jan 2020 08:15:21 GMT
x-envoy-upstream-service-time: 28
可以看到,在默認情況下,示例應用程序的入口首頁響應并沒有包含上述表格中安全相關的HTTP響應頭。通過Istio EnvoyFilter可以快速添加安全相關的HTTP響應頭。
操作步驟
按照ASM實例版本部署EnvoyFilter。
ASM實例版本為1.12.4.0及以上
您可以通過插件市場直接部署EnvoyFilter。
登錄ASM控制臺,在左側導航欄,選擇 。
在網格管理頁面,單擊目標實例名稱,然后在左側導航欄,選擇 。
在插件市場頁面,單擊添加HTTP響應頭,然后在插件詳情頁面,單擊新建插件實例。
在插件生效范圍區域,選中網關生效,然后單擊添加網關生效范圍。
在添加網關生效范圍對話框中,在選擇網關區域選中ingressgateway,單擊圖標,將ingressgateway添加到已選擇區域中,然后單擊確定。
說明ingressgateway為ASM默認部署的網關名稱,您也可以選擇希望生效添加HTTP響應頭能力的其它ASM網關。
在插件配置區域,刪除YAML框中的所有內容,然后打開生效開關,等待插件啟用。
在插件啟用后,ASM會自動創建EnvoyFilter。
ASM實例版本為1.12.4.0以下
您可以執行以下命令,直接部署EnvoyFilter。
kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: EnvoyFilter metadata: name: addheader-into-ingressgateway namespace: istio-system labels: asm-system: 'true' provider: asm spec: workloadSelector: # EnvoyFilter通過標簽選擇同一命名空間下的工作負載。 labels: istio: ingressgateway configPatches: # 需要修改的Envoy配置。 - applyTo: HTTP_FILTER match: context: GATEWAY proxy: proxyVersion: '^1\.9.*' listener: filterChain: filter: name: "envoy.filters.network.http_connection_manager" subFilter: name: "envoy.filters.http.router" patch: operation: INSERT_BEFORE value: # Lua腳本配置。 name: envoy.lua typed_config: "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua" inlineCode: |- function envoy_on_response(response_handle) function hasFrameAncestors(rh) s = rh:headers():get("Content-Security-Policy"); delimiter = ";"; defined = false; for match in (s..delimiter):gmatch("(.-)"..delimiter) do match = match:gsub("%s+", ""); if match:sub(1, 15)=="frame-ancestors" then return true; end end return false; end if not response_handle:headers():get("Content-Security-Policy") then csp = "frame-ancestors none;"; response_handle:headers():add("Content-Security-Policy", csp); elseif response_handle:headers():get("Content-Security-Policy") then if not hasFrameAncestors(response_handle) then csp = response_handle:headers():get("Content-Security-Policy"); csp = csp .. ";frame-ancestors none;"; response_handle:headers():replace("Content-Security-Policy", csp); end end if not response_handle:headers():get("X-Frame-Options") then response_handle:headers():add("X-Frame-Options", "deny"); end if not response_handle:headers():get("X-XSS-Protection") then response_handle:headers():add("X-XSS-Protection", "1; mode=block"); end if not response_handle:headers():get("X-Content-Type-Options") then response_handle:headers():add("X-Content-Type-Options", "nosniff"); end if not response_handle:headers():get("Referrer-Policy") then response_handle:headers():add("Referrer-Policy", "no-referrer"); end if not response_handle:headers():get("X-Download-Options") then response_handle:headers():add("X-Download-Options", "noopen"); end if not response_handle:headers():get("X-DNS-Prefetch-Control") then response_handle:headers():add("X-DNS-Prefetch-Control", "off"); end if not response_handle:headers():get("Feature-Policy") then response_handle:headers():add("Feature-Policy", "camera 'none';".. "microphone 'none';".. "geolocation 'none';".. "encrypted-media 'none';".. "payment 'none';".. "speaker 'none';".. "usb 'none';"); end if response_handle:headers():get("X-Powered-By") then response_handle:headers():remove("X-Powered-By"); end end EOF
proxyVersion
:配置為您當前的Istio版本。EnvoyFilter創建時需要設置proxyVersion來指定期望作用的Istio版本范圍,EnvoyFilter配置中的一些字段存在Istio版本不兼容的可能性。不同Istio版本的EnvoyFilter內容不同:如果您使用的Istio1.8及以下版本,請根據版本替換
proxyVersion
字段,并且替換上述EnvoyFilter中的envoy.filters.network.http_connection_manager
為envoy.http_connection_manager
、envoy.filters.http.router
為envoy.router
、type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
為type.googleapis.com/envoy.config.filter.http.lua.v2.Lua
。如果您使用的Istio1.9及以上版本,請根據版本替換
proxyVersion
字段。
執行以下命令,驗證安全HTTP響應頭是否添加成功。
請將
{入口網關的IP地址}
替換為實際的網關地址。關于如何獲取入口網關地址,請參見獲取入口網關地址。curl -I http://{入口網關的IP地址}/productpage
預期輸出:
HTTP/1.1 200 OK content-type: text/html; charset=utf-8 content-length: 4183 server: istio-envoy date: Tue, 28 Jan 2020 09:07:01 GMT x-envoy-upstream-service-time: 17 content-security-policy: frame-ancestors none; x-frame-options: deny x-xss-protection: 1; mode=block x-content-type-options: nosniff referrer-policy: no-referrer x-download-options: noopen x-dns-prefetch-control: off feature-policy: camera 'none';microphone 'none';geolocation 'none';encrypted-media 'none';payment 'none';speaker 'none';usb 'none';
由預期輸出得到,示例應用程序的入口首頁響應已經包含了HTTP響應頭的基準配置中所描述的安全相關的HTTP響應頭。
FAQ
為什么無法訪問帶有特殊字符的URL?
以特殊字符á為例,特殊字符á的編碼格式采用Unicode,而不是ASCII。因此請求的URL中帶有特殊字符,不符合URL規范。更多信息,請參見Envoy cannot parse non-alphanumeric characters if not urlencoded。