Skip to content

Commit

Permalink
ignore binary body in plugins (alibaba#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlanni authored Dec 19, 2023
1 parent 2548815 commit a140f78
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions plugins/wasm-go/pkg/wrapper/plugin_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
32 changes: 31 additions & 1 deletion plugins/wasm-go/pkg/wrapper/request_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit a140f78

Please sign in to comment.