Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore binary body in plugins #711

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}