-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add API key authentication middleware #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Simple API key authentication middleware | ||
| // Reads the expected key from API_KEY environment variable | ||
|
|
||
| const authenticate = (req, res, next) => { | ||
| const apiKey = req.headers['x-api-key']; | ||
| const expectedKey = process.env.API_KEY; | ||
|
|
||
| if (!expectedKey) { | ||
| // Auth not configured — skip in development | ||
| return next(); | ||
| } | ||
|
|
||
| if (!apiKey || apiKey !== expectedKey) { | ||
| return res.status(401).json({ error: 'Unauthorized' }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 [MEDIUM] security: Potential Timing Attack Vulnerability The direct string comparison |
||
| } | ||
|
|
||
| next(); | ||
| }; | ||
|
|
||
| module.exports = { authenticate }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 [HIGH] security: Unsafe Authentication Bypass in Production
The
if (!expectedKey)condition allows authentication to be completely bypassed if theAPI_KEYenvironment variable is not set. While intended for development, this is a critical security vulnerability if deployed to production without the variable configured, making all/tasksroutes publicly accessible. Authentication should fail loudly in production if the key is missing.Suggestion: