Skip to content

Commit a33b906

Browse files
committed
fix(wasm): deep-copy WasmBytes, use slog, pre-allocate, extract constants
- Proto serialization: deep-copy WasmBytes via append([]byte{}, ...) to prevent data plane from reading overwritten byte slices - Replace log.Printf with structured slog.Warn for inline/decode errors - Pre-allocate referencedConfigMapKeysForWasmPlugins with len(plugins) - Extract 'plugin.wasm' magic string as wasmConfigMapDefaultKey constant - Add 50MB LimitReader to URL download to prevent memory exhaustion - Remove unused 'log' import; add 'log/slog' import
1 parent 6fa0cce commit a33b906

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

internal/grpcserver/snapshot_proto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func toProtoWasmPlugin(item *ir.WasmPluginConfig) *controlv1.WasmPluginConfig {
226226
return &controlv1.WasmPluginConfig{
227227
Name: item.Name,
228228
Namespace: item.Namespace,
229-
WasmBytes: item.WasmBytes,
229+
WasmBytes: append([]byte{}, item.WasmBytes...),
230230
Sha256: item.SHA256,
231231
Hooks: item.Hooks,
232232
ConfigJson: item.ConfigJSON,

internal/translator/wasm_plugin.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/base64"
55
"fmt"
66
"io"
7-
"log"
7+
"log/slog"
88
"net/http"
99
"time"
1010

@@ -15,8 +15,14 @@ import (
1515
"github.com/nantian-gw/gateway/internal/ir"
1616
)
1717

18+
const (
19+
wasmDownloadTimeout = 30 * time.Second
20+
wasmConfigMapDefaultKey = "plugin.wasm"
21+
wasmConfigMapDefaultKeyLegacy = ""
22+
)
23+
1824
func referencedConfigMapKeysForWasmPlugins(plugins []wasmpluginv1alpha1.WasmPlugin) []client.ObjectKey {
19-
var keys []client.ObjectKey
25+
keys := make([]client.ObjectKey, 0, len(plugins))
2026
for _, p := range plugins {
2127
if p.Spec.Wasm.ConfigMap != nil && p.Spec.Wasm.ConfigMap.Name != "" {
2228
keys = append(keys, client.ObjectKey{
@@ -31,8 +37,8 @@ func referencedConfigMapKeysForWasmPlugins(plugins []wasmpluginv1alpha1.WasmPlug
3137
func wasmConfigMapData(configMaps []corev1.ConfigMap, namespace, name, key string) []byte {
3238
for _, cm := range configMaps {
3339
if cm.Namespace == namespace && cm.Name == name {
34-
if key == "" {
35-
key = "plugin.wasm"
40+
if key == "" || key == wasmConfigMapDefaultKeyLegacy {
41+
key = wasmConfigMapDefaultKey
3642
}
3743
return cm.BinaryData[key]
3844
}
@@ -59,23 +65,32 @@ func translateWasmPlugin(p wasmpluginv1alpha1.WasmPlugin, configMaps []corev1.Co
5965
if p.Spec.Wasm.Inline != "" {
6066
decoded, err := base64.StdEncoding.DecodeString(p.Spec.Wasm.Inline)
6167
if err != nil {
62-
log.Printf("wasm plugin %s/%s: failed to decode inline wasm bytes: %v", p.Namespace, p.Name, err)
68+
slog.Warn("wasm plugin: failed to decode inline wasm bytes",
69+
"namespace", p.Namespace,
70+
"name", p.Name,
71+
"error", err,
72+
)
6373
} else {
6474
cfg.WasmBytes = decoded
6575
}
6676
}
6777
if p.Spec.Wasm.URL != "" {
6878
wasmBytes, err := downloadWasmURL(p.Spec.Wasm.URL)
6979
if err != nil {
70-
log.Printf("wasm plugin %s/%s: failed to download from %s: %v", p.Namespace, p.Name, p.Spec.Wasm.URL, err)
80+
slog.Warn("wasm plugin: failed to download wasm from URL",
81+
"namespace", p.Namespace,
82+
"name", p.Name,
83+
"url", p.Spec.Wasm.URL,
84+
"error", err,
85+
)
7186
} else {
7287
cfg.WasmBytes = wasmBytes
7388
}
7489
}
7590
if p.Spec.Wasm.ConfigMap != nil {
7691
key := p.Spec.Wasm.ConfigMap.Key
7792
if key == "" {
78-
key = "plugin.wasm"
93+
key = wasmConfigMapDefaultKey
7994
}
8095
cfg.WasmBytes = wasmConfigMapData(configMaps, p.Namespace, p.Spec.Wasm.ConfigMap.Name, key)
8196
}
@@ -91,8 +106,6 @@ func translateWasmPlugins(plugins []wasmpluginv1alpha1.WasmPlugin, configMaps []
91106
return result
92107
}
93108

94-
const wasmDownloadTimeout = 30 * time.Second
95-
96109
func downloadWasmURL(url string) ([]byte, error) {
97110
client := &http.Client{Timeout: wasmDownloadTimeout}
98111
resp, err := client.Get(url)
@@ -103,5 +116,5 @@ func downloadWasmURL(url string) ([]byte, error) {
103116
if resp.StatusCode != http.StatusOK {
104117
return nil, fmt.Errorf("http %d", resp.StatusCode)
105118
}
106-
return io.ReadAll(resp.Body)
107-
}
119+
return io.ReadAll(io.LimitReader(resp.Body, 50<<20))
120+
}

0 commit comments

Comments
 (0)