Skip to content

Commit 6d4d059

Browse files
committed
fix: Disable privacy by default when syncing with older status-go versions
Fixing status-im/status-desktop#19037 In the profile pairing flow, when the sender is missing the `ThirdpartyServicesEnabled` field we'll assume the privacy feature isn't implemented by the sender and it's basically disabled. As a result we'll enable `ThirdpartyServicesEnabled` and keep the default field value inline with the DB default.
1 parent b7b5077 commit 6d4d059

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

multiaccounts/settings/structs.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ type Settings struct {
225225
PeerSyncingEnabled bool `json:"peer-syncing-enabled?,omitempty"`
226226
AutoRefreshTokensEnabled bool `json:"auto-refresh-tokens-enabled,omitempty"`
227227
LastTokensUpdate time.Time `json:"last-tokens-update,omitempty"`
228-
ThirdpartyServicesEnabled bool `json:"thirdparty_services_enabled,omitempty"`
228+
ThirdpartyServicesEnabled bool `json:"thirdparty_services_enabled"`
229229
}
230230

231231
func (s Settings) MarshalJSON() ([]byte, error) {
@@ -251,3 +251,32 @@ func (s Settings) GetFleet() string {
251251
}
252252
return *s.Fleet
253253
}
254+
255+
// UnmarshalJSON implements custom unmarshaling for Settings so that when the
256+
// thirdparty_services_enabled field is missing from incoming JSON it defaults
257+
// to true instead of the default value (false).
258+
func (s *Settings) UnmarshalJSON(data []byte) error {
259+
// Use an alias to avoid infinite recursion
260+
type Alias Settings
261+
262+
// Temporary struct to check if ThirdpartyServicesEnabled is present
263+
aux := &struct {
264+
*Alias
265+
ThirdpartyServicesEnabled *bool `json:"thirdparty_services_enabled"`
266+
}{
267+
Alias: (*Alias)(s),
268+
}
269+
270+
if err := json.Unmarshal(data, &aux); err != nil {
271+
return err
272+
}
273+
274+
// If the field was omitted, ensure it defaults to true.
275+
if aux.ThirdpartyServicesEnabled == nil {
276+
s.ThirdpartyServicesEnabled = true
277+
} else {
278+
s.ThirdpartyServicesEnabled = *aux.ThirdpartyServicesEnabled
279+
}
280+
281+
return nil
282+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package settings
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
func TestUnmarshal_ThirdpartyServicesEnabled_DefaultsToTrueWhenMissing(t *testing.T) {
9+
var s Settings
10+
data := []byte(`{}`)
11+
12+
if err := json.Unmarshal(data, &s); err != nil {
13+
t.Fatalf("unmarshal failed: %v", err)
14+
}
15+
16+
if !s.ThirdpartyServicesEnabled {
17+
t.Fatalf("expected ThirdpartyServicesEnabled to be true when omitted, got false")
18+
}
19+
}
20+
21+
func TestUnmarshal_ThirdpartyServicesEnabled_RespectsTrue(t *testing.T) {
22+
var s Settings
23+
data := []byte(`{"thirdparty_services_enabled": true}`)
24+
25+
if err := json.Unmarshal(data, &s); err != nil {
26+
t.Fatalf("unmarshal failed: %v", err)
27+
}
28+
29+
if !s.ThirdpartyServicesEnabled {
30+
t.Fatalf("expected ThirdpartyServicesEnabled to be true when explicitly set to true, got false")
31+
}
32+
}
33+
34+
func TestUnmarshal_ThirdpartyServicesEnabled_RespectsFalse(t *testing.T) {
35+
var s Settings
36+
data := []byte(`{"thirdparty_services_enabled": false}`)
37+
38+
if err := json.Unmarshal(data, &s); err != nil {
39+
t.Fatalf("unmarshal failed: %v", err)
40+
}
41+
42+
if s.ThirdpartyServicesEnabled {
43+
t.Fatalf("expected ThirdpartyServicesEnabled to be false when explicitly set to false, got true")
44+
}
45+
}
46+
47+
func TestUnmarshal_ThirdpartyServicesEnabled_NullTreatedAsMissing_DefaultsToTrue(t *testing.T) {
48+
var s Settings
49+
data := []byte(`{"thirdparty_services_enabled": null}`)
50+
51+
if err := json.Unmarshal(data, &s); err != nil {
52+
t.Fatalf("unmarshal failed: %v", err)
53+
}
54+
55+
if !s.ThirdpartyServicesEnabled {
56+
t.Fatalf("expected ThirdpartyServicesEnabled to be true when null (treated as missing), got false")
57+
}
58+
}
59+
60+
func TestMarshal_ThirdpartyServicesEnabled_IncludedWhenTrue(t *testing.T) {
61+
s := Settings{ThirdpartyServicesEnabled: true}
62+
63+
b, err := json.Marshal(&s)
64+
if err != nil {
65+
t.Fatalf("marshal failed: %v", err)
66+
}
67+
68+
var m map[string]interface{}
69+
if err := json.Unmarshal(b, &m); err != nil {
70+
t.Fatalf("unmarshal of marshaled bytes failed: %v", err)
71+
}
72+
73+
// When true we expect the key to be present and true
74+
v, ok := m["thirdparty_services_enabled"]
75+
if !ok {
76+
t.Fatalf("expected thirdparty_services_enabled key to be present when true")
77+
}
78+
if v != true {
79+
t.Fatalf("expected thirdparty_services_enabled to be true in JSON, got %v", v)
80+
}
81+
}
82+
83+
func TestMarshal_ThirdpartyServicesEnabled_IncludedWhenFalse(t *testing.T) {
84+
s := Settings{ThirdpartyServicesEnabled: false}
85+
86+
b, err := json.Marshal(&s)
87+
if err != nil {
88+
t.Fatalf("marshal failed: %v", err)
89+
}
90+
91+
var m map[string]interface{}
92+
if err := json.Unmarshal(b, &m); err != nil {
93+
t.Fatalf("unmarshal of marshaled bytes failed: %v", err)
94+
}
95+
96+
// The field tag includes `omitempty`, so when false the key is omitted.
97+
if _, ok := m["thirdparty_services_enabled"]; ok {
98+
// If present, verify it's false
99+
if m["thirdparty_services_enabled"] != false {
100+
t.Fatalf("expected thirdparty_services_enabled to be false in JSON when present, got %v", m["thirdparty_services_enabled"])
101+
}
102+
} else {
103+
// Key is omitted, which is not acceptable for false
104+
t.Fatalf("expected thirdparty_services_enabled key to be present when false")
105+
}
106+
}

0 commit comments

Comments
 (0)