-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1101 lines (969 loc) · 35.6 KB
/
Copy pathserver.js
File metadata and controls
1101 lines (969 loc) · 35.6 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Secure-by-default: hide Express development error pages (stack traces) unless
// explicitly overridden (e.g. NODE_ENV=development for local development).
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const express = require('express');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const { pipeline, Transform } = require('stream');
const tar = require('tar');
const {
CHUNKED_ENCRYPTION_MODE,
DEFAULT_CHUNK_SIZE,
createChunkedEncryptStream,
encryptedChunkedContentLength,
} = require('./lib/chunked-encryption');
const {
buildRequestSignatureMessage,
deriveRequestAuthKey: deriveProtocolRequestAuthKey,
encryptEnvelope,
hkdf,
hmac,
sha256Hex,
} = require('./lib/protocol');
const app = express();
app.disable('x-powered-by');
app.use(express.json({
verify: (req, res, buf) => {
req.rawBody = Buffer.from(buf);
},
}));
app.use(securityHeaders);
app.use(express.static(path.join(__dirname, 'public'), { setHeaders: staticAssetHeaders }));
// Admin app: a SEPARATE listener bound to loopback only (default :21900).
// The Cloudflare Tunnel forwards only the client port (PORT = 21891), so
// /admin and /api/local/* are never reachable via the public hostname —
// only from this machine (http://127.0.0.1:ADMIN_PORT/admin). For remote
// admin, SSH port-forward ADMIN_PORT instead of exposing it publicly.
const adminApp = express();
adminApp.disable('x-powered-by');
adminApp.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-store');
next();
});
adminApp.use(securityHeaders);
adminApp.use(express.json());
adminApp.use(express.static(path.join(__dirname, 'admin')));
adminApp.get('/admin', (req, res) => res.sendFile(path.join(__dirname, 'admin', 'admin.html')));
const MAX_TEXTS = 100;
const TEXT_EXPIRY_MS = 24 * 60 * 60 * 1000;
const MAX_CONCURRENT_DOWNLOADS = 3;
const MAX_ADMIN_UPLOAD_BYTES = Number(process.env.ADMIN_UPLOAD_MAX_BYTES || 50 * 1024 * 1024 * 1024);
const REQUEST_AUTH_WINDOW_MS = 5 * 60 * 1000;
const REQUEST_NONCE_TTL_MS = 10 * 60 * 1000;
const MAX_NONCES_PER_DEVICE = 512;
let activeDownloads = 0;
const seenRequestNonces = new Map();
const ADMIN_PORT = process.env.ADMIN_PORT || 21900;
const ADMIN_HOST = process.env.ADMIN_HOST || '127.0.0.1';
const DATA_DIR = process.env.CLOUDSYNCD_DATA_DIR
? path.resolve(process.env.CLOUDSYNCD_DATA_DIR)
: path.join(__dirname, 'data');
const STATE_FILE = path.join(DATA_DIR, 'state.json');
const ADMIN_TOKEN_FILE = path.join(DATA_DIR, '.admin-token');
const DOWNLOAD_HISTORY_FILE = path.join(DATA_DIR, 'download-history.json');
const MAX_DOWNLOAD_HISTORY = 500;
const PRIVATE_DIR_MODE = 0o700;
const PRIVATE_FILE_MODE = 0o600;
const sharedDir = process.env.CLOUDSYNCD_SHARED_DIR
? path.resolve(process.env.CLOUDSYNCD_SHARED_DIR)
: path.join(__dirname, 'shared');
// ============ Security Middleware ============
// Security headers for the tunnel-facing client app. HSTS is intentionally not
// set here: the origin speaks plain HTTP on loopback; HTTPS (and therefore
// HSTS) is terminated by the Cloudflare Tunnel in front of it — set HSTS there.
function securityHeaders(req, res, next) {
res.setHeader('Content-Security-Policy', [
"default-src 'self'",
"script-src 'self'",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"font-src 'self' https://fonts.gstatic.com",
"img-src 'self' data: blob:",
"connect-src 'self'",
"worker-src 'self'",
"object-src 'none'",
"base-uri 'self'",
"frame-ancestors 'none'",
"form-action 'self'",
].join('; '));
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('Referrer-Policy', 'no-referrer');
res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');
next();
}
function staticAssetHeaders(res, filePath) {
const name = path.basename(filePath);
if (name === 'index.html' || name === 'app.js' || name === 'download-sw.js') {
res.setHeader('Cache-Control', 'no-store');
}
}
// Minimal in-memory per-IP rate limiter for the unauthenticated pairing
// endpoints (the only unauthenticated routes on the tunnel-facing port). Keyed
// by CF-Connecting-IP (set by the Cloudflare Tunnel) with a socket-IP fallback.
// Caps PIN brute-force / pairing-session-burn DoS from a single source; a legit
// operator pairing from a different IP is unaffected.
const PAIR_RATE_WINDOW_MS = 60_000;
const PAIR_RATE_MAX = 10;
const pairRateBuckets = new Map();
function pairRateLimit(req, res, next) {
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress || 'unknown';
const now = Date.now();
let bucket = pairRateBuckets.get(ip);
if (!bucket || bucket.resetAt < now) {
bucket = { count: 0, resetAt: now + PAIR_RATE_WINDOW_MS };
pairRateBuckets.set(ip, bucket);
}
bucket.count++;
if (bucket.count > PAIR_RATE_MAX) {
res.setHeader('Retry-After', String(Math.ceil((bucket.resetAt - now) / 1000)));
return res.status(429).json({ error: 'Too many requests' });
}
if (pairRateBuckets.size > 4096) {
for (const [k, v] of pairRateBuckets) if (v.resetAt < now) pairRateBuckets.delete(k);
}
next();
}
// Uniform 401 for every request-auth failure — prevents a deviceId-existence
// oracle (differential 401 bodies) on the public endpoints.
function authFail(res) {
return res.status(401).json({ error: 'Invalid request authentication' });
}
// ============ Persistent State ============
function chmodBestEffort(target, mode) {
try {
fs.chmodSync(target, mode);
} catch (err) {
console.warn(`[STATE] Failed to chmod ${target}: ${err.message}`);
}
}
function ensurePrivateDataDir() {
fs.mkdirSync(DATA_DIR, { recursive: true, mode: PRIVATE_DIR_MODE });
chmodBestEffort(DATA_DIR, PRIVATE_DIR_MODE);
}
function ensurePrivateFile(filePath) {
if (fs.existsSync(filePath)) chmodBestEffort(filePath, PRIVATE_FILE_MODE);
}
function writePrivateFile(filePath, data) {
ensurePrivateDataDir();
const tmpPath = path.join(
path.dirname(filePath),
`.${path.basename(filePath)}.${process.pid}.${Date.now()}.tmp`
);
fs.writeFileSync(tmpPath, data, { mode: PRIVATE_FILE_MODE });
fs.renameSync(tmpPath, filePath);
chmodBestEffort(filePath, PRIVATE_FILE_MODE);
}
ensurePrivateDataDir();
ensurePrivateFile(STATE_FILE);
ensurePrivateFile(ADMIN_TOKEN_FILE);
ensurePrivateFile(DOWNLOAD_HISTORY_FILE);
function loadDownloadHistory() {
try {
if (!fs.existsSync(DOWNLOAD_HISTORY_FILE)) return [];
const parsed = JSON.parse(fs.readFileSync(DOWNLOAD_HISTORY_FILE, 'utf8'));
return Array.isArray(parsed) ? parsed.slice(0, MAX_DOWNLOAD_HISTORY) : [];
} catch (err) {
console.error('[DOWNLOAD] Failed to load history:', err.message);
return [];
}
}
let downloadHistory = loadDownloadHistory();
function saveDownloadHistory() {
writePrivateFile(DOWNLOAD_HISTORY_FILE, `${JSON.stringify(downloadHistory, null, 2)}\n`);
}
function beginDownloadRecord({ deviceId, type, name, size, fileCount }) {
const record = {
id: crypto.randomUUID(),
deviceId,
type,
name,
size,
...(fileCount == null ? {} : { fileCount }),
startedAt: new Date().toISOString(),
};
return (err) => {
const completedAt = new Date().toISOString();
downloadHistory.unshift({
...record,
completedAt,
durationMs: Math.max(0, new Date(completedAt).getTime() - new Date(record.startedAt).getTime()),
status: err ? 'failed' : 'completed',
...(err ? { error: String(err.message || err).slice(0, 300) } : {}),
});
downloadHistory = downloadHistory.slice(0, MAX_DOWNLOAD_HISTORY);
try {
saveDownloadHistory();
} catch (saveErr) {
console.error('[DOWNLOAD] Failed to save history:', saveErr.message);
}
};
}
function loadState() {
try {
if (fs.existsSync(STATE_FILE)) {
return JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'));
}
} catch (e) { console.error('[STATE] Failed to load:', e.message); }
return null;
}
function saveState(data) {
writePrivateFile(STATE_FILE, JSON.stringify(data, null, 2));
}
// Master key + admin token: persisted to state.json so they survive restarts.
// The master key is rotated only via the admin "rotate-key" action.
let masterKey = null; // Buffer, 32 bytes
let devices = []; // [{ id, pairedAt }]
let adminToken = null; // string, 32 hex chars
const saved = loadState();
if (saved && saved.masterKey) {
masterKey = Buffer.from(saved.masterKey, 'hex');
devices = saved.devices || [];
adminToken = saved.adminToken || crypto.randomBytes(16).toString('hex');
console.log(`[STATE] Loaded master key, ${devices.length} paired device(s)`);
} else {
masterKey = crypto.randomBytes(32);
devices = [];
adminToken = crypto.randomBytes(16).toString('hex');
console.log('[STATE] Generated new master key');
}
function persistState() {
saveState({ masterKey: masterKey.toString('hex'), devices, adminToken });
}
// Persist on boot so any freshly generated admin token is durably stored.
persistState();
function persistDevices() {
persistState();
}
// Rotate the master key: every paired device's derived auth key becomes invalid,
// so all devices are dropped and must re-pair. Files on disk stay plaintext
// (encrypted per request with the current key), so they remain available to
// re-paired devices. Ephemeral texts (encrypted with the old key) won't decrypt.
function rotateMasterKey() {
masterKey = crypto.randomBytes(32);
devices = [];
seenRequestNonces.clear();
persistState();
}
const startedAt = Date.now();
writePrivateFile(ADMIN_TOKEN_FILE, adminToken);
// ============ Crypto Helpers ============
function generatePin() { return crypto.randomInt(100000, 999999).toString(); }
function generateECDHKeyPair() {
const ecdh = crypto.createECDH('prime256v1');
ecdh.generateKeys();
return { ecdh, publicKey: ecdh.getPublicKey('hex') };
}
function deriveRequestAuthKey(deviceId) {
return deriveProtocolRequestAuthKey(masterKey, deviceId);
}
function safeEqualHex(left, right) {
if (typeof left !== 'string' || typeof right !== 'string') return false;
if (left.length !== right.length || left.length % 2 !== 0) return false;
try {
const leftBuf = Buffer.from(left, 'hex');
const rightBuf = Buffer.from(right, 'hex');
return leftBuf.length === rightBuf.length && crypto.timingSafeEqual(leftBuf, rightBuf);
} catch {
return false;
}
}
// Constant-time check for the local admin token (used by the admin UI + CLI).
function safeEqualAdminToken(token) {
if (typeof token !== 'string' || token.length === 0) return false;
try {
const a = Buffer.from(token);
const b = Buffer.from(adminToken);
return a.length === b.length && crypto.timingSafeEqual(a, b);
} catch {
return false;
}
}
function isLoopbackAdminHost(host) {
return host === '127.0.0.1' || host === '::1' || host === 'localhost';
}
function shouldRequireAdminToken() {
if (process.env.ADMIN_AUTH === '1') return true;
if (process.env.ADMIN_AUTH === '0') return false;
return !isLoopbackAdminHost(ADMIN_HOST);
}
const REQUIRE_ADMIN_TOKEN = shouldRequireAdminToken();
function requireAdminToken(req, res, next) {
if (!REQUIRE_ADMIN_TOKEN) return next();
const token = req.headers['x-admin-token'];
if (!safeEqualAdminToken(token)) {
return res.status(403).json({ error: 'Forbidden' });
}
next();
}
function pruneSeenNonces(now = Date.now()) {
for (const [deviceId, entries] of seenRequestNonces.entries()) {
for (const [nonce, seenAt] of entries.entries()) {
if (now - seenAt > REQUEST_NONCE_TTL_MS) {
entries.delete(nonce);
}
}
if (entries.size === 0) {
seenRequestNonces.delete(deviceId);
}
}
}
function hasSeenNonce(deviceId, nonce) {
const entries = seenRequestNonces.get(deviceId);
return !!entries && entries.has(nonce);
}
function rememberNonce(deviceId, nonce, now = Date.now()) {
let entries = seenRequestNonces.get(deviceId);
if (!entries) {
entries = new Map();
seenRequestNonces.set(deviceId, entries);
}
entries.set(nonce, now);
if (entries.size <= MAX_NONCES_PER_DEVICE) return;
const overflow = entries.size - MAX_NONCES_PER_DEVICE;
let removed = 0;
for (const key of entries.keys()) {
entries.delete(key);
removed++;
if (removed >= overflow) break;
}
}
function requireDeviceAuth(req, res, next) {
if (devices.length === 0) return res.status(403).json({ error: 'Not paired' });
const deviceId = req.headers['x-device-id'];
const timestamp = req.headers['x-auth-timestamp'];
const nonce = req.headers['x-auth-nonce'];
const signature = req.headers['x-auth-signature'];
if (!deviceId || !timestamp || !nonce || !signature) {
return authFail(res);
}
const timestampMs = Number(timestamp);
if (!Number.isFinite(timestampMs)) {
return authFail(res);
}
const now = Date.now();
if (Math.abs(now - timestampMs) > REQUEST_AUTH_WINDOW_MS) {
return authFail(res);
}
// Look up the device, but derive an auth key and run the constant-time
// signature check on the same code path for known AND unknown deviceIds, so
// an unknown deviceId yields the same 401 as a bad signature — no
// deviceId-existence oracle via differential responses. An unknown device can
// never authenticate.
const device = devices.find((entry) => entry.id === deviceId);
const bodyHash = sha256Hex(req.rawBody || Buffer.alloc(0));
const expectedSignature = hmac(
deriveRequestAuthKey(deviceId),
buildRequestSignatureMessage(req.method, req.originalUrl, String(timestamp), String(nonce), bodyHash)
);
const signatureOk = safeEqualHex(expectedSignature, signature) && !!device;
if (!signatureOk) {
return authFail(res);
}
pruneSeenNonces(now);
// A replayed (already-seen) nonce returns the same 401 as any other failure —
// do not reveal that the signature was otherwise valid.
if (hasSeenNonce(deviceId, nonce)) {
return authFail(res);
}
rememberNonce(deviceId, nonce, now);
req.authenticatedDeviceId = deviceId;
next();
}
function encrypt(key, plaintext) {
return encryptEnvelope(key, plaintext);
}
function createEncryptStream(key) {
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
return { cipher, iv };
}
function setDownloadHeaders(res, headers = {}) {
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Cache-Control', 'no-store');
for (const [header, value] of Object.entries(headers)) {
res.setHeader(header, value);
}
}
function createStreamCompletion(onComplete) {
let completed = false;
return (err) => {
if (completed) return false;
completed = true;
if (onComplete) onComplete(err);
return true;
};
}
function finishPipedDownload(err, { complete, res, label }) {
if (!complete(err)) return;
if (!err) return;
console.error(`[${label}] Stream error:`, err.message);
if (!res.headersSent) {
res.status(500).json({ error: `${label} failed` });
} else if (!res.destroyed) {
res.destroy(err);
}
}
function streamEncryptedResponse({ res, sourceStream, extraHeaders = {}, label, onComplete }) {
const { cipher, iv } = createEncryptStream(masterKey);
const appendAuthTag = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk);
},
flush(callback) {
try {
this.push(cipher.getAuthTag());
callback();
} catch (err) {
callback(err);
}
},
});
const complete = createStreamCompletion(onComplete);
setDownloadHeaders(res, {
'X-Encrypted-IV': iv.toString('hex'),
'X-Encrypted-Tag-Length': '16',
...extraHeaders,
});
pipeline(sourceStream, cipher, appendAuthTag, res, (err) => {
finishPipedDownload(err, { complete, res, label });
});
}
function streamChunkedEncryptedResponse({ res, sourceStream, fileSize, relPath, extraHeaders = {}, label, onComplete }) {
const { stream, ivPrefix, tagLength } = createChunkedEncryptStream(masterKey, { fileSize, relPath });
const complete = createStreamCompletion(onComplete);
setDownloadHeaders(res, {
'X-Encrypted-Mode': CHUNKED_ENCRYPTION_MODE,
'X-Encrypted-IV-Prefix': ivPrefix.toString('hex'),
'X-Encrypted-Chunk-Size': String(DEFAULT_CHUNK_SIZE),
'X-Encrypted-Tag-Length': String(tagLength),
'Content-Length': String(encryptedChunkedContentLength(fileSize, DEFAULT_CHUNK_SIZE, { relPath })),
...extraHeaders,
});
pipeline(sourceStream, stream, res, (err) => {
finishPipedDownload(err, { complete, res, label });
});
}
function withDownloadSlot(res, start) {
if (activeDownloads >= MAX_CONCURRENT_DOWNLOADS) {
res.status(503).json({ error: 'Too many concurrent downloads. Please try again later.' });
return false;
}
activeDownloads++;
start(() => {
activeDownloads = Math.max(0, activeDownloads - 1);
});
return true;
}
// ============ Pending Pairing Session ============
// Ephemeral — only lives in memory, one at a time
let pendingPair = null; // { pin, keyPair, attempts, createdAt }
function createPairSession() {
pendingPair = {
pin: generatePin(),
keyPair: generateECDHKeyPair(),
attempts: 0,
maxAttempts: 5,
};
console.log('\n========================================');
console.log(` Pairing PIN: ${pendingPair.pin}`);
console.log(' Enter this PIN on the remote device');
console.log('========================================\n');
return pendingPair.pin;
}
// ============ API: Pairing ============
app.get('/api/status', (req, res) => {
res.json({ paired: devices.length > 0 });
});
app.get('/api/pair/init', pairRateLimit, (req, res) => {
if (!pendingPair) {
return res.status(400).json({ error: 'No active pairing session. Generate a new PIN on the server.' });
}
res.json({ serverPublicKey: pendingPair.keyPair.publicKey });
});
app.post('/api/pair/verify', pairRateLimit, (req, res) => {
if (!pendingPair) {
return res.status(400).json({ error: 'No active pairing session' });
}
if (pendingPair.attempts >= pendingPair.maxAttempts) {
pendingPair = null;
return res.status(403).json({ error: 'Too many attempts. Generate a new PIN.' });
}
const { clientPublicKey, proof, deviceId } = req.body || {};
if (!clientPublicKey || !proof) {
return res.status(400).json({ error: 'Missing fields' });
}
try {
// Validate the client public key before the ECDH op and BEFORE burning an
// attempt: a P-256 uncompressed public key is 65 bytes (0x04 + 32 + 32).
// Malformed keys are rejected with 400 without counting as a PIN guess, so
// an attacker cannot cheaply burn the 5-attempt budget with garbage.
const pubBuf = Buffer.from(clientPublicKey, 'hex');
if (pubBuf.length !== 65 || pubBuf[0] !== 0x04) {
return res.status(400).json({ error: 'Invalid client public key' });
}
const sharedSecret = pendingPair.keyPair.ecdh.computeSecret(pubBuf);
const authKey = Buffer.from(hkdf(sharedSecret, 'syncd-auth', 'pin-verify', 32));
const expectedProof = hmac(authKey, pendingPair.pin);
// Constant-time compare (consistent with the request-auth and admin-token
// paths). Only a real PIN mismatch counts as an attempt.
if (!safeEqualHex(expectedProof, proof)) {
pendingPair.attempts++;
const remaining = pendingPair.maxAttempts - pendingPair.attempts;
console.log(`[PAIR] Invalid PIN attempt (${remaining} remaining)`);
if (pendingPair.attempts >= pendingPair.maxAttempts) {
pendingPair = null;
return res.status(403).json({ error: 'Too many attempts. Generate a new PIN.' });
}
return res.status(401).json({ error: 'Invalid PIN' });
}
// PIN verified — encrypt master key with transport key and send to client
const transportKey = Buffer.from(hkdf(sharedSecret, 'syncd-transport', 'master-key-delivery', 32));
const encryptedMasterKey = encrypt(transportKey, masterKey);
const serverProof = hmac(authKey, 'server-confirmed');
const id = deviceId || 'unknown';
const pairedAt = new Date().toISOString();
const existingDevice = devices.find((entry) => entry.id === id);
if (existingDevice) {
existingDevice.pairedAt = pairedAt;
} else {
devices.push({ id, pairedAt });
}
persistDevices();
pendingPair = null; // Invalidate PIN
console.log(`[PAIR] Device paired: ${id} (${devices.length} total)`);
res.json({ success: true, serverProof, encryptedMasterKey });
} catch (err) {
console.error('[PAIR] Error:', err.message);
// computeSecret throws on an off-curve/invalid point despite the 0x04
// length check — treat as a bad client key (400), not a server error, and
// do NOT burn an attempt or leak internal details.
res.status(400).json({ error: 'Invalid client public key' });
}
});
// ============ Local-only (admin app): Generate new PIN ============
adminApp.get('/api/local/auth', (req, res) => {
res.json({ requiresToken: REQUIRE_ADMIN_TOKEN });
});
adminApp.post('/api/local/new-pin', requireAdminToken, (req, res) => {
const pin = createPairSession();
res.json({ pin });
});
// ============ Local-only (admin app): Device Management ============
function revokeDevice(id) {
const before = devices.length;
devices = devices.filter((entry) => entry.id !== id);
if (devices.length !== before) persistDevices();
return before - devices.length;
}
function ensureSharedDir() {
fs.mkdirSync(sharedDir, { recursive: true });
return fs.realpathSync(sharedDir);
}
function normalizeSharedRelPath(relPath) {
const normalized = String(relPath || '').replace(/\\/g, '/').replace(/^\/+/, '');
const parts = normalized.split('/');
if (
!normalized
|| parts.some((part) => !part || part === '.' || part === '..' || part.startsWith('.'))
) {
throw new Error('Invalid shared file path');
}
return parts.join('/');
}
function resolveSharedWriteTarget(relPath) {
const realShared = ensureSharedDir();
const safeRelPath = normalizeSharedRelPath(relPath);
const target = path.resolve(path.join(realShared, safeRelPath));
const rel = path.relative(realShared, target);
if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel)) {
throw new Error('Shared file path escapes shared directory');
}
return { target, rel: safeRelPath };
}
function resolveSharedExistingFile(relPath) {
const { target, rel } = resolveSharedWriteTarget(relPath);
let lstat;
try {
lstat = fs.lstatSync(target);
} catch {
const err = new Error('Not found');
err.status = 404;
throw err;
}
if (!lstat.isFile()) {
const err = new Error('Not found');
err.status = 404;
throw err;
}
let realFile;
try {
realFile = fs.realpathSync(target);
} catch {
const err = new Error('Not found');
err.status = 404;
throw err;
}
const realShared = fs.realpathSync(sharedDir);
const realRel = path.relative(realShared, realFile);
if (realRel === '' || realRel.startsWith('..') || path.isAbsolute(realRel)) {
const err = new Error('Access denied');
err.status = 403;
throw err;
}
const stat = fs.statSync(realFile);
if (!stat.isFile()) {
const err = new Error('Not found');
err.status = 404;
throw err;
}
return { target, realFile, rel, stat };
}
function assertSharedParentContained(target) {
const realShared = fs.realpathSync(sharedDir);
const realParent = fs.realpathSync(path.dirname(target));
const rel = path.relative(realShared, realParent);
if (rel.startsWith('..') || path.isAbsolute(rel)) {
throw new Error('Shared file path escapes shared directory');
}
}
function removeEmptySharedParents(startDir) {
const realShared = fs.realpathSync(sharedDir);
let current = path.resolve(startDir);
while (current !== realShared && path.relative(realShared, current) && !path.relative(realShared, current).startsWith('..')) {
try {
fs.rmdirSync(current);
} catch {
return;
}
current = path.dirname(current);
}
}
function sharedPathError(res, err) {
const status = err && err.status ? err.status : 400;
res.status(status).json({ error: err.message || 'Invalid shared file path' });
}
adminApp.get('/api/local/status', requireAdminToken, (req, res) => {
if (!fs.existsSync(sharedDir)) fs.mkdirSync(sharedDir, { recursive: true });
const entries = walkDir(sharedDir);
let fileCount = 0;
let totalSize = 0;
for (const entry of entries) {
if (entry.type === 'file') {
fileCount++;
totalSize += entry.size;
}
}
cleanExpiredTexts();
res.json({
startedAt,
paired: devices.length > 0,
deviceCount: devices.length,
hasMasterKey: !!masterKey,
textsCount: sharedTexts.length,
textsMax: MAX_TEXTS,
textsExpiryMs: TEXT_EXPIRY_MS,
sharedFiles: fileCount,
sharedSize: totalSize,
});
});
adminApp.get('/api/local/files', requireAdminToken, (req, res) => {
ensureSharedDir();
res.json({ entries: walkDir(sharedDir) });
});
adminApp.get('/api/local/downloads', requireAdminToken, (req, res) => {
res.json({ entries: downloadHistory });
});
adminApp.delete('/api/local/downloads', requireAdminToken, (req, res) => {
const cleared = downloadHistory.length;
downloadHistory = [];
saveDownloadHistory();
res.json({ success: true, cleared });
});
adminApp.post('/api/local/files/clear', requireAdminToken, (req, res) => {
fs.rmSync(sharedDir, { recursive: true, force: true });
fs.mkdirSync(sharedDir, { recursive: true });
console.log('[ADMIN] Cleared shared directory');
res.json({ success: true, removed: 'all' });
});
adminApp.put(/^\/api\/local\/files\/(.*)/, requireAdminToken, (req, res) => {
let target;
let rel;
try {
const resolved = resolveSharedWriteTarget(req.params[0] || '');
target = resolved.target;
rel = resolved.rel;
} catch (err) {
return sharedPathError(res, err);
}
const contentLength = Number(req.headers['content-length'] || 0);
if (Number.isFinite(contentLength) && contentLength > MAX_ADMIN_UPLOAD_BYTES) {
return res.status(413).json({ error: 'Upload is too large' });
}
try {
fs.mkdirSync(path.dirname(target), { recursive: true });
assertSharedParentContained(target);
} catch (err) {
return sharedPathError(res, err);
}
const tmpPath = path.join(
path.dirname(target),
`.${path.basename(target)}.${process.pid}.${Date.now()}.upload`
);
let uploadedBytes = 0;
const limitStream = new Transform({
transform(chunk, encoding, callback) {
uploadedBytes += chunk.length;
if (uploadedBytes > MAX_ADMIN_UPLOAD_BYTES) {
callback(new Error('Upload is too large'));
return;
}
callback(null, chunk);
},
});
pipeline(req, limitStream, fs.createWriteStream(tmpPath, { mode: 0o600 }), (err) => {
if (err) {
fs.rmSync(tmpPath, { force: true });
const status = err.message === 'Upload is too large' ? 413 : 500;
if (!res.headersSent) res.status(status).json({ error: err.message || 'Upload failed' });
return;
}
try {
fs.renameSync(tmpPath, target);
console.log(`[ADMIN] Uploaded shared file: ${rel} (${uploadedBytes} bytes)`);
res.json({ success: true, name: rel, size: uploadedBytes });
} catch (renameErr) {
fs.rmSync(tmpPath, { force: true });
if (!res.headersSent) res.status(500).json({ error: renameErr.message || 'Upload failed' });
}
});
});
adminApp.delete(/^\/api\/local\/files\/(.*)/, requireAdminToken, (req, res) => {
let file;
try {
file = resolveSharedExistingFile(req.params[0] || '');
} catch (err) {
return sharedPathError(res, err);
}
fs.unlinkSync(file.target);
removeEmptySharedParents(path.dirname(file.target));
console.log(`[ADMIN] Deleted shared file: ${file.rel}`);
res.json({ success: true, deleted: file.rel });
});
adminApp.get('/api/local/devices', requireAdminToken, (req, res) => {
res.json({ devices });
});
adminApp.post('/api/local/revoke', requireAdminToken, (req, res) => {
const { deviceId } = req.body || {};
if (!deviceId || typeof deviceId !== 'string') {
return res.status(400).json({ error: 'Missing deviceId' });
}
const removed = revokeDevice(deviceId);
if (removed === 0) {
return res.status(404).json({ error: 'Device not found', devices });
}
console.log(`[DEVICE] Revoked: ${deviceId} (${devices.length} remaining)`);
res.json({ success: true, revoked: deviceId, remaining: devices.length });
});
adminApp.post('/api/local/revoke-all', requireAdminToken, (req, res) => {
const count = devices.length;
devices = [];
persistDevices();
console.log(`[DEVICE] Revoked all devices (${count} removed)`);
res.json({ success: true, revoked: count, remaining: 0 });
});
adminApp.post('/api/local/rotate-key', requireAdminToken, (req, res) => {
const revoked = devices.length;
rotateMasterKey();
console.log(`[KEY] Master key rotated; ${revoked} device(s) must re-pair`);
res.json({ success: true, revoked, message: 'Master key rotated. All devices must re-pair.' });
});
adminApp.post('/api/local/rotate-token', requireAdminToken, (req, res) => {
adminToken = crypto.randomBytes(16).toString('hex');
persistState();
writePrivateFile(ADMIN_TOKEN_FILE, adminToken);
console.log('[ADMIN] Admin token rotated');
// Return the new token so the admin session can stay logged in.
res.json({ success: true, adminToken });
});
// ============ File Sharing (encrypted) ============
function walkDir(dir, prefix = '') {
if (!fs.existsSync(dir)) return [];
let results = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name.startsWith('.')) continue;
const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
results.push({ name: relPath, type: 'dir' });
results = results.concat(walkDir(fullPath, relPath));
} else if (entry.isFile()) {
const stat = fs.statSync(fullPath);
results.push({ name: relPath, type: 'file', size: stat.size, modified: stat.mtime.toISOString() });
}
}
return results;
}
app.get('/api/files', requireDeviceAuth, (req, res) => {
if (!fs.existsSync(sharedDir)) fs.mkdirSync(sharedDir, { recursive: true });
const tree = walkDir(sharedDir);
res.json({ encrypted: encrypt(masterKey, Buffer.from(JSON.stringify(tree))) });
});
function resolveSharedFile(relPath, res) {
try {
return resolveSharedExistingFile(relPath);
} catch (err) {
sharedPathError(res, err);
return null;
}
}
function sharedFileHeaders(relPath, fileSize) {
return {
'X-File-Name': encodeURIComponent(path.basename(relPath)),
'X-File-Size': String(fileSize),
};
}
function sendSharedFileDownload({ req, res, file, relPath, chunked }) {
const fileSize = file.stat.size;
withDownloadSlot(res, (releaseSlot) => {
const recordDownload = beginDownloadRecord({
deviceId: req.authenticatedDeviceId,
type: 'file',
name: relPath,
size: fileSize,
});
const onComplete = (err) => {
releaseSlot();
recordDownload(err);
};
if (chunked) {
streamChunkedEncryptedResponse({
res,
sourceStream: fs.createReadStream(file.realFile, { highWaterMark: DEFAULT_CHUNK_SIZE }),
fileSize,
relPath,
label: 'FILE-CHUNKED',
extraHeaders: sharedFileHeaders(relPath, fileSize),
onComplete,
});
return;
}
streamEncryptedResponse({
res,
sourceStream: fs.createReadStream(file.realFile),
label: 'FILE',
extraHeaders: {
'Content-Length': String(fileSize + 16),
...sharedFileHeaders(relPath, fileSize),
},
onComplete,
});
});
}
app.get(/^\/api\/files-chunked\/(.*)/, requireDeviceAuth, (req, res) => {
const relPath = req.params[0] || '';
const file = resolveSharedFile(relPath, res);
if (!file) return;
sendSharedFileDownload({ req, res, file, relPath: file.rel, chunked: true });
});
app.get(/^\/api\/files\/(.*)/, requireDeviceAuth, (req, res) => {
const relPath = req.params[0] || '';
const file = resolveSharedFile(relPath, res);
if (!file) return;
sendSharedFileDownload({ req, res, file, relPath: file.rel, chunked: false });
});
// ============ Batch Download ============
app.get('/api/batch', requireDeviceAuth, (req, res) => {
if (!fs.existsSync(sharedDir)) fs.mkdirSync(sharedDir, { recursive: true });
const since = req.query.since ? new Date(req.query.since) : null;
const files = [];
let totalSize = 0;
for (const entry of walkDir(sharedDir)) {
if (entry.type !== 'file') continue;