Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fx: authentication security issues

towfiqi9ca6e879cc20
diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts
index ae02410..3fc2f10 100644
--- a/backend/src/controllers/UserController.ts
+++ b/backend/src/controllers/UserController.ts
@@ -17,21 +17,23 @@ export class UserController {
const theUserName = configService.config.USER_NAME;
if (username === theUserName && password === configService.config.USER_PASSWORD) {
- const token = jwt.sign({ user: theUserName }, configService.config.SECRET);
-
// Express way of setting cookies
const sessionDuration = configService.config.SESSION_DURATION || 7;
+ const token = jwt.sign({ user: theUserName }, configService.config.SECRET, {
+ expiresIn: `${sessionDuration}d`,
+ });
res.cookie('token', token, {
httpOnly: true,
sameSite: 'lax',
maxAge: sessionDuration * 24 * 60 * 60 * 1000,
+ secure: configService.config.APP_URL?.startsWith('https'),
});
res.status(200).json({ success: true, error: null });
return;
}
- const error = username !== theUserName ? 'Incorrect Username' : 'Incorrect Password';
+ const error = 'Incorrect Username or Password';
res.status(401).json({ success: false, error });
return;
}
diff --git a/backend/src/middlewares/authMiddleware.ts b/backend/src/middlewares/authMiddleware.ts
index 12e8fd2..8ea05c0 100644
--- a/backend/src/middlewares/authMiddleware.ts
+++ b/backend/src/middlewares/authMiddleware.ts
@@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import Cookies from 'cookies';
import { configService } from '../services/ConfigService';
+import { safeCompare } from '../utils/helpers';
const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
const cookies = new Cookies(req, res);
@@ -14,9 +15,11 @@ const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
'GET:/api/health',
'POST:/api/plans',
];
- const verifiedAPI = req.headers.authorization
- ? req.headers.authorization.substring('Bearer '.length) === configService.config.APIKEY
- : false;
+
+ const verifiedAPI = safeCompare(
+ req.headers.authorization ? req.headers.authorization.substring('Bearer '.length) : '',
+ configService.config.APIKEY || ''
+ );
const accessingAllowedRoute =
req.url &&
req.method &&
diff --git a/backend/src/utils/helpers.ts b/backend/src/utils/helpers.ts
index e69902b..ecaa22e 100644
--- a/backend/src/utils/helpers.ts
+++ b/backend/src/utils/helpers.ts
@@ -1,4 +1,5 @@
import { customAlphabet } from 'nanoid';
+import { timingSafeEqual } from 'crypto';
export const isDevMode = process.env.NODE_ENV === 'development';
@@ -56,3 +57,8 @@ export function setNestedValue(obj: any, path: string, value: any): void {
target[lastKey] = value;
}
+
+export const safeCompare = (a: string, b: string): boolean => {
+ if (a.length !== b.length) return false;
+ return timingSafeEqual(Buffer.from(a), Buffer.from(b));
+};