Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 259 additions & 0 deletions backend/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
package gobridge

import (
"archive/zip"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

cryptozip "github.com/yeka/zip"
)

const minimalCertPEM = "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----\n"
const minimalKeyPEM = "-----BEGIN PRIVATE KEY-----\nMIIB\n-----END PRIVATE KEY-----\n"
const minimalConfigXML = `<configuration version="37"></configuration>`

func writeBackupZip(t *testing.T, dst string, entries map[string][]byte, fakeEncryptionBit bool) {
t.Helper()
f, err := os.Create(dst)
if err != nil {
t.Fatalf("create %s: %v", dst, err)
}
defer f.Close()
w := zip.NewWriter(f)
for name, data := range entries {
hdr := &zip.FileHeader{Name: name, Method: zip.Deflate}
if fakeEncryptionBit {
hdr.Flags |= 0x1
}
hdr.SetMode(0o600)
writer, err := w.CreateHeader(hdr)
if err != nil {
t.Fatalf("create header %s: %v", name, err)
}
if _, err := writer.Write(data); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
if err := w.Close(); err != nil {
t.Fatalf("close zip: %v", err)
}
}

func writeEncryptedZip(t *testing.T, dst string, entries map[string][]byte, password string) {
t.Helper()
f, err := os.Create(dst)
if err != nil {
t.Fatalf("create %s: %v", dst, err)
}
defer f.Close()
w := cryptozip.NewWriter(f)
for name, data := range entries {
writer, err := w.Encrypt(name, password, cryptozip.AES256Encryption)
if err != nil {
t.Fatalf("encrypt header %s: %v", name, err)
}
if _, err := writer.Write(data); err != nil {
t.Fatalf("write %s: %v", name, err)
}
}
if err := w.Close(); err != nil {
t.Fatalf("close encrypted zip: %v", err)
}
}

func TestImportConfig_RoundTrip(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src")
dst := filepath.Join(dir, "dst")
if err := os.MkdirAll(src, 0o700); err != nil {
t.Fatal(err)
}
must := func(name, data string) {
if err := os.WriteFile(filepath.Join(src, name), []byte(data), 0o600); err != nil {
t.Fatal(err)
}
}
must(configFileName, minimalConfigXML)
must(certFileName, minimalCertPEM)
must(keyFileName, minimalKeyPEM)

prefsPath := filepath.Join(dir, prefsFileName)
if err := os.WriteFile(prefsPath, []byte(`{"wifi_only_sync":true}`), 0o600); err != nil {
t.Fatal(err)
}
asyncPath := filepath.Join(dir, asyncFileName)
if err := os.WriteFile(asyncPath, []byte(`{"@syncup/vaults":"[]"}`), 0o600); err != nil {
t.Fatal(err)
}
extras, _ := json.Marshal([]map[string]string{
{"name": prefsFileName, "path": prefsPath},
{"name": asyncFileName, "path": asyncPath},
})

zipPath := filepath.Join(dir, "backup.zip")
api := NewMobileAPI()
out := api.ExportConfig(src, zipPath, string(extras))
if strings.Contains(out, `"error"`) {
t.Fatalf("export failed: %s", out)
}

in := api.ImportConfig(zipPath, dst, "")
if strings.Contains(in, `"error"`) {
t.Fatalf("import failed: %s", in)
}
for _, name := range []string{configFileName, certFileName, keyFileName, prefsFileName, asyncFileName} {
if _, err := os.Stat(filepath.Join(dst, name)); err != nil {
t.Errorf("expected %s in dst: %v", name, err)
}
}
if !strings.Contains(in, `"importedPrefs":true`) {
t.Errorf("expected importedPrefs:true in %s", in)
}
if !strings.Contains(in, `"importedAsync":true`) {
t.Errorf("expected importedAsync:true in %s", in)
}
}

func TestImportConfig_ForkBackup(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "dst")
zipPath := filepath.Join(dir, "fork-backup.zip")

entries := map[string][]byte{
"config.xml": []byte(minimalConfigXML),
"cert.pem": []byte(minimalCertPEM),
"key.pem": []byte(minimalKeyPEM),
"https-cert.pem": []byte(minimalCertPEM),
"https-key.pem": []byte(minimalKeyPEM),
"sharedpreferences.dat": []byte{0xAC, 0xED, 0x00, 0x05},
"index-v2/000001.log": []byte("ignored"),
"index-v2/CURRENT": []byte("ignored"),
"index-v2/sub/MANIFEST-1": []byte("ignored"),
}
writeBackupZip(t, zipPath, entries, false)

api := NewMobileAPI()
out := api.ImportConfig(zipPath, dst, "")
if strings.Contains(out, `"error"`) {
t.Fatalf("import failed: %s", out)
}
for _, name := range []string{configFileName, certFileName, keyFileName, httpsCertFileName, httpsKeyFileName} {
if _, err := os.Stat(filepath.Join(dst, name)); err != nil {
t.Errorf("expected %s after fork import: %v", name, err)
}
}
if _, err := os.Stat(filepath.Join(dst, "index-v2")); err == nil {
t.Error("index-v2/ should not be extracted")
}
if _, err := os.Stat(filepath.Join(dst, "sharedpreferences.dat")); err == nil {
t.Error("sharedpreferences.dat should not be extracted")
}
if !strings.Contains(out, `"importedPrefs":false`) {
t.Errorf("expected importedPrefs:false in %s", out)
}
}

func TestImportConfig_EncryptedRoundTrip(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "dst")
zipPath := filepath.Join(dir, "encrypted.zip")
writeEncryptedZip(t, zipPath, map[string][]byte{
"config.xml": []byte(minimalConfigXML),
"cert.pem": []byte(minimalCertPEM),
"key.pem": []byte(minimalKeyPEM),
"https-cert.pem": []byte(minimalCertPEM),
"index-v2/log.0": []byte("ignored"),
}, "topsecret")

api := NewMobileAPI()

out := api.ImportConfig(zipPath, dst, "")
if !strings.Contains(out, "password required") {
t.Errorf("expected password-required error, got %s", out)
}

out = api.ImportConfig(zipPath, dst, "wrong")
if !strings.Contains(out, "wrong password") && !strings.Contains(out, "password") {
t.Errorf("expected wrong-password error, got %s", out)
}

out = api.ImportConfig(zipPath, dst, "topsecret")
if strings.Contains(out, `"error"`) {
t.Fatalf("expected success with correct password, got %s", out)
}
for _, name := range []string{configFileName, certFileName, keyFileName, httpsCertFileName} {
if _, err := os.Stat(filepath.Join(dst, name)); err != nil {
t.Errorf("expected %s after encrypted import: %v", name, err)
}
}
}

func TestImportConfig_RejectsZipSlip(t *testing.T) {
dir := t.TempDir()
zipPath := filepath.Join(dir, "evil.zip")
writeBackupZip(t, zipPath, map[string][]byte{
"config.xml": []byte(minimalConfigXML),
"cert.pem": []byte(minimalCertPEM),
"key.pem": []byte(minimalKeyPEM),
"../../../etc/passwd": []byte("nope"),
}, false)

api := NewMobileAPI()
out := api.ImportConfig(zipPath, filepath.Join(dir, "dst"), "")
if !strings.Contains(out, "unsafe zip entry") {
t.Errorf("expected zip-slip rejection, got %s", out)
}
}

func TestImportConfig_MissingRequired(t *testing.T) {
dir := t.TempDir()
zipPath := filepath.Join(dir, "incomplete.zip")
writeBackupZip(t, zipPath, map[string][]byte{
"config.xml": []byte(minimalConfigXML),
"cert.pem": []byte(minimalCertPEM),
}, false)

api := NewMobileAPI()
out := api.ImportConfig(zipPath, filepath.Join(dir, "dst"), "")
if !strings.Contains(out, "archive missing") {
t.Errorf("expected missing-file error, got %s", out)
}
}

func TestImportConfig_RollbackOnInvalidConfig(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "dst")
if err := os.MkdirAll(dst, 0o700); err != nil {
t.Fatal(err)
}
for _, n := range backupFiles {
if err := os.WriteFile(filepath.Join(dst, n), []byte("ORIGINAL-"+n), 0o600); err != nil {
t.Fatal(err)
}
}

zipPath := filepath.Join(dir, "bad-xml.zip")
writeBackupZip(t, zipPath, map[string][]byte{
"config.xml": []byte("not xml at all"),
"cert.pem": []byte(minimalCertPEM),
"key.pem": []byte(minimalKeyPEM),
}, false)

api := NewMobileAPI()
out := api.ImportConfig(zipPath, dst, "")
if !strings.Contains(out, "error") {
t.Fatalf("expected validation error, got %s", out)
}
for _, n := range backupFiles {
data, err := os.ReadFile(filepath.Join(dst, n))
if err != nil {
t.Fatalf("original %s missing after rollback: %v", n, err)
}
if string(data) != "ORIGINAL-"+n {
t.Errorf("rollback lost original %s: got %q", n, data)
}
}
}
15 changes: 8 additions & 7 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ require (
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0 // indirect
github.com/wlynxg/anet v0.0.5 // indirect
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
golang.org/x/crypto v0.50.0 // indirect
golang.org/x/crypto v0.51.0 // indirect
golang.org/x/exp v0.0.0-20260209203927-2842357ff358 // indirect
golang.org/x/mobile v0.0.0-20260410095206-2cfb76559b7b // indirect
golang.org/x/mod v0.35.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/mobile v0.0.0-20260508232728-bebd421c7fa8 // indirect
golang.org/x/mod v0.36.0 // indirect
golang.org/x/net v0.54.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
golang.org/x/sys v0.44.0 // indirect
golang.org/x/text v0.37.0 // indirect
golang.org/x/time v0.14.0 // indirect
golang.org/x/tools v0.44.0 // indirect
golang.org/x/tools v0.45.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.67.6 // indirect
Expand Down
30 changes: 16 additions & 14 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0 h1:okhMind4q9H1OxF44gN
github.com/vitrun/qart v0.0.0-20160531060029-bf64b92db6b0/go.mod h1:TTbGUfE+cXXceWtbTHq6lqcTvYPBKLNejBEbnUsQJtU=
github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9 h1:K8gF0eekWPEX+57l30ixxzGhHH/qscI3JCnuhbN6V4M=
github.com/yeka/zip v0.0.0-20231116150916-03d6312748a9/go.mod h1:9BnoKCcgJ/+SLhfAXj15352hTOuVmG5Gzo8xNRINfqI=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
Expand All @@ -192,15 +194,15 @@ go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
golang.org/x/exp v0.0.0-20260209203927-2842357ff358 h1:kpfSV7uLwKJbFSEgNhWzGSL47NDSF/5pYYQw1V0ub6c=
golang.org/x/exp v0.0.0-20260209203927-2842357ff358/go.mod h1:R3t0oliuryB5eenPWl3rrQxwnNM3WTwnsRZZiXLAAW8=
golang.org/x/mobile v0.0.0-20260410095206-2cfb76559b7b h1:Qt2eaXcZ8x20iAcoZ6AceeMMtnjuPHvC51KRCH1DKSQ=
golang.org/x/mobile v0.0.0-20260410095206-2cfb76559b7b/go.mod h1:5Fu78lew5ucMXt8w2KYcwvxu2rkC/liHzUvaoiI+H/M=
golang.org/x/mobile v0.0.0-20260508232728-bebd421c7fa8 h1:Rdmo5lL58MyzJamU436yZClTmhPhjCE+qrBk4LXicLc=
golang.org/x/mobile v0.0.0-20260508232728-bebd421c7fa8/go.mod h1:ltIbhcRzKgwHa4ZxKJeiv0nyzcXUUYCqMyO0Y+vPmXw=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
golang.org/x/mod v0.36.0 h1:JJjpVx6myfUsUdAzZuOSTTmRE0PfZeNWzzvKrP7amb4=
golang.org/x/mod v0.36.0/go.mod h1:moc6ELqsWcOw5Ef3xVprK5ul/MvtVvkIXLziUOICjUQ=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -209,8 +211,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -237,23 +239,23 @@ golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
golang.org/x/tools v0.45.0 h1:18qN3FAooORvApf5XjCXgsuayZOEtXf6JK18I3+ONa8=
golang.org/x/tools v0.45.0/go.mod h1:LuUGqqaXcXMEFEruIVJVm5mgDD8vww/z/SR1gQ4uE/0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Loading
Loading