forked from DumbWareio/DumbBudget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
685 lines (589 loc) · 22.5 KB
/
server.js
File metadata and controls
685 lines (589 loc) · 22.5 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
const dotenv = require('dotenv').config();
const express = require('express');
const session = require('express-session');
const helmet = require('helmet');
const crypto = require('crypto');
const path = require('path');
const cookieParser = require('cookie-parser');
const fs = require('fs').promises;
const app = express();
const PORT = process.env.PORT || 3000;
const BASE_URL = process.env.BASE_URL || `http://localhost:${PORT}`;
// Get the project name from package.json to use for the PIN environment variable
const projectName = require('./package.json').name.toUpperCase().replace(/-/g, '_');
const PIN = process.env[`${projectName}_PIN`];
// Ensure data directory exists
const DATA_DIR = path.join(__dirname, 'data');
const TRANSACTIONS_FILE = path.join(DATA_DIR, 'transactions.json');
// Debug logging setup
const DEBUG = process.env.DEBUG === 'TRUE';
function debugLog(...args) {
if (DEBUG) {
console.log('[DEBUG]', ...args);
}
}
// Add logging to BASE_PATH extraction
const BASE_PATH = (() => {
if (!process.env.BASE_URL) {
debugLog('No BASE_URL set, using empty base path');
return '';
}
try {
const url = new URL(process.env.BASE_URL);
const path = url.pathname.replace(/\/$/, ''); // Remove trailing slash
debugLog('Extracted base path:', path);
return path;
} catch {
// If BASE_URL is just a path (e.g. /budget)
const path = process.env.BASE_URL.replace(/\/$/, '');
debugLog('Using BASE_URL as path:', path);
return path;
}
})();
async function ensureDataDir() {
try {
await fs.access(DATA_DIR);
} catch {
await fs.mkdir(DATA_DIR);
}
}
async function loadTransactions() {
try {
await fs.access(TRANSACTIONS_FILE);
const data = await fs.readFile(TRANSACTIONS_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
// If file doesn't exist or is invalid, return empty structure
return {
[new Date().toISOString().slice(0, 7)]: {
income: [],
expenses: []
}
};
}
}
async function saveTransactions(transactions) {
// Ensure data directory exists before saving
await ensureDataDir();
await fs.writeFile(TRANSACTIONS_FILE, JSON.stringify(transactions, null, 2));
}
// Initialize data directory
ensureDataDir().catch(console.error);
// Log whether PIN protection is enabled
if (!PIN || PIN.trim() === '') {
console.log('PIN protection is disabled');
} else {
console.log('PIN protection is enabled');
}
// Brute force protection
const loginAttempts = new Map();
const MAX_ATTEMPTS = 5;
const LOCKOUT_TIME = 15 * 60 * 1000; // 15 minutes in milliseconds
function resetAttempts(ip) {
loginAttempts.delete(ip);
}
function isLockedOut(ip) {
const attempts = loginAttempts.get(ip);
if (!attempts) return false;
if (attempts.count >= MAX_ATTEMPTS) {
const timeElapsed = Date.now() - attempts.lastAttempt;
if (timeElapsed < LOCKOUT_TIME) {
return true;
}
resetAttempts(ip);
}
return false;
}
function recordAttempt(ip) {
const attempts = loginAttempts.get(ip) || { count: 0, lastAttempt: 0 };
attempts.count += 1;
attempts.lastAttempt = Date.now();
loginAttempts.set(ip, attempts);
}
// Security middleware - minimal configuration like DumbDrop
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false,
crossOriginResourcePolicy: false,
originAgentCluster: false,
dnsPrefetchControl: false,
frameguard: false,
hsts: false,
ieNoOpen: false,
noSniff: false,
permittedCrossDomainPolicies: false,
referrerPolicy: false,
xssFilter: false
}));
app.use(express.json());
app.use(cookieParser());
// Session configuration - simplified like DumbDrop
app.use(session({
secret: crypto.randomBytes(32).toString('hex'),
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
httpOnly: true,
sameSite: 'lax'
}
}));
// Constant-time PIN comparison to prevent timing attacks
function verifyPin(storedPin, providedPin) {
if (!storedPin || !providedPin) return false;
if (storedPin.length !== providedPin.length) return false;
try {
return crypto.timingSafeEqual(
Buffer.from(storedPin),
Buffer.from(providedPin)
);
} catch {
return false;
}
}
// Add logging to authentication middleware
const authMiddleware = (req, res, next) => {
debugLog('Auth middleware for path:', req.path);
// If no PIN is set, bypass authentication
if (!PIN || PIN.trim() === '') {
debugLog('PIN protection disabled, bypassing auth');
return next();
}
// Check if user is authenticated via session
if (!req.session.authenticated) {
debugLog('User not authenticated, redirecting to login');
return res.redirect(BASE_PATH + '/login');
}
debugLog('User authenticated, proceeding');
next();
};
// Mount all routes under BASE_PATH
app.use(BASE_PATH, express.static('public', { index: false }));
// Routes
app.get(BASE_PATH + '/', authMiddleware, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get(BASE_PATH + '/login', (req, res) => {
if (!PIN || PIN.trim() === '') {
return res.redirect(BASE_PATH + '/');
}
if (req.session.authenticated) {
return res.redirect(BASE_PATH + '/');
}
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.get(BASE_PATH + '/api/config', (req, res) => {
let instanceName = process.env.INSTANCE_NAME;
if (instanceName == undefined) {
instanceName = 'DumbBudget';
} else {
instanceName = `DumbBudget - ${process.env.INSTANCE_NAME}`
}
res.json({ instanceName: instanceName });
});
app.get(BASE_PATH + '/pin-length', (req, res) => {
// If no PIN is set, return 0 length
if (!PIN || PIN.trim() === '') {
return res.json({ length: 0 });
}
res.json({ length: PIN.length });
});
app.post(BASE_PATH + '/verify-pin', (req, res) => {
// If no PIN is set, authentication is successful
if (!PIN || PIN.trim() === '') {
req.session.authenticated = true;
return res.status(200).json({ success: true });
}
const ip = req.ip;
// Check if IP is locked out
if (isLockedOut(ip)) {
const attempts = loginAttempts.get(ip);
const timeLeft = Math.ceil((LOCKOUT_TIME - (Date.now() - attempts.lastAttempt)) / 1000 / 60);
return res.status(429).json({
error: `Too many attempts. Please try again in ${timeLeft} minutes.`
});
}
const { pin } = req.body;
if (!pin || typeof pin !== 'string') {
return res.status(400).json({ error: 'Invalid PIN format' });
}
// Add artificial delay to further prevent timing attacks
const delay = crypto.randomInt(50, 150);
setTimeout(() => {
if (verifyPin(PIN, pin)) {
// Reset attempts on successful login
resetAttempts(ip);
// Set authentication in session
req.session.authenticated = true;
// Set secure cookie
res.cookie(`${projectName}_PIN`, pin, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
maxAge: 24 * 60 * 60 * 1000 // 24 hours
});
res.status(200).json({ success: true });
} else {
// Record failed attempt
recordAttempt(ip);
const attempts = loginAttempts.get(ip);
const attemptsLeft = MAX_ATTEMPTS - attempts.count;
res.status(401).json({
error: 'Invalid PIN',
attemptsLeft: Math.max(0, attemptsLeft)
});
}
}, delay);
});
// Cleanup old lockouts periodically
setInterval(() => {
const now = Date.now();
for (const [ip, attempts] of loginAttempts.entries()) {
if (now - attempts.lastAttempt >= LOCKOUT_TIME) {
loginAttempts.delete(ip);
}
}
}, 60000); // Clean up every minute
// Helper function to get transactions within date range
async function getTransactionsInRange(startDate, endDate) {
const transactions = await loadTransactions();
const allTransactions = [];
// Collect all transactions within the date range
Object.values(transactions).forEach(month => {
// Safely handle income transactions
if (month && Array.isArray(month.income)) {
const incomeInRange = month.income.filter(t =>
t.date >= startDate && t.date <= endDate
).map(t => ({ ...t, type: 'income' }));
allTransactions.push(...incomeInRange);
}
// Safely handle expense transactions
if (month && Array.isArray(month.expenses)) {
const expensesInRange = month.expenses.filter(t =>
t.date >= startDate && t.date <= endDate
).map(t => ({ ...t, type: 'expense' }));
allTransactions.push(...expensesInRange);
}
});
return allTransactions.sort((a, b) => new Date(b.date) - new Date(a.date));
}
// API Routes - all under BASE_PATH
app.post(BASE_PATH + '/api/transactions', authMiddleware, async (req, res) => {
try {
const { type, amount, description, category, date } = req.body;
// Basic validation
if (!type || !amount || !description || !date) {
return res.status(400).json({ error: 'Missing required fields' });
}
if (type !== 'income' && type !== 'expense') {
return res.status(400).json({ error: 'Invalid transaction type' });
}
if (type === 'expense' && !category) {
return res.status(400).json({ error: 'Category required for expenses' });
}
const transactions = await loadTransactions() || {};
const [year, month] = date.split('-');
const key = `${year}-${month}`;
// Initialize month structure if it doesn't exist
if (!transactions[key]) {
transactions[key] = {
income: [],
expenses: []
};
}
// Ensure arrays exist
if (!Array.isArray(transactions[key].income)) {
transactions[key].income = [];
}
if (!Array.isArray(transactions[key].expenses)) {
transactions[key].expenses = [];
}
// Add transaction
const newTransaction = {
id: crypto.randomUUID(),
amount: parseFloat(amount),
description,
date
};
if (type === 'expense') {
newTransaction.category = category;
transactions[key].expenses.push(newTransaction);
} else {
transactions[key].income.push(newTransaction);
}
await saveTransactions(transactions);
res.status(201).json(newTransaction);
} catch (error) {
console.error('Error adding transaction:', error);
res.status(500).json({ error: 'Failed to add transaction' });
}
});
app.get(BASE_PATH + '/api/transactions/:year/:month', authMiddleware, async (req, res) => {
try {
const { year, month } = req.params;
const key = `${year}-${month.padStart(2, '0')}`;
const transactions = await loadTransactions();
const monthData = transactions[key] || { income: [], expenses: [] };
// Combine and sort transactions by date
const allTransactions = [
...monthData.income.map(t => ({ ...t, type: 'income' })),
...monthData.expenses.map(t => ({ ...t, type: 'expense' }))
].sort((a, b) => new Date(b.date) - new Date(a.date));
res.json(allTransactions);
} catch (error) {
console.error('Error fetching transactions:', error);
res.status(500).json({ error: 'Failed to fetch transactions' });
}
});
app.get(BASE_PATH + '/api/totals/:year/:month', authMiddleware, async (req, res) => {
try {
const { year, month } = req.params;
const key = `${year}-${month.padStart(2, '0')}`;
const transactions = await loadTransactions();
const monthData = transactions[key] || { income: [], expenses: [] };
const totals = {
income: monthData.income.reduce((sum, t) => sum + t.amount, 0),
expenses: monthData.expenses.reduce((sum, t) => sum + t.amount, 0),
balance: 0
};
totals.balance = totals.income - totals.expenses;
res.json(totals);
} catch (error) {
console.error('Error calculating totals:', error);
res.status(500).json({ error: 'Failed to calculate totals' });
}
});
app.get(BASE_PATH + '/api/transactions/range', authMiddleware, async (req, res) => {
try {
const { start, end } = req.query;
if (!start || !end) {
return res.status(400).json({ error: 'Start and end dates are required' });
}
const transactions = await getTransactionsInRange(start, end);
res.json(transactions);
} catch (error) {
console.error('Error fetching transactions:', error);
res.status(500).json({ error: 'Failed to fetch transactions' });
}
});
app.get(BASE_PATH + '/api/totals/range', authMiddleware, async (req, res) => {
try {
const { start, end } = req.query;
if (!start || !end) {
return res.status(400).json({ error: 'Start and end dates are required' });
}
const transactions = await getTransactionsInRange(start, end);
const totals = {
income: transactions
.filter(t => t.type === 'income')
.reduce((sum, t) => sum + t.amount, 0),
expenses: transactions
.filter(t => t.type === 'expense')
.reduce((sum, t) => sum + t.amount, 0),
balance: 0
};
totals.balance = totals.income - totals.expenses;
res.json(totals);
} catch (error) {
console.error('Error calculating totals:', error);
res.status(500).json({ error: 'Failed to calculate totals' });
}
});
app.get(BASE_PATH + '/api/export/:year/:month', authMiddleware, async (req, res) => {
try {
const { year, month } = req.params;
const key = `${year}-${month.padStart(2, '0')}`;
const transactions = await loadTransactions();
const monthData = transactions[key] || { income: [], expenses: [] };
// Combine all transactions
const allTransactions = [
...monthData.income.map(t => ({ ...t, type: 'income' })),
...monthData.expenses.map(t => ({ ...t, type: 'expense' }))
].sort((a, b) => new Date(b.date) - new Date(a.date));
// Convert to CSV
const csvRows = ['Date,Type,Category,Description,Amount'];
allTransactions.forEach(t => {
csvRows.push(`${t.date},${t.type},${t.category || ''},${t.description},${t.amount}`);
});
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', `attachment; filename=transactions-${key}.csv`);
res.send(csvRows.join('\n'));
} catch (error) {
console.error('Error exporting transactions:', error);
res.status(500).json({ error: 'Failed to export transactions' });
}
});
app.get(BASE_PATH + '/api/export/range', authMiddleware, async (req, res) => {
try {
const { start, end } = req.query;
if (!start || !end) {
return res.status(400).json({ error: 'Start and end dates are required' });
}
const transactions = await getTransactionsInRange(start, end);
// Convert to CSV with specified format
const csvRows = ['Category,Date,Description,Value'];
transactions.forEach(t => {
const category = t.type === 'income' ? 'Income' : t.category;
const value = t.type === 'income' ? t.amount : -t.amount;
// Escape description to handle commas and quotes
const escapedDescription = t.description.replace(/"/g, '""');
const formattedDescription = escapedDescription.includes(',') ? `"${escapedDescription}"` : escapedDescription;
csvRows.push(`${category},${t.date},${formattedDescription},${value}`);
});
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', `attachment; filename=transactions-${start}-to-${end}.csv`);
res.send(csvRows.join('\n'));
} catch (error) {
console.error('Error exporting transactions:', error);
res.status(500).json({ error: 'Failed to export transactions' });
}
});
app.put(BASE_PATH + '/api/transactions/:id', authMiddleware, async (req, res) => {
try {
const { id } = req.params;
const { type, amount, description, category, date } = req.body;
// Basic validation
if (!type || !amount || !description || !date) {
return res.status(400).json({ error: 'Missing required fields' });
}
if (type !== 'income' && type !== 'expense') {
return res.status(400).json({ error: 'Invalid transaction type' });
}
if (type === 'expense' && !category) {
return res.status(400).json({ error: 'Category required for expenses' });
}
const transactions = await loadTransactions();
let found = false;
// Find and update the transaction
for (const key of Object.keys(transactions)) {
const monthData = transactions[key];
// Check in income array
const incomeIndex = monthData.income.findIndex(t => t.id === id);
if (incomeIndex !== -1) {
// If type changed, move to expenses
if (type === 'expense') {
const transaction = monthData.income.splice(incomeIndex, 1)[0];
transaction.category = category;
monthData.expenses.push({
...transaction,
amount: parseFloat(amount),
description,
date
});
} else {
monthData.income[incomeIndex] = {
...monthData.income[incomeIndex],
amount: parseFloat(amount),
description,
date
};
}
found = true;
break;
}
// Check in expenses array
const expenseIndex = monthData.expenses.findIndex(t => t.id === id);
if (expenseIndex !== -1) {
// If type changed, move to income
if (type === 'income') {
const transaction = monthData.expenses.splice(expenseIndex, 1)[0];
delete transaction.category;
monthData.income.push({
...transaction,
amount: parseFloat(amount),
description,
date
});
} else {
monthData.expenses[expenseIndex] = {
...monthData.expenses[expenseIndex],
amount: parseFloat(amount),
description,
category,
date
};
}
found = true;
break;
}
}
if (!found) {
return res.status(404).json({ error: 'Transaction not found' });
}
await saveTransactions(transactions);
res.json({ success: true });
} catch (error) {
console.error('Error updating transaction:', error);
res.status(500).json({ error: 'Failed to update transaction' });
}
});
app.delete(BASE_PATH + '/api/transactions/:id', authMiddleware, async (req, res) => {
try {
const { id } = req.params;
const transactions = await loadTransactions();
let found = false;
// Find and delete the transaction
for (const key of Object.keys(transactions)) {
const monthData = transactions[key];
// Check in income array
const incomeIndex = monthData.income.findIndex(t => t.id === id);
if (incomeIndex !== -1) {
monthData.income.splice(incomeIndex, 1);
found = true;
break;
}
// Check in expenses array
const expenseIndex = monthData.expenses.findIndex(t => t.id === id);
if (expenseIndex !== -1) {
monthData.expenses.splice(expenseIndex, 1);
found = true;
break;
}
}
if (!found) {
return res.status(404).json({ error: 'Transaction not found' });
}
await saveTransactions(transactions);
res.json({ success: true });
} catch (error) {
console.error('Error deleting transaction:', error);
res.status(500).json({ error: 'Failed to delete transaction' });
}
});
// Supported currencies list - must match client-side list
const SUPPORTED_CURRENCIES = [
'USD', 'EUR', 'GBP', 'JPY', 'AUD',
'CAD', 'CHF', 'CNY', 'HKD', 'NZD',
'MXN', 'RUB', 'SGD', 'KRW', 'INR',
'BRL', 'ZAR', 'TRY', 'PLN', 'SEK',
'NOK', 'DKK', 'IDR'
];
// Get current currency setting
app.get(BASE_PATH + '/api/settings/currency', authMiddleware, (req, res) => {
const currency = process.env.CURRENCY || 'USD';
if (!SUPPORTED_CURRENCIES.includes(currency)) {
return res.status(200).json({ currency: 'USD' });
}
res.status(200).json({ currency });
});
// Get list of supported currencies
app.get(BASE_PATH + '/api/settings/supported-currencies', authMiddleware, (req, res) => {
res.status(200).json({ currencies: SUPPORTED_CURRENCIES });
});
// Add logging to config endpoint
app.get(BASE_PATH + '/config.js', (req, res) => {
debugLog('Serving config.js with BASE_PATH:', BASE_PATH);
res.type('application/javascript').send(`
window.appConfig = {
basePath: '${BASE_PATH}',
debug: ${DEBUG}
};
`);
});
// Add logging to server startup
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
debugLog('Debug mode enabled');
debugLog('Base path:', BASE_PATH);
});