阿里云服务网格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月前
|
存储 机器学习/深度学习 人工智能
阿里云视觉智能开放平台确实拥有视频目标检测的能力
【2月更文挑战第9天】阿里云视觉智能开放平台确实拥有视频目标检测的能力
96 7
|
5月前
|
JavaScript 前端开发 Serverless
在阿里云函数计算FC部署好HTTP API后
在阿里云函数计算FC部署好HTTP API后
121 5
|
3月前
|
人工智能 监控 算法
阿里云加强公共云能力服务香港客户,建立新合作支持各行各业加快数字转型
阿里云加强公共云能力服务香港客户,建立新合作支持各行各业加快数字转型
91 1
|
4月前
电子好书发您分享《阿里云云原生一体化数仓新能力解读》
电子好书发您分享《阿里云云原生一体化数仓新能力解读》
262 2
|
3月前
|
弹性计算 监控 NoSQL
扩展至千万级用户:阿里云平台的初学者指南
【1月更文挑战第7天】这几天支原体感染了,每天半夜都咳醒,实在撑不住了才断更几天。
|
3月前
|
存储 人工智能 Cloud Native
阿里云向量检索服务测评:高效、易用、强大的向量管理与查询能力
摘要:本文对阿里云向量检索服务进行了全面的测评,从其基于自研Proxima内核的高性能、全托管、云原生特性,到简洁易用的SDK/API接口,再到在多种应用场景下的集成能力,为读者提供了一个全面了解阿里云向量检索服务的机会。
1112 0
|
3月前
|
人工智能
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景,如语义检索、知识库搭建、AI多模态搜索等
266 0
|
3月前
|
关系型数据库 MySQL Serverless
评测报告:阿里云 PolarDB MySQL 版 Serverless 能力
本次评测全面评估了阿里云 PolarDB MySQL 版的 Serverless 能力,包括资源弹升速度、资源伸缩广度、稳定性、颗粒度、自动启停以及全局数据强一致性。结果显示,PolarDB 在这些维度上均表现出色,能够快速响应负载变化、灵活调整资源配置、保证业务连续性和稳定性,同时精细控制成本。与同类型产品相比,PolarDB 在多方面具有优势。我们建议阿里云继续优化资源调度算法、拓展边缘场景支持,并加强与生态伙伴的合作,以进一步提升产品竞争力和用户满意度。
|
4月前
|
Python
HTTP请求头
这是一段描述如何处理HTTP请求头的Python代码。具体来说,这段代码的功能包括:
43 3
|
4月前
|
人工智能 文字识别
阿里云的 口算判题 AI能力(文字识别OCR)接口 返回数据的时候 没有正确答案只有 对错吗?
阿里云的 口算判题 AI能力(文字识别OCR)接口 返回数据的时候 没有正确答案只有 对错吗?
172 0