阿里云服务网格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请求头。

相关文章
|
5月前
|
JavaScript 前端开发 Serverless
在阿里云函数计算FC部署好HTTP API后
在阿里云函数计算FC部署好HTTP API后
121 5
|
3月前
|
弹性计算 监控 NoSQL
扩展至千万级用户:阿里云平台的初学者指南
【1月更文挑战第7天】这几天支原体感染了,每天半夜都咳醒,实在撑不住了才断更几天。
|
3月前
|
人工智能
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景,如语义检索、知识库搭建、AI多模态搜索等
263 0
|
4月前
|
Python
HTTP请求头
这是一段描述如何处理HTTP请求头的Python代码。具体来说,这段代码的功能包括:
43 3
|
6月前
|
存储 缓存 网络协议
场景应用:常见的HTTP协议请求头有哪些?
场景应用:常见的HTTP协议请求头有哪些?
|
7月前
|
前端开发 Java
java在过滤器中为http请求加请求头header
现在有一个需求场景是,每一个请求我都需要在请求头里面加上token这个请求头,作为一种校验机制,传统的接口可以通过设置一个全局的变量,然后通过页面携带过来(大概就是先将我们的token放在session中,写一个服务用来获取session中的token,然后主页面用ajax调用接口,将token放在隐藏域中,然后将请求头放进来,用ajax方法,这里不想洗说了),但是有一种情况是通过页面传递的并不一定都会适用所有接口,比如上传和下载的接口有时候头里面就没有token参数,可能是上传和下载是用表单提交的 这个时候如何将请求头通过后台的方法加进来? 想到用过滤器,用后台方法强制加入请求头。
91 0