Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Preserve canonical Unicode and duplicate-key encoding in raw patches

pulse-triage[bot]cb61f75419d9
diff --git a/internal/websocket/raw_merge_patch_test.go b/internal/websocket/raw_merge_patch_test.go
index 39a6f5931e..2a6e3bb9bc 100644
--- a/internal/websocket/raw_merge_patch_test.go
+++ b/internal/websocket/raw_merge_patch_test.go
@@ -1,6 +1,7 @@
package websocket
import (
+ "bytes"
"encoding/json"
"fmt"
"math/rand"
@@ -15,13 +16,13 @@ func assertRawMergeMatchesReference(t testing.TB, previous, current []byte) {
if (wantErr == nil) != (gotErr == nil) {
t.Fatalf("error mismatch: before=%q after=%q reference=%v raw=%v", previous, current, wantErr, gotErr)
}
- if wantErr == nil && !reflect.DeepEqual(decodeExactDeltaJSON(t, want), decodeExactDeltaJSON(t, got)) {
+ if wantErr == nil && (!reflect.DeepEqual(decodeExactDeltaJSON(t, want), decodeExactDeltaJSON(t, got)) || !bytes.Equal(want, got)) {
t.Fatalf("patch mismatch: before=%s after=%s reference=%s raw=%s", previous, current, want, got)
}
}
func TestRawMergePatchSemanticEquivalence(t *testing.T) {
- cases := []string{`null`, `true`, `false`, `0`, `1.0`, `1e0`, `9007199254740993`, `18446744073709551615`, `"<&>"`, `"\u003c\u0026\u003e"`, `[]`, `[null,1.0,{"z":2,"a":1}]`, `[null,1.0,{"a":1,"z":2}]`, `{}`, `{"id":"r","nested":{"id":"child","n":9007199254740993}}`, `{"nested":{"n":9007199254740993,"id":"child"},"id":"r"}`, `{"id":"r","nested":{"id":"changed","n":9007199254740994}}`, `{"id":"old","id":"r","nested":{"a":1,"a":2}}`, `{"id":"r","nested":{"a":2}}`, `{"id":null,"optional":null}`, `{"id":"r","optional":[]}`, `{"id":"r","optional":{}}`}
+ cases := []string{`{"id":"\ud800","addition":{"invalid":"\ud800","duplicate":1,"duplicate":2}}`, string([]byte{'{', '"', 'x', '"', ':', '"', 0xff, '"', '}'}), `null`, `true`, `false`, `0`, `1.0`, `1e0`, `9007199254740993`, `18446744073709551615`, `"<&>"`, `"\u003c\u0026\u003e"`, `[]`, `[null,1.0,{"z":2,"a":1}]`, `[null,1.0,{"a":1,"z":2}]`, `{}`, `{"id":"r","nested":{"id":"child","n":9007199254740993}}`, `{"nested":{"n":9007199254740993,"id":"child"},"id":"r"}`, `{"id":"r","nested":{"id":"changed","n":9007199254740994}}`, `{"id":"old","id":"r","nested":{"a":1,"a":2}}`, `{"id":"r","nested":{"a":2}}`, `{"id":null,"optional":null}`, `{"id":"r","optional":[]}`, `{"id":"r","optional":{}}`}
for i, a := range cases {
for j, b := range cases {
t.Run(fmt.Sprintf("%d-%d", i, j), func(t *testing.T) { assertRawMergeMatchesReference(t, []byte(a), []byte(b)) })
diff --git a/internal/websocket/state_delta.go b/internal/websocket/state_delta.go
index 38b667088f..f117e45186 100644
--- a/internal/websocket/state_delta.go
+++ b/internal/websocket/state_delta.go
@@ -372,7 +372,18 @@ func diffRawJSONMergeValue(previous, current json.RawMessage, anchorIdentity boo
for key, value := range after {
old, exists := before[key]
if !exists {
- patch[key] = value
+ // Preserve the old decoder/encoder semantics for additions too:
+ // duplicate keys and invalid Unicode escapes are normalised, not
+ // copied as a new browser-visible representation.
+ decoded, err := decodeJSONMergeValue(value)
+ if err != nil {
+ return nil, false, err
+ }
+ encoded, err := json.Marshal(decoded)
+ if err != nil {
+ return nil, false, err
+ }
+ patch[key] = encoded
continue
}
nested, changed, err := diffRawJSONMergeValue(old, value, false)
@@ -389,7 +400,15 @@ func diffRawJSONMergeValue(previous, current json.RawMessage, anchorIdentity boo
// Preserve the existing identity anchor without decoding current twice.
if anchorIdentity {
if id, ok := after["id"]; ok {
- patch["id"] = id
+ decoded, err := decodeJSONMergeValue(id)
+ if err != nil {
+ return nil, false, err
+ }
+ encoded, err := json.Marshal(decoded)
+ if err != nil {
+ return nil, false, err
+ }
+ patch["id"] = encoded
}
}
encoded, err := json.Marshal(patch)