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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: 前面的系列文档中介绍了如何创建服务网格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月前
|
安全 API 持续交付
阿里云云效产品使用问题之如何从流水线访问内网平台的HTTP接口
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
2月前
|
Cloud Native 容器 Kubernetes
基于阿里云服务网格流量泳道的全链路流量管理(三):无侵入式的宽松模式泳道
本文简要讨论了使用流量泳道来实现全链路流量灰度管理的场景与方案,并回顾了阿里云服务网格 ASM 提供的严格与宽松两种模式的流量泳道、以及这两种模式各自的优势与挑战。接下来介绍了一种基于 OpenTelemetry 社区提出的 baggage 透传能力实现的无侵入式的宽松模式泳道,这种类型的流量泳道同时具有对业务代码侵入性低、同时保持宽松模式的灵活特性的特点。同时,我们还介绍了新的基于权重的流量引流策略,这种策略可以基于统一的流量匹配规则,将匹配到的流量以设定好的比例分发到不同的流量泳道。
73490 16
基于阿里云服务网格流量泳道的全链路流量管理(三):无侵入式的宽松模式泳道
|
1月前
|
人工智能 自然语言处理 安全
使用阿里云服务网格高效管理LLM流量:(一)流量路由
ASM支持通过LLMProvider和LLMRoute资源管理大型语言模型流量。LLMProvider负责注册LLM服务,LLMRoute负责设定流量规则,应用可灵活切换模型,满足不同场景需求。
|
2月前
|
负载均衡 测试技术 网络安全
阿里云服务网格ASM多集群实践(一)多集群管理概述
服务网格多集群管理网络打通和部署模式的多种最佳实践
|
1月前
|
Cloud Native 测试技术 开发者
阿里云服务网格ASM多集群实践(二):高效按需的应用多环境部署与全链路灰度发布
介绍服务网格ASM提出的一种多集群部署下的多环境部署与全链路灰度发布解决方案。
|
2月前
|
人工智能 安全 Go
使用阿里云服务网格 ASM LLMProxy 插件保障大模型用户数据安全
本文介绍如何使用ASM LLMProxy动态为LLM请求添加API_KEY、使用模式匹配以及私有大模型判别请求敏感信息并根据判别结果拒绝请求等功能,帮助用户提升LLM场景下的安全水位。
|
2月前
|
负载均衡 Kubernetes 算法
服务网格 ASM 负载均衡算法全面解析
在本文中,笔者将解析服务网格的多种负载均衡算法的实现原理和使用场景,为服务网格负载均衡算法的选择提供参考。
|
2月前
|
安全 搜索推荐
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
基础入门 HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断
|
2月前
|
API Python
使用Python获取HTTP请求头数据
在Python Web开发中,`requests`库用于发送HTTP请求,请求头是关键元素,包含客户端信息和请求详情。要查看请求头,先创建`Request`对象,打印其`headers`属性,然后使用`get`等方法发送请求并获取响应头。别忘了处理不同HTTP方法、内容类型以及异常。使用`Session`管理会话状态,并考虑日志记录以调试。通过控制请求头,能有效与服务器通信。
50 0
|
3月前
使用阿里云语音通知http批量推送模式获取用户回执短信内容
本文使用阿里云语音通知配置http批量推送模式获取用户回执信息,并进行测试
330 0