forked from actualbudget/actual-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-sync.js
444 lines (379 loc) · 12.1 KB
/
app-sync.js
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
let { Buffer } = require('buffer');
let fs = require('fs/promises');
let { join } = require('path');
let express = require('express');
let uuid = require('uuid');
let AdmZip = require('adm-zip');
let { validateUser } = require('./util/validate-user');
let errorMiddleware = require('./util/error-middleware');
let config = require('./load-config');
let { getAccountDb } = require('./account-db');
let simpleSync = require('./sync-simple');
let fullSync = require('./sync-full');
let actual = require('@actual-app/api');
let SyncPb = actual.internal.SyncProtoBuf;
const app = express();
app.use(errorMiddleware);
async function init() {
let fileDir = join(process.env.ACTUAL_USER_FILES || config.userFiles);
console.log('Initializing Actual with user file dir:', fileDir);
await actual.init({
config: {
dataDir: fileDir
}
});
}
// This is a version representing the internal format of sync
// messages. When this changes, all sync files need to be reset. We
// will check this version when syncing and notify the user if they
// need to reset.
const SYNC_FORMAT_VERSION = 2;
app.post('/sync', async (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let requestPb;
try {
requestPb = SyncPb.SyncRequest.deserializeBinary(req.body);
} catch (e) {
res.status(500);
res.send({ status: 'error', reason: 'internal-error' });
return;
}
let accountDb = getAccountDb();
let file_id = requestPb.getFileid() || null;
let group_id = requestPb.getGroupid() || null;
let key_id = requestPb.getKeyid() || null;
let since = requestPb.getSince() || null;
let messages = requestPb.getMessagesList();
if (!since) {
throw new Error('`since` is required');
}
let currentFiles = accountDb.all(
'SELECT group_id, encrypt_keyid, encrypt_meta, sync_version FROM files WHERE id = ?',
[file_id]
);
if (currentFiles.length === 0) {
res.status(400);
res.send('file-not-found');
return;
}
let currentFile = currentFiles[0];
if (
currentFile.sync_version == null ||
currentFile.sync_version < SYNC_FORMAT_VERSION
) {
res.status(400);
res.send('file-old-version');
return;
}
// When resetting sync state, something went wrong. There is no
// group id and it's awaiting a file to be uploaded.
if (currentFile.group_id == null) {
res.status(400);
res.send('file-needs-upload');
return;
}
// Check to make sure the uploaded file is valid and has been
// encrypted with the same key it is registered with (this might
// be wrong if there was an error during the key creation
// process)
let uploadedKeyId = currentFile.encrypt_meta
? JSON.parse(currentFile.encrypt_meta).keyId
: null;
if (uploadedKeyId !== currentFile.encrypt_keyid) {
res.status(400);
res.send('file-key-mismatch');
return;
}
// The changes being synced are part of an old group, which
// means the file has been reset. User needs to re-download.
if (group_id !== currentFile.group_id) {
res.status(400);
res.send('file-has-reset');
return;
}
// The data is encrypted with a different key which is
// unacceptable. We can't accept these changes. Reject them and
// tell the user that they need to generate the correct key
// (which necessitates a sync reset so they need to re-download).
if (key_id !== currentFile.encrypt_keyid) {
res.status(400);
res.send('file-has-new-key');
return false;
}
// TODO: We also provide a "simple" sync method which currently isn't
// used. This method just stores the messages locally and doesn't
// load the whole app at all. If we want to support end-to-end
// encryption, this method is required because we can't read the
// messages. Using it looks like this:
//
// let simpleSync = require('./sync-simple');
// let {trie, newMessages } = simpleSync.sync(messages, since, file_id);
let { trie, newMessages } = await fullSync.sync(messages, since, file_id);
// encode it back...
let responsePb = new SyncPb.SyncResponse();
responsePb.setMerkle(JSON.stringify(trie));
for (let i = 0; i < newMessages.length; i++) {
let msg = newMessages[i];
let envelopePb = new SyncPb.MessageEnvelope();
envelopePb.setTimestamp(msg.timestamp);
envelopePb.setIsencrypted(msg.is_encrypted === 1);
envelopePb.setContent(msg.content);
responsePb.addMessages(envelopePb);
}
res.set('Content-Type', 'application/actual-sync');
res.send(Buffer.from(responsePb.serializeBinary()));
});
app.post('/user-get-key', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let { fileId } = req.body;
let rows = accountDb.all(
'SELECT encrypt_salt, encrypt_keyid, encrypt_test FROM files WHERE id = ?',
[fileId]
);
if (rows.length === 0) {
res.status(400).send('file-not-found');
return;
}
let { encrypt_salt, encrypt_keyid, encrypt_test } = rows[0];
res.send(
JSON.stringify({
status: 'ok',
data: { id: encrypt_keyid, salt: encrypt_salt, test: encrypt_test }
})
);
});
app.post('/user-create-key', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let { fileId, keyId, keySalt, testContent } = req.body;
accountDb.mutate(
'UPDATE files SET encrypt_salt = ?, encrypt_keyid = ?, encrypt_test = ? WHERE id = ?',
[keySalt, keyId, testContent, fileId]
);
res.send(JSON.stringify({ status: 'ok' }));
});
app.post('/reset-user-file', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let { fileId } = req.body;
let files = accountDb.all('SELECT group_id FROM files WHERE id = ?', [
fileId
]);
if (files.length === 0) {
res.status(400).send('User or file not found');
return;
}
let { group_id } = files[0];
accountDb.mutate('UPDATE files SET group_id = NULL WHERE id = ?', [fileId]);
if (group_id) {
// TODO: Instead of doing this, just delete the db file named
// after the group
// db.mutate('DELETE FROM messages_binary WHERE group_id = ?', [group_id]);
// db.mutate('DELETE FROM messages_merkles WHERE group_id = ?', [group_id]);
}
res.send(JSON.stringify({ status: 'ok' }));
});
app.post('/upload-user-file', async (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let name = decodeURIComponent(req.headers['x-actual-name']);
let fileId = req.headers['x-actual-file-id'];
let groupId = req.headers['x-actual-group-id'] || null;
let encryptMeta = req.headers['x-actual-encrypt-meta'] || null;
let syncFormatVersion = req.headers['x-actual-format'] || null;
let keyId = encryptMeta ? JSON.parse(encryptMeta).keyId : null;
if (!fileId) {
throw new Error('fileId is required');
}
let currentFiles = accountDb.all(
'SELECT group_id, encrypt_keyid, encrypt_meta FROM files WHERE id = ?',
[fileId]
);
if (currentFiles.length) {
let currentFile = currentFiles[0];
// The uploading file is part of an old group, so reject
// it. All of its internal sync state is invalid because its
// old. The sync state has been reset, so user needs to
// either reset again or download from the current group.
if (groupId !== currentFile.group_id) {
res.status(400);
res.send('file-has-reset');
return;
}
// The key that the file is encrypted with is different than
// the current registered key. All data must always be
// encrypted with the registered key for consistency. Key
// changes always necessitate a sync reset, which means this
// upload is trying to overwrite another reset. That might
// be be fine, but since we definitely cannot accept a file
// encrypted with the wrong key, we bail and suggest the
// user download the latest file.
if (keyId !== currentFile.encrypt_keyid) {
res.status(400);
res.send('file-has-new-key');
return;
}
}
// TODO: If we want to support end-to-end encryption, we'd write the
// raw file down because it's an encrypted blob. This isn't
// supported yet in the self-hosted version because it's unclear if
// it's still needed, given that you own your server
//
// await fs.writeFile(join(config.userFiles, `${fileId}.blob`), req.body);
let zip = new AdmZip(req.body);
try {
zip.extractAllTo(join(config.userFiles, fileId), true);
} catch (err) {
console.log('Error writing file', err);
res.send(JSON.stringify({ status: 'error' }));
return;
}
let rows = accountDb.all('SELECT id FROM files WHERE id = ?', [fileId]);
if (rows.length === 0) {
// it's new
groupId = uuid.v4();
accountDb.mutate(
'INSERT INTO files (id, group_id, sync_version, name, encrypt_meta) VALUES (?, ?, ?, ?, ?)',
[fileId, groupId, syncFormatVersion, name, encryptMeta]
);
res.send(JSON.stringify({ status: 'ok', groupId }));
} else {
if (!groupId) {
// sync state was reset, create new group
groupId = uuid.v4();
accountDb.mutate('UPDATE files SET group_id = ? WHERE id = ?', [
groupId,
fileId
]);
}
// Regardless, update some properties
accountDb.mutate(
'UPDATE files SET sync_version = ?, encrypt_meta = ?, name = ? WHERE id = ?',
[syncFormatVersion, encryptMeta, name, fileId]
);
res.send(JSON.stringify({ status: 'ok', groupId }));
}
});
app.get('/download-user-file', async (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let fileId = req.headers['x-actual-file-id'];
// Do some authentication
let rows = accountDb.all(
'SELECT id FROM files WHERE id = ? AND deleted = FALSE',
[fileId]
);
if (rows.length === 0) {
res.status(400).send('User or file not found');
return;
}
let zip = new AdmZip();
try {
zip.addLocalFolder(join(config.userFiles, fileId), '/');
} catch (e) {
res.status(500).send('Error reading files');
return;
}
let buffer = zip.toBuffer();
res.setHeader('Content-Disposition', `attachment;filename=${fileId}`);
res.send(buffer);
});
app.post('/update-user-filename', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let { fileId, name } = req.body;
// Do some authentication
let rows = accountDb.all(
'SELECT id FROM files WHERE id = ? AND deleted = FALSE',
[fileId]
);
if (rows.length === 0) {
res.status(500).send('User or file not found');
return;
}
accountDb.mutate('UPDATE files SET name = ? WHERE id = ?', [name, fileId]);
res.send(JSON.stringify({ status: 'ok' }));
});
app.get('/list-user-files', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let rows = accountDb.all('SELECT * FROM files');
res.send(
JSON.stringify({
status: 'ok',
data: rows.map(row => ({
deleted: row.deleted,
fileId: row.id,
groupId: row.group_id,
name: row.name,
encryptKeyId: row.encrypt_keyid
}))
})
);
});
app.get('/get-user-file-info', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let fileId = req.headers['x-actual-file-id'];
let rows = accountDb.all(
'SELECT * FROM files WHERE id = ? AND deleted = FALSE',
[fileId]
);
if (rows.length === 0) {
res.send(JSON.stringify({ status: 'error' }));
return;
}
let row = rows[0];
res.send(
JSON.stringify({
status: 'ok',
data: {
deleted: row.deleted,
fileId: row.id,
groupId: row.group_id,
name: row.name,
encryptMeta: row.encrypt_meta ? JSON.parse(row.encrypt_meta) : null
}
})
);
});
app.post('/delete-user-file', (req, res) => {
let user = validateUser(req, res);
if (!user) {
return;
}
let accountDb = getAccountDb();
let { fileId } = req.body;
accountDb.mutate('UPDATE files SET deleted = TRUE WHERE id = ?', [fileId]);
res.send(JSON.stringify({ status: 'ok' }));
});
module.exports.handlers = app;
module.exports.init = init;