forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.test.ts
More file actions
461 lines (405 loc) · 15.8 KB
/
Copy pathbackup.test.ts
File metadata and controls
461 lines (405 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// src/lib/__tests__/backup.test.ts
import { beforeEach, describe, expect, it, vi } from "vitest"
// ---------------------------------------------------------------------------
// Mocks — must appear before any real module imports
// ---------------------------------------------------------------------------
vi.mock("@/lib/db", () => ({
db: {
select: vi.fn(),
insert: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
},
}))
vi.mock("@/lib/db/schema", () => ({
appSettings: {},
trackers: {},
trackerSnapshots: {},
trackerRoles: {},
downloadClients: {},
tagGroups: {},
tagGroupMembers: {},
clientSnapshots: {},
clientUptimeBuckets: {},
dismissedAlerts: {},
notificationTargets: {},
backupHistory: {},
}))
import {
CURRENT_BACKUP_VERSION,
decryptBackupPayload,
encryptBackupPayload,
generateBackupPayload,
validateBackupJson,
} from "@/lib/backup"
import { db } from "@/lib/db"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function validPayload() {
return {
manifest: {
version: CURRENT_BACKUP_VERSION,
appVersion: "0.1.0",
createdAt: "2026-03-09T08:00:00.000Z",
encrypted: false,
instanceUrl: null,
counts: {
trackers: 1,
trackerSnapshots: 1,
trackerRoles: 0,
downloadClients: 0,
tagGroups: 0,
tagGroupMembers: 0,
clientSnapshots: 0,
},
},
settings: {
encryptionSalt: "a".repeat(64),
storeUsernames: true,
backupScheduleEnabled: false,
backupScheduleFrequency: "daily",
backupRetentionCount: 14,
backupEncryptionEnabled: false,
proxyEnabled: false,
proxyType: "socks5",
qbitmanageEnabled: false,
},
trackers: [
{
id: 1,
name: "TestTracker",
baseUrl: "https://example.com",
apiPath: "/api/user",
platformType: "unit3d",
encryptedApiToken: "base64ciphertext",
isActive: true,
color: "#00d4ff",
qbtTag: null,
useProxy: false,
sortOrder: 0,
joinedAt: null,
userPausedAt: null,
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-03-09T00:00:00.000Z",
},
],
trackerSnapshots: [
{
id: 1,
trackerId: 1,
polledAt: "2026-03-09T08:00:00.000Z",
uploadedBytes: "5368709120",
downloadedBytes: "1073741824",
ratio: 5.0,
bufferBytes: "536870912",
seedingCount: 42,
leechingCount: 0,
seedbonus: 1234.56,
hitAndRuns: 0,
username: "testuser",
group: "VIP",
},
],
trackerRoles: [],
downloadClients: [],
tagGroups: [],
tagGroupMembers: [],
clientSnapshots: [],
clientUptimeBuckets: [],
}
}
// ---------------------------------------------------------------------------
// Validation tests
// ---------------------------------------------------------------------------
describe("validateBackupJson", () => {
it("accepts a valid payload", () => {
expect(() => validateBackupJson(validPayload())).not.toThrow()
})
it("rejects null payload", () => {
expect(() => validateBackupJson(null)).toThrow("payload must be an object")
})
it("rejects wrong backup version", () => {
const p = validPayload()
p.manifest.version = 999
expect(() => validateBackupJson(p)).toThrow("unsupported backup version")
})
it("rejects invalid manifest.createdAt", () => {
const p = validPayload()
p.manifest.createdAt = "not-a-date"
expect(() => validateBackupJson(p)).toThrow("manifest.createdAt")
})
it("rejects negative count", () => {
const p = validPayload()
p.manifest.counts.trackers = -1
expect(() => validateBackupJson(p)).toThrow("non-negative integer")
})
it("rejects short encryption salt", () => {
const p = validPayload()
p.settings.encryptionSalt = "abcd"
expect(() => validateBackupJson(p)).toThrow("exactly 64 hex")
})
it("rejects non-hex encryption salt", () => {
const p = validPayload()
p.settings.encryptionSalt = "g".repeat(64)
expect(() => validateBackupJson(p)).toThrow("exactly 64 hex")
})
it("rejects invalid backup frequency", () => {
const p = validPayload()
p.settings.backupScheduleFrequency = "hourly"
expect(() => validateBackupJson(p)).toThrow("backupScheduleFrequency")
})
it("rejects retention count out of range", () => {
const p = validPayload()
p.settings.backupRetentionCount = 0
expect(() => validateBackupJson(p)).toThrow("backupRetentionCount")
})
it("rejects tracker with empty name", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).name = ""
expect(() => validateBackupJson(p)).toThrow("trackers[0].name")
})
it("rejects tracker with invalid URL", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).baseUrl = "not-a-url"
expect(() => validateBackupJson(p)).toThrow("trackers[0].baseUrl")
})
it("rejects tracker with non-http/https protocol", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).baseUrl = "ftp://example.com"
expect(() => validateBackupJson(p)).toThrow("must use http or https")
})
it("rejects tracker baseUrl targeting localhost", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).baseUrl = "http://localhost/api"
expect(() => validateBackupJson(p)).toThrow("must not target localhost")
})
it("rejects tracker baseUrl targeting private IP", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).baseUrl = "http://192.168.1.1/api"
expect(() => validateBackupJson(p)).toThrow("must not target localhost")
})
it("rejects tracker baseUrl targeting cloud metadata endpoint", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).baseUrl = "http://169.254.169.254/latest"
expect(() => validateBackupJson(p)).toThrow("must not target localhost")
})
it("accepts tracker with empty API token (post-restore backups)", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).encryptedApiToken = ""
expect(() => validateBackupJson(p)).not.toThrow()
})
it("rejects invalid hex color", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).color = "red"
expect(() => validateBackupJson(p)).toThrow("trackers[0].color")
})
it("accepts null color", () => {
const p = validPayload()
;(p.trackers[0] as Record<string, unknown>).color = null
expect(() => validateBackupJson(p)).not.toThrow()
})
it("rejects non-BigInt uploadedBytes", () => {
const p = validPayload()
;(p.trackerSnapshots[0] as Record<string, unknown>).uploadedBytes = "not-a-number"
expect(() => validateBackupJson(p)).toThrow("trackerSnapshots[0].uploadedBytes")
})
it("rejects invalid snapshot polledAt", () => {
const p = validPayload()
;(p.trackerSnapshots[0] as Record<string, unknown>).polledAt = "invalid"
expect(() => validateBackupJson(p)).toThrow("trackerSnapshots[0].polledAt")
})
it("rejects download client with port out of range", () => {
const p = validPayload() as unknown as Record<string, unknown>
p.downloadClients = [
{
id: 1,
host: "192.168.1.1",
port: 99999,
encryptedUsername: "enc",
encryptedPassword: "enc",
},
]
expect(() => validateBackupJson(p)).toThrow("downloadClients[0].port")
})
it("rejects tagGroupMember without groupId", () => {
const p = validPayload() as unknown as Record<string, unknown>
p.tagGroupMembers = [{ tag: "test", label: "Test" }]
expect(() => validateBackupJson(p)).toThrow("tagGroupMembers[0].groupId")
})
})
// ---------------------------------------------------------------------------
// Encryption round-trip
// ---------------------------------------------------------------------------
describe("Backup encryption", () => {
it("encrypt then decrypt produces the original payload", async () => {
const { deriveKey } = await import("@/lib/crypto")
const password = "test-password"
const payload = validPayload()
const envelope = await encryptBackupPayload(
payload as Parameters<typeof encryptBackupPayload>[0],
password
)
expect(envelope.format).toBe("tracker-tracker-encrypted-backup")
expect(envelope.version).toBe(1)
expect(typeof envelope.ciphertext).toBe("string")
expect(typeof envelope.encryptionSalt).toBe("string")
// Derive key from password using the same salt from envelope
const key = await deriveKey(password, envelope.encryptionSalt)
const decrypted = decryptBackupPayload(envelope, key)
expect(decrypted.manifest.version).toBe(CURRENT_BACKUP_VERSION)
expect(decrypted.trackers).toHaveLength(1)
expect((decrypted.trackers[0] as Record<string, unknown>).name).toBe("TestTracker")
})
it("decrypt rejects wrong key", async () => {
const { deriveKey } = await import("@/lib/crypto")
const password1 = "password-one"
const password2 = "password-two"
const payload = validPayload()
const envelope = await encryptBackupPayload(
payload as Parameters<typeof encryptBackupPayload>[0],
password1
)
// Derive wrong key using different password but same salt
const wrongKey = await deriveKey(password2, envelope.encryptionSalt)
expect(() => decryptBackupPayload(envelope, wrongKey)).toThrow()
})
it("decrypt rejects invalid format field", () => {
const envelope = {
format: "not-a-backup" as "tracker-tracker-encrypted-backup",
version: 1 as const,
createdAt: new Date().toISOString(),
encryptionSalt: "fake-salt",
ciphertext: "bogus",
}
const key = Buffer.alloc(32, 1)
expect(() => decryptBackupPayload(envelope, key)).toThrow("Invalid backup envelope format")
})
})
// ---------------------------------------------------------------------------
// Security invariants
// ---------------------------------------------------------------------------
describe("Backup security invariants", () => {
it("encryptionSalt must be present in settings", () => {
const p = validPayload()
expect(p.settings.encryptionSalt).toBeDefined()
expect(typeof p.settings.encryptionSalt).toBe("string")
})
it("userPausedAt is included in tracker backup payload when set", () => {
const p = validPayload()
const tracker = p.trackers[0]
;(tracker as Record<string, unknown>).userPausedAt = "2026-03-21T12:00:00.000Z"
expect(tracker.userPausedAt).toBe("2026-03-21T12:00:00.000Z")
})
it("pausedAt is excluded from tracker backup payload (transient runtime state)", () => {
const p = validPayload()
const tracker = p.trackers[0]
expect(tracker).not.toHaveProperty("pausedAt")
expect(tracker).not.toHaveProperty("paused_at")
})
it("lastErrorAt is excluded from tracker backup payload (transient runtime state)", () => {
const p = validPayload()
const tracker = p.trackers[0]
expect(tracker).not.toHaveProperty("lastErrorAt")
expect(tracker).not.toHaveProperty("last_error_at")
})
})
// ---------------------------------------------------------------------------
// generateBackupPayload — sensitive field exclusion
//
// These tests call the real generateBackupPayload() with a mocked DB that
// returns a realistic appSettings row including the three sensitive fields
// that must NEVER appear in a backup:
// - passwordHash (Argon2 credential, must never leave the server)
// - failedLoginAttempts (transient security state)
// - encryptedSchedulerKey (internal key, not user-restorable)
//
// If someone accidentally removes a field from the destructuring exclusion list
// in backup.ts, these tests will catch the regression.
// ---------------------------------------------------------------------------
describe("generateBackupPayload sensitive field exclusion", () => {
// A realistic appSettings DB row that includes every sensitive field.
const sensitiveSettingsRow = {
id: 1,
passwordHash: "$argon2id$v=19$m=65536,t=3,p=4$SUPERSECRETPASSWORDHASH",
encryptionSalt: "a".repeat(64),
failedLoginAttempts: 3,
encryptedSchedulerKey: "base64-encrypted-scheduler-key-value",
createdAt: new Date("2026-01-01T00:00:00.000Z"),
// Safe fields that should survive into the backup
storeUsernames: true,
backupScheduleEnabled: false,
backupScheduleFrequency: "daily",
backupRetentionCount: 14,
backupEncryptionEnabled: false,
proxyEnabled: false,
proxyType: "socks5",
qbitmanageEnabled: false,
}
// Build a chainable mock that returns the sensitive row for the settings query
// and empty arrays for all other table queries.
function makeSelectMock() {
const emptyChain = {
from: vi.fn().mockReturnThis(),
limit: vi.fn().mockResolvedValue([]),
orderBy: vi.fn().mockResolvedValue([]),
where: vi.fn().mockReturnThis(),
}
const settingsChain = {
from: vi.fn().mockReturnThis(),
limit: vi.fn().mockResolvedValue([sensitiveSettingsRow]),
orderBy: vi.fn().mockResolvedValue([]),
where: vi.fn().mockReturnThis(),
}
// db.select() is called multiple times. The first call is for appSettings
// (uses .limit(1)), subsequent calls are for other tables (use .orderBy()).
// We track call count to distinguish the settings query from the rest.
let callCount = 0
return vi.fn().mockImplementation(() => {
callCount += 1
return callCount === 1 ? settingsChain : emptyChain
})
}
beforeEach(() => {
vi.mocked(db.select).mockImplementation(makeSelectMock())
})
it("does NOT include passwordHash in backup settings output", async () => {
const payload = await generateBackupPayload()
expect(payload.settings).not.toHaveProperty("passwordHash")
expect(JSON.stringify(payload.settings)).not.toContain("SUPERSECRETPASSWORDHASH")
})
it("does NOT include failedLoginAttempts in backup settings output", async () => {
const payload = await generateBackupPayload()
expect(payload.settings).not.toHaveProperty("failedLoginAttempts")
})
it("does NOT include encryptedSchedulerKey in backup settings output", async () => {
const payload = await generateBackupPayload()
expect(payload.settings).not.toHaveProperty("encryptedSchedulerKey")
expect(JSON.stringify(payload.settings)).not.toContain("base64-encrypted-scheduler-key-value")
})
it("does NOT include id or createdAt in backup settings output", async () => {
const payload = await generateBackupPayload()
// id and createdAt are also stripped — verify defensively
expect(payload.settings).not.toHaveProperty("id")
expect(payload.settings).not.toHaveProperty("createdAt")
})
it("DOES include encryptionSalt in backup settings output (needed for restore)", async () => {
const payload = await generateBackupPayload()
expect(payload.settings).toHaveProperty("encryptionSalt", "a".repeat(64))
})
it("DOES include safe settings fields in backup output", async () => {
const payload = await generateBackupPayload()
expect(payload.settings).toHaveProperty("storeUsernames", true)
expect(payload.settings).toHaveProperty("backupScheduleEnabled", false)
expect(payload.settings).toHaveProperty("proxyEnabled", false)
expect(payload.settings).toHaveProperty("qbitmanageEnabled", false)
})
it("backup settings output contains no recognizable sensitive string values", async () => {
const payload = await generateBackupPayload()
const serialized = JSON.stringify(payload.settings)
// Raw password hash must not leak in any form
expect(serialized).not.toContain("argon2id")
// Scheduler key must not leak in any form
expect(serialized).not.toContain("base64-encrypted-scheduler-key-value")
})
})