fix(wasm-go): transformer performs an add op when the replace key does not exist (#2706)

This commit is contained in:
WeixinX
2025-08-04 20:38:29 +08:00
committed by GitHub
parent 5f65b4f5b0
commit abc31169a2
6 changed files with 535 additions and 45 deletions

View File

@@ -1 +1 @@
1.0.0
1.0.1-alpha

View File

@@ -0,0 +1,26 @@
version: '3.7'
services:
envoy:
image: higress-registry.cn-hangzhou.cr.aliyuncs.com/higress/gateway:v2.0.7
entrypoint: /usr/local/bin/envoy
# 注意这里对wasm开启了debug级别日志正式部署时则默认info级别
command: -c /etc/envoy/envoy.yaml --component-log-level wasm:debug
#depends_on:
# - httpbin
networks:
- wasmtest
ports:
- "10000:10000"
volumes:
- ./envoy.yaml:/etc/envoy/envoy.yaml
- ./plugin.wasm:/etc/envoy/main.wasm
httpbin:
image: kong/httpbin:latest
networks:
- wasmtest
ports:
- "12345:80"
networks:
wasmtest: {}

View File

@@ -0,0 +1,92 @@
admin:
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 9901
static_resources:
listeners:
- name: listener_0
address:
socket_address:
protocol: TCP
address: 0.0.0.0
port_value: 10000
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
scheme_header_transformation:
scheme_to_overwrite: https
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match:
prefix: "/"
route:
cluster: httpbin
http_filters:
- name: wasmdemo
typed_config:
"@type": type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
name: wasmdemo
vm_config:
runtime: envoy.wasm.runtime.v8
code:
local:
filename: /etc/envoy/main.wasm
configuration:
"@type": "type.googleapis.com/google.protobuf.StringValue"
value: |
{
"reqRules": [
{
"operate": "replace",
"headers": [
{
"key": "hello",
"newValue": "higress"
}
],
"querys": [
{
"key": "k1",
"newValue": "newQueryV1"
}
],
"body": [
{
"key": "k2",
"newValue": "newBodyV2"
}
]
}
]
}
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: httpbin
connect_timeout: 30s
type: LOGICAL_DNS
# Comment out the following line to test on v6 networks
dns_lookup_family: V4_ONLY
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: httpbin
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: httpbin
port_value: 80

View File

@@ -915,16 +915,13 @@ func (h kvHandler) handle(host, path string, kvs map[string][]string, mapSourceD
}
}
case ReplaceK:
// replace: 若指定 key 不存在,则无操作;否则替换 value 为 newValue
// replace: 若指定 key 不存在,相当于添加操作;否则替换 value 为 newValue
for _, replace := range kvtOp.replaceKvtGroup {
key, newValue := replace.key, replace.newValue
if _, ok := kvs[key]; !ok {
continue
}
if replace.reg != nil {
newValue = replace.reg.matchAndReplace(newValue, host, path)
}
kvs[replace.key] = []string{newValue}
kvs[key] = []string{newValue}
}
case AddK:
// add: 若指定 key 存在则无操作;否则添加 key:value
@@ -1047,12 +1044,9 @@ func (h jsonHandler) handle(host, path string, oriData []byte, mapSourceData map
}
}
case ReplaceK:
// replace: 若指定 key 不存在,则操作;否则替换 value 为 newValue
// replace: 若指定 key 不存在,则相当于添加操作;否则替换 value 为 newValue
for _, replace := range kvtOp.replaceKvtGroup {
key, newValue, valueType := replace.key, replace.newValue, replace.typ
if !gjson.GetBytes(data, key).Exists() {
continue
}
if valueType == "string" && replace.reg != nil {
newValue = replace.reg.matchAndReplace(newValue, host, path)
}