mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-02-02 20:43:36 +00:00
feat: Implement API key authentication with rate limiting and secure comparison
- Added rate limiting to the authentication middleware to prevent brute-force attacks. - Introduced a secure comparison function to mitigate timing attacks during API key validation. - Created a new rate limiter class to track failed authentication attempts and block requests after exceeding the maximum allowed failures. - Updated the authentication middleware to handle rate limiting and secure key comparison. - Enhanced error handling for rate-limited requests, providing appropriate responses to clients.
This commit is contained in:
@@ -65,6 +65,18 @@ export function cleanupExpiredTokens(): void {
|
||||
// Clean up expired tokens every 5 minutes
|
||||
setInterval(cleanupExpiredTokens, 5 * 60 * 1000);
|
||||
|
||||
/**
|
||||
* Extract Bearer token from Authorization header
|
||||
* Returns undefined if header is missing or malformed
|
||||
*/
|
||||
export function extractBearerToken(req: Request): string | undefined {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return undefined;
|
||||
}
|
||||
return authHeader.slice(7); // Remove 'Bearer ' prefix
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a terminal session token
|
||||
*/
|
||||
@@ -116,8 +128,9 @@ export function terminalAuthMiddleware(req: Request, res: Response, next: NextFu
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for session token
|
||||
const token = (req.headers['x-terminal-token'] as string) || (req.query.token as string);
|
||||
// Extract token from Authorization header only (Bearer token format)
|
||||
// Query string tokens are not supported due to security risks (URL logging, referrer leakage)
|
||||
const token = extractBearerToken(req);
|
||||
|
||||
if (!validateTerminalToken(token)) {
|
||||
res.status(401).json({
|
||||
|
||||
Reference in New Issue
Block a user