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

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器镜像服务 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请求头。

相关文章
|
12天前
|
消息中间件 API 数据库
在微服务架构中,每个服务通常都是一个独立运行、独立部署、独立扩展的组件,它们之间通过轻量级的通信机制(如HTTP/RESTful API、gRPC等)进行通信。
在微服务架构中,每个服务通常都是一个独立运行、独立部署、独立扩展的组件,它们之间通过轻量级的通信机制(如HTTP/RESTful API、gRPC等)进行通信。
|
14天前
|
Cloud Native 测试技术 开发者
阿里云服务网格ASM多集群实践(二):高效按需的应用多环境部署与全链路灰度发布
介绍服务网格ASM提出的一种多集群部署下的多环境部署与全链路灰度发布解决方案。
|
1月前
|
负载均衡 测试技术 网络安全
阿里云服务网格ASM多集群实践(一)多集群管理概述
服务网格多集群管理网络打通和部署模式的多种最佳实践
|
1月前
|
人工智能 安全 Go
使用阿里云服务网格 ASM LLMProxy 插件保障大模型用户数据安全
本文介绍如何使用ASM LLMProxy动态为LLM请求添加API_KEY、使用模式匹配以及私有大模型判别请求敏感信息并根据判别结果拒绝请求等功能,帮助用户提升LLM场景下的安全水位。
|
1月前
|
负载均衡 Kubernetes 算法
服务网格 ASM 负载均衡算法全面解析
在本文中,笔者将解析服务网格的多种负载均衡算法的实现原理和使用场景,为服务网格负载均衡算法的选择提供参考。
|
2月前
|
SQL DataWorks Java
DataWorks操作报错合集之在阿里云 DataWorks 中,代码在开发测试阶段能够成功运行,但在提交后失败并报错“不支持https”如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
DataWorks操作报错合集之在阿里云 DataWorks 中,代码在开发测试阶段能够成功运行,但在提交后失败并报错“不支持https”如何解决
|
1月前
|
网络协议 安全 应用服务中间件
阿里云 网站https设置 sll申请与nginx跳转配置
阿里云 网站https设置 sll申请与nginx跳转配置
57 0
|
2月前
|
域名解析 网络协议 应用服务中间件
阿里云SSL证书配置(HTTPS证书配置)
该内容是一个关于如何在阿里云上准备和购买SSL证书,以及如何为网站启用HTTPS的步骤指南。首先,需要注册并实名认证阿里云账号,然后在SSL证书控制台选择证书类型、品牌和时长进行购买。申请证书时填写域名信息,并进行DNS验证,这包括在阿里云域名管理板块添加解析记录。完成验证后提交审核,等待证书审核通过并下载Nginx格式的证书文件。最后,将证书配置到网站服务器以启用HTTPS。整个过程涉及账户注册、实名认证、证书购买、DNS设置和证书下载及安装。
697 0
|
2月前
阿里云提供https://www.fcomcn.com备案耐心指导。
到现在备案已经有八个网站了,现在是https://www.fcomcn.com 在三天前提交了备案,初审速度阿里云还是很快的,商标是人民井茶,可能是名字有带人民就不能通过,所以备案了外星人。在这里谢谢阿里云工作人员。
70 4
阿里云提供https://www.fcomcn.com备案耐心指导。
|
2月前
|
安全 网络安全 CDN
阿里云CDN HTTPS 证书配置流程
阿里云CDN HTTPS 证书配置流程
489 1