From a140f780d299de66f2d30840b184d671e56f25e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=84=E6=BD=AD?= Date: Tue, 19 Dec 2023 16:47:15 +0800 Subject: [PATCH] ignore binary body in plugins (#711) --- plugins/wasm-go/pkg/wrapper/plugin_wrapper.go | 8 +++++ .../wasm-go/pkg/wrapper/request_wrapper.go | 32 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go index 124d0afd8..3b761362f 100644 --- a/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go +++ b/plugins/wasm-go/pkg/wrapper/plugin_wrapper.go @@ -263,6 +263,10 @@ func (ctx *CommonHttpCtx[PluginConfig]) OnHttpRequestHeaders(numHeaders int, end return types.ActionContinue } ctx.config = config + // To avoid unexpected operations, plugins do not read the binary content body + if IsBinaryRequestBody() { + ctx.needRequestBody = false + } if ctx.plugin.vm.onHttpRequestHeaders == nil { return types.ActionContinue } @@ -295,6 +299,10 @@ func (ctx *CommonHttpCtx[PluginConfig]) OnHttpResponseHeaders(numHeaders int, en if ctx.config == nil { return types.ActionContinue } + // To avoid unexpected operations, plugins do not read the binary content body + if IsBinaryResponseBody() { + ctx.needResponseBody = false + } if ctx.plugin.vm.onHttpResponseHeaders == nil { return types.ActionContinue } diff --git a/plugins/wasm-go/pkg/wrapper/request_wrapper.go b/plugins/wasm-go/pkg/wrapper/request_wrapper.go index c71693414..7dc36ddd4 100644 --- a/plugins/wasm-go/pkg/wrapper/request_wrapper.go +++ b/plugins/wasm-go/pkg/wrapper/request_wrapper.go @@ -14,7 +14,11 @@ package wrapper -import "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" +import ( + "strings" + + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" +) func GetRequestScheme() string { scheme, err := proxywasm.GetHttpRequestHeader(":scheme") @@ -51,3 +55,29 @@ func GetRequestMethod() string { } return method } + +func IsBinaryRequestBody() bool { + contentType, _ := proxywasm.GetHttpRequestHeader("content-type") + if strings.Contains(contentType, "octet-stream") || + strings.Contains(contentType, "grpc") { + return true + } + encoding, _ := proxywasm.GetHttpRequestHeader("content-encoding") + if encoding != "" { + return true + } + return false +} + +func IsBinaryResponseBody() bool { + contentType, _ := proxywasm.GetHttpResponseHeader("content-type") + if strings.Contains(contentType, "octet-stream") || + strings.Contains(contentType, "grpc") { + return true + } + encoding, _ := proxywasm.GetHttpResponseHeader("content-encoding") + if encoding != "" { + return true + } + return false +}