阿里云服务网格ASM之扩展能力(1):在ASM中通过EnvoyFilter添加HTTP请求头

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 前面的系列文档中介绍了如何创建服务网格ASM实例,并介绍了如何将一个应用示例部署到 ASM 实例中,本文在此基础上介绍如何通过定义EnvoyFilter来添加HTTP请求头,以满足一些场景下的要求,例如使用安全请求头保护应用程序的安全等。

本系列文章讲讲述阿里云服务网格ASM的一些扩展能力:

欢迎扫码入群进一步交流:
image

背景信息

安全的HTTP请求头可以支持以非常简单的方式提高Web应用程序的安全性。OWASP提供了最佳实践指南和编程框架,描述了如何使用安全请求头保护应用程序的安全,包括了如下的基准设置:

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

通过curl命令可以看到Bookinfo示例应用程序的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请求头。

接下来,将会介绍如何在ASM中通过Istio EnvoyFilter添加安全的HTTP请求头。

定义EnvoyFilter

  • 如果还没有建立 kubectl 命令行客户端与 ASM 实例的连接,请参见通过 kubectl 连接 ASM 实例进行配置。
  • 部署Istio EnvoyFilter,执行如下命令:

  1. apply -f - <apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
    name: security-by-default-header-filter
    spec:
    filters:

    • listenerMatch:

      listenerType: GATEWAY

      filterType: HTTP

    filterName: envoy.lua
    filterConfig:

    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

  • 将看到以下输出显示过滤器已成功部署:
envoyfilter.networking.istio.io/security-by-default-header-filter created

验证HTTP请求头

  • 通过curl命令确认添加了安全HTTP请求头,执行如下:
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请求头。

由此可见,在ASM中可以使用EnvoyFilter以非常简单的方式添加HTTP请求头。

相关文章
|
2月前
|
数据采集 JSON 网络协议
请你详细介绍一下HTTP请求头的常用格式
【2月更文挑战第21天】【2月更文挑战第65篇】请你详细介绍一下HTTP请求头的常用格式
|
4月前
|
缓存
HTTP 请求头Cache-Control 详解
HTTP 请求头Cache-Control 详解
|
6月前
|
JavaScript 前端开发 Serverless
在阿里云函数计算FC部署好HTTP API后
在阿里云函数计算FC部署好HTTP API后
124 5
|
14天前
|
SQL DataWorks Java
DataWorks操作报错合集之在阿里云 DataWorks 中,代码在开发测试阶段能够成功运行,但在提交后失败并报错“不支持https”如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
26 1
DataWorks操作报错合集之在阿里云 DataWorks 中,代码在开发测试阶段能够成功运行,但在提交后失败并报错“不支持https”如何解决
|
14天前
使用阿里云语音通知http批量推送模式获取用户回执短信内容
本文使用阿里云语音通知配置http批量推送模式获取用户回执信息,并进行测试
38 0
|
1月前
|
域名解析 网络协议 应用服务中间件
阿里云SSL证书配置(HTTPS证书配置)
该内容是一个关于如何在阿里云上准备和购买SSL证书,以及如何为网站启用HTTPS的步骤指南。首先,需要注册并实名认证阿里云账号,然后在SSL证书控制台选择证书类型、品牌和时长进行购买。申请证书时填写域名信息,并进行DNS验证,这包括在阿里云域名管理板块添加解析记录。完成验证后提交审核,等待证书审核通过并下载Nginx格式的证书文件。最后,将证书配置到网站服务器以启用HTTPS。整个过程涉及账户注册、实名认证、证书购买、DNS设置和证书下载及安装。
154 0
|
2月前
|
数据采集 缓存 前端开发
http和https请求服务器的时候在请求头部分都带什么到服务器呢?
HTTP和HTTPS请求头基本结构相似,HTTPS多了一层SSL/TLS加密。常见请求头如Accept(指定内容类型)、Authorization(身份验证)、Cookie(会话跟踪)、User-Agent(标识用户代理)等。HTTPS特有的头包括Upgrade-Insecure-Requests(升级到HTTPS)、Strict-Transport-Security(强制使用HTTPS)、Sec-Fetch-*(安全策略)和X-Content-Type-Options、X-Frame-Options等(增强安全性)。实际应用中,请求头会根据需求和安全策略变化。
24 0
|
2月前
|
安全 网络安全 CDN
阿里云CDN HTTPS 证书配置流程
阿里云CDN HTTPS 证书配置流程
216 1
|
2月前
|
数据采集 API
请解释什么是 HTTP 请求头,以及在爬虫中为什么要设置请求头?
请解释什么是 HTTP 请求头,以及在爬虫中为什么要设置请求头?
25 2
|
2月前
阿里云十分钟初审https://www.fcomcn.com
https://www.fcomcn.com 但是到现在十天了
33 0